简体   繁体   English

如何将NodeJS文件转换为Vanilla文件

[英]How convert NodeJS files to Vanilla files

I have a module in NodeJs, the entry main is main.js: 我在NodeJs中有一个模块,条目主要是main.js:

main.js
└─┬ source
  ├── a.js
  ├── b.js
  ├── c.js
  └── d.js

This module has module.export in the main file like 这个模块在主文件中有module.export

module.exports = ZTree;

And have a requires in each file. 并且在每个文件中都有一个要求。 I don't use requires of node modules or other modules. 我不使用节点模块或其他模块的需求。 Only files in source folder. 文件夹中的文件。

So, I want to use this module in a web application, in a single js file. 因此,我想在Web应用程序中的单个js文件中使用此模块。 So I tried with webpack: 所以我尝试了webpack:

const path = require('path');

module.exports = {
    entry: './ztree/main.js',
    output: {
        path: path.resolve(__dirname, 'rtree'),
        filename: 'ztree.js'
    },
}

But when I set in my test.html the script: 但是当我在test.html中设置脚本时:

 <script src="./ztree.js"></script>

I can't use it. 我不能用

So, my question is how I can convert "NodeJS files" to "Vanilla files" for web. 因此,我的问题是如何将“ NodeJS文件”转换为“ Vanilla文件”。

Thanks! 谢谢!

Thanks to @justin-summerlin for solve the answer. 感谢@ justin-summerlin解决问题的答案。

Must add libraryTarget and library in the output on webpack.config.js 必须添加libraryTarget图书馆webpack.config.js输出

const path = require('path');

module.exports = {
    entry: './ztree/main.js',
    output: {
        path: path.resolve(__dirname, 'rtree'),
        filename: 'app.js',
        libraryTarget: "var",
        library: 'ZTree'
    },
}

And in your HTML file you can use it like: 在HTML文件中,您可以像这样使用它:

let ztree = new ZTree(); // As your library name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM