【发布时间】:2018-06-16 12:18:25
【问题描述】:
我只是声明一个key-frames:
@keyframes spinner {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
但是在Webpack 为我制作的CSS 文件中,我看到了:
animation: 1s infinite alternate :local(spinner)
什么是:local???
我搜索并找到了一些解决方案,但为什么它出现在 css 中?
一些解决方案是,在我使用声明的动画的地方我必须使用:global:
:global .i-spinner4 {
animation: 1s infinite alternate spinner;
}
我使用了我找到的那个解决方案,它是正确的,但是在 webpack -p css 版本中它完全消失了,这意味着 prod 版本中的 PostCSS 解析器完全忽略它......
我该如何解决这个问题???
这是我的开发版webpack 配置:
const path = require('path');
const ExtractTextPlugin = require("extract-text-webpack-plugin"),
CleanWebpackPlugin = require('clean-webpack-plugin');
const DistDir = path.resolve(__dirname, './dist'),
SrcDir = path.resolve(__dirname, './src');
module.exports = {
resolve: {
alias: {
AppRoot: path.resolve(__dirname, './src/app'),
PcssRoot: path.resolve(__dirname, './src/pcss')
}
},
module: {
rules: [
{
test: /\.js$/,
exclude: ['src/app/redux', 'node_modules'],
include: SrcDir,
loader: 'babel-loader'
},
{
test: /\.pcss$/,
exclude: /node_modules/,
include: SrcDir,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {
importLoaders: 1,
modules: true,
localIdentName: '[local]',
sourceMap: true
}
},
{
loader: 'postcss-loader',
options: {
ident: 'postcss',
sourceMap: true,
syntax: 'postcss-scss',
map: true,
plugins: () => ([
require('postcss-partial-import')({
prefix: "_",
extension: ".pcss",
glob: false,
path: ['./src/pcss']
}),
require('postcss-nested-ancestors'),
require('postcss-apply'),
require('postcss-custom-properties'),
require('postcss-nested'),
require('postcss-cssnext')({
features: {
nesting: false
},
warnForDuplicates: false
}),
require('postcss-extend'),
require('css-mqpacker')({
sort: true
}),
require('autoprefixer')({
browsers: [`last 15 versions`]
})
])
}
}
]
})
},
{
test: /\.(woff|woff2|eot|ttf|svg)$/,
exclude: /node_modules/,
loader: 'file-loader',
options: {
limit: 1024,
name: '[name].[ext]',
publicPath: '../font/',
outputPath: 'asset/font/'
}
}
]
},
plugins: [
new ExtractTextPlugin({
filename: `asset/css/style.css`
}),
new CleanWebpackPlugin(`${DistDir}/asset`)
],
entry: {
"sd": `${SrcDir}/app/index.js`
},
externals: {
config: JSON.stringify(require(SrcDir + '/config/config.dev.json'))
},
output: {
path: DistDir,
filename: "asset/js/[name].bundle.js"
},
devServer: {inline: true},
devtool: 'source-map'
};
这是我的产品版本webpack:
const path = require('path');
const ExtractTextPlugin = require("extract-text-webpack-plugin"),
CleanWebpackPlugin = require('clean-webpack-plugin');
const DistDir = path.resolve(__dirname, './dist'),
SrcDir = path.resolve(__dirname, './src');
let q = [],
cache = {};
function randomNaming(length,limit) {
let result = '',
chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_',
fchars = 'abcdefghjklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_';
do {
if (q.length >= (52 * Math.pow(64, limit - 1)) && limit >= length) {
return 'OutOfPossibility';
} else if (q.length >= (52 * Math.pow(64, limit - 1)) && limit < length) {
++limit;
}
result = '';
result += fchars[Math.floor(Math.random() * fchars.length)];
for (let i = limit - 1; i > 0; --i) {
result += chars[Math.floor(Math.random() * chars.length)];
}
} while (q.includes(result));
q.push(result);
return result;
}
module.exports = {
resolve: {
alias: {
AppRoot: path.resolve(__dirname, './src/app'),
PcssRoot: path.resolve(__dirname, './src/pcss')
}
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
include: SrcDir,
loader: 'babel-loader'
},
{
test: /\.pcss$/,
exclude: /node_modules/,
include: SrcDir,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {
importLoaders: 1,
modules: true,
getLocalIdent: (loaderContext, localIdentName, localName, options) => {
var randName = randomNaming(3,2);
if (localName.match(/^i-/i)) {
randName = `i-${randName}`;
} else if (localName.match(/^i_/i)) {
randName = `i_`;
} else {
randName = `${randName}`;
}
if (typeof cache[localName] == 'undefined') {
cache[localName] = randName;
return cache[localName];
} else {
return cache[localName];
}
}
}
},
{
loader: 'postcss-loader',
options: {
ident: 'postcss',
syntax: 'postcss-scss',
plugins: () => ([
require('postcss-partial-import')({
prefix: "_",
extension: ".pcss",
glob: false,
path: ['./src/pcss']
}),
require('postcss-nested-ancestors'),
require('postcss-apply'),
require('postcss-custom-properties'),
require('postcss-nested'),
require('postcss-cssnext')({
features: {
nesting: false
},
warnForDuplicates: false
}),
require('postcss-extend'),
require('css-mqpacker')({
sort: true
}),
require('autoprefixer')({
browsers: [`last 15 versions`]
})/*,
require('postcss-csso')({
comments: false
})*/
])
}
}
]
})
},
{
test: /\.(woff|woff2|eot|ttf|svg)$/,
exclude: /node_modules/,
loader: 'file-loader',
options: {
limit: 1024,
name: '[hash:base64:3].[ext]',
publicPath: '../font/',
outputPath: 'asset/font/'
}
}
]
},
plugins: [
new ExtractTextPlugin({
filename: `asset/css/style.css`
}),
new CleanWebpackPlugin(`${DistDir}/asset`)
],
entry: {
"sd": `${SrcDir}/app/index.js`
},
externals: {
config: JSON.stringify(require(SrcDir + '/config/config.prod.json'))
},
output: {
path: DistDir,
filename: "asset/js/[name].bundle.js"
},
devServer: {inline: true},
};
【问题讨论】:
标签: webpack keyframe postcss css-modules css-loader