webpack4X修改SplitChunksPluginvendorsfilename报错

32次阅读

共计 806 个字符,预计需要花费 3 分钟才能阅读完成。

报错内容:You are trying to set a filename for a chunk which is (also) loaded on demand. The runtime can only handle loading of chunks which match the chunkFilename schema. Using a custom filename would fail at runtime. (cache group: defaultVendors)

module.exports = {
    // 此处省略其他配置项
    ...
    optimization: {
        splitChunks: {
            chunks: 'all',
            minSize: 30000,
            //minRemainingSize: 0,
            maxSize: 0,
            minChunks: 1,
            maxAsyncRequests: 6,
            maxInitialRequests: 4,
            automaticNameDelimiter: '~',
            // 下面两个属性如果设置成两个 false,打包后就不会出现 verndors~ 前缀
            // cacheGroups: {
            //     vendors: false,
            //     default: false
            // }
            cacheGroups: {
                defaultVendors: {test: /[\\/]node_modules[\\/]/,
                    priority: -10
                    //filename: 'vendors'
                },
                default: {
                    //minChunks: 2,
                    priority: -20,
                    reuseExistingChunk: true
                    //filename: 'common'
                }
            }
        }
    }
    ...
}
  • 解决办法:注释掉 filename
  • 原因在文档中有:


缓存组设置 filename 时,在 chunks 项配置为 inital 时才会生效,我们分割同步代码时,可以设置 chunk 为 inital,这样就可以自定义 filename 了。否则会报错。

  • chunks 设置成 inital,我理解的是非异步模块

正文完
 0