aboutsummaryrefslogtreecommitdiff
path: root/node_modules/@babel/helper-module-imports/README.md
blob: 93c134d0fbfad23b1109591e4e9cadd188a72723 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# @babel/helper-module-imports

## Installation

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

## Usage

### `import "source"`

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

### `import { named } from "source"`

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

### `import { named as _hintedName } from "source"`

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

### `import _default from "source"`

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

### `import hintedName from "source"`

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

### `import * as _namespace from "source"`

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

## Examples

### Adding a named import

```js
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);
      }
    },
  };
}
```