What is the best way to include babel polyfill using multiple entry points
我正在使用使用多个入口点的webpack配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | 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 })); }); } ] }; |
有什么办法可以在我的webpack配置中包含babel polyfill。 或将其包括在我的设置中的最佳方法是什么?
我是否必须在所有模块中都包含它?
提前非常感谢您!
最简单的方法就是改变
1 2 | main: './resources/assets/js/main.js', services: './resources/assets/js/services.js' |
成为
1 2 | main: ['babel-polyfill', './resources/assets/js/main.js'], services: ['./resources/assets/js/services.js'] |
这样,就可以将polyfill作为每个入口点的一部分加载并执行,而您的文件不需要知道它。
假定
注意
上面的内容适用于Babel5。对于Babel 6,您将
开发的另一种选择(也许不是用于生产的最佳设置)是在其自己的模块中具有
1 2 3 4 5 | entry: { 'babel-polyfill': ['babel-polyfill'], main: './resources/assets/js/main.js', services: './resources/assets/js/services.js' } |
背景:我尝试了以上答案中的建议,但仍然出现错误:"仅允许使用babel-polyfill的一个实例"。我正在使用webpack 2,所以也许这是一个因素。上面的解决方案对于我的目的来说是很好的,因为我正在编写一个可重用的库以包含在较大的应用程序中,并且只想在库库中使用一个单独的演示包进行测试。但是对于完整的应用程序,除非您使用的是HTTP / 2,否则可能需要一个额外的HTTP请求来将polyfill加载到生产环境中不是一个好主意。