Webpack将CSS编译为CSS并最小化

时间:2019-07-28 10:01:17

标签: css node.js webpack sass sass-loader

我是webpack的新手,我在努力将scss转换为css然后最小化。

文件结构

 ?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

我正在像这样编译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"

我发现类似sass-loader的东西,但是无法正常工作。

webpack.config.js

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

我不知道将包含文件的路径放在哪里以及将输出文件的路径放在哪里。

如果有任何建议,我将不胜感激。

2 个答案:

答案 0 :(得分:1)

您可以使用mine config来做到这一点。我正在使用optimize-css-assets-webpack-plugin

您可以查看我的完整配置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"
                    }
                ]
            }
        ]
    }
};

但是我建议您使用postcss来缩小CSS。我正在使用WebpackShellPlugin运行minify命令

答案 1 :(得分:1)

如果您只是希望能够从Javascript模块中导入.scss文件并将其直接应用于DOM,则只需简单地遵循以下文档即可:

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

,然后将Postcss添加到组合中:

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

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': {}
  }
};

如果要将已编译的CSS提取到单独的文件中,请执行以下操作:

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

特别是:

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