babel-plugin-component is a Babel plugin designed to optimize bundle sizes for component-based UI libraries by transforming ES module import statements into modular CommonJS `require` calls. It automatically handles importing only the necessary components and their corresponding styles, preventing the inclusion of an entire library. The current stable version is 1.1.1, published in 2018, indicating a lack of active development or a slow release cadence. Its primary differentiator is its extensive configuration options for handling various component and style library directory structures, including support for independent theme packages and custom style paths, making it highly adaptable for libraries like Element UI and Ant Design. This plugin works by rewriting import paths at compile time, reducing the amount of JavaScript and CSS shipped to the client.
npm install babel-plugin-componentVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to configure 'babel-plugin-component' in a Babel configuration file to enable modular imports and style loading for a hypothetical component library.
For Babel 6, use `npm i babel-plugin-component@0 -D`. For Babel 7+, use `npm i babel-plugin-component -D` (which installs 1.x).
If your component files use camelCase naming, set `camel2Dash: false` in your plugin options: `["component", { libraryName: "your-lib", camel2Dash: false }]`.Carefully review your component library's actual directory structure and adjust `libDir`, `root`, `style`, `styleLibraryName`, and `styleLibrary.path` options to precisely match it. Use absolute paths for `style` if it's external.
Consider alternatives like `babel-plugin-import` if you are working on a new project or encountering compatibility issues with modern tooling. Evaluate the risk of using an unmaintained dependency for long-term projects.
Ensure `babel-plugin-component` is installed (`npm i babel-plugin-component -D`). In your `.babelrc` or `babel.config.js`, reference it as `["component", options]`, not `["babel-plugin-component", options]`.
Check the `style` option: if it's `true`, ensure `lib/componentName/style.css` exists. If `style: 'css'`, ensure `lib/componentName/index.css` or similar exists. Verify `libDir` matches your library's output directory. If component names are camelCase, set `camel2Dash: false` if your library's files are not dash-cased.
Given the plugin's abandonment, there may be no direct fix for newer Babel versions or syntax. You might need to downgrade Babel, transpile newer syntax before this plugin runs (if possible), or migrate to a more actively maintained plugin like `babel-plugin-import`.