javascript - 无法使用 webpack 缩小 css 代码

标签 javascript css webpack minify webpack-4

网络包版本:4.16.3

全部编译成功。
我在 bundle.css 中编译后的代码不是 minify。
我尝试在 text-webpack-plugin 中使用 minimize: true,但它不起作用。

对于编译,我在命令行中使用命令:webpack 在我的工作目录中

我做错了什么?

我的 wepback 配置:

'use strict'

const webpack = require("webpack");
const ExtractTextPlugin = require('extract-text-webpack-plugin')

module.exports = {
  context: __dirname,
  mode: 'production',
  entry: __dirname + "/js/init.js",
  output: {
    path: __dirname + "/dist",
    filename: "bundle.js"
  },
  plugins: [
    new webpack.ProvidePlugin({
      $: "jquery",
      jQuery: "jquery",
      noUiSlider: 'nouislider',
      Vue: 'vue'
    }),
    new ExtractTextPlugin("bundle.css")
  ],
  module: {
    'rules': [
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          fallback: "style-loader",
          use: {
            loader: "css-loader",
            options: {
              minimize: true
            }
          }
        })
      }
      , {
        test: /\.less$/,
        use: ExtractTextPlugin.extract({
          fallback: "style-loader",
          use: {
            loader: "css-loader!less-loader",
          }
        })
      }, {
        test: /\.(png|jpe?g|gif|cur)$/,
        loader: 'url-loader?limit=8192&name=imgs/[name]-[hash].[ext]'
      }
    ]
  }
};

最佳答案

使用OptimizeCSSAssetsPlugin为了缩小 css Assets ,提取器仅用于分离输出 Assets 。请注意,缩小适用于生产模式,即确保在 webpack 构建命令中传递“--mode production”。

    {....,
        optimization: {
                minimizer: [
                   //Incase you want to uglify/minify js
                    new UglifyJsPlugin({
                        cache: true,
                        parallel: true,
                        sourceMap: true
                    }),
                    new OptimizeCSSAssetsPlugin({
                        cssProcessorOptions: { discardComments: { removeAll: true } },
                        canPrint: true
                    })
                ]
         }
    ....}

关于javascript - 无法使用 webpack 缩小 css 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51758302/

相关文章:

reactjs - Webpack 将 React 组件拆分为 "0.js" "1.js"等

node.js - nodemon 在 package.json 中执行 "prestart"脚本

javascript - 如何在 javascript 中依次激活 3 个不同的函数?

javascript - jQuery:多个音频播放器按钮

javascript - c能否在我的脚本执行之前触发 DOM 事件?

jquery - CSS3 焦点动画

html - 基于 Zurb 的 Foundation 3.2.5 的布局和 Opera 中某些页面的额外宽度

html - 当我应用 HTML5 shiv 时,为什么我的 HTML5 标签没有在 IE8 中设置样式?

javascript - Laravel Vue 无法加载环境变量

javascript - jQuery滚动图像动画: Moving in the wrong direction