Babel Plugin Component Modularization

1.1.1 · abandoned · verified Tue Apr 21

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.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to configure 'babel-plugin-component' in a Babel configuration file to enable modular imports and style loading for a hypothetical component library.

// .babelrc or babel.config.js
module.exports = {
  plugins: [
    ["component", {
      libraryName: "your-component-library",
      style: true, // or 'css', or a custom path like 'your-component-library/lib/style.css'
      camel2Dash: true // Converts 'ComponentName' to 'component-name'
    }]
  ]
};

// src/App.js (example usage in application code)
import { Button, Alert } from 'your-component-library';

function App() {
  return (
    <div>
      <Button>Click me</Button>
      <Alert type="info" message="Hello"></Alert>
    </div>
  );
}

// Transformed output (simplified conceptual example):
// var Button = require('your-component-library/lib/button');
// require('your-component-library/lib/button/style.css');
// var Alert = require('your-component-library/lib/alert');
// require('your-component-library/lib/alert/style.css');

view raw JSON →