webpack.config.dev.js和webpack.config.pro.js找到module里面css的相關(guān)配置在options配置里面加上一下兩句代碼
options: {
importLoaders: 1,
// 改動(dòng)
modules: true, // 新增對(duì)css modules的支持
localIdentName: '[name]__[local]__[hash:base64:5]', //
},
如果再項(xiàng)目中用到antd的時(shí)候,會(huì)發(fā)現(xiàn)antd的樣式無效了泰鸡,這個(gè)時(shí)候我們可以做一個(gè)處理::將antd組件的css的webpack配置和非antd組件的csswebpack配置分開
{
test: /\.(css|less)$/,
exclude: /node_modules|antd\.css/,
use: [
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
// 改動(dòng)
modules: true, // 新增對(duì)css modules的支持
localIdentName: '[name]__[local]__[hash:base64:5]', //
},
},
{
loader: require.resolve('postcss-loader'),
options: {
// Necessary for external CSS imports to work
// https://github.com/facebookincubator/create-react-app/issues/2677
ident: 'postcss',
plugins: () => [
require('postcss-flexbugs-fixes'),
autoprefixer({
browsers: [
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 9', // React doesn't support IE8 anyway
],
flexbox: 'no-2009',
}),
],
},
},
{
loader: require.resolve('less-loader') // compiles Less to CSS
}
],
},
{
test: /\.(css|less)$/,
include: /node_modules|antd\.css/,
use: [
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
},
},
{
loader: require.resolve('postcss-loader'),
options: {
// Necessary for external CSS imports to work
// https://github.com/facebookincubator/create-react-app/issues/2677
ident: 'postcss',
plugins: () => [
require('postcss-flexbugs-fixes'),
autoprefixer({
browsers: [
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 9', // React doesn't support IE8 anyway
],
flexbox: 'no-2009',
}),
],
},
},
{
loader: require.resolve('less-loader') // compiles Less to CSS
}
],
},