【问题标题】:Optimize bundle.js file size优化 bundle.js 文件大小
【发布时间】:2020-03-20 10:40:28
【问题描述】:

下面是 webpack 配置代码,它的大小约为 6.2 MB。在生产模式下,它看起来需要更多时间加载登录页面 url,从第二次开始,它看起来不错,第一次出现问题,需要建议减少 bundle.js 文件大小

webpack.base.js

module.exports = {
    //Running babel to every file
    module: {
        rules: [
            {
                test: /\.js?$/,
                loader: 'babel-loader',
                exclude: /node_modules/,
                options: {
                    presets: [
                        'react',
                        'stage-0',
                        ['env', { targets: { browsers: ['last 2 versions'] } }]
                    ]
                }
            }, {
                test: /\.css$/,
                loader: "style-loader!css-loader"
            },
            {
                test: /\.less$/,
                use:[
                    {
                        loader: "style-loader"
                    },
                    {
                        loader: 'css-loader'
                    },
                    {
                        loader: "less-loader"
                    }
                ]
            }, 
            {
                test: /\.(jpe?g|png|gif|woff|woff2|eot|ttf|svg|otf)(\?[a-z0-9=.]+)?$/,
                loader: 'url-loader?limit=100000'
            }

        ]
    }//end module
}

webpack.client.js:

const config = {
    entry: './src/client/client.js',

    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'public')
    }
};

module.exports = merge(baseConfig, config)

webpack.server.js:

const server_config = {
    //letting webapck know that this bundle is created for node server.
    target: 'node',

    entry: './src/server/server.js',

    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'build')
    },
    externals: [webpackNodeExternals()],
    node:{
        __dirname:false
    }
};

module.exports = merge(baseConfig, server_config);

【问题讨论】:

    标签: javascript node.js reactjs webpack bundling-and-minification


    【解决方案1】:

    这是我优化 webpack 的方法。你可以查看我的完整配置here

    当你想在生产环境中构建时,首先你需要运行 webpack 生产模式

    webpack -p --mode=production
    

    下面是小配置

    const path = require("path");
    const webpack = require("webpack");
    const MiniCssExtractPlugin = require("mini-css-extract-plugin");
    const CompressionPlugin = require("compression-webpack-plugin");
    const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
    const WebpackShellPlugin = require('webpack-shell-plugin');
    const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
    
    module.exports = {
        optimization: {
            minimizer: [
                new UglifyJsPlugin({ // minify js file
                    cache: true,
                    parallel: true,
                    sourceMap: false,
                    extractComments: 'all',
                    uglifyOptions: {
                        compress: true,
                        output: null
                    }
                }),
                new OptimizeCSSAssetsPlugin({ // minify css
                    cssProcessorOptions: {
                        safe: true,
                        discardComments: {
                            removeAll: true,
                        },
                    },
                })
            ]
        },
        plugins: [
            new CompressionPlugin({ // gzip js and css 
                test: /\.(js|css)/
            }),
            new UglifyJsPlugin(), // uglify js
            new WebpackShellPlugin({
                onBuildStart: ['echo "Starting postcss command"'],
                onBuildEnd: ['postcss --dir wwwroot/dist wwwroot/dist/*.css'] // uglify css using postcss
            })
        ],
        module: {
            rules: [{
                    test: /\.scss$/,
                    use: [
                        'style-loader',
                        MiniCssExtractPlugin.loader,
                        {
                            loader: "css-loader",
                            options: {
                                minimize: true,
                                sourceMap: true
                            }
                        },
                        {
                            loader: "sass-loader"
                        }
                    ]
                },
                {
                    test: /\.(js|jsx)$/,
                    exclude: /node_modules/,
                    loader: ["babel-loader", "eslint-loader"]
                },
                {
                    test: /\.(jpe?g|png|gif)$/i,
                    loader: "file-loader"
                },
                {
                    test: /\.(woff|ttf|otf|eot|woff2|svg)$/i,
                    loader: "file-loader"
                }
            ]
        }
    };
    

    【讨论】:

      猜你喜欢
      • 2010-10-11
      • 2010-10-23
      • 1970-01-01
      • 2018-05-15
      • 1970-01-01
      • 2021-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多