在create-react-app创建的项目中对antd的样式按需引入


在create-react-app创建的项目中对antd的样式按需引入

  1. 下载 customize-cra, react-app-rewired, babel-plugin-import

    yarn add customize-cra react-app-rewired babel-plugin-import --dev

    ​ customize-cra文档:https://github.com/arackaf/customize-cra

    ​ react-app-rewired文档:https://github.com/timarney/react-app-rewired

    ​ babel-plugin-import文档:https://github.com/ant-design/babel-plugin-import

  1. 在package.json的同级创建config-overrides.js文件

  2. config-overrides.js文件配置

    const { override, fixBabelImports } = require('customize-cra');
    
    module.exports = override(
        fixBabelImports('import', {   
            libraryName: 'antd',
            libraryDirectory: 'lib',
            style: "css",  //如果是less文件则改为true
        })
    );

    fixBabelImports配置:https://github.com/arackaf/customize-cra/blob/master/api.md#fixbabelimportslibraryname-options

import部分配置找babel-plugin-import:https://github.com/ant-design/babel-plugin-import#usage

  1. 修改package.json的script选项

    ​ 安装react-app-rewired后的:https://github.com/timarney/react-app-rewired#3-flip-the-existing-calls-to-react-scripts-in-npm-scripts-for-start-build-and-test

      /* 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",
        "eject": "react-scripts eject"
    }

文章作者: liheng
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 liheng !
 上一篇
babel学习笔记 babel学习笔记
学习地址:姜瑞涛的官方网站 babel是一个工具集,主要用于将ES6的js代码转为ES5等向后兼容的js代码 一、必要部分: babel配置文件:babelrc、babel.js、babel.config.js或package.json
2021-01-08 liheng
下一篇 
js设计模式 js设计模式
一、设计模式 重中之重!!!:找出变化的地方,使变化的地方与不变的地方分离 1. 单例模式 singleton 定义:保证一个类仅有一个实例,并提供一个访问它的全局访问点 实现:用变量标志当前的是否已经为该类创造过实例对象,如果创造过则
2020-10-16 liheng
  目录