babel-plugin-import-expander

0.3.0 • Public • Published

babel-plugin-import-expander

Build Status npm version

Expand multiple members import to specific module import, mainly for performance concerns.

Example

In

import { A, B, C as D } from '../components';

Out

import A from '../components/A/A';
import B from '../components/B/B';
import D from '../components/C/C';

Installation

$ npm install babel-plugin-import-expander --save-dev

Usage

Via .babelrc (Recommended)

{
  "plugins": [
    ["import-expander", {
      "rules": {
        "condition": "^(\\.|\\/).*\\/components$",
        "template": "{source}/{name}/{name}"
      }
    }]
  ]
}
  • rules

    Options can be one object or a list of objects, each object has two properties:

    • condition

      One or multiple string format regular expressions, if the source of ImportDeclaration matches any of them, it will be replaced by the following template.

    • template

      Used to replace hit source with a simple variable placeholder presentation. There are mainly two variable placeholders: {source} represents the source of ImportDeclaration, and {name} represents imported name.

Via Node API

require('babel-core').transform('code', {
  plugins: ['import-expander', {
    rules: {
      condition: '^(\\.|\\/).*\\/components$',
      template: '{source}/{name}/{name}'
    }
  }],
});

Package Sidebar

Install

npm i babel-plugin-import-expander

Weekly Downloads

50

Version

0.3.0

License

MIT

Unpacked Size

99.8 kB

Total Files

7

Last publish

Collaborators

  • springuper