React在crate- react- app中使用less文件

创建启动项目

yarn start

修改App.jsx/App.js文件内容

/*    App.jsx    */ 
// import './App.css';
import './App.less'

function App() {
  return (
    <div className="App">
        <div className="demo">
          这是一个项目
        </div>
    </div>
  );
}

export default App;

启动项目

安装 craco 并修改 package.json 里的 scripts 属性。

yarn add @craco/craco

在这里插入图片描述

/* package.json */
"scripts": {
-   "start": "react-scripts start",
-   "build": "react-scripts build",
-   "test": "react-scripts test",
+   "start": "craco start",
+   "build": "craco build",
+   "test": "craco test",
}

在这里插入图片描述

安装 craco-less 并创建或修改 craco.config.js

yarn add craco-less

在这里插入图片描述
craco.config.js文件创建在根目录
在这里插入图片描述

const CracoLessPlugin = require('craco-less');

module.exports = {
  plugins: [
    {
      plugin: CracoLessPlugin,
      options: {
        lessLoaderOptions: {
          lessOptions: {
            modifyVars: { '@primary-color': '#1DA57A' },
            javascriptEnabled: true,
          },
        },
      },
    },
  ],
};

less文件编辑后运行

less文件随便写点东西,然后引用运行

 /* Less */
 @color: #999;
 @bgColor: skyblue;//不要添加引号
 @width: 50%;
.demo{
    color: @color;
    background-color: @bgColor;
    width: @width;
    height: 50px;
    margin: 0 auto;
    text-align: center;
}

在这里插入图片描述
搜了很多博主的文章发现大多数都是需要暴露webpack配置,那种方法我没试过,但是我觉得很麻烦。
注意eject命令是一次性命令,运行后无法恢复
npm run eject
这时项目结构就会多出了一个config文件,以及package.json 文件中多了很多内容

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值