React18安装antd的按需引入配置
react安装最好使用 yarn 来安装插件,因为大部分使用npm时会出现各种错误
npm install yarn -g
切换并使用淘宝镜像:
yarn config set registry=https://registry.npm.taobao.org -g
在react版本16.8之前:
yarn add react-app-rewired@2.1.6 customize-cra@1.0.0 babel-plugin-import@1.13.1 less@3.12.2 less-loader@7.1.0
并修改package.json文件
-
"scripts": {
-
"start": "react-app-rewired start",
-
"build": "react-app-rewired build",
-
"test": "react-app-rewired test",
-
"eject": "react-scripts eject"
-
},
在根目录下创建config-overrides.js
-
//配置具体的修改规则
-
const { override, fixBabelImports,addLessLoader} = require('customize-cra');
-
-
module.exports = override(
-
fixBabelImports('import', {
-
libraryName: 'antd',
-
libraryDirectory: 'es',
-
style: true,
-
}),
-
addLessLoader({
-
lessOptions:{
-
javascriptEnabled: true,
-
modifyVars: { '@primary-color': 'green' },
-
}
-
}),
-
);
备注:不用在组件里亲自引入样式了, 即: import 'antd/dist/antd.min.css'应该删除
在React18版本后引入antd配合craco来设置
安装 @craco/craco 跟 babel-plugin-import
yarn add @craco/craco babel-plugin-import
将package.json文件修改为
-
"scripts": {
-
"start": "craco start",
-
"build": "craco build",
-
"test": "craco test",
-
"eject": "react-scripts eject"
-
},
此时在安装craco-less,并在根目录创建 craco.config.js文件
-
/*
-
注意:这边一定需要加上一个 --force, 否则会出现一个在 node_modules中会出现一个error
-
错误是: 在tsconfig-paths中有个文件没有引入的错误, 使用npm i craco-less --force
-
就没有以下问题
-
*/
-
npm i craco-less --force
-
const CracoLessPlugin = require("craco-less");
-
// 需要安装两个插件:`yarn add craco-less` `yarn add babel-plugin-import` 从而实现样式的按需引入
-
module.exports = {
-
plugins: [
-
{
-
plugin: CracoLessPlugin,
-
options: {
-
lessLoaderOptions: {
-
lessOptions: {
-
modifyVars: { "@primary-color": "#1DA57A" },
-
javascriptEnabled: true,
-
},
-
},
-
},
-
},
-
],
-
babel: {
-
//支持装饰器
-
plugins: [
-
[
-
"import",
-
{
-
libraryName: "antd",
-
libraryDirectory: "es",
-
style: true, //设置为true即是less 这里用的是css,如果是scss,则需要设置为false,否则不能正常生效
-
},
-
],
-
],
-
},
-
};
后面 npm start 或 yarn start
附上我的package.json文件
-
{
-
"name": "react-admin_client",
-
"version": "0.1.0",
-
"private": true,
-
"dependencies": {
-
"@craco/craco": "^6.4.4",
-
"@testing-library/jest-dom": "^5.16.4",
-
"@testing-library/react": "^13.3.0",
-
"@testing-library/user-event": "^13.5.0",
-
"antd": "^4.21.5",
-
"babel-plugin-import": "^1.13.5",
-
"craco-less": "^2.0.0",
-
"react": "^18.2.0",
-
"react-dom": "^18.2.0",
-
"react-scripts": "5.0.1",
-
"web-vitals": "^2.1.4"
-
},
-
"scripts": {
-
"start": "craco start",
-
"build": "craco build",
-
"test": "craco test",
-
"eject": "react-scripts eject"
-
},
-
"eslintConfig": {
-
"extends": [
-
"react-app",
-
"react-app/jest"
-
]
-
},
-
"browserslist": {
-
"production": [
-
">0.2%",
-
"not dead",
-
"not op_mini all"
-
],
-
"development": [
-
"last 1 chrome version",
-
"last 1 firefox version",
-
"last 1 safari version"
-
]
-
}
-
}
-
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgafakk
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13