无法使用Webpack缩小CSS代码

时间:2018-08-09 03:15:23

标签: javascript css webpack minify webpack-4

Webpack版本:4.16.3

所有编译成功。
在bundle.css中编译后,我的代码没有缩小。
我尝试在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]'
      }
    ]
  }
};

5 个答案:

答案 0 :(得分:4)

对于高于4的webpack版本,您可能希望使用mini-css-extract-plugin而不是ExtractTextPlugin插件

答案 1 :(得分:4)

使用OptimizeCSSAssetsPlugin最小化CSS资产,提取器仅用于分离输出资产。请注意,最小化适用于生产模式,即确保在webpack build命令中传递“ --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
                    })
                ]
         }
    ....}

答案 2 :(得分:0)

这是我的配置。我认为您滥用use

const MiniCssExtractPlugin = require("mini-css-extract-plugin");
{....,
test: /\.(scss|sass)$/,
use: [
    process.env.NODE_ENV !== 'production' ? 'style-loader' : MiniCssExtractPlugin.loader, // creates style nodes from JS strings
    {// translates CSS into CommonJS
       loader: 'css-loader',
       options: {
          importLoaders: 1,
          minimize: true
       }
    },
    'postcss-loader',
    "sass-loader" // compiles Sass to CSS
]
}

答案 3 :(得分:0)

答案 4 :(得分:-1)

完整的 Webpack 5 示例

这是一个完整的例子。不区分生产和开发,但应该很简单。

它产生 dist/main.css,这是一个包含两者的缩小的 CSS:

  • 我们的main.scss
  • normalize.css 通过 node_modules

在 index.html 测试文件中可以看到两者的效果。

webpack.config.js

const path = require('path');

const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

const nodeModulesPath = path.resolve(__dirname, 'node_modules');

module.exports = {
  entry: {
    main: ['./main.scss'],
  },
  mode: 'none',
  module: {
    rules: [
      {
        test: /\.(scss|css)$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          {
            loader: "sass-loader",
            options: {
              sassOptions: {
                includePaths: [nodeModulesPath],
              },
            },
          },
        ],
      },
    ],
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: '[name].css',
    }),
  ],
  optimization: {
    minimizer: [
      new CssMinimizerPlugin(),
    ],
    minimize: true,
  }
};

package.json

{
  "name": "webpack-cheat",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "build": "webpack",
    "sass": "rm -f dist/main.css && sass main.scss dist/main.css"
  },
  "author": "Ciro Santilli",
  "license": "MIT",
  "devDependencies": {
    "css-loader": "5.2.4",
    "css-minimizer-webpack-plugin": "3.0.2",
    "mini-css-extract-plugin": "2.1.0",
    "normalize.css": "8.0.1",
    "sass": "1.32.11",
    "sass-loader": "11.0.1",
    "style-loader": "2.0.0",
    "webpack": "5.36.1",
    "webpack-cli": "4.6.0",
    "webpack-dev-server": "3.11.2"
  }
}

main.scss

@use "normalize.css/normalize.css";

body {
  background-color: red;
}

index.html

<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>Sass import</title>
<link rel="stylesheet" href="dist/main.css">
</head>
<body>
<p>Hello</p>
</body>
</html>