1 | # babelify [![Build Status](https://travis-ci.org/babel/babelify.svg?branch=master)](https://travis-ci.org/babel/babelify)
|
2 |
|
3 | [Babel](https://github.com/babel/babel) [browserify](https://github.com/substack/node-browserify) transform.
|
4 |
|
5 | As of [Babel 6.0.0](http://babeljs.io/blog/2015/10/29/6.0.0/) there are **no plugins included by default**. For babelify to be useful, you must also include some [presets](http://babeljs.io/docs/plugins/#presets) and/or [plugins](http://babeljs.io/docs/plugins/#transform).
|
6 |
|
7 | ## Installation
|
8 |
|
9 | ```sh
|
10 | # Babel 7
|
11 | $ npm install --save-dev babelify @babel/core
|
12 |
|
13 | # Babel 6
|
14 | $ npm install --save-dev babelify@8 babel-core
|
15 | ```
|
16 |
|
17 | ## Usage
|
18 |
|
19 | ### CLI
|
20 |
|
21 | ```sh
|
22 | $ browserify script.js -o bundle.js -t [ babelify --presets [ @babel/preset-env @babel/preset-react ] --plugins [ @babel/plugin-transform-class-properties ] ]
|
23 | ```
|
24 |
|
25 | ### Node
|
26 |
|
27 | ```javascript
|
28 | var fs = require("fs");
|
29 | var browserify = require("browserify");
|
30 | browserify("./script.js")
|
31 | .transform("babelify", {presets: ["@babel/preset-env", "@babel/preset-react"]})
|
32 | .bundle()
|
33 | .pipe(fs.createWriteStream("bundle.js"));
|
34 | ```
|
35 |
|
36 | **NOTE:** [Presets and plugins](http://babeljs.io/docs/plugins/) need to be installed as separate modules. For the above examples to work, you'd need to also install [`@babel/preset-env`](https://www.npmjs.com/package/@babel/preset-env) and [`@babel/preset-react`](https://www.npmjs.com/package/@babel/preset-react):
|
37 |
|
38 | ```sh
|
39 | $ npm install --save-dev @babel/preset-env @babel/preset-react
|
40 | ```
|
41 |
|
42 | ### Options
|
43 |
|
44 | Selected options are discussed below. See the [babel](http://babeljs.io/) docs for the complete list of [options](http://babeljs.io/docs/usage/options/).
|
45 |
|
46 | Options may be passed in via standard [browserify](https://github.com/substack/node-browserify#btransformtr-opts) ways:
|
47 |
|
48 | ```sh
|
49 | $ browserify -t [ babelify --presets [ @babel/preset-env @babel/preset-react ] ]
|
50 | ```
|
51 |
|
52 | ```js
|
53 | browserify().transform("babelify", {presets: ["@babel/preset-env", "@babel/preset-react"]});
|
54 | ```
|
55 |
|
56 | ```js
|
57 | var babelify = require("babelify");
|
58 | browserify().transform(babelify, {presets: ["@babel/preset-env", "@babel/preset-react"]});
|
59 | ```
|
60 |
|
61 | Or, with the `configure` method:
|
62 |
|
63 | ```js
|
64 | browserify().transform(babelify.configure({
|
65 | presets: ["@babel/preset-env", "@babel/preset-react"]
|
66 | }));
|
67 | ```
|
68 |
|
69 | #### Customizing extensions
|
70 |
|
71 | By default, all files with the extensions `.js`, `.es`, `.es6` and `.jsx` are compiled. You can change this by passing an array of extensions.
|
72 |
|
73 | **NOTE:** This will override the default ones so if you want to use any of them
|
74 | you have to add them back.
|
75 |
|
76 | ```js
|
77 | browserify().transform("babelify", {extensions: [".babel"]});
|
78 | ```
|
79 |
|
80 | ```sh
|
81 | $ browserify -t [ babelify --extensions .babel ]
|
82 | ```
|
83 |
|
84 | Now you can use:
|
85 |
|
86 | ```js
|
87 | import NavBar from "nav-bar.babel";
|
88 | var Panels = require("panels.babel");
|
89 | ```
|
90 |
|
91 | **NOTE:** By default, Browserify will only lookup `.js` and `.json` files when the extension is ommited (like node's `require`). To lookup additional extensions, use browserify's [`extensions` option](https://github.com/substack/node-browserify#browserifyfiles--opts).
|
92 |
|
93 | ```js
|
94 | browserify({
|
95 | extensions: [".babel"]
|
96 | }).transform("babelify", {
|
97 | extensions: [".babel"]
|
98 | });
|
99 | ```
|
100 |
|
101 | ```sh
|
102 | $ browserify --extensions=.babel -t [ babelify --extensions .babel ]
|
103 | ```
|
104 |
|
105 | Now you can omit the extension and compile `.babel` files:
|
106 |
|
107 | ```js
|
108 | import NavBar from "nav-bar";
|
109 | var Panels = require("panels");
|
110 | ```
|
111 |
|
112 | #### Source maps
|
113 |
|
114 | By default, browserify sets the source map sources paths relative to the basedir (or to `process.cwd()` if not set). To make the sources paths absolute, set the `sourceMapsAbsolute` option on babelify:
|
115 |
|
116 | ```js
|
117 | browserify().transform("babelify", {
|
118 | sourceMapsAbsolute: true
|
119 | });
|
120 | ```
|
121 |
|
122 | ```sh
|
123 | $ browserify -t [ babelify --sourceMapsAbsolute ]
|
124 | ```
|
125 |
|
126 | #### Additional options
|
127 |
|
128 | ```javascript
|
129 | browserify().transform(babelify.configure({
|
130 | // Optional ignore regex - if any filenames **do** match this regex then
|
131 | // they aren't compiled
|
132 | ignore: /regex/,
|
133 |
|
134 | // Optional only regex - if any filenames **don't** match this regex
|
135 | // then they aren't compiled
|
136 | only: /my_es6_folder/
|
137 | }))
|
138 | ```
|
139 |
|
140 | ```sh
|
141 | $ browserify -t [ babelify --ignore regex --only my_es6_folder ]
|
142 | ```
|
143 |
|
144 | #### Babel result (metadata and others)
|
145 |
|
146 | Babelify emits a `babelify` event with Babel's full result object as the first
|
147 | argument, and the filename as the second. Browserify doesn't pass-through the
|
148 | events emitted by a transform, so it's necessary to get a reference to the
|
149 | transform instance before you can attach a listener for the event:
|
150 |
|
151 | ```js
|
152 | var b = browserify().transform(babelify);
|
153 |
|
154 | b.on("transform", function(tr) {
|
155 | if (tr instanceof babelify) {
|
156 | tr.once("babelify", function(result, filename) {
|
157 | result; // => { code, map, ast, metadata }
|
158 | });
|
159 | }
|
160 | });
|
161 | ```
|
162 |
|
163 | ## FAQ
|
164 |
|
165 | ### Why aren't files in `node_modules` being transformed?
|
166 |
|
167 | This is the default browserify behavior.
|
168 |
|
169 | A possible solution is to add:
|
170 |
|
171 | ```json
|
172 | {
|
173 | "browserify": {
|
174 | "transform": ["babelify"]
|
175 | }
|
176 | }
|
177 | ```
|
178 |
|
179 | to the root of all your modules `package.json` that you want to be transformed. If you'd like to
|
180 | specify options then you can use:
|
181 |
|
182 | ```json
|
183 | {
|
184 | "browserify": {
|
185 | "transform": [["babelify", { "presets": ["@babel/preset-env"] }]]
|
186 | }
|
187 | }
|
188 | ```
|
189 |
|
190 | Another solution (proceed with caution!) is to run babelify as a [global](https://github.com/substack/node-browserify#btransformtr-opts) transform. Use the babel [`ignore` option](http://babeljs.io/docs/usage/options/) to narrow the number of files transformed:
|
191 |
|
192 | ```js
|
193 | browserify().transform("babelify", {
|
194 | global: true,
|
195 | ignore: /\/node_modules\/(?!app\/)/
|
196 | });
|
197 | ```
|
198 |
|
199 | The above example will result in a transform that also includes the `app` module in `node_modules`: the `global` flag transform all files, and the `ignore` regular expression then excludes all those in the `node_modules` directory *except* those that are in `node_modules/app` (since `?!` will match if the given suffix is absent).
|
200 |
|
201 | ### Why am I not getting source maps?
|
202 |
|
203 | To use source maps, enable them in browserify with the [`debug`](https://github.com/substack/node-browserify#browserifyfiles--opts) option:
|
204 |
|
205 | ```js
|
206 | browserify({debug: true}).transform("babelify");
|
207 | ```
|
208 |
|
209 | ```sh
|
210 | $ browserify -d -t [ babelify ]
|
211 | ```
|
212 |
|
213 | If you want the source maps to be of the post-transpiled code, then leave `debug` on, but turn off babelify's `sourceMaps`:
|
214 |
|
215 | ```js
|
216 | browserify({debug: true}).transform("babelify", {sourceMaps: false});
|
217 | ```
|
218 |
|
219 | ```sh
|
220 | $ browserify -d -t [ babelify --no-sourceMaps ]
|
221 | ```
|