cssnano压缩keyframes名的问题

事情是这样的,之前在npm上发了两个包,一个miniprogress和一个mininotice,用的webpack打包,配置如下:

const path = require('path')
const htmlWebpackPlugin = require('html-webpack-plugin')
const webpack = require('webpack')
const ExtractTextPlugin = require("extract-text-webpack-plugin")
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');

const css_extract = new ExtractTextPlugin({
    filename:"notice.css"
});

webpackConfig = {
    entry: path.resolve(__dirname, './src/notice.js'),
    output: {
        path: path.resolve(__dirname, './lib/'),
        filename: 'notice.js',
        publicPath: '/',
        library: "mininotice",
        libraryTarget: "umd"
    },
    module: {
        rules: [{
            test: /\.js$/,
            exclude: /(node_modules|bower_components)/,
            use: {
                loader: 'babel-loader'
            }
        }, {
            test: /\.css$/,
            use: css_extract.extract({
                fallback: "style-loader",
                use: [{
                    loader: "css-loader",
                    options: {
                        modules: true,
                        localIdentName: '[path][name]__[local]--[hash:base64:5]'
                    }
                }, {
                    loader: "postcss-loader"
                }]
            })
        }]
    },
    plugins: [
    new webpack.optimize.UglifyJsPlugin({
        compress: {
            warnings: false
        }
    }),
    new OptimizeCssAssetsPlugin({
        assetNameRegExp: /\.css$/g,
        cssProcessor: require('cssnano')(),
        cssProcessorOptions: { discardComments: {removeAll: true } },
        canPrint: true
    }),
    css_extract
    ]
}
webpack(webpackConfig, function (err, stats) {
    if (err) throw err
        process.stdout.write(stats.toString({
            colors: true,
            modules: false,
            children: false,
            chunks: false,
            chunkModules: false
        }) + '\n\n')
    console.log('pack complete')
})

然后当我在项目中同时使用这两个包的时候,发现动画部分有误,和我当初写的不同,于是打开source查看css源文件,发现我在src中写的@keyframes的名字变成了一个字母a,并且两个包都是a,于是查询OptimizeCssAssetsPlugin相关资料,发现和cssnano的配置有关,默认的是按最优最小压缩的,所以keyframes名也压缩了,导致同时使用这两个包会造成冲突...
大概就是这样,经过文档和查询,下面是解决该冲突的方法:

new OptimizeCssAssetsPlugin({
        assetNameRegExp: /\.css$/g,
        cssProcessor: require('cssnano')({
            reduceIdents: false,// https://github.com/ben-eb/cssnano/issues/247
        }),
        cssProcessorOptions: { discardComments: {removeAll: true } },
        canPrint: true
    }),

配置后附上答案地址,有兴趣的可以去看看,希望能帮到某些人!

你可能感兴趣的:(cssnano压缩keyframes名的问题)