【问题标题】:Webpack build task overriding optimizationsWebpack 构建任务覆盖优化
【发布时间】:2018-12-11 02:21:07
【问题描述】:

问题:生产构建任务正在缩小文件,然后覆盖未缩小文件的文件。

上下文: 我正在使用环境变量来使用不同的 webpack 配置文件。 npm run dev 使用 webpack.config.dev.js 并将未压缩的文件放入 /public。 npm run dist 使用 webpack.config.prod.js 并将缩小的文件放入 /dist。两者都需要 webpack.config.common.js。

dist 任务发生的时间顺序似乎存在问题。它首先将缩小的文件添加到 /dist,然后将它们删除以用未缩小的文件替换它们。当然,这违背了缩小它们的目的。

问题:dist 任务是否有意外运行,或者您是否发现任何可能导致此问题的原因?

我是 webpack 的新手,任何帮助都将不胜感激。谢谢。

package.json 脚本:

  "scripts": {
    "dev": "NODE_ENV=development webpack --config webpack.config.dev.js --mode development --watch",
    "dist": "NODE_ENV=production webpack --config webpack.config.prod.js --mode production && fractal build"
  },

webpack.config.common.js:

const webpack = require('webpack');
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const magicImporter = require('node-sass-magic-importer');
const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = {
    entry: { main: './assets/js/main.js'},
    resolve: {
        modules: [
            path.resolve(__dirname, 'assets/js'),
            path.resolve(__dirname, 'assets'),
            path.resolve(__dirname, 'favicon.ico'),
            'node_modules'
        ],
        extensions: ['.js']
    },
    devtool: "inline-source-map",
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader",
                    options: {
                        presets: ['@babel/preset-env'],
                        babelrc: false
                    }
                }
            },
            {
                test: /\.(css|scss)$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    {
                        loader: 'css-loader',
                        options: {
                            sourceMap: true
                        }
                    },
                    {
                        loader: 'postcss-loader'
                    },
                    {
                        loader: 'resolve-url-loader',
                        options: {
                            sourceMap: true
                        }
                    },
                    {
                        loader: 'sass-loader',
                        options: {
                            importer: magicImporter(),
                            sourceMap: true
                        }
                    }
                ]
            }
    },
    plugins: [      
        new MiniCssExtractPlugin({
            filename: 'assets/css/main.css'
        }),

        new CopyWebpackPlugin([
            {
                from: 'assets/img/**/*',
                to: 'assets/img/',
                flatten: true
            },
            {
                from: 'assets/video/**/*',
                to: 'assets/video/',
                flatten: true
            },
            {
                from: 'assets/webvtt/**/*',
                to: 'assets/webvtt/',
                flatten: true
            },
            {
                from: 'favicon.ico',
                to: ''
            },
            {
                from: '*.png',
                to: ''
            }
        ])
    ]
};

webpack.config.dev.js:

const webpack = require('webpack');
const path = require('path');
const merge = require('webpack-merge');
const common = require('./webpack.config.common.js');

module.exports = merge(common, {
    mode: 'development',
    output: {
        path: path.resolve(__dirname, 'public/'),
        publicPath: '/',
        filename: 'assets/js/main.js'
    }
});

webpack.config.prod.js:

const webpack = require('webpack');
const path = require('path');
const merge = require('webpack-merge');
const common = require('./webpack.config.common.js');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');

module.exports = merge(common, {
    mode: 'production',
    output: {
        path: path.resolve(__dirname, 'dist/'),
        publicPath: '/',
        filename: 'assets/js/main.js'
    },
    optimization: {
        minimizer: [
            new UglifyJsPlugin({
                cache: false,
                parallel: true,
                sourceMap: false,
                uglifyOptions: {
                    warnings: false,
                    parse: {},
                    compress: {},
                    mangle: true,
                    output: null,
                    toplevel: false,
                    nameCache: null,
                    ie8: false,
                    keep_fnames: false
                },
                extractComments: {
                    condition: /^\**!|@preserve|@license|@cc_on/i,
                    filename: 'assets/js/extracted-comments.js'
                }
            }),
            new OptimizeCSSAssetsPlugin({})
        ]
    }
});

