项目搭建react + react-router + react-redux + typescript + axios + webpack + less

npx create-react-app my-app --template typescript
cd my-app
npm start

yarn add antd

yarn add react-app-rewired customize-cra
/* package.json */
"scripts": {
     
-   "start": "react-scripts start",
+   "start": "react-app-rewired start",
-   "build": "react-scripts build",
+   "build": "react-app-rewired build",
-   "test": "react-scripts test",
+   "test": "react-app-rewired test",
}

然后在项目根目录创建一个 config-overrides.js 用于修改默认配置。

module.exports = function override(config, env) {
     
  // do stuff with the webpack config...
  return config;
};

babel-plugin-import 是一个用于按需加载组件代码和样式的 babel 插件(原理),现在我们尝试安装它并修改 config-overrides.js 文件。

$ yarn add babel-plugin-import
+ const {
      override, fixBabelImports } = require('customize-cra');

- module.exports = function override(config, env) {
     
-   // do stuff with the webpack config...
-   return config;
- };
+ module.exports = override(
+   fixBabelImports('import', {
     
+     libraryName: 'antd',
+     libraryDirectory: 'es',
+     style: 'css',
+   }),
+ );
 // src/App.tsx
  import React, {
      Component } from 'react';
+ import {
      Button } from 'antd';
  import './App.css';

  class App extends Component {
     
    render() {
     
      return (
        <div className="App">
          <Button type="primary">Button</Button>
        </div>
      );
    }
  }

  export default App;
yarn add less less-loader
- const {
      override, fixBabelImports } = require('customize-cra');
+ const {
      override, fixBabelImports, addLessLoader } = require('customize-cra');

module.exports = override(
  fixBabelImports('import', {
     
    libraryName: 'antd',
    libraryDirectory: 'es',
-   style: 'css',
+   style: true,
  }),
+ addLessLoader({
     
+   javascriptEnabled: true,
+   modifyVars: {
      '@primary-color': '#1DA57A' },
+ }),
);

使用 Day.js 替换 momentjs 优化打包大小#

yarn add antd-dayjs-webpack-plugin
npm install --save react-router-dom
# or
yarn add react-router-dom
$ npm install http-proxy-middleware --save
$ # or
$ yarn add http-proxy-middleware

Next, create src/setupProxy.js and place the following contents in it:

const {
      createProxyMiddleware } = require('http-proxy-middleware');

module.exports = function(app) {
     
  // ...
};

You can now register proxies as you wish! Here’s an example using the above http-proxy-middleware:

const {
      createProxyMiddleware } = require('http-proxy-middleware');

module.exports = function(app) {
     
  app.use(
    '/api',
    createProxyMiddleware({
     
      target: 'http://localhost:5000',
      changeOrigin: true,
    })
  );
};
npm install axios
# or
yarn add axios
npm install react-app-polyfill
# or
yarn add react-app-polyfill

https://github.com/facebook/create-react-app/blob/master/packages/react-app-polyfill/README.md

If you are supporting Internet Explorer 9 or Internet Explorer 11 you should include both the ie9 or ie11 and stable modules:

For IE9:

// These must be the first lines in src/index.js
import 'react-app-polyfill/ie9';
import 'react-app-polyfill/stable';

// ...

For IE11:

// These must be the first lines in src/index.js
import 'react-app-polyfill/ie11';
import 'react-app-polyfill/stable';

// ...
#package.json
{
     
    "name": "my-app",
    "version": "0.1.0",
    "private": true,
    "dependencies": {
     
        "@testing-library/jest-dom": "^4.2.4",
        "@testing-library/react": "^9.3.2",
        "@testing-library/user-event": "^7.1.2",
        "@types/jest": "^24.0.0",
        "@types/node": "^12.0.0",
        "@types/react": "^16.9.0",
        "@types/react-dom": "^16.9.0",
        "antd": "^4.1.3",
        "axios": "^0.19.2",
        "http-proxy-middleware": "^1.0.3",
        "react": "^16.13.1",
        "react-app-polyfill": "^1.0.6",
        "react-dom": "^16.13.1",
        "react-router-dom": "^5.1.2",
        "react-scripts": "3.4.1",
        "typescript": "~3.7.2"
    },
    "scripts": {
     
        "start": "react-app-rewired start",
        "build": "react-app-rewired build",
        "test": "react-app-rewired test",
        "eject": "react-app-rewired eject"
    },
    "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"
        ]
    }
}

你可能感兴趣的:(项目架构,项目搭建,node.js,reactjs,typescript,javascript)