简体   繁体   English

Webpack将CSS编译为CSS并最小化

[英]Webpack compile scss to css and minify

I am new with webpack and I am struggling how to convert scss to css and then minify. 我是webpack的新手,我正在努力如何将scss转换为css然后最小化。

File structure 档案结构

 📦public
     ┣ 📂dist
     ┃ ┣ 📂css
     ┃ ┗ 📂js
     ┃ ┃ ┗ 📜adminMain.js
     ┣ 📂src
     ┃ ┣ 📂css
     ┃ ┃ ┃ ┣ 📜admin.css
     ┃ ┃ ┃ ┣ 📜admin.css.map
     ┃ ┃ ┃ ┣ 📜admin.scss
     ┃ ┃ ┃ ┣ 📜main.css
     ┃ ┃ ┃ ┣ 📜main.css.map
     ┃ ┃ ┃ ┗ 📜main.scss
     ┃ ┗ 📂js
     ┃ ┃ ┗ 📜adminMain.js

I am compiling js like this 我正在像这样编译js

"dev": "webpack --mode development ./public/src/js/adminMain.js --output ./public/dist/js/adminMain.js",
"build": "webpack --mode production ./public/src/js/adminMain.js --output ./public/dist/js/adminMain.js"

I found thing like sass-loader but cant make it work. 我发现类似sass-loader东西,但无法使其工作。

webpack.config.js webpack.config.js

module.exports = {
    module: {
        rules: [{
            test: /\.scss$/,
            use: [{
                loader: "sass-loader",
                options: {
                    minimize: true
                }
            }]
        }]
    }
};

i dont know where to put path to included file and where to put path to output. 我不知道在哪里放置包含文件的路径以及在哪里放置输出路径。

I will be thankfull for any advice. 我将非常感谢您的任何建议。

You can use mine config to do that. 您可以使用我的配置来做到这一点。 I'm using optimize-css-assets-webpack-plugin 我正在使用optimize-css-assets-webpack-plugin

You can view my full config here 您可以在这里查看我的完整配置

const path = require("path");
const webpack = require("webpack");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CompressionPlugin = require("compression-webpack-plugin");
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const WebpackShellPlugin = require('webpack-shell-plugin');
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");

process.traceDeprecation = true;

module.exports = {
    output: {
        path: path.resolve(__dirname, "wwwroot/dist"),
        filename: "[name].js",
        publicPath: "/dist/"
    },
    optimization: {
        minimizer: [
            new UglifyJsPlugin({
                cache: true,
                parallel: true,
                sourceMap: false,
                extractComments: 'all',
                uglifyOptions: {
                    compress: true,
                    output: null
                }
            }),
            new OptimizeCSSAssetsPlugin({
                cssProcessorOptions: {
                    safe: true,
                    discardComments: {
                        removeAll: true,
                    },
                },
            })
        ]
    },
    plugins: [
        new webpack.ContextReplacementPlugin(/\.\/locale$/, 'empty-module', false, /jsx$/),
        new webpack.LoaderOptionsPlugin({
            options: {}
        }),
        new MiniCssExtractPlugin({
            filename: "[name].css",
            chunkFilename: "[id].css"
        }),
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery",
            Popper: ['popper.js', 'default']
        }),
        new CompressionPlugin({
            test: /\.(js|css)/
        }),
        new UglifyJsPlugin(),
        new WebpackShellPlugin({
            onBuildStart: ['echo "Starting postcss command"'],
            onBuildEnd: ['postcss --dir wwwroot/dist wwwroot/dist/*.css']
        })
    ],
    resolve: {
        modules: [
            path.resolve('./React/js/App'),
            path.resolve('./React/js/App/Modules/Client'),
            path.resolve('./React/js/App/Modules/Adnmin'),
            path.resolve('./node_modules')
        ]
    },
    module: {
        rules: [{
                test: /\.scss$/,
                use: [
                    'style-loader',
                    MiniCssExtractPlugin.loader,
                    {
                        loader: "css-loader",
                        options: {
                            minimize: true,
                            sourceMap: true
                        }
                    },
                    {
                        loader: "sass-loader"
                    }
                ]
            }
        ]
    }
};

But I would recommend you using postcss to minify css. 但是我建议您使用postcss来缩小CSS。 I'm using WebpackShellPlugin to run minify command 我正在使用WebpackShellPlugin运行minify命令

If you simply want to be able to import .scss files from your Javascript modules and have it directly applied to the DOM, you can simply follow this documentation first: 如果仅希望能够从Javascript模块导入.scss文件并将其直接应用于DOM,则可以首先简单地遵循以下文档:

https://webpack.js.org/loaders/sass-loader/ https://webpack.js.org/loaders/sass-loader/

and then add Postcss to the mix: 然后将Postcss添加到混合中:

https://github.com/postcss/postcss-loader https://github.com/postcss/postcss-loader

tldr; tldr;

npm install sass-loader node-sass style-loader css-loader postcss-loader cssnano --save-dev
// webpack.config.js
module.exports = {
  module: {
    rules: [
      {
        test: /\.scss$/,
        use: [
          'style-loader', // creates style nodes from JS strings
          {
            loader: 'css-loader', // translates CSS into CommonJS
            options: {
              importLoaders: 1
            }
          },
          'postcss-loader', // post process the compiled CSS
          'sass-loader' // compiles Sass to CSS, using Node Sass by default
        ]
      }
    ]
  }
};
// postcss.config.js
module.exports = {
  plugins: {
    'cssnano': {}
  }
};

If you want to extract the compiled CSS into a separate file, there is: 如果要将已编译的CSS提取到单独的文件中,请执行以下操作:

https://github.com/webpack-contrib/mini-css-extract-plugin https://github.com/webpack-contrib/mini-css-extract-plugin

Especially: 特别:

https://github.com/webpack-contrib/mini-css-extract-plugin#advanced-configuration-example https://github.com/webpack-contrib/mini-css-extract-plugin#advanced-configuration-example

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

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