# babel-import-util

> Utility for manipulating imports within babel plugins

Latest version **3.0.1** (published 2025-03-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install babel-import-util
pnpm add babel-import-util
yarn add babel-import-util
bun add babel-import-util
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.1 |
| Published | 2025-03-04 |
| First published | 2021-07-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 12.* |
| Dependencies | 0 |
| Unpacked size | 47.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Edward Faulkner |
| Maintainers | ef4 |

## Links

- npm: https://www.npmjs.com/package/babel-import-util
- Repository: https://github.com/ef4/babel-import-util
- Homepage: https://github.com/ef4/babel-import-util#readme
- Issues: https://github.com/ef4/babel-import-util/issues
- npm.io page: https://npm.io/package/babel-import-util

## Recent versions

- 3.0.1 (latest) — 2025-03-04
- 3.0.0 — 2024-04-26
- 2.1.1 — 2024-04-26
- 2.1.0 — 2024-04-26
- 2.0.3 — 2024-04-18
- 2.0.2 — 2024-04-09
- 2.0.1 — 2023-09-25
- 1.4.1 — 2023-07-15
- 2.0.0 — 2023-07-15
- 1.4.0 — 2023-07-15
- 1.3.0 — 2022-11-09
- 1.2.2 — 2022-04-12
- 1.2.1 — 2022-04-11
- 1.2.0 — 2022-04-11
- 1.1.0 — 2021-12-08
- … 3 more at https://npm.io/package/babel-import-util/versions

## README

# babel-import-util

Makes it easier for a babel plugin to emit imported names. Key benefits:

- the output composes correctly with subsequent babel plugins, because we update Babel's understanding of the bindings
- redundant imports will be deduplicated automatically
- written in TypeScript

## Usage by example:

If you want to rewrite:

```js
myTarget('hello world');
```

To:

```js
import { theMethod } from 'my-implementation';
theMethod('hello world');
```

Your plugin would look like this:

```js
function testTransform(babel) {
  return {
    visitor: {
      Program: {
        enter(path, state) {
          // Always instantiate the ImportUtil instance at the Program scope
          state.importUtil = new ImportUtil(babel, path);
        },
      },
      CallExpression(path, state) {
        let callee = path.get('callee');
        if (callee.isIdentifier() && callee.node.name === 'myTarget') {
          state.importUtil.replaceWith(callee, (i) =>
            i.import(callee, 'my-implementation', 'theMethod')
          );
        }
      },
    },
  };
}
```

## API

```ts
import type { NodePath } from '@babel/traverse';
import type * as t from '@babel/types';

class ImportUtil {
  /*
   Replace `target` with the new node produced by your callback. Your
   callback can use `i.import` to gain access to imported identifiers.

   Example:

   util.replaceWith(path, (i) =>
     t.callExpression(i.import('my-library', 'someFunction'), [])
   );
  */
  replaceWith<T extends t.Node, R extends t.Node>(
    target: NodePath<T>,
    fn: (i: Importer) => R
  ): NodePath<R>;

  /*
    Similar to `replaceWith` above, except instead of replacing the target 
    we will insert the new Node before or after it.
  */
  insertAfter<T extends t.Node, R extends t.Node>(
    target: NodePath<T>,
    fn: (i: Importer) => R
  ): NodePath<R>;
  insertBefore<T extends t.Node, R extends t.Node>(
    target: NodePath<T>,
    fn: (i: Importer) => R
  ): NodePath<R>;

  // If needed, adds a bare import like:
  //    import "your-module";
  importForSideEffect(moduleSpecifier: string): void;

  // Remove an import specifier. If the removed specifier is
  // the last one on the whole import statement, the whole
  // statement is also removed.
  //
  // You can use "default" and "*" as exportedName to handle
  // those special cases.
  removeImport(moduleSpecifier: string, exportedName: string): void;

  // Remove all imports from the given moduleSpecifier. Unlike
  // removeImport(), this can also remove "bare" import statements
  //  that were purely for side effect.
  removeAllImports(moduleSpecifier: string): void;

  // Import the given value (if needed) and return an Identifier representing
  // it.
  // CAUTION: this is a lower-level API that leaves some of the reference
  // safety up to you. It's better to use replaceWith, insertAfter, insertBefore,
  // or mutate. But this can still be helpful in contexts where you're already
  // planning to manage babel's scopes anyawy.
  import(
    // the spot at which you will insert the Identifier we return to you
    target: NodePath<t.Node>,

    // the path to the module you're importing from
    moduleSpecifier: string,

    // the name you're importing from that module. Use "default" for the default
    // export. Use "*" for the namespace.
    exportedName: string,

    // Optional hint for helping us pick a name for the imported binding
    nameHint?: string
  ): t.Identifier;
}
```

---
_Source: https://npm.io/package/babel-import-util · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
