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