A Babel plugin that transforms modular imports (e.g., import { Button } from 'antd') into individual require/import calls for only the used components and their styles, reducing bundle size. Version 1.13.8 is the latest stable release, maintained by the Ant Design team. It supports customizable library names, directories, style imports (including CSS, SASS/LESS, or none), and camelCase-to-kebab-case conversion. Key differentiators: works with antd, antd-mobile, lodash, material-ui, and many other libraries; supports styleLibraryDirectory for custom style layouts; and is widely adopted in the React ecosystem for on-demand loading without tree-shaking.
npm install babel-plugin-importVerified import paths — ran on the pinned version, not inferred.
Configures babel-plugin-import for antd with CSS style imports, showing the transformation of a component import.
Instead of [
{"libraryName":"antd"},
{"libraryName":"antd-mobile"}
], define two plugin entries with unique names: [['import',{libraryName:'antd'},'antd'], ['import',{libraryName:'antd-mobile'},'antd-mobile']]Use style: true to import LESS/Sass source files and allow tree-shaking of styles.
Always use named imports from the library root (e.g., import { Button } from 'antd') to allow the plugin to work.Add a third string argument to each plugin entry for uniqueness, e.g., ['import', {...}, 'antd'].Use ES module imports (import ... from 'antd') instead of CommonJS require.
Either use separate plugin entries or pass a single options object. For multiple libraries: [['import', {libraryName:'antd'}, 'antd'], ['import', {libraryName:'antd-mobile'}, 'antd-mobile']]Run: npm install babel-plugin-import --save-dev
Verify the library name and style path. For antd, use libraryName: 'antd' and style: 'css' for built CSS, or style: true for LESS source.
Update babel-plugin-import to the latest version (1.13.8) and ensure @babel/core is >=7.0.0.