Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Webpack 使用多个入口点包含babel polyfill的最佳方式是什么_Webpack_Babeljs - Fatal编程技术网

Webpack 使用多个入口点包含babel polyfill的最佳方式是什么

Webpack 使用多个入口点包含babel polyfill的最佳方式是什么,webpack,babeljs,Webpack,Babeljs,我正在使用使用多个入口点的网页包配置: 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

我正在使用使用多个入口点的网页包配置:

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
                }));
            });
        }
    ]
};
有没有办法在我的网页配置中包含。或者在我的设置中包含它的最佳方式是什么

我是否必须将其作为要求包含在我的所有模块中


提前非常感谢

最简单的方法就是改变

main: './resources/assets/js/main.js',
services: './resources/assets/js/services.js'
将来

因此,polyfill作为每个入口点的一部分被加载和执行,而您的文件不需要知道它

这假设
main
services
都加载在同一页面上。如果它们是两个单独的页面,那么您希望两个数组中都有
babel polyfill
条目


上述情况适用于巴别塔5号。对于Babel 6,您需要
npm安装——保存Babel polyfill
并使用
Babel polyfill
而不是
Babel/polyfill
,开发的替代方案(可能不是生产的最佳设置)是在自己的模块中使用
Babel polyfill

entry: {
    'babel-polyfill': ['babel-polyfill'],
    main: './resources/assets/js/main.js',
    services: './resources/assets/js/services.js'
}

背景:我尝试了上面答案中的建议,但仍然得到一个错误:“只允许一个babel polyfill实例”。我正在使用Webpack2,所以这可能是一个因素。上述解决方案适合我的目的,因为我正在编写一个可重用的库以包含在更大的应用程序中,并且只希望库repo中有一个单独的演示包以用于测试目的。但是对于一个完整的应用程序,除非您使用的是HTTP/2,否则在生产环境中加载polyfill时需要额外的HTTP请求可能不是一个好主意。

为什么要使用--save dev而不是--save?一般来说,您不会在生产环境中构建JS,您应该在部署开发依赖项之前编译一次,然后在生产过程中,您不需要任何网页包。如果您在同一页面上使用两个入口点(
未捕获错误:只允许一个babel polyfill实例
),这将失败。出于好奇,如果您有一个多入口应用程序,您是否必须包含“babel polyfill”对于
webpack.config.js中的每个入口点,并在每个入口点js文件的顶部包含
import babel polyfill
?对于babel 7,npm范围的polyfill入口路径将是
main:['@babel/polyfill','./resources/assets/js/main.js']
entry: {
    'babel-polyfill': ['babel-polyfill'],
    main: './resources/assets/js/main.js',
    services: './resources/assets/js/services.js'
}