【问题标题】:How to use uglifyjs to minify and compress css/scss in webpack/vue cli?如何在 webpack/vue cli 中使用 uglifyjs 缩小和压缩 css/scss?
【发布时间】:2020-06-27 08:04:32
【问题描述】:

我有带有 cli 的 vue 应用程序。我想使用uglifyjs plugin

所以我将这段代码添加到我的 vue.config.js 中:

configureWebpack: {
    optimization: {
      minimizer: [
        new UglifyJsPlugin({
          uglifyOptions: {
            warnings: false,
            parse: {},
            compress: {},
            mangle: true, // Note `mangle.properties` is `false` by default.
            output: null,
            toplevel: false,
            nameCache: null,
            ie8: false,
            keep_fnames: false,
         },
     }),
  ],
},

我想压缩 *.scss 和 *.vue 文件中存在的所有 css。如何配置UglifyJsPlugin进行压缩缩小?例如,我有这个选择器:.some-thing,输出应该是:.x

这里有什么不工作:

app.vue

<template>
  <div class="some">appp</div>
</template>

<script>
export default {
  name: 'App',
};
</script>

<style lang="scss" scoped>
 .some { border:1px solid red;}
</style>

我运行这个命令(哪个 vue cli 构建):

npm run build (for production).

我的完整 vue.config.js:

const merge = require('lodash/merge');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

module.exports = {

  css: {
    loaderOptions: {
      sass: {
        prependData: `
              @import "~@/sass/mixins.scss";
            `,
      },
    },
  },

  configureWebpack: {
    optimization: {
      minimizer: [
        new UglifyJsPlugin({
          uglifyOptions: {
            warnings: false,
            parse: {},
            compress: {},
            mangle: true, // Note `mangle.properties` is `false` by default.
            output: null,
            toplevel: false,
            nameCache: null,
            ie8: false,
            keep_fnames: false,
          },
        }),
      ],
    },
  },

};

css/app.css 内容如下:

.some[..] {border:1px solid red;}...

** 编辑 ** 在@Tony Ngo 回答之后:

const LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const CompressionPlugin = require('compression-webpack-plugin');

module.exports = {

  css: {
    loaderOptions: {
      sass: {
        prependData: `
              @import "~@/sass/mixins.scss";
            `,
      },
    },
  },

  configureWebpack: {
    optimization: {
      minimizer: [
        new UglifyJsPlugin({
          cache: true,
          parallel: true,
          sourceMap: false,
          extractComments: 'all',
          uglifyOptions: {
            compress: true,
            output: null,
          },
        }),
        new OptimizeCSSAssetsPlugin({
          cssProcessorOptions: {
            safe: true,
            discardComments: {
              removeAll: true,
            },
          },
        }),
      ],
    },
    plugins: [
      new CompressionPlugin({
        test: /\.(js|css)/,
      }),
      new UglifyJsPlugin(),
    ],
  },

  chainWebpack: (config) => {
    // nothing here yet
  },
};

.some 仍然在我的 app.css 包中。我想缩小和压缩,所以我希望像.x

【问题讨论】:

    标签: javascript css node.js webpack sass


    【解决方案1】:

    你可以查看我的完整代码here

    这里是丑化代码所需的基本设置

    module.exports = {
        optimization: {
            minimizer: [
                new UglifyJsPlugin({
                    cache: true,
                    parallel: true,
                    sourceMap: false,
                    extractComments: 'all',
                    uglifyOptions: {
                        compress: true,
                        output: null
                    }
                }),
                new OptimizeCSSAssetsPlugin({
                    cssProcessorOptions: {
                        safe: true,
                        discardComments: {
                            removeAll: true,
                        },
                    },
                })
            ]
        },
        plugins: [
            new MiniCssExtractPlugin({
                filename: "[name].css",
                chunkFilename: "[id].css"
            }),
            new CompressionPlugin({
                test: /\.(js|css)/
            }),
            new UglifyJsPlugin(),
        ],
        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"
                }
            ]
        }
    };
    

    【讨论】:

    • 好吧,我照你说的做了。但是当我打开 app.css 时,我仍然看到 .some 类。如果有帮助 - 我可以将我的问题编辑到我的新 vue.config.js
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-15
    • 2019-12-05
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    • 2016-04-26
    • 1970-01-01
    相关资源
    最近更新 更多