webpack4 output filename和chunkFileName作用

根据官网描述~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
                }
            },
        }
    }

};

结果如图:

clipboard.png
按我的理解,配置中output的filename是[name].asd.js,按理说打包出来的index1.js,index2.js, index3.js应该对应这index1.asd.js, index2.asd.js, index3.asd.js, 其他的js文件是带哈希的,现在反而是不在入口文件的manifest chunk是按照filename格式命名的,求解答

阅读 5k
2 个回答

后来google了一下,主要是还是在配置了optimization.runtimeChunk的时候会导致这样的问题,可以理解成配置了runtimeChunk后入口文件就是runtimeChunk指定的chunk,而其他的chunk都不算入口chunk了,如果去掉,则还是以input中配置的为入口chunk,受filename命名影响,异步引入的chunk受chunkFilename影响

新手上路,请多包涵

因为你用了inline-source-map,所以那三个js都打包成一个文件,并用了out的命名格式

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