在webpack中创建多个块时,数组表示法和对象表示法之间的区别是什么

What is the difference between array notation and object notation when creating multiple chunks in webpack?

本文关键字:表示 对象 之间 是什么 区别 数组 创建 webpack      更新时间:2023-09-26

我在我的项目中打开了热重加载功能,就像这个

entry: [
    'webpack-hot-middleware/client',
    './src/js/entry.js'
],
output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.js',
    publicPath: '/'
},

一切都很正常,直到我决定将供应商模块移到不同的文件中,它才起作用。然后我意识到,用数组创建多个块(如前所述)与用对象表示法创建不同,就像这个一样

entry: {
    hot: 'webpack-hot-middleware/client',
    app: './src/js/entry.js'
},
output: {
    path: path.join(__dirname, 'dist'),
    filename: '[name].bundle.js',
    publicPath: '/'
}

我在index.html中包含了app.bundle.jshot.bundle.js,但这仍然不起作用。知道为什么吗?

如果您只想指定几个块,您可以向其中一个添加热加载脚本:

entry: {
    vandor: './vendor/vendor.js',
    app: ['webpack-hot-middleware/client', './src/js/entry.js']
},

如果你愿意,你可以动态地做:

entry: {
    vandor: ['./vendor/vendor.js'],
    app: ['./src/js/entry.js']
},
...
webpackConfig.entry.app.unshift('webpack-hot-middleware/client');