无法缩小CSS文件

时间:2018-04-25 10:46:10

标签: webpack minify webpack-4

我尝试将 Webpack 4 用于我的项目。除extract-text-webpack-plugin以外,所有插件都有效。

实际行为:当我构建项目时,根本没有错误和缩小文件

预期行为:在styles.css文件夹中获取缩小的CSS文件(dist

输出

terminal

文件结构

files

webpack.config

const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
    entry: {
        'index': './src/index.js',
    },
    resolveLoader: {
        modules: [
            'node_modules',
        ],
    },
    mode: 'production',
    module: {
        rules: [
            {
                test: /\.html$/,
                use: [
                    {
                        loader: 'html-loader',
                        options: {
                            minimize: true,
                        },
                    },
                ],
            },
            {
                test: /\.css$/,
                use: ExtractTextPlugin.extract( 'css-loader' ),
            },
            {
                test: /\.(png|jpg|gif)$/,
                use: [
                    {
                        loader: 'file-loader',
                        options: {
                            name: '[path][name].[ext]',
                            emitFile: false,
                        },
                    },
                ],
            },
        ],
    },
    plugins: [
        new ExtractTextPlugin( {
            filename: './src/styles.css',
        }),
        new HtmlWebpackPlugin({
            template: './src/index.html',
            filename: 'index.html',
            inject: 'body',
            hash: true,
            minify: {
                removeAttributeQuotes: true,
                collapseWhitespace: true,
                html5: true,
                removeComments: true,
                removeEmptyAttributes: true,
                minifyCSS: true,
            },
        }),
        new UglifyJsPlugin({
            cache: true,
            parallel: true,
            uglifyOptions: {
                compress: false,
                ecma: 6,
                mangle: true,
            },
            sourceMap: true,
        }),
    ],
};

1 个答案:

答案 0 :(得分:3)

你需要:

  1. 将样式表添加到入口点

      

    entry: ['./src/index.js', './src/styles.css']

  2. 将选项添加到ExtractTextPlugin

    的规则中
    use: ExtractTextPlugin.extract({
       loader: 'css-loader',
       options: {
           minimize: true,
       },
    })
    
  3. plugins

  4. 中更改文件的路径名
      

    filename: './styles.css'

    完整配置

    const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
    const HtmlWebpackPlugin = require('html-webpack-plugin');
    const ExtractTextPlugin = require('extract-text-webpack-plugin');
    
    module.exports = {
        entry: ['./src/index.js', './src/styles.css'],
        resolveLoader: {
            modules: [
                'node_modules',
            ],
        },
        mode: 'production',
        module: {
            rules: [
                {
                    test: /\.html$/,
                    use: [
                        {
                            loader: 'html-loader',
                            options: {
                                minimize: true,
                            },
                        },
                    ],
                },
                {
                    test: /\.css$/,
                    use: ExtractTextPlugin.extract({
                        loader: 'css-loader',
                        options: {
                            minimize: true,
                        },
                    }),
                },
                {
                    test: /\.(png|jpg|gif)$/,
                    use: [
                        {
                            loader: 'file-loader',
                            options: {
                                name: '[path][name].[ext]',
                                emitFile: false,
                            },
                        },
                    ],
                },
            ],
        },
        plugins: [
            new ExtractTextPlugin( {
                filename: './styles.css',
            }),
            new HtmlWebpackPlugin({
                template: './src/index.html',
                filename: 'index.html',
                inject: 'body',
                hash: true,
                minify: {
                    removeAttributeQuotes: true,
                    collapseWhitespace: true,
                    html5: true,
                    removeComments: true,
                    removeEmptyAttributes: true,
                    minifyCSS: true,
                },
            }),
            new UglifyJsPlugin({
                cache: true,
                parallel: true,
                uglifyOptions: {
                    compress: false,
                    ecma: 6,
                    mangle: true,
                },
                sourceMap: true,
            }),
        ],
    };