1 | # babel-helper-module-imports
|
2 |
|
3 | ## Installation
|
4 |
|
5 | ```sh
|
6 | npm install babel-helper-module-imports --save
|
7 | ```
|
8 |
|
9 | ## Usage
|
10 |
|
11 | ### `import "source"`
|
12 |
|
13 | ```js
|
14 | import { addSideEffect } from "babel-helper-module-imports";
|
15 | addSideEffect(path, 'source');
|
16 | ```
|
17 |
|
18 | ### `import { named } from "source"`
|
19 |
|
20 | ```js
|
21 | import { addNamed } from "babel-helper-module-imports";
|
22 | addNamed(path, 'named', 'source');
|
23 | ```
|
24 |
|
25 | ### `import { named as _hintedName } from "source"`
|
26 |
|
27 | ```js
|
28 | import { addNamed } from "babel-helper-module-imports";
|
29 | addNamed(path, 'named', 'source', { nameHint: "hintedName" });
|
30 | ```
|
31 |
|
32 | ### `import _default from "source"`
|
33 |
|
34 | ```js
|
35 | import { addDefault } from "babel-helper-module-imports";
|
36 | addDefault(path, 'source');
|
37 | ```
|
38 |
|
39 | ### `import hintedName from "source"`
|
40 |
|
41 | ```js
|
42 | import { addDefault } from "babel-helper-module-imports";
|
43 | addDefault(path, 'source', { nameHint: "hintedName" })
|
44 | ```
|
45 |
|
46 | ### `import * as _namespace from "source"`
|
47 |
|
48 | ```js
|
49 | import { addNamespace } from "babel-helper-module-imports";
|
50 | addNamespace(path, 'source');
|
51 | ```
|
52 |
|
53 | ## Examples
|
54 |
|
55 | ### Adding a named import
|
56 |
|
57 | ```js
|
58 | import { addNamed } from "babel-helper-module-imports";
|
59 |
|
60 | export 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 | ```
|