UNPKG

1.54 kBMarkdownView Raw
1# babel-helper-module-imports
2
3## Installation
4
5```sh
6npm install babel-helper-module-imports --save
7```
8
9## Usage
10
11### `import "source"`
12
13```js
14import { addSideEffect } from "babel-helper-module-imports";
15addSideEffect(path, 'source');
16```
17
18### `import { named } from "source"`
19
20```js
21import { addNamed } from "babel-helper-module-imports";
22addNamed(path, 'named', 'source');
23```
24
25### `import { named as _hintedName } from "source"`
26
27```js
28import { addNamed } from "babel-helper-module-imports";
29addNamed(path, 'named', 'source', { nameHint: "hintedName" });
30```
31
32### `import _default from "source"`
33
34```js
35import { addDefault } from "babel-helper-module-imports";
36addDefault(path, 'source');
37```
38
39### `import hintedName from "source"`
40
41```js
42import { addDefault } from "babel-helper-module-imports";
43addDefault(path, 'source', { nameHint: "hintedName" })
44```
45
46### `import * as _namespace from "source"`
47
48```js
49import { addNamespace } from "babel-helper-module-imports";
50addNamespace(path, 'source');
51```
52
53## Examples
54
55### Adding a named import
56
57```js
58import { addNamed } from "babel-helper-module-imports";
59
60export default function({ types: t }) {
61 return {
62 visitor: {
63 ReferencedIdentifier(path) {
64 let importName = this.importName;
65 if (importName) {
66 importName = t.cloneDeep(importName);
67 } else {
68 // require('bluebird').coroutine
69 importName = this.importName = addNamed(path, 'coroutine', 'bluebird');
70 }
71
72 path.replaceWith(importName);
73 }
74 },
75 };
76}
77```