@babel/helper-module-imports

  1. npm install @babel/helper-module-imports --save

Usage

import "source"

  1. import { addSideEffect } from "@babel/helper-module-imports";
  2. addSideEffect(path, 'source');

import { named as _named } from "source"

  1. import { addNamed } from "@babel/helper-module-imports";
  2. // if the hintedName isn't set, the function will gennerate a uuid as hintedName itself such as '_named'
  3. addNamed(path, 'named', 'source');

import { named as _hintedName } from "source"

  1. import { addNamed } from "@babel/helper-module-imports";
  2. addNamed(path, 'named', 'source', { nameHint: "hintedName" });

import _default from "source"

  1. import { addDefault } from "@babel/helper-module-imports";
  2. addDefault(path, 'source');

import hintedName from "source"

  1. import { addDefault } from "@babel/helper-module-imports";
  2. addDefault(path, 'source', { nameHint: "hintedName" })

import * as _namespace from "source"

  1. import { addNamespace } from "@babel/helper-module-imports";
  2. addNamespace(path, 'source');

Examples

Adding a named import

  1. import { addNamed } from "@babel/helper-module-imports";
  2. export default function({ types: t }) {
  3. return {
  4. visitor: {
  5. ReferencedIdentifier(path) {
  6. let importName = this.importName;
  7. if (importName) {
  8. importName = t.cloneDeep(importName);
  9. } else {
  10. // require('bluebird').coroutine
  11. importName = this.importName = addNamed(path, 'coroutine', 'bluebird');
  12. }
  13. path.replaceWith(importName);
  14. }
  15. },
  16. };
  17. }