react-create-app创建react项目

    1. npx create-react-app demo

    卸载包

    1. npm uninstall react react-dom

    安装包

    1. npm i @craco/craco craco-less @babel/preset-react
    2. npm i antd react react-dom -g
    3. npm link antd react react-dom

    修改package.json

    1. {
    2. ...
    3. "scripts": {
    4. "start": "craco start",
    5. "build": "craco build",
    6. "eject": "react-scripts eject"
    7. },
    8. ...
    9. }

    项目根目录下配置craco.config.js

    1. const CracoLessPlugin = require('craco-less');
    2. const path = require('path');
    3. const resolve = dir => path.resolve(__dirname, dir);
    4. const ModuleScopePlugin = require("react-dev-utils/ModuleScopePlugin");
    5. module.exports = {
    6. babel: {
    7. presets: [
    8. ['@babel/preset-react']
    9. ],
    10. },
    11. plugins: [
    12. {
    13. plugin: CracoLessPlugin,
    14. options: {
    15. lessLoaderOptions: {
    16. lessOptions: {
    17. modifyVars: {},
    18. javascriptEnabled: true,
    19. },
    20. },
    21. },
    22. },
    23. ],
    24. webpack: {
    25. configure: (webpackConfig, { env }) => {
    26. webpackConfig.resolve.plugins.forEach(plugin => {
    27. if (plugin instanceof ModuleScopePlugin) {
    28. //添加本地项目的引用
    29. plugin.allowedFiles.add(resolve("../../GxKit.React/src/index.js"));
    30. }
    31. });
    32. if (env === "production") {
    33. //发布时 变为本地化页面
    34. webpackConfig.output.publicPath = './';
    35. }
    36. return webpackConfig;
    37. },
    38. alias: {
    39. //添加本地项目的别名
    40. 'gxkit-react$': resolve("../../GxKit.React/src/index.js"),
    41. }
    42. }
    43. };

    jsconfig.json

    1. {
    2. "compilerOptions": {
    3. "baseUrl": "./",
    4. "paths": {
    5. "gxkit-react": [
    6. "../../GxKit.React/src/index.js"
    7. ]
    8. }
    9. },
    10. "exclude": [
    11. "node_modules",
    12. "dist"
    13. ]
    14. }