webpack css optimization and minify

萝らか妹 提交于 2019-12-10 16:13:09

问题


I use webpack 4 and need to optimize and minify css after build comand. I found optimize-css-assets-webpack-plugin but this plugin doesn't work. Do you use these plugins or loaders for optimizing css or not? Do we need to use it or not?


回答1:


With webpack 4 you need to bring your own. To minify the output, use a plugin like optimize-css-assets-webpack-plugin. However, note that setting optimization.minimizer overrides the defaults provided by webpack, so make sure to also specify a JS minimizer:

const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");

module.exports = {
  optimization: {
    minimizer: [
      new UglifyJsPlugin({
        cache: true,
        parallel: true,
        sourceMap: true // set to true if you want JS source maps
      }),
      new OptimizeCSSAssetsPlugin({})
    ]
  },
  plugins: [

  ],
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          "css-loader"
        ]
      }
    ]
  }
}


来源:https://stackoverflow.com/questions/50628268/webpack-css-optimization-and-minify

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!