简体   繁体   English

Webpack:SCSS 到 CSS 文件

[英]Webpack: SCSS to CSS file

How do I take my SCSS source and compile it into a (bonus: minified) CSS file?如何获取我的 SCSS 源并将其编译为(奖励:缩小)CSS 文件?

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.阅读文档和搜索 web 清楚的一件事是,每个人似乎都在煮自己的汤,对于这样的常见任务没有标准/推荐的方式。

This is what I have done here这就是我在这里所做的

First I will transform my scss to css file using this setting in webpack首先,我将使用 webpack 中的此设置将我的 scss 转换为 css 文件

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.然后我使用 webpack-shell-plugin 运行 postcss 命令来缩小 css 文件。 (Bonus I'm also add gzip using Compression plugin to transform css to gzip) (奖金我还使用压缩插件添加 gzip 将 css 转换为 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: 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: 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 . node-sass模块已经输出了缩小的 CSS,所以如果你知道在你的项目中不会遇到任何原始的 CSS,你可以去掉OptimizeCssAssetsPlugin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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