UNPKG

6.69 kBMarkdownView Raw
1# babel-loader [![Build Status](https://travis-ci.org/babel/babel-loader.svg?branch=master)](https://travis-ci.org/babel/babel-loader)
2 > Babel is a compiler for writing next generation JavaScript.
3
4 This package allows transpiling JavaScript files using [Babel](https://github.com/babel/babel) and [webpack](https://github.com/webpack/webpack).
5
6 __Notes:__ Issues with the output should be reported on the babel [issue tracker](https://github.com/babel/babel/issues);
7
8## Installation
9
10```bash
11npm install babel-loader babel-core babel-preset-es2015 --save-dev
12```
13
14__Note:__ [npm](https://npmjs.com) will deprecate [auto-installing of peerDependencies](https://github.com/npm/npm/issues/6565) on the next major release, so required peer dependencies like babel-core and webpack will have to be listed explicitly in your `package.json`.
15
16__Note:__ If you're upgrading from babel 5 to babel 6, please take a look [at this guide](https://medium.com/@malyw/how-to-update-babel-5-x-6-x-d828c230ec53#.yqxukuzdk).
17
18## Usage
19
20[Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html)
21
22 Within your webpack configuration object, you'll need to add the babel-loader to the list of modules, like so:
23
24 ```javascript
25module: {
26 loaders: [
27 {
28 test: /\.jsx?$/,
29 exclude: /(node_modules|bower_components)/,
30 loader: 'babel' // 'babel-loader' is also a legal name to reference
31 }
32 ]
33}
34 ```
35
36### Options
37
38See the `babel` [options](http://babeljs.io/docs/usage/options/).
39
40You can pass options to the loader by writing them as a [query string](https://github.com/webpack/loader-utils):
41
42 ```javascript
43module: {
44 loaders: [
45 {
46 test: /\.jsx?$/,
47 exclude: /(node_modules|bower_components)/,
48 loader: 'babel?presets[]=es2015'
49 }
50 ]
51}
52 ```
53
54 or by using the query property:
55
56 ```javascript
57module: {
58 loaders: [
59 {
60 test: /\.jsx?$/,
61 exclude: /(node_modules|bower_components)/,
62 loader: 'babel',
63 query: {
64 presets: ['es2015']
65 }
66 }
67 ]
68}
69 ```
70
71 This loader also supports the following loader-specific option:
72
73 * `cacheDirectory`: Default `false`. When set, the given directory will be used to cache the results of the loader. Future webpack builds will attempt to read from the cache to avoid needing to run the potentially expensive Babel recompilation process on each run. If the value is blank (`loader: 'babel-loader?cacheDirectory'`) the loader will use the default OS temporary file directory.
74
75 * `cacheIdentifier`: Default is a string composed by the babel-core's version, the babel-loader's version and the contents of .babelrc file if it exists. This can set to a custom value to force cache busting if the identifier changes.
76
77
78 __Note:__ The `sourceMap` option is ignored, instead sourceMaps are automatically enabled when webpack is configured to use them (via the `devtool` config option).
79
80## Troubleshooting
81
82### babel-loader is slow!
83
84 Make sure you are transforming as few files as possible. Because you are probably
85 matching `/\.js$/`, you might be transforming the `node_modules` folder or other unwanted
86 source.
87
88 To exclude `node_modules`, see the `exclude` option in the `loaders` config as documented above.
89
90 You can also speed up babel-loader by as much as 2x by using the `cacheDirectory` option.
91 This will cache transformations to the filesystem.
92
93### babel is injecting helpers into each file and bloating my code!
94
95 babel uses very small helpers for common functions such as `_extend`. By default
96 this will be added to every file that requires it.
97
98 You can instead require the babel runtime as a separate module to avoid the duplication.
99
100 The following configuration disables automatic per-file runtime injection in babel, instead
101 requiring `babel-runtime` and making all helper references use it.
102
103 See the [docs](https://babeljs.io/docs/usage/runtime) for more information.
104
105 **NOTE:** You must run `npm install babel-runtime --save` to include this in your project.
106
107```javascript
108loaders: [
109 // the optional 'runtime' transformer tells babel to require the runtime
110 // instead of inlining it.
111 {
112 test: /\.jsx?$/,
113 exclude: /(node_modules|bower_components)/,
114 loader: 'babel?presets[]=es2015'
115 }
116]
117```
118
119### using `cacheDirectory` fails with ENOENT Error
120
121If using cacheDirectory results in an error similar to the following:
122
123```
124ERROR in ./frontend/src/main.jsx
125Module build failed: Error: ENOENT, open 'true/350c59cae6b7bce3bb58c8240147581bfdc9cccc.json.gzip'
126 @ multi app
127```
128(notice the `true/` in the filepath)
129
130That means that most likely, you're not setting the options correctly, and you're doing something similar to:
131
132```javascript
133loaders: [
134 {
135 test: /\.jsx?$/,
136 exclude: /(node_modules|bower_components)/,
137 loader: 'babel?cacheDirectory=true'
138 }
139]
140```
141
142That's not the correct way of setting boolean values. You should do instead:
143
144```javascript
145loaders: [
146 {
147 test: /\.jsx?$/,
148 exclude: /(node_modules|bower_components)/,
149 loader: 'babel?cacheDirectory'
150 }
151]
152```
153
154or use the [query](https://webpack.github.io/docs/using-loaders.html#query-parameters) property:
155
156```javascript
157loaders: [
158 // the optional 'runtime' transformer tells babel to require the runtime
159 // instead of inlining it.
160 {
161 test: /\.jsx?$/,
162 exclude: /(node_modules|bower_components)/,
163 loader: 'babel',
164 query: {
165 cacheDirectory: true
166 }
167 }
168]
169```
170
171
172### custom polyfills (e.g. Promise library)
173
174Since Babel includes a polyfill that includes a custom [regenerator runtime](https://github.com/facebook/regenerator/blob/master/runtime.js) and [core.js](https://github.com/zloirock/core-js), the following usual shimming method using `webpack.ProvidePlugin` will not work:
175
176```javascript
177// ...
178 new webpack.ProvidePlugin({
179 'Promise': 'bluebird'
180 }),
181// ...
182```
183
184The following approach will not work either:
185
186```javascript
187require('babel-runtime/core-js/promise').default = require('bluebird');
188
189var promise = new Promise;
190```
191
192which outputs to (using `runtime`):
193
194```javascript
195'use strict';
196
197var _Promise = require('babel-runtime/core-js/promise')['default'];
198
199require('babel-runtime/core-js/promise')['default'] = require('bluebird');
200
201var promise = new _Promise();
202```
203
204The previous `Promise` library is referenced and used before it is overridden.
205
206One approach is to have a "bootstrap" step in your application that would first override the default globals before your application:
207
208```javascript
209// bootstrap.js
210
211require('babel-runtime/core-js/promise').default = require('bluebird');
212
213// ...
214
215require('./app');
216```
217
218## [License](http://couto.mit-license.org/)