【问题讨论】:

    标签: webpack build-process webpack-4


    【解决方案1】:

    我能够通过将我所有的 webpack 配置合并回 webpack.config.js 并使用检查环境的条件来推动优化而不是使用不同的配置来解决这个问题。

    我认为问题在于将fractal build 任务附加到dist 任务。我认为 webpack 正确地缩小了文件,然后分形在此之后构建它们未缩小。我不确定为什么合并文件解决了它(我的dist 任务仍然附加了fractal build),但我很高兴它现在按我的预期工作。

    更新了 package.json 脚本:

      "scripts": {
        "dev": "NODE_ENV=development webpack --mode development --watch",
        "dist": "NODE_ENV=production webpack --mode production && fractal build"
      },
    

    更新了 webpack.config.js:

    const webpack = require('webpack');
    const path = require('path');
    const MiniCssExtractPlugin = require('mini-css-extract-plugin');
    const magicImporter = require('node-sass-magic-importer');
    const CopyWebpackPlugin = require('copy-webpack-plugin');
    const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
    const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
    
    module.exports = {
        mode: process.env.NODE_ENV,
        entry: { main: './assets/js/main.js'},
        output: {
            path: path.resolve(__dirname, 'public/'),
            publicPath: '/',
            filename: 'assets/js/main.js'
        },
        devtool: "inline-source-map",
        module: {
            rules: [
                {
                    test: /\.js$/,
                    exclude: /node_modules/,
                    use: {
                        loader: "babel-loader",
                        options: {
                            presets: ['@babel/preset-env'],
                            babelrc: false
                        }
                    }
                },
                {
                    test: /\.(css|scss)$/,
                    use: [
                        MiniCssExtractPlugin.loader,
                        {
                            loader: 'css-loader',
                            options: {
                                sourceMap: true
                            }
                        },
                        {
                            loader: 'postcss-loader'
                        },
                        {
                            loader: 'resolve-url-loader',
                            options: {
                                sourceMap: true
                            }
                        },
                        {
                            loader: 'sass-loader',
                            options: {
                                importer: magicImporter(),
                                sourceMap: true
                            }
                        }
                    ]
                }
            ]
        },
        plugins: [      
            new MiniCssExtractPlugin({
                filename: 'assets/css/main.css'
            }),
    
            new CopyWebpackPlugin([
                {
                    from: 'assets/img/**/*',
                    to: 'assets/img/',
                    flatten: true
                },
                {
                    from: 'assets/video/**/*',
                    to: 'assets/video/',
                    flatten: true
                },
                {
                    from: 'assets/webvtt/**/*',
                    to: 'assets/webvtt/',
                    flatten: true
                },
                {
                    from: 'favicon.ico',
                    to: ''
                },
                {
                    from: '*.png',
                    to: ''
                }
            ])
        ],
        optimization: {
            minimizer: []
        }
    };
    
    if (process.env.NODE_ENV === 'production') {
        module.exports.optimization.minimizer.push(
            new UglifyJsPlugin({
                cache: false,
                parallel: true,
                sourceMap: false,
                uglifyOptions: {
                    warnings: false,
                    parse: {},
                    compress: {},
                    mangle: true,
                    output: null,
                    toplevel: false,
                    nameCache: null,
                    ie8: false,
                    keep_fnames: false
                },
                extractComments: {
                    condition: /^\**!|@preserve|@license|@cc_on/i,
                    filename: 'assets/js/extracted-comments.js'
                }
            }),
            new OptimizeCSSAssetsPlugin({})
        );
    }
    

    作为参考,这行一直在fractal.js中:

    fractal.web.set('builder.dest', __dirname + '/dist');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-29
      • 1970-01-01
      • 1970-01-01
      • 2018-11-16
      • 1970-01-01
      • 2016-08-24
      • 1970-01-01
      相关资源
      最近更新 更多