Webpack 4 - 缩小从 SCSS 编译而来的 CSS

Webpack 4 - Minify CSS that was compiled from SCSS

开发一个wordpress站点并使用webpack打包资源。

我有一个设置,其中我正在将 SCSS 编译为 CSS 等。我想缩小输出 CSS 并删除注释。我尝试添加 optimize-css-assets-webpack-plugin 并配置它 as example suggests,但它不起作用(没有错误)...

那么我该如何修改这个 webpack 配置,以便 (1) 去除注释和 (2) 缩小输出?

webpack.config.js:

const path = require("path");
const config = require('./config.js');
const BrowserSyncPlugin = require('browser-sync-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

module.exports = {
  mode: 'production',
  entry: ["./src/app.js", "./src/scss/style.scss"],
  output: {
    path: path.resolve(__dirname, "wp-content/themes/ezdigital"),
    filename: "js/[name].js"
  },
  module: {
    rules: [
      {
        test: /\.scss$/,
        use: [
          {
            loader: "file-loader",
            options: {
              name: "[name].css"
            }
          },
          {
            loader: "extract-loader"
          },
          {
            loader: "css-loader?-url"
          },
          {
            loader: "postcss-loader"
          },
          {
            loader: "sass-loader"
          }
        ]
      },
      {
        test: /\.js$/,
        exclude: /(node_modules)/,
        use: {
          loader: "babel-loader",
          options: {
            presets: ["env"]
          }
        }
      },
      {
        test: /\.(png|svg|jpg|gif)$/,
        use: ["file-loader"]
      }
    ]
  },
  //remove comments from JS files
  optimization: {
    minimizer: [
      new UglifyJsPlugin({
        uglifyOptions: {
          output: {
            comments: false,
          },
        },
      }),
    ],
  },
  plugins: [
    new BrowserSyncPlugin( {
            proxy: config.url,
            files: [
                '**/*.php'
            ],
            reloadDelay: 0
        }
  )
]

};

额外postss.config.js:

module.exports = {
    plugins: {
        'autoprefixer': {}
    }
}

Package.json:

{
  "name": "EZTheme",
  "version": "1.0.0",
  "description": "EZ Theme",
  "main": "index.js",
  "scripts": {
    "test": "test",
    "build": "webpack",
    "start": "webpack --watch",
    "serve": "webpack-dev-server --open"
  },
  "repository": {
    "type": "git",
    "url": ""
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": ""
  },
  "homepage": ""
  "devDependencies": {
    "@fortawesome/fontawesome-free": "^5.6.3",
    "autoprefixer": "^9.3.1",
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-preset-env": "^1.7.0",
    "browser-sync-webpack-plugin": "^2.2.2",
    "css-loader": "^1.0.1",
    "exports-loader": "^0.7.0",
    "extract-loader": "^3.1.0",
    "extract-text-webpack-plugin": "^4.0.0-alpha.0",
    "file-loader": "^2.0.0",
    "jquery": "^3.3.1",
    "node-sass": "^4.11.0",
    "optimize-css-assets-webpack-plugin": "^5.0.1",
    "popper.js": "^1.14.6",
    "postcss-cli": "^6.0.1",
    "postcss-loader": "^3.0.0",
    "sass-loader": "^7.1.0",
    "style-loader": "^0.23.1",
    "uglifyjs-webpack-plugin": "^2.1.1",
    "webpack": "^4.28.2",
    "webpack-cli": "^3.1.2",
    "webpack-dev-server": "^3.1.14"
  },
  "dependencies": {
    "bootstrap": "^4.1.3"
  }
}

原来问题是我试图使用 extract-text-webpack-plugin,它不适用于 webpack 4。

相反,我切换到 mini-css-extract-plugin 以及 optimize-css-assets-webpack-插件

我的新 webpack.config.js 文件:

const path = require("path");
const config = require('./config.js');
const BrowserSyncPlugin = require('browser-sync-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
var OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {
  mode: 'production',
  entry: ["./src/app.js", "./src/scss/style.scss"],
  output: {
    path: path.resolve(__dirname, "wp-content/themes/ezdigital"),
    filename: "js/[name].js"
  },
  module: {
    rules: [
      {
        test: /\.scss$/,
        use: [
          {
            loader: "file-loader",
            options: {
              name: "[name].css"
            }
          },
          {
            loader: "extract-loader"
          },
          {
            loader: "css-loader?-url"
          },
          {
            loader: "postcss-loader"
          },
          {
            loader: "sass-loader"
          }
        ]
      },
      {
        test: /\.js$/,
        exclude: /(node_modules)/,
        use: {
          loader: "babel-loader",
          options: {
            presets: ["env"]
          }
        }
      },
      {
        test: /\.(png|svg|jpg|gif)$/,
        use: ["file-loader"]
      }
    ]
  },
  //remove comments from JS files
  optimization: {
    minimizer: [
      new UglifyJsPlugin({
        uglifyOptions: {
          output: {
            comments: false,
          },
        },
      }),
      new OptimizeCSSAssetsPlugin({
        cssProcessorPluginOptions: {
          preset: ['default', { discardComments: { removeAll: true } }],
        }
      })
    ],
  },
  plugins: [
  new MiniCssExtractPlugin({
    filename: "[name].css"
  }),
    new BrowserSyncPlugin( {
            proxy: config.url,
            files: [
                '**/*.php'
            ],
            reloadDelay: 0
        }
  )
]

};