我按文档这样做的
1 create-react-app
创建了一个项目,然后装了antd
和babel-plugin-import
,然后按文档说的,配置了.babelrc
,内容是:
{
"plugins": [
[
"import",
{
"libraryName": "antd",
"style": "css"
}
]
]
}
然后还是不能按需加载,控制台仍然显示
index.js:8 You are using a whole package of antd, please use https://www.npmjs.com/package/babel-plugin-import to reduce app bundle size.
package.json 内容如下:
{
"name": "mongodb-pbac-client-react",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.9",
"@testing-library/react": "^11.2.5",
"@testing-library/user-event": "^12.7.1",
"antd": "^4.12.3",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.2",
"web-vitals": "^1.1.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts 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"
]
},
"devDependencies": {
"babel-plugin-import": "^1.13.3"
}
}
只有一个组件,里面引入部分是这样的:
import { Button } from 'antd'; // 只引入这个,有按钮,没有样式,而且 console 显示没有按需加载
// import 'antd/dist/antd.css'; // 引入所有样式
// import Button from 'antd/lib/button';
// import 'antd/lib/button/style/css';
这东西到底怎么玩的,只是要这个简单的功能,网上查到的都很复杂,不知道怎么改
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.