事情是這樣的,之前在npm上發(fā)了兩個包鼠证,一個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')
})
然后當我在項目中同時使用這兩個包的時候量九,發(fā)現(xiàn)動畫部分有誤,和我當初寫的不同颂碧,于是打開source查看css源文件荠列,發(fā)現(xiàn)我在src中寫的@keyframes的名字變成了一個字母a,并且兩個包都是a载城,于是查詢OptimizeCssAssetsPlugin相關(guān)資料肌似,發(fā)現(xiàn)和cssnano的配置有關(guān),默認的是按最優(yōu)最小壓縮的诉瓦,所以keyframes名也壓縮了川队,導(dǎo)致同時使用這兩個包會造成沖突...
大概就是這樣受楼,經(jīng)過文檔和查詢,下面是解決該沖突的方法:
new OptimizeCssAssetsPlugin({
assetNameRegExp: /\.css$/g,
cssProcessor: require('cssnano')({
reduceIdents: false,// https://github.com/ben-eb/cssnano/issues/247
}),
cssProcessorOptions: { discardComments: {removeAll: true } },
canPrint: true
}),
配置后附上答案地址呼寸,有興趣的可以去看看艳汽,希望能幫到某些人!