704fdc617050f8be41f7581eaf9815e0108e1e1d78a998be98039e45805cefd7ebef182908dacbeaa6b3b2b83d6277c5aa37efc6cde173f86785e55e666ce2 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # babel-register
  2. > The require hook will bind itself to node's require and automatically compile files on the fly.
  3. One of the ways you can use Babel is through the require hook. The require hook
  4. will bind itself to node's `require` and automatically compile files on the
  5. fly. This is equivalent to CoffeeScript's
  6. [coffee-script/register](http://coffeescript.org/v2/annotated-source/register.html).
  7. ## Install
  8. ```sh
  9. npm install babel-register --save-dev
  10. ```
  11. ## Usage
  12. ```js
  13. require("babel-register");
  14. ```
  15. All subsequent files required by node with the extensions `.es6`, `.es`, `.jsx`
  16. and `.js` will be transformed by Babel.
  17. <blockquote class="babel-callout babel-callout-info">
  18. <h4>Polyfill not included</h4>
  19. <p>
  20. You must include the <a href="https://babeljs.io/docs/usage/polyfill/">polyfill</a> separately
  21. when using features that require it, like generators.
  22. </p>
  23. </blockquote>
  24. ### Ignores `node_modules` by default
  25. **NOTE:** By default all requires to `node_modules` will be ignored. You can
  26. override this by passing an ignore regex via:
  27. ```js
  28. require("babel-register")({
  29. // This will override `node_modules` ignoring - you can alternatively pass
  30. // an array of strings to be explicitly matched or a regex / glob
  31. ignore: false
  32. });
  33. ```
  34. ## Specifying options
  35. ```javascript
  36. require("babel-register")({
  37. // Optional ignore regex - if any filenames **do** match this regex then they
  38. // aren't compiled.
  39. ignore: /regex/,
  40. // Ignore can also be specified as a function.
  41. ignore: function(filename) {
  42. if (filename === "/path/to/es6-file.js") {
  43. return false;
  44. } else {
  45. return true;
  46. }
  47. },
  48. // Optional only regex - if any filenames **don't** match this regex then they
  49. // aren't compiled
  50. only: /my_es6_folder/,
  51. // Setting this will remove the currently hooked extensions of .es6, `.es`, `.jsx`
  52. // and .js so you'll have to add them back if you want them to be used again.
  53. extensions: [".es6", ".es", ".jsx", ".js"],
  54. // Setting this to false will disable the cache.
  55. cache: true
  56. });
  57. ```
  58. You can pass in all other [options](https://babeljs.io/docs/usage/api/#options) as well,
  59. including `plugins` and `presets`. But note that the closest [`.babelrc`](https://babeljs.io/docs/usage/babelrc/)
  60. to each file still applies, and takes precedence over any options you pass in here.
  61. ## Environment variables
  62. By default `babel-node` and `babel-register` will save to a json cache in your
  63. temporary directory.
  64. This will heavily improve with the startup and compilation of your files. There
  65. are however scenarios where you want to change this behaviour and there are
  66. environment variables exposed to allow you to do this.
  67. ### BABEL_CACHE_PATH
  68. Specify a different cache location.
  69. ```sh
  70. BABEL_CACHE_PATH=/foo/my-cache.json babel-node script.js
  71. ```
  72. ### BABEL_DISABLE_CACHE
  73. Disable the cache.
  74. ```sh
  75. BABEL_DISABLE_CACHE=1 babel-node script.js
  76. ```