配置antd按需加载,alias路径别名,以及常规配置

约定不等于承诺〃 2022-01-27 11:43 493阅读 0赞

配置antd按需加载,alias路径别名,以及常规配置

1. 按需加载和alias别名

安装一下需要的插件

  1. npm i antd react-app-rewired customize-cra babel-plugin-import less less-loader -S

复制代码第二步,修改package.json文件,将:

  1. "scripts": {
  2. "start": "react-scripts start",
  3. "build": "react-scripts build",
  4. "test": "react-scripts test",
  5. }

复制代码修改为:

  1. "scripts": {
  2. "start": "react-app-rewired start",
  3. "build": "react-app-rewired build",
  4. "test": "react-app-rewired test",
  5. }

复制代码第三步
更多参考官网:
antd官网

在跟目录创建一个config-overrides.js的文件,内容为:

  1. const { override, fixBabelImports, addLessLoader, addWebpackAlias } = require('customize-cra');
  2. const path = require('path');
  3. function resolve(dir) {
  4. return path.join(__dirname, '.', dir)
  5. }
  6. module.exports = override(
  7. // antd按需加载,不需要每个页面都引入"antd/dist/antd.css"了
  8. fixBabelImports('import', {
  9. libraryName: 'antd',
  10. libraryDirectory: 'es',
  11. style: true //这里一定要写true,css和less都不行
  12. }),
  13. // 配置路径别名
  14. addWebpackAlias({
  15. @: resolve("src")
  16. })
  17. )

2.装饰器配置

  • 安装

npm install @babel/plugin-proposal-decorators --save

  • 配置

在config-overrides.js中引入addDecoratorsLegacy即可

  1. const { override, fixBabelImports, addWebpackAlias ,addDecoratorsLegacy} = require('customize-cra');
  2. const path = require("path")
  3. module.exports = override(
  4. fixBabelImports('import', {
  5. libraryName: 'antd',
  6. libraryDirectory: 'es',
  7. style: 'css',
  8. }),
  9. addWebpackAlias({
  10. '@': path.join(__dirname, "src")
  11. }),
  12. addDecoratorsLegacy()//使用装饰器
  13. );
  • 使用

    import React, { Component } from ‘react’

    1. const withName = (Com) => {
    2. class Hoc extends Component {
    3. constructor(props) {
    4. super(props);
    5. }
    6. render() {
    7. return (
    8. <Com { ...this.props} name="hansu"></Com>
    9. )
    10. }
    11. }
    12. return Hoc
    13. }
    14. @withName
    15. class Comment extends Component{
    16. render(){
    17. return (
    18. <div>
    19. { this.props.name}
    20. </div>
    21. )}
    22. }

    export default Comment

3.vscode插件

常用插件

  • ES7 React/Redux/GraphQL/React-Native snippets

rcc:普通组件
rfc:纯组件

  • React-Native/React/Redux snippets for es6/es7

imr:引入react
ccr:引入组件

发表评论

表情:
评论列表 (有 0 条评论,493人围观)

还没有评论,来说两句吧...

相关阅读