简体   繁体   English

Webpack:如何将多个 javascript 文件捆绑到一个输出文件中?

[英]Webpack: How do I bundle multiple javascript files into a single output file?

Suppose I have two files, main.js and app.js ;假设我有两个文件, main.jsapp.js how do I use Webpack to bundle both of them into one file: bundle.js ?我如何使用 Webpack 将它们捆绑到一个文件中: bundle.js

create one entry.js which is your webpack entry file and in this your require your additional files创建一个entry.js这是你的 webpack 入口文件,在这个你require你的额外文件

webpack.config.js webpack.config.js

module.exports = {
   entry: './src/entry.js'
   ...
};

/src/entry.js /src/entry.js

// new syntax
import './main.js';
import './app.js';

// or old syntax
require('./main.js');
require('./app.js');

if these two files depend on each other, it would be be beneficial to reflect this in your dependency tree and require main.js in your app.js and not in entry.js for example如果这两个文件相互依赖,那么在您的依赖树中反映这一点并在您的app.js中而不是在main.js中需要 main.js 将是entry.js

You can pass an array of entries:您可以传递一组条目:

module.exports = {
   entry: ['./main.js', './app.js'],
   ...
};

Also, you can pass them as a CLI option :此外,您可以将它们作为CLI 选项传递:

webpack ./main.js ./app.js --config=webpack.config.js

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

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