我看到了FlipKart,他们将所有classNames及其CSS都哈希。 我知道他们使用SCSS来构建CSS。 如何配置我的WebPack以使我的导出类似于:
这是我的网站:
var webpack = require('webpack'),
path = require('path'),
ExtractTextPlugin = require('extract-text-webpack-plugin'),
UglifyJSPlugin = require('uglifyjs-webpack-plugin'),
OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
var DistDir = path.resolve(__dirname, 'app/dist'),
SrcDir = path.resolve(__dirname, 'app/src');
module.exports = {
entry: SrcDir,
output: {
path: DistDir,
filename: "bundle.min.js"
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
include: SrcDir,
loader: 'babel-loader'
},
{
test: /\.(scss|css)$/,
exclude: /node_modules/,
use: ExtractTextPlugin.extract({
use: [{
loader: "css-loader", options: {
modules: true,
localIdentName: '[hash:base64:3]',
sourceMap: false
}
}, {
loader: "sass-loader", options: {
sourceMap: false
}
}],
fallback: "style-loader"
})
},
{
test: /\.png$/,
loader: "file-loader",
exclude: /node_modules/
}
]
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify('production')
}
}),
new ExtractTextPlugin("bundle.min.css"),
new OptimizeCssAssetsPlugin({
cssProcessor: require('cssnano'),
cssProcessorOptions: { discardComments: {removeAll: true } },
canPrint: true
}),
new UglifyJSPlugin({
compress: {
unused: true,
dead_code: true,
warnings: false,
drop_debugger: true,
conditionals: true,
evaluate: true,
drop_console: true,
sequences: true,
booleans: true
},
comments: false
}),
new webpack.IgnorePlugin(/^\.\/locale$/, [/moment$/]),
new webpack.NoEmitOnErrorsPlugin(),
new webpack.optimize.AggressiveMergingPlugin()
]
};
但它只是在CSS文件中哈希我的类名,而DOM中的类名是dev版本,例如DOM中的一个类名元素是product__bag--red
,
它在CSS文件中的类名是_1x4
。
我如何配置webpack或extract-text-plugin,我在DOM中看到_1x4
而不是product__bag--red
?
答案 0 :(得分:0)
单独的CSS
文件:
.app {
display: flex;
}
单独的React
组件文件:
import style from './file.css'
class Component extends React.Component {
render () {
return (
<div className={style.app}></div>
)
}
}
单独的公共JavaScript
文件:
import style from './file.css'
document.body.innerHTML = `
<div class=${style.app}></div>
`
设置modules
后,我们必须将classNames
作为{Object}