Skip to content

Convert import default package API to modular reference to reduce package size and transforms member style imports.

License

Notifications You must be signed in to change notification settings

shinken008/babel-plugin-transform-imports-api

Repository files navigation

babel-plugin-transform-imports-api

NPM version GitHub license

Convert import default package API to modular reference to reduce package size and transforms member style imports. Inspired by babel-plugin-transform-taroapi and babel-transform-imports.

example

import Taro from '@tarojs/taro-h5'
Taro.request(...)

This code will become:

import { request } from '@tarojs/taro-h5'
request(...)

and when the configure is:

// .babelrc
{
  packagesApis: new Map([
    ['@tarojs/taro-h5', new Set(['request'])],
  ]),
  usePackgesImport: true,
  packagesImport: {
    '@tarojs/taro-h5': {
      transform: (importName, matches) => `@tarojs/taro-h5/lib/${importName.toUpperCase()}`,
      preventFullImport: true,
    },
  }
}

this code will become:

import request from '@tarojs/taro-h5/lib/request';
request(...)

Usage

Step 1: Install

yarn add --dev babel-plugin-transform-imports-api

or

npm install --save-dev babel-plugin-transform-imports-api

Step 1: Configure .babelrc

{
  plugins: [
    [require(plugin), {
      packagesApis: new Map([
        ['packageName1', new Set(['api'])],
        ['packageName2', new Set(['api'])],
      ]),
      usePackgesImport: false, // Whether to use packagesImport
      packagesImport: {
        'packageName1': {
          transform: (importName, matches) => `packageName1/lib/${importName.toUpperCase()}`,
          preventFullImport: true,
        },
      }
    }]
  ]
}

About

Convert import default package API to modular reference to reduce package size and transforms member style imports.

Resources

License

Stars

Watchers

Forks

Packages

No packages published