react 项目中使用less
插件安装
1 | npm install react-app-rewired customize-cra babel-plugin-import -D |
在项目根目录创建文件 config-overrides.js
1 | const {...} = require('customize-cra'); |
react 按需加载配置
1 | const { override, fixBabelImports } = require("customize-cra"); |
修改package.json
1 | "scripts": { |
编译less文件
1 | yarn add --dev less less-loader //less-loader用5.0版本,高版本停用了一些配属性//config-overrides.js内容 |
装饰器
1
2
3
4
5
6npm install -D @babel/plugin-proposal-decorators
//config-overrides.js
const { override, addDecoratorsLegacy} = require('customize-cra');
module.exports = override(
addDecoratorsLegacy()
);问题解决
出现如下报错
ValidationError: Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
options has an unknown property ‘modifyVars’. These properties are valid:
object { lessOptions?, prependData?, appendData?, sourceMap?, implementation? }。解决方案
将1
2
3
4addLessLoader({
javascriptEnabled: true,
modifyVars: {}
}),修改为
1
2
3
4
5
6addLessLoader({
lessOptions: {
javascriptEnabled: true,
modifyVars: {}
}
}),
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Comment
DisqusValine