0c5c480fd432a2c0b1cb092e00c033d5233823cd317ff03291864fcc9c4830c3deccf648189a4a9e19eb28f1c3e801a3ecf8677fe85a19f4b2bb4e7cd3cf6f 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # babel-plugin-transform-es2015-block-scoping
  2. > Compile ES2015 block scoping (const and let) to ES5
  3. ## Installation
  4. ```sh
  5. npm install --save-dev babel-plugin-transform-es2015-block-scoping
  6. ```
  7. ## Usage
  8. ### Via `.babelrc` (Recommended)
  9. **.babelrc**
  10. Without options:
  11. ```json
  12. {
  13. "plugins": ["transform-es2015-block-scoping"]
  14. }
  15. ```
  16. With options:
  17. ```json
  18. {
  19. "plugins": [
  20. ["transform-es2015-block-scoping", {
  21. "throwIfClosureRequired": true
  22. }]
  23. ]
  24. }
  25. ```
  26. ### Via CLI
  27. ```sh
  28. babel --plugins transform-es2015-block-scoping script.js
  29. ```
  30. ### Via Node API
  31. ```javascript
  32. require("babel-core").transform("code", {
  33. plugins: ["transform-es2015-block-scoping"]
  34. });
  35. ```
  36. ## Options `throwIfClosureRequired`
  37. In cases such as the following it's impossible to rewrite let/const without adding an additional function and closure while transforming:
  38. ```javascript
  39. for (let i = 0; i < 5; i++) {
  40. setTimeout(() => console.log(i), 1);
  41. }
  42. ```
  43. 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.