Babel
  • Docs
  • Setup
  • Try it out
  • Videos
  • Blog
  • Donate
  • Team
  • GitHub

›Helper Packages

Guides

  • What is Babel?
  • Usage Guide
  • Configure Babel
  • Learn ES2015
  • Upgrade to Babel 7

Config Reference

  • Config Files
  • Config Options
  • Presets
  • Plugins
  • Plugins List
  • Compiler assumptions

Presets

  • @babel/preset-env
  • @babel/preset-react
  • @babel/preset-typescript
  • @babel/preset-flow

Misc

  • Roadmap
  • Caveats
  • Features Timeline
  • FAQ
  • Editors

Integration Packages

  • @babel/cli
  • @babel/polyfill
  • @babel/plugin-transform-runtime
  • @babel/register
  • @babel/standalone

Tooling Packages

  • @babel/parser
  • @babel/core
  • @babel/generator
  • @babel/code-frame
  • @babel/runtime
  • @babel/template
  • @babel/traverse
  • @babel/types

Helper Packages

  • helper-compilation-targets
  • helper-module-imports
Edit

@babel/helper-module-imports

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

Usage

import "source"

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

import { named as _named } from "source"

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"

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

import _default from "source"

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

import hintedName from "source"

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

import * as _namespace from "source"

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

Examples

Adding a named import

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);
      }
    },
  };
}
← helper-compilation-targets
  • Usage
    • import "source"
    • import { named as _named } from "source"
    • import { named as _hintedName } from "source"
    • import _default from "source"
    • import hintedName from "source"
    • import * as _namespace from "source"
  • Examples
    • Adding a named import
Babel
Docs
Learn ES2015
Community
VideosUser ShowcaseStack OverflowSlack ChannelTwitter
More
BlogGitHub OrgGitHub RepoWebsite RepoOld 6.x SiteOld 5.x Site