1、安装依赖库
npm install less@3.9.0
npm install less-loader@4.1.0
2、弹出配置文件以便自定义项目配置
npm run eject
3、修改config/webpack.config.js中的配置
- 添加样式文件正则
const lessRegex = /\.(scss|less)$/;
const lessModuleRegex = /\.module\.(less)$/;
- 修改
getStyleLoaders函数
a.增加less-loader节点
{
loader: require.resolve('less-loader'),
options: lessOptions,
}
b.模仿sassRegex,添加以下配置代码
{
test: lessRegex,
exclude: lessModuleRegex,
use: getStyleLoaders(
{
importLoaders: 3,
sourceMap: isEnvProduction
? shouldUseSourceMap
: isEnvDevelopment,
},
'less-loader'
),
sideEffects: true,
},
{
test: lessModuleRegex,
use: getStyleLoaders(
{
importLoaders: 3,
sourceMap: isEnvProduction
? shouldUseSourceMap
: isEnvDevelopment,
modules: {
getLocalIdent: getCSSModuleLocalIdent,
},
},
'less-loader'
),
},