简体   繁体   中英

How convert NodeJS files to Vanilla files

I have a module in NodeJs, the entry main is main.js:

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

This module has module.export in the main file like

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. So I tried with 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:

 <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.

Thanks!

Thanks to @justin-summerlin for solve the answer.

Must add libraryTarget and library in the output on 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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