webpack4配置momet按需加载,每个文件依然有moment?

新手上路,请多包涵

配置了moment按需加载

var webpackConfig = {
    mode: 'development',
    entry: {
        index: './src/index.js',
        another: './src/another-module.js',
    },
    output: {
        filename: '[name].[contenthash].js',
    },
    plugins: [
        // new CleanWebpackPlugin(['dist/*']) for < v2 versions of CleanWebpackPlugin
        new CleanWebpackPlugin(),
        new HtmlWebpackPlugin({
            title: 'Caching',
            template: './src/index.html',
        }),
        new LodashModuleReplacementPlugin(),
        new webpack.ContextReplacementPlugin(
            /moment[/\\]locale$/,
            /zh-cn|es|zh-tw|ja/,
        ),
        // new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
    ],
    optimization: {
        moduleIds: 'hashed',
        runtimeChunk: 'single',
        splitChunks: {
            maxInitialRequests: 4,
            cacheGroups: {
                vendors: {
                    test: /[\\/]node_modules[\\/]/,
                    name: 'vendors',
                    chunks: 'all',
                    priority: -10,
                },
                vendor1: {
                    name: 'vendor-antd',
                    test: (module) => {
                        return /[\\/]node_modules[\\/]@ant-design|antd/.test(module.context);
                    },
                    chunks: 'all',
                    priority: 2
                },
            },
        },
    },
    module:{
        rules: [{
            test: /\.js$/,
            exclude: /node_modules/,
            use: {
                loader: 'babel-loader',
                options: {
                    presets: ['@babel/preset-env','@babel/preset-react'],
                    plugins: [
                        'lodash',
                        ["import", {
                            "libraryName": "antd",
                            // "libraryDirectory": "es",
                            // "style": "css" // `style: true` 会加载 less 文件
                        }]
                    ],
                }
            }
        },
        {//antd样式
            test: /\.css$/,
            exclude: /src/,
            use: [
                {
                    loader: "style-loader"
                },
                {
                    loader: "css-loader",
                    options: {
                        importLoaders: 1
                    }
                }
            ]
        }
        ],
    },
    
    // externals: {
    //     lodash: {
    //         commonjs: 'lodash',
    //         commonjs2: 'lodash',
    //         amd: 'lodash',
    //         root: '_',
    //     },
    // },
    // optimization: {
    //     splitChunks: {
    //         chunks: 'all',
    //     },
    // },
};

图片描述

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