我正在使用带有多个入口点的 webpack 配置:
var fs = require('fs');
var webpack = require('webpack');
var commonsPlugin = new webpack.optimize.CommonsChunkPlugin('common.[hash].js');
module.exports = {
cache: true,
entry: {
main: './resources/assets/js/main.js',
services: './resources/assets/js/services.js'
},
output: {
path: './public/assets/web/js',
filename: '[name].[hash].js',
publicPath: '/assets/web/js/',
chunkFilename: '[id].[chunkhash].js'
},
resolve: {
modulesDirectories: ['node_modules', 'web_modules', 'modules']
},
module: {
loaders: [{
test: /\.js$/,
exclude: [/node_modules/, /web_modules/],
loader: 'babel-loader',
query: {
stage: 0
}
}]
},
plugins: [commonsPlugin,
new webpack.ProvidePlugin({
jQuery: 'jquery',
$: 'jquery',
'window.jQuery': 'jquery'
}),
function() {
this.plugin('done', function(stats) {
fs.writeFile('./cache.json', JSON.stringify({
hash: stats.hash
}));
});
}
]
};
有没有办法包含 babel polyfill 在我的 webpack 配置中。或者将其包含在我的设置中的最佳方式是什么?
我是否必须将其作为要求包含在我的所有模块中?
提前非常感谢您!
最佳答案
最简单的方法就是改变
main: './resources/assets/js/main.js',
services: './resources/assets/js/services.js'
成为
main: ['babel-polyfill', './resources/assets/js/main.js'],
services: ['./resources/assets/js/services.js']
这样,polyfill 作为每个入口点的一部分被加载和执行,而您的文件不需要知道它。
这假设main
和services
都加载在同一页面上。如果它们是两个单独的页面,则您需要在两个数组中都包含 babel-polyfill 条目。
注意
以上内容适用于 Babel 5。对于 Babel 6,您需要 npm install --save babel-polyfill
并使用 babel-polyfill
而不是 babel/polyfill
.
关于webpack - 使用多个入口点包含 babel polyfill 的最佳方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33380063/