react引入antd並配置按需加載和自定義主題


  1. 下載組件庫包

    yarn add antd
    
  2. 使用craco對create-react-app進行自定義配置

    1. yarn add @craco/craco
      
    2. 根目錄創建craco.config.js文件

      /* craco.config.js */
      module.exports = {
        // ...
      };
      
    3. 更改package.json

      "scripts": {
          "start": "craco start",
          "build": "craco build",
          "test": "craco test",
          "eject": "react-scripts eject"
        },
      
  3. 實現按需打包

    1. yarn add babel-plugin-import
      
    2. craco.config.js添加

      babel:{  
          plugins: [
            [   
              "import", 
              {
                "libraryName": "antd",
                "libraryDirectory": "es",
                 "style": true //設置為true即是less
               }
           ]
          ]
       }
      
  4. 實現自定義主題

    1. yarn add craco-less
      
    2. craco.config.js添加

      module.exports = {
          plugins: [{
              plugin: CracoLessPlugin,
              options: {
                  lessLoaderOptions: {
                      lessOptions: {
                          modifyVars: {
                              '@primary-color': '#1DA57A'
                          },
                          javascriptEnabled: true,
                      },
                  },
              },
          },],
      };
      


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM