如何在sass-loader和ExtractTextPlugin完成后运行POSTCSS?

时间:2016-03-21 01:59:03

标签: css sass webpack postcss

我想弄清楚如何在我的最终输出css文件上运行postcss。

'strict';

const path = require('path');
const webpack = require('webpack');
const StatsPlugin = require('stats-webpack-plugin');

/* POSTCSS Optimizations of CSS files */
const clean = require('postcss-clean');
const colorMin = require('postcss-colormin');
const discardDuplicates = require('postcss-discard-duplicates');
const discardEmpty = require('postcss-discard-empty');
const mergeRules = require('postcss-merge-rules');
const mergeLonghand = require('postcss-merge-longhand');
const minifyFonts = require('postcss-minify-font-values');
const orderedValues = require('postcss-ordered-values');
const uniqueSelectors = require('postcss-unique-selectors');

/* EXTRACT CSS for optimization and parallel loading */
const ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
    entry: './src/index',
    output: {
        path: path.join(__dirname, 'dist'),
        filename: '[name].js',
        chunkFilename: '[id].bundle.js',
        publicPath: '/dist/',
        soureMapFilename: '[file].map'
    },
    plugins: [
        new webpack.optimize.OccurenceOrderPlugin(),
        new webpack.NoErrorsPlugin(),
        new StatsPlugin('stats.json'),
        new ExtractTextPlugin('assets/css/[name].css?[hash]-[chunkhash]-[contenthash]-[name]', {
            disable: false,
            allChunks: true
        })
    ],
    node: {
        net: 'empty',
        tls: 'empty',
        dns: 'empty'
    },
    module: {
        loaders: [{
            test: /\.js$/,
            loaders: ['babel'],
            exclude: /node_modules/,
            include: __dirname
        },
        {
            test: /\.scss$/i,
            loader: ExtractTextPlugin.extract('style', ['css', 'postcss', 'sass'])
        },
        {
            test: /\.css$/,
            loader: ExtractTextPlugin.extract('style', ['css'])
        },
        {
            test: /\.(eot|woff|woff2|ttf|svg|png|jpg)$/,
            loader: 'url-loader?limit=30000&name=[name]-[hash].[ext]'
        }]
    },
    postcss() {
        return [mergeRules, mergeLonghand, colorMin, clean, discardEmpty,
                orderedValues, minifyFonts, uniqueSelectors, discardDuplicates];
    },
    sassLoader: {
        includePaths: [path.resolve(__dirname, './node_modules')]
    }
};

我当前的配置适用于编译所有依赖的SASS并将其与静态CSS导入并使用ExtractTextPlugin提取它们。

似乎我可以在CSS的块上运行POSTCSS优化,但不是最终产品。这意味着我无法摆脱重复的CSS规则。

如何在最终状态CSS文件上运行POSTCSS后,sass-loader和extractTextPlugin已经发挥了作用?

2 个答案:

答案 0 :(得分:9)

我遇到问题,使用ExtractTextPlugin进行类似的设置工作,我的问题在于我如何使用ExtractTextPlugin插件。

我只是将它用于生产版本,这对我有用:

module: {
    loaders: [
        {   // styles
            test: /\.scss$/,
            include: [ path.join(__dirname, 'source/styles') ],
            loader: ExtractTextPlugin.extract('style', ['css', 'postcss', 'sass'])
        }
    ]
},

注意['css','postcss','sass']的数组。那是我失踪的部分。此数组将首先解析然后运行将由插件最终提取的样式。

而且,在我正在使用new ExtractTextPlugin('styles-[chunkhash].css')的插件数组上。

答案 1 :(得分:0)

由于ExtractTextPlugin目前不支持" onComplete"回调或类似,您可以使用WebpackShellPlugin

设置一个运行所需var input = document.GetElementByID('inputBox').innerHTML; 插件的脚本,并在postcss中执行您的脚本以处理已编译的CSS。