react antd 按需加载

新手,在学习react 按需加载antd时一直失败,尝试了几次终于成功了

环境

node :v12.14.1

实现

创建项目

create-react-app antd—demo

进入项目目录

cd antd-demo

拉去react配置文件

yarn eject

拉去react配置文件

yarn eject

安装babel-plugin-import

yarn add  babel-plugin-import --save-dev

安装antd

yarn add antd --save

配置package.json

   "babel": {
        .....
        "plugins": [
            [
                "import",
                {
                    "libraryName": "antd",
                    "style": "css"
                }
            ]
        ]
    }

尝试在app.jsx引入

import React from 'react';
import './App.css';
import { Button } from 'antd';
function App() {
  return (
    <Button>App</Button>
  );
}

export default App;

样式效果
image.png

完整的package.json

{
    "name": "antd-demo",
    "version": "0.1.0",
    "private": true,
    "dependencies": {
        "@babel/core": "7.7.4",
        "@svgr/webpack": "4.3.3",
        "@testing-library/jest-dom": "^4.2.4",
        "@testing-library/react": "^9.3.2",
        "@testing-library/user-event": "^7.1.2",
        "@typescript-eslint/eslint-plugin": "^2.8.0",
        "@typescript-eslint/parser": "^2.8.0",
        "antd": "^3.26.7",
        "babel-eslint": "10.0.3",
        "babel-jest": "^24.9.0",
        "babel-loader": "8.0.6",
        "babel-plugin-import": "^1.13.0",
        "babel-plugin-named-asset-import": "^0.3.5",
        "babel-preset-react-app": "^9.1.0",
        "camelcase": "^5.3.1",
        "case-sensitive-paths-webpack-plugin": "2.2.0",
        "css-loader": "3.2.0",
        "dotenv": "8.2.0",
        "dotenv-expand": "5.1.0",
        "eslint": "^6.6.0",
        "eslint-config-react-app": "^5.1.0",
        "eslint-loader": "3.0.2",
        "eslint-plugin-flowtype": "3.13.0",
        "eslint-plugin-import": "2.18.2",
        "eslint-plugin-jsx-a11y": "6.2.3",
        "eslint-plugin-react": "7.16.0",
        "eslint-plugin-react-hooks": "^1.6.1",
        "file-loader": "4.3.0",
        "fs-extra": "^8.1.0",
        "html-webpack-plugin": "4.0.0-beta.5",
        "identity-obj-proxy": "3.0.0",
        "jest": "24.9.0",
        "jest-environment-jsdom-fourteen": "0.1.0",
        "jest-resolve": "24.9.0",
        "jest-watch-typeahead": "0.4.2",
        "mini-css-extract-plugin": "0.8.0",
        "optimize-css-assets-webpack-plugin": "5.0.3",
        "pnp-webpack-plugin": "1.5.0",
        "postcss-flexbugs-fixes": "4.1.0",
        "postcss-loader": "3.0.0",
        "postcss-normalize": "8.0.1",
        "postcss-preset-env": "6.7.0",
        "postcss-safe-parser": "4.0.1",
        "react": "^16.12.0",
        "react-app-polyfill": "^1.0.5",
        "react-dev-utils": "^10.0.0",
        "react-dom": "^16.12.0",
        "resolve": "1.12.2",
        "resolve-url-loader": "3.1.1",
        "sass-loader": "8.0.0",
        "semver": "6.3.0",
        "style-loader": "1.0.0",
        "terser-webpack-plugin": "2.2.1",
        "ts-pnp": "1.1.5",
        "url-loader": "2.3.0",
        "webpack": "4.41.2",
        "webpack-dev-server": "3.9.0",
        "webpack-manifest-plugin": "2.2.0",
        "workbox-webpack-plugin": "4.3.1"
    },
    "scripts": {
        "start": "node scripts/start.js",
        "build": "node scripts/build.js",
        "test": "node scripts/test.js"
    },
    "eslintConfig": {
        "extends": "react-app"
    },
    "browserslist": {
        "production": [
            ">0.2%",
            "not dead",
            "not op_mini all"
        ],
        "development": [
            "last 1 chrome version",
            "last 1 firefox version",
            "last 1 safari version"
        ]
    },
    "jest": {
        "roots": [
            "<rootDir>/src"
        ],
        "collectCoverageFrom": [
            "src/**/*.{js,jsx,ts,tsx}",
            "!src/**/*.d.ts"
        ],
        "setupFiles": [
            "react-app-polyfill/jsdom"
        ],
        "setupFilesAfterEnv": [
            "<rootDir>/src/setupTests.js"
        ],
        "testMatch": [
            "<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}",
            "<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}"
        ],
        "testEnvironment": "jest-environment-jsdom-fourteen",
        "transform": {
            "^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
            "^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
            "^(?!.*\\.(js|jsx|ts|tsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
        },
        "transformIgnorePatterns": [
            "[/\\\\]node_modules[/\\\\].+\\.(js|jsx|ts|tsx)$",
            "^.+\\.module\\.(css|sass|scss)$"
        ],
        "modulePaths": [],
        "moduleNameMapper": {
            "^react-native$": "react-native-web",
            "^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy"
        },
        "moduleFileExtensions": [
            "web.js",
            "js",
            "web.ts",
            "ts",
            "web.tsx",
            "tsx",
            "json",
            "web.jsx",
            "jsx",
            "node"
        ],
        "watchPlugins": [
            "jest-watch-typeahead/filename",
            "jest-watch-typeahead/testname"
        ]
    },
    "babel": {
        "presets": [
            "react-app"
        ],
        "plugins": [
            [
                "import",
                {
                    "libraryName": "antd",
                    "style": "css"
                }
            ]
        ]
    }
}

最后

刚开始学习,总会遇到些问题,大家细心一点多试几次总会成功的,加油

阅读 1k

推荐阅读
我爱前端
用户专栏

记录前端开发过程过程中的问题

18 人关注
15 篇文章
专栏主页