# @babel/helper-module-imports
- npm
npm install @babel/helper-module-imports --save
- yarn
yarn add @babel/helper-module-imports
# 用法
# import "source"
(opens new window)
import { addSideEffect } from "@babel/helper-module-imports";
addSideEffect(path, 'source');
# import { named as _named } from "source"
(opens new window)
import { addNamed } from "@babel/helper-module-imports";
// if the hintedName isn't set, the function will gennerate a uuid as hintedName itself such as '_named'
addNamed(path, 'named', 'source');
# import { named as _hintedName } from "source"
(opens new window)
import { addNamed } from "@babel/helper-module-imports";
addNamed(path, 'named', 'source', { nameHint: "hintedName" });
# import _default from "source"
(opens new window)
import { addDefault } from "@babel/helper-module-imports";
addDefault(path, 'source');
# import _hintedName from "source"
(opens new window)
import { addDefault } from "@babel/helper-module-imports";
// If 'hintedName' exists in scope, the name will be '_hintedName2', '_hintedName3', ...
addDefault(path, 'source', { nameHint: "hintedName" })
# import * as _namespace from "source"
(opens new window)
import { addNamespace } from "@babel/helper-module-imports";
addNamespace(path, 'source');
# 例子
# 添加命名导入 (opens new window)
import { addNamed } from "@babel/helper-module-imports";
export default function({ types: t }) {
return {
visitor: {
ReferencedIdentifier(path) {
let importName = this.importName;
if (importName) {
importName = t.cloneDeep(importName);
} else {
// require('bluebird').coroutine
importName = this.importName = addNamed(path, 'coroutine', 'bluebird');
}
path.replaceWith(importName);
}
},
};
}