babel-plugin-transform-imports-api
v1.0.0
Published
Convert import default package API to modular reference to reduce package size and transforms member style imports.
Downloads
9,932
Maintainers
Readme
babel-plugin-transform-imports-api
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,
},
}
}]
]
}