1 |
9bb1e829
|
cagy
|
# babel-plugin-transform-es2015-block-scoping
|
2 |
|
|
|
3 |
|
|
> Compile ES2015 block scoping (const and let) to ES5
|
4 |
|
|
|
5 |
|
|
## Installation
|
6 |
|
|
|
7 |
|
|
```sh
|
8 |
|
|
npm install --save-dev babel-plugin-transform-es2015-block-scoping
|
9 |
|
|
```
|
10 |
|
|
|
11 |
|
|
## Usage
|
12 |
|
|
|
13 |
|
|
### Via `.babelrc` (Recommended)
|
14 |
|
|
|
15 |
|
|
**.babelrc**
|
16 |
|
|
|
17 |
|
|
Without options:
|
18 |
|
|
|
19 |
|
|
```json
|
20 |
|
|
{
|
21 |
|
|
"plugins": ["transform-es2015-block-scoping"]
|
22 |
|
|
}
|
23 |
|
|
```
|
24 |
|
|
|
25 |
|
|
With options:
|
26 |
|
|
|
27 |
|
|
```json
|
28 |
|
|
{
|
29 |
|
|
"plugins": [
|
30 |
|
|
["transform-es2015-block-scoping", {
|
31 |
|
|
"throwIfClosureRequired": true
|
32 |
|
|
}]
|
33 |
|
|
]
|
34 |
|
|
}
|
35 |
|
|
```
|
36 |
|
|
|
37 |
|
|
### Via CLI
|
38 |
|
|
|
39 |
|
|
```sh
|
40 |
|
|
babel --plugins transform-es2015-block-scoping script.js
|
41 |
|
|
```
|
42 |
|
|
|
43 |
|
|
### Via Node API
|
44 |
|
|
|
45 |
|
|
```javascript
|
46 |
|
|
require("babel-core").transform("code", {
|
47 |
|
|
plugins: ["transform-es2015-block-scoping"]
|
48 |
|
|
});
|
49 |
|
|
```
|
50 |
|
|
|
51 |
|
|
## Options `throwIfClosureRequired`
|
52 |
|
|
|
53 |
|
|
In cases such as the following it's impossible to rewrite let/const without adding an additional function and closure while transforming:
|
54 |
|
|
|
55 |
|
|
```javascript
|
56 |
|
|
for (let i = 0; i < 5; i++) {
|
57 |
|
|
setTimeout(() => console.log(i), 1);
|
58 |
|
|
}
|
59 |
|
|
```
|
60 |
|
|
|
61 |
|
|
In extremely performance-sensitive code, this can be undesirable. If `"throwIfClosureRequired": true` is set, Babel throws when transforming these patterns instead of automatically adding an additional function.
|