简体   繁体   中英

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

Suppose I have two files, main.js and app.js ; how do I use Webpack to bundle both of them into one file: bundle.js ?

create one entry.js which is your webpack entry file and in this your require your additional files

webpack.config.js

module.exports = {
   entry: './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

You can pass an array of entries:

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

Also, you can pass them as a CLI option :

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

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