create-react-app 添加less-css-modules
- create-react-app demo
- npm run eject
- npm i less less-loader --save
- npm i less-watch-compiler --save-dev
在package.json 中添加
"scripts":{ "build-css": "less-watch-compiler --run-once --main-file=index.less src/less src/css", "watch-css": "npm run build-css && less-watch-compiler --main-file=index.less src/less src/css" }
- 运行npm run watch-css
在config 中的 webpack.config.js 中配置
// 修改前 { loader: require.resolve('css-loader'), options: cssOptions }, // 修改后 { loader: require.resolve('css-loader'), options: { importLoaders: 1, modules: true, localIdentName: "[name]__[local]___[hash:base64:5]" // 更改css modules 生成方法 }, }, // 修改webpack 配置less 部分, 按照sass 相同配置 const lessRegex = /\.less$/ const lessModuleRegex = /\.module\.less$/ oneOf:[ ..., { test: lessRegex, use: getStyleLoaders( { importLoaders: 2, sourceMap: isEnvProduction ? shouldUseSourceMap : isEnvDevelopment, }, 'less-loader' ) }, { test: lessModuleRegex, use: getStyleLoaders( { importLoaders: 2, sourceMap: isEnvProduction ? shouldUseSourceMap : isEnvDevelopment, modules: true, getLocalIdent: getCSSModuleLocalIdent }, 'less-loader' ), sideEffects: true }, ]
8.删除原有依赖 重新安装依赖, 然后npm start 运行即可
相关推荐
前端 2020-08-03
骷髅狗 2020-08-02
wghou 2020-06-21
wghou 2020-06-16
覆雪蓝枫 2020-06-16
骷髅狗 2020-06-14
骷髅狗 2020-06-12
sixyearsorless 2020-05-27
zhanghaibing00 2020-05-27
maiktom 2020-05-26
比格杰森 2020-05-26
e度空间 2020-05-17
maiktom 2020-05-11
buttonChan 2020-05-10
Enjoyendless 2020-05-08
sixyearsorless 2020-05-07
骷髅狗 2020-04-24