要在 create-react-app 脚手架里使用 less 的方法

要在 create-react-app 脚手架里使用 less 的方法:
  1. 在 powershell 里暴露脚手架配置
yarn eject
  1. 在暴露的 config 文件里面打开 webpack.config.js 文件,然后设置 less
  • 66 行 插入 less 更改:
const lessRegex = /\.less$/
const lessModuleRegex = /\.module\.less$/
  • 500 行,插入:
{
    test: lessRegex,
    exclude: lessModuleRegex,
    use: getStyleLoaders({
    importLoaders: 3,
    sourceMap: isEnvProduction
    ? shouldUseSourceMap
    : isEnvDevelopment,
    }, "less-loader"),
    // Don't consider CSS imports dead code even if the
    // containing package claims to have no side effects.
    // Remove this when webpack adds a warning or an error for this.
    // See https://github.com/webpack/webpack/issues/6571
    sideEffects: true,
    },
    // Adds support for CSS Modules (https://github.com/css-modules/css-modules)
    // using the extension .module.css
    {
    test: lessModuleRegex,
    use: getStyleLoaders({
    importLoaders: 3,
    sourceMap: isEnvProduction
    ? shouldUseSourceMap
    : isEnvDevelopment,
    modules: {
    getLocalIdent: getCSSModuleLocalIdent,
    },
    }, "less-loader"),
},
  1. 在 powershell 里安装对应的包(在这里安装的 less-loader 的包是低版本 高版本可能存在不兼容的问题)
yarn add less less-loader@5.0.0
  1. 配置全部变量(在 react-app-env.d.ts 文件里加入)
declare module '*.less' {
  interface Style {
    [propName: string]: string
  }
  const style: Style
  export default style
}
  1. 引用方式(文件名字为 style.module.less)
import style from './style.module.less'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值