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.
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(...)
yarn add --dev babel-plugin-transform-imports-api
or
npm install --save-dev babel-plugin-transform-imports-api
{
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,
},
}
}]
]
}