安装配置babel-plugin-import

宿嘉
2023-12-01

antd按需加载

  • 安装babel-plugin-import
// yarn
yarn add babel-plugin-import
//npm
npm install --save babel-plugin-import
  • 暴露webpack
// yarn
yarn eject
//npm
npm run eject
  • 配置webpack.config.js

webpack3.0之后
修改webpack.config.dev和.prod下搜索 Process JS with Babel.下的那个options

// Process JS with Babel
options: {
             plugins: [
               ['import',[{  // 导入一个插件
                 libraryName: 'antd',   // 暴露的库名
                 style: true // 直接将ants样式文件动态编译成行内样式插入
               }]]
             ],
             cacheDirectory: true,
},

webpack4.0之后
搜索Process application JS with Babel找到该注释下的babel-loader配置
添加如下配置

// Process application JS with Babel
plugins: [
                    ['import',{  // 导入一个插件
                      libraryName: 'antd',   // 暴露的库名
                      style: true // 直接将ants样式文件动态编译成行内样式插入
                    }],
          ]

最后重启即可

更多配置设置

 类似资料: