Webpack:如何合并css和更少,然后应用cssnano

时间:2017-09-20 12:32:24

标签: webpack source-maps webpack-3 cssnano less-loader

我设法将我的css和更少的资源(从javascript“导入”)编译为all-my-LESS|CSS,使用ExtractTextPlugin提取它们并将它们与MergeFilesPlugin合并到combinedStyles.css

我缺少的一点:如何运行cssnano(例如通过postcss?)作为结束位? (哦,虽然我有内联源代码地图,但我没有设法生成外部combinedStyles.map文件。)

这是我的合并webpack.config.babel.js(忽略babel位,只是意味着,你可以用ES6编写它,带有更高级的import语句):

import path from 'path';

const webpack = require('webpack'); //to access built-in plugins
import ExtractTextPlugin from 'extract-text-webpack-plugin';
import MergeFilesPlugin from 'merge-files-webpack-plugin';

const extractCSS = new ExtractTextPlugin("all-my-CSS.css");
const extractLESS = new ExtractTextPlugin("all-my-LESS.css");

export default {
    entry: [ './src/index_5.js' ],
    output: {
        path: path.resolve( 'dist')
        filename: 'bundle.js',
        sourceMapFilename: './bundle.js.map'
    },

    module: {
        rules: [{
                test: /\.css$/,
                use: extractCSS.extract(
                    [{  // This is basically "use"
                        loader: "css-loader",
                        options: {
                            minimize: false, // done later
                            sourceMap: true,
                            modules: false, // no css modules, all global styles
                        }
                    }]
                )
            },
            {
                test: /\.less$/,
                use: extractLESS.extract(  // This is basically "use"
                    [// No style-loader here! We want this external!
                    {
                        loader: "css-loader", // translates CSS into CommonJS
                        options: {
                            minimize: false,
                            sourceMap: true
                        }
                    }, {
                        loader: "less-loader", // compiles Less to CSS
                        options: {
                            sourceMap: true,
                        }
                    }]
                )
            }
        ] // rules
    }, // module

    devtool: 'inline-source-map',
    devServer: { inline: true },

    plugins: [
        extractCSS,
        extractLESS,
        new MergeFilesPlugin({
            filename: 'combinedStyles.css', //output filename
            test: /\.css$/,
            deleteSourceFiles: false // for now
        })
    ]
};

1 个答案:

答案 0 :(得分:1)

我已将postcss-loader添加到css-nano,请检查是否有帮助。

此外我没有看到这里需要使用MergeFilesPlugins(只是我的意见)。 ExtractTextPlugin在这里很有用。

只需使用一个ExtractTextPlugin并将所有css或更少的文件放在一个文件夹中(使用.css或.less ext),将有选择地将加载器应用于它们,稍后在插件中使用

new ExtractTextPlugin('style.css')

获取一个常见的css文件。

使用cssnano:

[{
                test: /\.css$/,
                use: ExtractTextPlugin.extract(
                    [{  // This is basically "use"
                        loader: "css-loader",
                        options: {
                            minimize: false, // done later
                            sourceMap: true,
                            modules: false, // no css modules, all global styles
                        }
                    },
                    {
                        loader: 'postcss-loader',
                        options: {
                                plugins: function () {
                                        return [
                                        require('cssnano')({
                                            autoprefixer: false,
                                            safe: true
                                            })
                                        ];
                                }
                           }
                    }]
                )
            },
            {
                test: /\.less$/,
                use: ExtractTextPlugin.extract(  // This is basically "use"
                    [// No style-loader here! We want this external!
                    {
                        loader: "css-loader", // translates CSS into CommonJS
                        options: {
                            minimize: false,
                            sourceMap: true
                        }
                    },
                    {
                        loader: 'postcss-loader',
                        options: {
                                plugins: function () {
                                        return [
                                        require('cssnano')({
                                            autoprefixer: false,
                                            safe: true
                                            })
                                        ];
                                 }
                            }
                    },
                    {
                        loader: "less-loader", // compiles Less to CSS
                        options: {
                            sourceMap: true,
                        }
                    }]
                )
            }
        ]