简体   繁体   中英

Webpack: SCSS to CSS file

How do I take my SCSS source and compile it into a (bonus: minified) CSS file?

One thing that reading to documentation and searching the web makes clear is that everyone seems to be cooking their own soup, there's no standard/recommended way for such a common task.

This is what I have done here

First I will transform my scss to css file using this setting in webpack

module: {
        rules: [{
                test: /\.scss$/,
                use: [
                    'style-loader',
                    MiniCssExtractPlugin.loader,
                    {
                        loader: "css-loader",
                        options: {
                            minimize: true,
                            sourceMap: true
                        }
                    },
                    {
                        loader: "sass-loader"
                    }
                ]
            }
        ]
    }

Then I'm using webpack-shell-plugin to run postcss command to minify css file. (Bonus I'm also add gzip using Compression plugin to transform css to gzip)

plugins: [
        new CompressionPlugin({
            test: /\.(js|css)/
        }),
        new WebpackShellPlugin({
            onBuildStart: ['echo "Starting postcss command"'],
            onBuildEnd: ['postcss --dir wwwroot/dist wwwroot/dist/*.css']
        })
    ],

Below a minimal example that worked for me:

package.json:

{
  …
  "devDependencies": {
    "css-loader": "^3.2.0",
    "mini-css-extract-plugin": "^0.8.0",
    "node-sass": "^4.12.0",
    "optimize-css-assets-webpack-plugin": "^5.0.3",
    "sass-loader": "^8.0.0",
    "webpack": "^4.40.2",
    "webpack-cli": "^3.3.9"
  }
}

webpack.config.js:

const path = require("path");
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');

module.exports = {
  …
  module: {
    rules: [
      { // CSS assets remaining in the pipeline (e.g. frameworks)
        test: /\.css$/,
        use: [
          "style-loader", // or e.g. "vue-style-loader" etc. (optional)
          MiniCssExtractPlugin.loader,
          "css-loader"
        ]
      },
      { // SCSS
        test: /\.scss$/,
        use: [
          "style-loader" // (optional)
          MiniCssExtractPlugin.loader,
          "css-loader",
          "sass-loader"
        ]
      }
    ]
  },

  // Plugins
  plugins: [
    new MiniCssExtractPlugin({
      filename: "./css/my-style.css" // relative to `output.path` by default
    }),
    new OptimizeCssAssetsPlugin() // construction suffices, no additional calls needed
  ]
};

The node-sass module already outputs minified CSS, so if you know you won't encounter any raw CSS in your project, you can get rid of the OptimizeCssAssetsPlugin .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM