Webpack:如何将多个javascript文件捆绑到一个输出文件中?
问题描述:
假设我有两个文件, main.js 和 app.js ;如何使用Webpack将它们捆绑到一个文件中: bundle.js ?
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?
答
创建一个 entry.js
这是你的webpack条目文件,在这个 require
你的附加文件
create one entry.js
which is your webpack entry file and in this your require
your additional files
webpack.config.js
webpack.config.js
module.exports = {
entry: './src/entry.js'
...
};
/src/entry.js
/src/entry.js
require('./main.js');
require('./app.js');
如果这两个文件相互依赖,那么在依赖关系树中反映这一点将是有益的并在 app.js
中要求 main.js
而不是 entry.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