根据官网描述~output配置中chunkFileName用以命名未出现在入口文件的打包文件,filename用来命名入口文件对应的打包文件名,但是测试的时候结果不对应,是什么原因?webpack配置如下:
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
module.exports = {
entry: {
'index1': './src/index1.js',
'index2': './src/index2.js',
'index3': './src/index3.js'
},
output: {
filename: '[name].asd.js',
chunkFilename: '[name].[contenthash].js',
path: path.resolve(__dirname, 'distribution')
},
//use inline-source-map for development:
devtool: 'inline-source-map',
//use source-map for production:
// devtool: 'source-map',
devServer: {
contentBase: './distribution'
},
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
include: [path.resolve(__dirname, 'src')],
exclude: [path.resolve(__dirname, 'node_modules')]
}
]
},
plugins: [
new CleanWebpackPlugin(['distribution']),
new BundleAnalyzerPlugin({
openAnalyzer: false,
analyzerMode: 'static',
reportFilename: 'bundle-analyzer-report.html'
}),
new HtmlWebpackPlugin({
template: './src/index1.html',
filename: 'index1.html',
chunks: ['index1', 'manifest', 'vendor', 'common']
}),
new HtmlWebpackPlugin({
template: './src/index2.html',
filename: 'index2.html',
chunks: ['index2', 'manifest', 'vendor', 'common']
}),
new HtmlWebpackPlugin({
template: './src/index3.html',
filename: 'index3.html',
chunks: ['index3', 'manifest', 'vendor', 'common']
}),
new webpack.HashedModuleIdsPlugin()
],
// optimization: {
// runtimeChunk: {
// "name": "manifest"
// },
// splitChunks: {
// cacheGroups: {
// default: false,
// vendors: false,
// vendor: {
// test: /[\\/]node_modules[\\/]/,
// chunks: 'initial',
// enforce: true,
// priority: 10,
// name: 'vendor'
// },
// common: {
// chunks: "all",
// minChunks: 2,
// name: 'common',
// enforce: true,
// priority: 5
// }
// }
// }
// }
optimization: {
runtimeChunk: {
'name': 'manifest'
},
splitChunks: {
chunks: 'all',
minSize: 0,
cacheGroups: {
default: false,
vendors: false,
vendor: {
test: /[\\/]node_modules[\\/]/,
chunks: 'initial',
enforce: true,
priority: 10,
name: 'vendor'
},
common: {
chunks: 'all',
minChunks: 2,
name: 'common',
enforce: true,
priority: 5
}
},
}
}
};
结果如图:
按我的理解,配置中output的filename是[name].asd.js,按理说打包出来的index1.js,index2.js, index3.js应该对应这index1.asd.js, index2.asd.js, index3.asd.js, 其他的js文件是带哈希的,现在反而是不在入口文件的manifest chunk是按照filename格式命名的,求解答
后来google了一下,主要是还是在配置了optimization.runtimeChunk的时候会导致这样的问题,可以理解成配置了runtimeChunk后入口文件就是runtimeChunk指定的chunk,而其他的chunk都不算入口chunk了,如果去掉,则还是以input中配置的为入口chunk,受filename命名影响,异步引入的chunk受chunkFilename影响