当前位置: 首页 > news >正文

craco-less使用问题

craco-less使用问题

问题背景

前端是用React搭建,使用craco配置,相关库或插件版本如下

 "@craco/craco": "^7.1.0","react-scripts": "^5.0.1","craco-less": "^3.0.1"

在生产环境,Node.js构建时,报错如下

Error: Found an unhandled loader in the developement webpack config: node_modules/style-loader/index.jsThis error probably occurred because you updated react-scripts or craco. Please try updating craco-less to the latest version:$ yarn upgrade craco-lessOr:$ npm update craco-lessIf that doesn't work, craco-less needs to be fixed to support the latest version.
Please check to see if there's already an issue in the FormAPI/craco-less repo:* https://github.com/FormAPI/craco-less/issues?q=is%3Aissue+webpack+unknown+ruleIf not, please open an issue and we'll take a look. (Or you can send a PR!)You might also want to look for related issues in the craco and create-react-app repos:* https://github.com/sharegate/craco/issues?q=is%3Aissue+webpack+unknown+rule* https://github.com/facebook/create-react-app/issues?q=is%3Aissue+webpack+unknown+ruleat throwUnexpectedConfigError (.../node_modules/@craco/craco/lib/plugin-utils.js:29:11)at throwError (.../node_modules/craco-less/lib/craco-less.js:14:5)at .../node_modules/craco-less/lib/craco-less.js:119:7at Array.forEach (<anonymous>)at Object.overrideWebpackConfig (.../node_modules/craco-less/lib/craco-less.js:51:11)at overrideWebpack (.../node_modules/@craco/craco/lib/features/plugins.js:42:40)at .../node_modules/@craco/craco/lib/features/plugins.js:64:29at Array.forEach (<anonymous>)at applyWebpackConfigPlugins (.../node_modules/@craco/craco/lib/features/plugins.js:63:29)at mergeWebpackConfig (.../node_modules/@craco/craco/lib/features/webpack/merge-webpack-config.js:67:30)

解决方案

尝试在craco-less的 github 和网上寻找方法,设置NODE_NODE_ENV依旧无效,项目中用到less无非是想使用CSS Module,实现 CSS 的局部使用。craco-less最近的一次更新还是 8 个月之前,且最新版本上仍然有很多 issues 未被解决,因此放弃craco-less,改用craco-css-modules

craco-css-modules的参考地址:https://github.com/crazyurus/craco-css-modules

craco.config.js配置如下

const CracoCSSModules = require("craco-css-modules");module.exports = {plugins: [{plugin: CracoCSSModules,},],devServer: {port: 8081,},
};

使用

实现CSS Module前后区别:

.bgImg {width: 100%;height: 100%;object-fit: cover;
}
// Before
import styles from "./index.module.less";// After
import styles from "./index.css";

结果

本地运行和生产环境的流水线都没有问题

效果如下:
在这里插入图片描述

http://www.lryc.cn/news/443639.html

相关文章:

  • 14 vue3之内置组件trastion全系列
  • 力扣(leetcode)每日一题 LCR 187 破冰游戏(还是考的约瑟夫环)
  • nginx模块篇(四)
  • 奇安信渗透2面经验分享
  • 【计算机网络篇】电路交换,报文交换,分组交换
  • 【TypeScript入坑】什么是TypeScript?
  • Agile Modbus STM32裸机移植 从机使用
  • mysql5.7.44安装教程
  • etsts
  • C++_22_异常
  • 开源 AI 智能名片链动 2+1 模式 O2O 商城小程序在社群活动中的应用与时机选择
  • 从HarmonyOS升级到HarmonyOS NEXT-环信SDK数据迁移
  • Spring Boot-Bean注入问题
  • 【在Linux世界中追寻伟大的One Piece】IP分片和组装的具体过程
  • 2024年中国研究生数学建模竞赛A/C/D/E题全析全解
  • 【图虫创意-注册安全分析报告-无验证方式导致安全隐患】
  • 解决 npm ERR! node-sass 和 gyp ERR! node-gyp 报错问题
  • Golang | Leetcode Golang题解之第421题数组中两个数的最大异或值
  • 每天一道面试题(15):谈谈你对CAS的理解
  • 如何将MySQL卸载干净(win11)
  • 【Linux】简易日志系统
  • yum 集中式安装 LNMP
  • 淘宝扭蛋机小程序,扭蛋机文化下的新体验
  • Go搭建TcpSocket服务器
  • hadoop3跑第一个例子wordcount
  • Maven笔记(二):进阶使用
  • Apache ZooKeeper 及 Curator 使用总结
  • 深入探索:MATLAB中的硬件支持包(HSP)及其应用
  • 5.内容创作的未来:ChatGPT如何辅助写作(5/10)
  • Day26_0.1基础学习MATLAB学习小技巧总结(26)——数据插值