babel-loader 7.1.5 → 8.0.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +95 -19
- package/lib/Error.js +36 -0
- package/lib/cache.js +218 -0
- package/lib/index.js +121 -155
- package/lib/transform.js +48 -0
- package/lib/utils/relative.js +8 -8
- package/package.json +13 -17
- package/lib/fs-cache.js +0 -180
- package/lib/resolve-rc.js +0 -26
- package/lib/utils/exists.js +0 -13
- package/lib/utils/read.js +0 -14
package/README.md
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
> This readme is for babel-loader v8 + Babel v7
|
2
|
+
> Check the [7.x branch](https://github.com/babel/babel-loader/tree/7.x) for docs with Babel v6
|
3
|
+
|
1
4
|
[](https://www.npmjs.com/package/babel-loader)
|
2
5
|
[](https://travis-ci.org/babel/babel-loader)
|
3
6
|
[](https://ci.appveyor.com/project/danez/babel-loader/branch/master)
|
@@ -19,20 +22,16 @@ __Notes:__ Issues with the output should be reported on the babel [issue tracker
|
|
19
22
|
|
20
23
|
<h2 align="center">Install</h2>
|
21
24
|
|
22
|
-
> webpack
|
23
|
-
>
|
24
|
-
> webpack 2.x | babel-loader >= 7.x (recommended) (^6.2.10 will also work, but with deprecation warnings)
|
25
|
-
>
|
26
|
-
> webpack 3.x | babel-loader >= 7.1
|
25
|
+
> webpack 3.x | babel-loader 8.x | babel 7.x
|
27
26
|
|
28
27
|
```bash
|
29
|
-
|
28
|
+
npm install "babel-loader@^8.0.0-beta" @babel/core @babel/preset-env webpack
|
30
29
|
```
|
31
30
|
|
32
|
-
|
31
|
+
> webpack 3.x babel-loader 7.x | babel 6.x
|
33
32
|
|
34
33
|
```bash
|
35
|
-
npm install
|
34
|
+
npm install babel-loader babel-core babel-preset-env webpack
|
36
35
|
```
|
37
36
|
|
38
37
|
<h2 align="center">Usage</h2>
|
@@ -50,7 +49,7 @@ module: {
|
|
50
49
|
use: {
|
51
50
|
loader: 'babel-loader',
|
52
51
|
options: {
|
53
|
-
presets: ['env']
|
52
|
+
presets: ['@babel/preset-env']
|
54
53
|
}
|
55
54
|
}
|
56
55
|
}
|
@@ -74,8 +73,8 @@ module: {
|
|
74
73
|
use: {
|
75
74
|
loader: 'babel-loader',
|
76
75
|
options: {
|
77
|
-
presets: ['env'],
|
78
|
-
plugins: [require('babel
|
76
|
+
presets: ['@babel/preset-env'],
|
77
|
+
plugins: [require('@babel/plugin-proposal-object-rest-spread')]
|
79
78
|
}
|
80
79
|
}
|
81
80
|
}
|
@@ -89,7 +88,8 @@ This loader also supports the following loader-specific option:
|
|
89
88
|
|
90
89
|
* `cacheIdentifier`: Default is a string composed by the babel-core's version, the babel-loader's version, the contents of .babelrc file if it exists and the value of the environment variable `BABEL_ENV` with a fallback to the `NODE_ENV` environment variable. This can be set to a custom value to force cache busting if the identifier changes.
|
91
90
|
|
92
|
-
* `
|
91
|
+
* `babelrc`: Default `true`. When `false`, no options from `.babelrc` files will be used; only the options passed to
|
92
|
+
`babel-loader` will be used.
|
93
93
|
|
94
94
|
__Note:__ The `sourceMap` option is ignored, instead sourceMaps are automatically enabled when webpack is configured to use them (via the `devtool` config option).
|
95
95
|
|
@@ -118,7 +118,7 @@ requiring `babel-plugin-transform-runtime` and making all helper references use
|
|
118
118
|
|
119
119
|
See the [docs](http://babeljs.io/docs/plugins/transform-runtime/) for more information.
|
120
120
|
|
121
|
-
**NOTE:** You must run `npm install babel
|
121
|
+
**NOTE:** You must run `npm install @babel/plugin-transform-runtime --save-dev` to include this in your project and `babel-runtime` itself as a dependency with `npm install @babel/runtime --save`.
|
122
122
|
|
123
123
|
```javascript
|
124
124
|
rules: [
|
@@ -130,8 +130,8 @@ rules: [
|
|
130
130
|
use: {
|
131
131
|
loader: 'babel-loader',
|
132
132
|
options: {
|
133
|
-
presets: ['env'],
|
134
|
-
plugins: ['transform-runtime']
|
133
|
+
presets: ['@babel/preset-env'],
|
134
|
+
plugins: ['@babel/plugin-transform-runtime']
|
135
135
|
}
|
136
136
|
}
|
137
137
|
}
|
@@ -153,7 +153,7 @@ Since [babel-plugin-transform-runtime](https://github.com/babel/babel/tree/maste
|
|
153
153
|
The following approach will not work either:
|
154
154
|
|
155
155
|
```javascript
|
156
|
-
require('babel
|
156
|
+
require('@babel/runtime/core-js/promise').default = require('bluebird');
|
157
157
|
|
158
158
|
var promise = new Promise;
|
159
159
|
```
|
@@ -163,9 +163,9 @@ which outputs to (using `runtime`):
|
|
163
163
|
```javascript
|
164
164
|
'use strict';
|
165
165
|
|
166
|
-
var _Promise = require('babel
|
166
|
+
var _Promise = require('@babel/runtime/core-js/promise')['default'];
|
167
167
|
|
168
|
-
require('babel
|
168
|
+
require('@babel/runtime/core-js/promise')['default'] = require('bluebird');
|
169
169
|
|
170
170
|
var promise = new _Promise();
|
171
171
|
```
|
@@ -177,7 +177,7 @@ One approach is to have a "bootstrap" step in your application that would first
|
|
177
177
|
```javascript
|
178
178
|
// bootstrap.js
|
179
179
|
|
180
|
-
require('babel
|
180
|
+
require('@babel/runtime/core-js/promise').default = require('bluebird');
|
181
181
|
|
182
182
|
// ...
|
183
183
|
|
@@ -205,4 +205,80 @@ In the case one of your dependencies is installing `babel` and you cannot uninst
|
|
205
205
|
}
|
206
206
|
```
|
207
207
|
|
208
|
+
## Customized Loader
|
209
|
+
|
210
|
+
`babel-loader` exposes a loader-builder utility that allows users to add custom handling
|
211
|
+
of Babel's configuration for each file that it processes.
|
212
|
+
|
213
|
+
`.custom` accepts a callback that will be called with the loader's instance of
|
214
|
+
`babel` so that tooling can ensure that it using exactly the same `@babel/core`
|
215
|
+
instance as the loader itself.
|
216
|
+
|
217
|
+
### Example
|
218
|
+
|
219
|
+
```js
|
220
|
+
module.exports = require("babel-loader").custom(babel => {
|
221
|
+
function myPlugin() {
|
222
|
+
return {
|
223
|
+
visitor: {},
|
224
|
+
};
|
225
|
+
}
|
226
|
+
|
227
|
+
return {
|
228
|
+
// Passed the loader options.
|
229
|
+
customOptions({ opt1, opt2, ...loader }) {
|
230
|
+
return {
|
231
|
+
// Pull out any custom options that the loader might have.
|
232
|
+
custom: { opt1, opt2 },
|
233
|
+
|
234
|
+
// Pass the options back with the two custom options removed.
|
235
|
+
loader,
|
236
|
+
};
|
237
|
+
},
|
238
|
+
|
239
|
+
// Passed Babel's 'PartialConfig' object.
|
240
|
+
config(cfg) {
|
241
|
+
if (cfg.hasFilesystemConfig()) {
|
242
|
+
// Use the normal config
|
243
|
+
return cfg.options;
|
244
|
+
}
|
245
|
+
|
246
|
+
return {
|
247
|
+
...cfg.options,
|
248
|
+
plugins: [
|
249
|
+
...(cfg.options.plugins || []),
|
250
|
+
|
251
|
+
// Include a custom plugin in the options.
|
252
|
+
myPlugin,
|
253
|
+
],
|
254
|
+
};
|
255
|
+
},
|
256
|
+
|
257
|
+
result(result) {
|
258
|
+
return {
|
259
|
+
...result,
|
260
|
+
code: result.code + "\n// Generated by some custom loader",
|
261
|
+
};
|
262
|
+
},
|
263
|
+
};
|
264
|
+
});
|
265
|
+
```
|
266
|
+
|
267
|
+
### `customOptions(options: Object): { custom: Object, loader: Object }`
|
268
|
+
|
269
|
+
Given the loader's options, split custom options out of `babel-loader`'s
|
270
|
+
options.
|
271
|
+
|
272
|
+
|
273
|
+
### `config(cfg: PartialConfig): Object`
|
274
|
+
|
275
|
+
Given Babel's `PartialConfig` object, return the `options` object that should
|
276
|
+
be passed to `babel.transform`.
|
277
|
+
|
278
|
+
|
279
|
+
### `result(result: Result): Result`
|
280
|
+
|
281
|
+
Given Babel's result object, allow loaders to make additional tweaks to it.
|
282
|
+
|
283
|
+
|
208
284
|
## [License](http://couto.mit-license.org/)
|
package/lib/Error.js
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
const STRIP_FILENAME_RE = /^[^:]+: /;
|
4
|
+
|
5
|
+
const format = err => {
|
6
|
+
if (err instanceof SyntaxError) {
|
7
|
+
err.name = "SyntaxError";
|
8
|
+
err.message = err.message.replace(STRIP_FILENAME_RE, "");
|
9
|
+
err.hideStack = true;
|
10
|
+
} else if (err instanceof TypeError) {
|
11
|
+
err.name = null;
|
12
|
+
err.message = err.message.replace(STRIP_FILENAME_RE, "");
|
13
|
+
err.hideStack = true;
|
14
|
+
}
|
15
|
+
|
16
|
+
return err;
|
17
|
+
};
|
18
|
+
|
19
|
+
class LoaderError extends Error {
|
20
|
+
constructor(err) {
|
21
|
+
super();
|
22
|
+
const {
|
23
|
+
name,
|
24
|
+
message,
|
25
|
+
codeFrame,
|
26
|
+
hideStack
|
27
|
+
} = format(err);
|
28
|
+
this.name = "BabelLoaderError";
|
29
|
+
this.message = `${name ? `${name}: ` : ""}${message}\n\n${codeFrame}\n`;
|
30
|
+
this.hideStack = hideStack;
|
31
|
+
Error.captureStackTrace(this, this.constructor);
|
32
|
+
}
|
33
|
+
|
34
|
+
}
|
35
|
+
|
36
|
+
module.exports = LoaderError;
|
package/lib/cache.js
ADDED
@@ -0,0 +1,218 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _next(value) { step("next", value); } function _throw(err) { step("throw", err); } _next(); }); }; }
|
4
|
+
|
5
|
+
/**
|
6
|
+
* Filesystem Cache
|
7
|
+
*
|
8
|
+
* Given a file and a transform function, cache the result into files
|
9
|
+
* or retrieve the previously cached files if the given file is already known.
|
10
|
+
*
|
11
|
+
* @see https://github.com/babel/babel-loader/issues/34
|
12
|
+
* @see https://github.com/babel/babel-loader/pull/41
|
13
|
+
*/
|
14
|
+
const fs = require("fs");
|
15
|
+
|
16
|
+
const os = require("os");
|
17
|
+
|
18
|
+
const path = require("path");
|
19
|
+
|
20
|
+
const zlib = require("zlib");
|
21
|
+
|
22
|
+
const crypto = require("crypto");
|
23
|
+
|
24
|
+
const mkdirpOrig = require("mkdirp");
|
25
|
+
|
26
|
+
const findCacheDir = require("find-cache-dir");
|
27
|
+
|
28
|
+
const promisify = require("util.promisify");
|
29
|
+
|
30
|
+
const transform = require("./transform"); // Lazily instantiated when needed
|
31
|
+
|
32
|
+
|
33
|
+
let defaultCacheDirectory = null;
|
34
|
+
const readFile = promisify(fs.readFile);
|
35
|
+
const writeFile = promisify(fs.writeFile);
|
36
|
+
const gunzip = promisify(zlib.gunzip);
|
37
|
+
const gzip = promisify(zlib.gzip);
|
38
|
+
const mkdirp = promisify(mkdirpOrig);
|
39
|
+
/**
|
40
|
+
* Read the contents from the compressed file.
|
41
|
+
*
|
42
|
+
* @async
|
43
|
+
* @params {String} filename
|
44
|
+
*/
|
45
|
+
|
46
|
+
const read =
|
47
|
+
/*#__PURE__*/
|
48
|
+
function () {
|
49
|
+
var _ref = _asyncToGenerator(function* (filename) {
|
50
|
+
const data = yield readFile(filename);
|
51
|
+
const content = yield gunzip(data);
|
52
|
+
return JSON.parse(content);
|
53
|
+
});
|
54
|
+
|
55
|
+
return function read(_x) {
|
56
|
+
return _ref.apply(this, arguments);
|
57
|
+
};
|
58
|
+
}();
|
59
|
+
/**
|
60
|
+
* Write contents into a compressed file.
|
61
|
+
*
|
62
|
+
* @async
|
63
|
+
* @params {String} filename
|
64
|
+
* @params {String} result
|
65
|
+
*/
|
66
|
+
|
67
|
+
|
68
|
+
const write =
|
69
|
+
/*#__PURE__*/
|
70
|
+
function () {
|
71
|
+
var _ref2 = _asyncToGenerator(function* (filename, result) {
|
72
|
+
const content = JSON.stringify(result);
|
73
|
+
const data = yield gzip(content);
|
74
|
+
return yield writeFile(filename, data);
|
75
|
+
});
|
76
|
+
|
77
|
+
return function write(_x2, _x3) {
|
78
|
+
return _ref2.apply(this, arguments);
|
79
|
+
};
|
80
|
+
}();
|
81
|
+
/**
|
82
|
+
* Build the filename for the cached file
|
83
|
+
*
|
84
|
+
* @params {String} source File source code
|
85
|
+
* @params {Object} options Options used
|
86
|
+
*
|
87
|
+
* @return {String}
|
88
|
+
*/
|
89
|
+
|
90
|
+
|
91
|
+
const filename = function (source, identifier, options) {
|
92
|
+
const hash = crypto.createHash("SHA1");
|
93
|
+
const contents = JSON.stringify({
|
94
|
+
source,
|
95
|
+
options,
|
96
|
+
identifier
|
97
|
+
});
|
98
|
+
hash.end(contents);
|
99
|
+
return hash.read().toString("hex") + ".json.gz";
|
100
|
+
};
|
101
|
+
/**
|
102
|
+
* Handle the cache
|
103
|
+
*
|
104
|
+
* @params {String} directory
|
105
|
+
* @params {Object} params
|
106
|
+
*/
|
107
|
+
|
108
|
+
|
109
|
+
const handleCache =
|
110
|
+
/*#__PURE__*/
|
111
|
+
function () {
|
112
|
+
var _ref3 = _asyncToGenerator(function* (directory, params) {
|
113
|
+
const {
|
114
|
+
source,
|
115
|
+
options = {},
|
116
|
+
cacheIdentifier,
|
117
|
+
cacheDirectory
|
118
|
+
} = params;
|
119
|
+
const fallback = typeof cacheDirectory !== "string" && directory !== os.tmpdir(); // Make sure the directory exists.
|
120
|
+
|
121
|
+
try {
|
122
|
+
yield mkdirp(directory);
|
123
|
+
} catch (err) {
|
124
|
+
if (fallback) {
|
125
|
+
return handleCache(os.tmpdir(), params);
|
126
|
+
}
|
127
|
+
|
128
|
+
throw err;
|
129
|
+
}
|
130
|
+
|
131
|
+
const file = path.join(directory, filename(source, cacheIdentifier, options));
|
132
|
+
|
133
|
+
try {
|
134
|
+
// No errors mean that the file was previously cached
|
135
|
+
// we just need to return it
|
136
|
+
return yield read(file);
|
137
|
+
} catch (err) {} // Otherwise just transform the file
|
138
|
+
// return it to the user asap and write it in cache
|
139
|
+
|
140
|
+
|
141
|
+
const result = yield transform(source, options);
|
142
|
+
|
143
|
+
try {
|
144
|
+
yield write(file, result);
|
145
|
+
} catch (err) {
|
146
|
+
if (fallback) {
|
147
|
+
// Fallback to tmpdir if node_modules folder not writable
|
148
|
+
return handleCache(os.tmpdir(), params);
|
149
|
+
}
|
150
|
+
|
151
|
+
throw err;
|
152
|
+
}
|
153
|
+
|
154
|
+
return result;
|
155
|
+
});
|
156
|
+
|
157
|
+
return function handleCache(_x4, _x5) {
|
158
|
+
return _ref3.apply(this, arguments);
|
159
|
+
};
|
160
|
+
}();
|
161
|
+
/**
|
162
|
+
* Retrieve file from cache, or create a new one for future reads
|
163
|
+
*
|
164
|
+
* @async
|
165
|
+
* @param {Object} params
|
166
|
+
* @param {String} params.directory Directory to store cached files
|
167
|
+
* @param {String} params.identifier Unique identifier to bust cache
|
168
|
+
* @param {String} params.source Original contents of the file to be cached
|
169
|
+
* @param {Object} params.options Options to be given to the transform fn
|
170
|
+
* @param {Function} params.transform Function that will transform the
|
171
|
+
* original file and whose result will be
|
172
|
+
* cached
|
173
|
+
*
|
174
|
+
* @example
|
175
|
+
*
|
176
|
+
* cache({
|
177
|
+
* directory: '.tmp/cache',
|
178
|
+
* identifier: 'babel-loader-cachefile',
|
179
|
+
* source: *source code from file*,
|
180
|
+
* options: {
|
181
|
+
* experimental: true,
|
182
|
+
* runtime: true
|
183
|
+
* },
|
184
|
+
* transform: function(source, options) {
|
185
|
+
* var content = *do what you need with the source*
|
186
|
+
* return content;
|
187
|
+
* }
|
188
|
+
* }, function(err, result) {
|
189
|
+
*
|
190
|
+
* });
|
191
|
+
*/
|
192
|
+
|
193
|
+
|
194
|
+
module.exports =
|
195
|
+
/*#__PURE__*/
|
196
|
+
function () {
|
197
|
+
var _ref4 = _asyncToGenerator(function* (params) {
|
198
|
+
let directory;
|
199
|
+
|
200
|
+
if (typeof params.cacheDirectory === "string") {
|
201
|
+
directory = params.cacheDirectory;
|
202
|
+
} else {
|
203
|
+
if (defaultCacheDirectory === null) {
|
204
|
+
defaultCacheDirectory = findCacheDir({
|
205
|
+
name: "babel-loader"
|
206
|
+
}) || os.tmpdir();
|
207
|
+
}
|
208
|
+
|
209
|
+
directory = defaultCacheDirectory;
|
210
|
+
}
|
211
|
+
|
212
|
+
return yield handleCache(directory, params);
|
213
|
+
});
|
214
|
+
|
215
|
+
return function (_x6) {
|
216
|
+
return _ref4.apply(this, arguments);
|
217
|
+
};
|
218
|
+
}();
|
package/lib/index.js
CHANGED
@@ -1,183 +1,149 @@
|
|
1
1
|
"use strict";
|
2
2
|
|
3
|
-
var
|
4
|
-
var loaderUtils = require("loader-utils");
|
5
|
-
var path = require("path");
|
6
|
-
var cache = require("./fs-cache.js");
|
7
|
-
var exists = require("./utils/exists");
|
8
|
-
var relative = require("./utils/relative");
|
9
|
-
var read = require("./utils/read");
|
10
|
-
var resolveRc = require("./resolve-rc.js");
|
11
|
-
var pkg = require("../package.json");
|
12
|
-
var fs = require("fs");
|
13
|
-
|
14
|
-
/**
|
15
|
-
* Error thrown by Babel formatted to conform to Webpack reporting.
|
16
|
-
*/
|
17
|
-
function BabelLoaderError(name, message, codeFrame, hideStack, error) {
|
18
|
-
Error.call(this);
|
19
|
-
|
20
|
-
this.name = "BabelLoaderError";
|
21
|
-
this.message = formatMessage(name, message, codeFrame);
|
22
|
-
this.hideStack = hideStack;
|
23
|
-
this.error = error;
|
24
|
-
|
25
|
-
Error.captureStackTrace(this, BabelLoaderError);
|
26
|
-
}
|
27
|
-
|
28
|
-
BabelLoaderError.prototype = Object.create(Error.prototype);
|
29
|
-
BabelLoaderError.prototype.constructor = BabelLoaderError;
|
3
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _next(value) { step("next", value); } function _throw(err) { step("throw", err); } _next(); }); }; }
|
30
4
|
|
31
|
-
|
5
|
+
const babel = require("@babel/core");
|
32
6
|
|
33
|
-
|
34
|
-
return (name ? name + ": " : "") + message + "\n\n" + codeFrame + "\n";
|
35
|
-
};
|
7
|
+
const pkg = require("../package.json");
|
36
8
|
|
37
|
-
|
38
|
-
var forceEnv = options.forceEnv;
|
39
|
-
var tmpEnv = void 0;
|
9
|
+
const cache = require("./cache");
|
40
10
|
|
41
|
-
|
11
|
+
const transform = require("./transform");
|
42
12
|
|
43
|
-
|
44
|
-
tmpEnv = process.env.BABEL_ENV;
|
45
|
-
process.env.BABEL_ENV = forceEnv;
|
46
|
-
}
|
13
|
+
const relative = require("./utils/relative");
|
47
14
|
|
48
|
-
|
49
|
-
try {
|
50
|
-
result = babel.transform(source, options);
|
51
|
-
} catch (error) {
|
52
|
-
if (forceEnv) restoreBabelEnv(tmpEnv);
|
53
|
-
if (error.message && error.codeFrame) {
|
54
|
-
var message = error.message;
|
55
|
-
var name = void 0;
|
56
|
-
var hideStack = void 0;
|
57
|
-
if (error instanceof SyntaxError) {
|
58
|
-
message = message.replace(STRIP_FILENAME_RE, "");
|
59
|
-
name = "SyntaxError";
|
60
|
-
hideStack = true;
|
61
|
-
} else if (error instanceof TypeError) {
|
62
|
-
message = message.replace(STRIP_FILENAME_RE, "");
|
63
|
-
hideStack = true;
|
64
|
-
}
|
65
|
-
throw new BabelLoaderError(name, message, error.codeFrame, hideStack, error);
|
66
|
-
} else {
|
67
|
-
throw error;
|
68
|
-
}
|
69
|
-
}
|
70
|
-
var code = result.code;
|
71
|
-
var map = result.map;
|
72
|
-
var metadata = result.metadata;
|
15
|
+
const loaderUtils = require("loader-utils");
|
73
16
|
|
74
|
-
|
75
|
-
|
17
|
+
function subscribe(subscriber, metadata, context) {
|
18
|
+
if (context[subscriber]) {
|
19
|
+
context[subscriber](metadata);
|
76
20
|
}
|
21
|
+
}
|
77
22
|
|
78
|
-
|
23
|
+
module.exports = makeLoader();
|
24
|
+
module.exports.custom = makeLoader;
|
79
25
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
26
|
+
function makeLoader(callback) {
|
27
|
+
const overrides = callback ? callback(babel) : undefined;
|
28
|
+
return function (source, inputSourceMap) {
|
29
|
+
// Make the loader async
|
30
|
+
const callback = this.async();
|
31
|
+
loader.call(this, source, inputSourceMap, overrides).then(args => callback(null, ...args), err => callback(err));
|
84
32
|
};
|
85
|
-
};
|
86
|
-
|
87
|
-
function restoreBabelEnv(prevValue) {
|
88
|
-
if (prevValue === undefined) {
|
89
|
-
delete process.env.BABEL_ENV;
|
90
|
-
} else {
|
91
|
-
process.env.BABEL_ENV = prevValue;
|
92
|
-
}
|
93
33
|
}
|
94
34
|
|
95
|
-
function
|
96
|
-
|
97
|
-
context[s](metadata);
|
98
|
-
}
|
35
|
+
function loader(_x, _x2, _x3) {
|
36
|
+
return _loader.apply(this, arguments);
|
99
37
|
}
|
100
38
|
|
101
|
-
|
102
|
-
|
39
|
+
function _loader() {
|
40
|
+
_loader = _asyncToGenerator(function* (source, inputSourceMap, overrides) {
|
41
|
+
const filename = this.resourcePath;
|
42
|
+
let loaderOptions = loaderUtils.getOptions(this) || {};
|
43
|
+
let customOptions;
|
103
44
|
|
104
|
-
|
45
|
+
if (overrides && overrides.customOptions) {
|
46
|
+
const result = yield overrides.customOptions.call(this, loaderOptions);
|
47
|
+
customOptions = result.custom;
|
48
|
+
loaderOptions = result.loader;
|
49
|
+
} // Deprecation handling
|
105
50
|
|
106
|
-
// Handle options
|
107
|
-
var loaderOptions = loaderUtils.getOptions(this) || {};
|
108
|
-
var fileSystem = this.fs ? this.fs : fs;
|
109
|
-
var babelrcPath = null;
|
110
|
-
if (loaderOptions.babelrc !== false) {
|
111
|
-
babelrcPath = typeof loaderOptions.babelrc === "string" && exists(fileSystem, loaderOptions.babelrc) ? loaderOptions.babelrc : resolveRc(fileSystem, path.dirname(filename));
|
112
|
-
}
|
113
51
|
|
114
|
-
|
115
|
-
|
116
|
-
|
52
|
+
if ("forceEnv" in loaderOptions) {
|
53
|
+
console.warn("The option `forceEnv` has been removed in favor of `envName` in Babel 7.");
|
54
|
+
}
|
117
55
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
}
|
129
|
-
|
56
|
+
if (typeof loaderOptions.babelrc === "string") {
|
57
|
+
console.warn("The option `babelrc` should not be set to a string anymore in the babel-loader config. " + "Please update your configuration and set `babelrc` to true or false.\n" + "If you want to specify a specific babel config file to inherit config from " + "please use the `extends` option.\nFor more information about this options see " + "https://babeljs.io/docs/core-packages/#options");
|
58
|
+
} // Set babel-loader's default options.
|
59
|
+
|
60
|
+
|
61
|
+
const {
|
62
|
+
sourceRoot = process.cwd(),
|
63
|
+
sourceMap = this.sourceMap,
|
64
|
+
sourceFileName = relative(sourceRoot, filename)
|
65
|
+
} = loaderOptions;
|
66
|
+
const programmaticOptions = Object.assign({}, loaderOptions, {
|
67
|
+
filename,
|
68
|
+
inputSourceMap: inputSourceMap || undefined,
|
69
|
+
sourceRoot,
|
70
|
+
sourceMap,
|
71
|
+
sourceFileName
|
72
|
+
}); // Remove loader related options
|
73
|
+
|
74
|
+
delete programmaticOptions.cacheDirectory;
|
75
|
+
delete programmaticOptions.cacheIdentifier;
|
76
|
+
delete programmaticOptions.metadataSubscribers;
|
77
|
+
|
78
|
+
if (!babel.loadPartialConfig) {
|
79
|
+
throw new Error(`babel-loader ^8.0.0-beta.3 requires @babel/core@7.0.0-beta.41, but ` + `you appear to be using "${babel.version}". Either update your ` + `@babel/core version, or pin you babel-loader version to 8.0.0-beta.2`);
|
80
|
+
}
|
130
81
|
|
131
|
-
|
82
|
+
const config = babel.loadPartialConfig(programmaticOptions);
|
132
83
|
|
133
|
-
|
134
|
-
|
135
|
-
}
|
84
|
+
if (config) {
|
85
|
+
let options = config.options;
|
136
86
|
|
137
|
-
|
138
|
-
|
139
|
-
|
87
|
+
if (overrides && overrides.config) {
|
88
|
+
options = yield overrides.config.call(this, config, {
|
89
|
+
source,
|
90
|
+
customOptions
|
91
|
+
});
|
92
|
+
}
|
140
93
|
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
return callback(null, code, map);
|
170
|
-
});
|
171
|
-
}
|
94
|
+
const {
|
95
|
+
cacheDirectory = null,
|
96
|
+
cacheIdentifier = JSON.stringify({
|
97
|
+
options,
|
98
|
+
"@babel/core": transform.version,
|
99
|
+
"@babel/loader": pkg.version
|
100
|
+
}),
|
101
|
+
metadataSubscribers = []
|
102
|
+
} = loaderOptions;
|
103
|
+
let result;
|
104
|
+
|
105
|
+
if (cacheDirectory) {
|
106
|
+
result = yield cache({
|
107
|
+
source,
|
108
|
+
options,
|
109
|
+
transform,
|
110
|
+
cacheDirectory,
|
111
|
+
cacheIdentifier
|
112
|
+
});
|
113
|
+
} else {
|
114
|
+
result = yield transform(source, options);
|
115
|
+
} // TODO: Babel should really provide the full list of config files that
|
116
|
+
// were used so that this can also handle files loaded with 'extends'.
|
117
|
+
|
118
|
+
|
119
|
+
if (typeof config.babelrc === "string") {
|
120
|
+
this.addDependency(config.babelrc);
|
121
|
+
}
|
172
122
|
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
123
|
+
if (result) {
|
124
|
+
if (overrides && overrides.result) {
|
125
|
+
result = yield overrides.result.call(this, result, {
|
126
|
+
source,
|
127
|
+
customOptions,
|
128
|
+
config,
|
129
|
+
options
|
130
|
+
});
|
131
|
+
}
|
132
|
+
|
133
|
+
const {
|
134
|
+
code,
|
135
|
+
map,
|
136
|
+
metadata
|
137
|
+
} = result;
|
138
|
+
metadataSubscribers.forEach(subscriber => {
|
139
|
+
subscribe(subscriber, metadata, this);
|
140
|
+
});
|
141
|
+
return [code, map];
|
142
|
+
}
|
143
|
+
} // If the file was ignored, pass through the original content.
|
177
144
|
|
178
|
-
metadataSubscribers.forEach(function (s) {
|
179
|
-
return passMetadata(s, _this, metadata);
|
180
|
-
});
|
181
145
|
|
182
|
-
|
183
|
-
};
|
146
|
+
return [source, inputSourceMap];
|
147
|
+
});
|
148
|
+
return _loader.apply(this, arguments);
|
149
|
+
}
|
package/lib/transform.js
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _next(value) { step("next", value); } function _throw(err) { step("throw", err); } _next(); }); }; }
|
4
|
+
|
5
|
+
const babel = require("@babel/core");
|
6
|
+
|
7
|
+
const promisify = require("util.promisify");
|
8
|
+
|
9
|
+
const LoaderError = require("./Error");
|
10
|
+
|
11
|
+
const transform = promisify(babel.transform);
|
12
|
+
|
13
|
+
module.exports =
|
14
|
+
/*#__PURE__*/
|
15
|
+
function () {
|
16
|
+
var _ref = _asyncToGenerator(function* (source, options) {
|
17
|
+
let result;
|
18
|
+
|
19
|
+
try {
|
20
|
+
result = yield transform(source, options);
|
21
|
+
} catch (err) {
|
22
|
+
throw err.message && err.codeFrame ? new LoaderError(err) : err;
|
23
|
+
}
|
24
|
+
|
25
|
+
if (!result) return null;
|
26
|
+
const {
|
27
|
+
code,
|
28
|
+
map,
|
29
|
+
metadata
|
30
|
+
} = result;
|
31
|
+
|
32
|
+
if (map && (!map.sourcesContent || !map.sourcesContent.length)) {
|
33
|
+
map.sourcesContent = [source];
|
34
|
+
}
|
35
|
+
|
36
|
+
return {
|
37
|
+
code,
|
38
|
+
map,
|
39
|
+
metadata
|
40
|
+
};
|
41
|
+
});
|
42
|
+
|
43
|
+
return function (_x, _x2) {
|
44
|
+
return _ref.apply(this, arguments);
|
45
|
+
};
|
46
|
+
}();
|
47
|
+
|
48
|
+
module.exports.version = babel.version;
|
package/lib/utils/relative.js
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
"use strict";
|
2
2
|
|
3
|
-
|
3
|
+
const path = require("path");
|
4
4
|
|
5
|
-
module.exports = function relative(
|
6
|
-
|
7
|
-
|
5
|
+
module.exports = function relative(root, file) {
|
6
|
+
const rootPath = root.replace(/\\/g, "/").split("/")[1];
|
7
|
+
const filePath = file.replace(/\\/g, "/").split("/")[1]; // If the file is in a completely different root folder
|
8
|
+
// use the absolute path of the file
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
return filename;
|
10
|
+
if (rootPath && rootPath !== filePath) {
|
11
|
+
return file;
|
12
12
|
}
|
13
13
|
|
14
|
-
return path.relative(
|
14
|
+
return path.relative(root, file);
|
15
15
|
};
|
package/package.json
CHANGED
@@ -1,39 +1,39 @@
|
|
1
1
|
{
|
2
2
|
"name": "babel-loader",
|
3
|
-
"version": "
|
3
|
+
"version": "8.0.0-beta.3",
|
4
4
|
"description": "babel module loader for webpack",
|
5
5
|
"files": [
|
6
6
|
"lib"
|
7
7
|
],
|
8
8
|
"main": "lib/index.js",
|
9
9
|
"engines": {
|
10
|
-
"node": ">=
|
10
|
+
"node": ">= 6.9"
|
11
11
|
},
|
12
12
|
"dependencies": {
|
13
13
|
"find-cache-dir": "^1.0.0",
|
14
14
|
"loader-utils": "^1.0.2",
|
15
|
-
"mkdirp": "^0.5.1"
|
15
|
+
"mkdirp": "^0.5.1",
|
16
|
+
"util.promisify": "^1.0.0"
|
16
17
|
},
|
17
18
|
"peerDependencies": {
|
18
|
-
"babel
|
19
|
-
"webpack": "2
|
19
|
+
"@babel/core": "^7.0.0 || ^7.0.0-rc || ^7.0.0-beta.41",
|
20
|
+
"webpack": ">=2"
|
20
21
|
},
|
21
22
|
"devDependencies": {
|
22
|
-
"
|
23
|
-
"babel
|
24
|
-
"babel-
|
23
|
+
"@babel/cli": "^7.0.0-beta.41",
|
24
|
+
"@babel/core": "^7.0.0-beta.41",
|
25
|
+
"@babel/preset-env": "^7.0.0-beta.41",
|
26
|
+
"ava": "0.25.0",
|
25
27
|
"babel-eslint": "^8.0.0",
|
26
28
|
"babel-plugin-istanbul": "^4.0.0",
|
27
29
|
"babel-plugin-react-intl": "^2.1.3",
|
28
|
-
"babel-preset-env": "^1.2.0",
|
29
|
-
"babel-register": "^6.18.0",
|
30
30
|
"cross-env": "^5.0.0",
|
31
31
|
"eslint": "^4.1.0",
|
32
32
|
"eslint-config-babel": "^7.0.0",
|
33
33
|
"eslint-plugin-flowtype": "^2.25.0",
|
34
34
|
"eslint-plugin-prettier": "^2.1.2",
|
35
35
|
"husky": "^0.14.0",
|
36
|
-
"lint-staged": "^
|
36
|
+
"lint-staged": "^7.1.0",
|
37
37
|
"nyc": "^11.0.1",
|
38
38
|
"prettier": "^1.2.2",
|
39
39
|
"react": "^16.0.0",
|
@@ -45,7 +45,7 @@
|
|
45
45
|
"scripts": {
|
46
46
|
"clean": "rimraf lib/",
|
47
47
|
"build": "babel src/ --out-dir lib/",
|
48
|
-
"format": "prettier --write --trailing-comma all
|
48
|
+
"format": "prettier --write --trailing-comma all 'src/**/*.js' 'test/**/*.test.js' 'test/helpers/*.js' && prettier --write --trailing-comma es5 'scripts/*.js'",
|
49
49
|
"lint": "eslint src test",
|
50
50
|
"precommit": "lint-staged",
|
51
51
|
"prepublish": "yarn run clean && yarn run build",
|
@@ -80,9 +80,6 @@
|
|
80
80
|
"text",
|
81
81
|
"json"
|
82
82
|
],
|
83
|
-
"require": [
|
84
|
-
"babel-register"
|
85
|
-
],
|
86
83
|
"sourceMap": false,
|
87
84
|
"instrument": false
|
88
85
|
},
|
@@ -94,8 +91,7 @@
|
|
94
91
|
],
|
95
92
|
"source": [
|
96
93
|
"src/**/*.js"
|
97
|
-
]
|
98
|
-
"babel": "inherit"
|
94
|
+
]
|
99
95
|
},
|
100
96
|
"lint-staged": {
|
101
97
|
"scripts/*.js": [
|
package/lib/fs-cache.js
DELETED
@@ -1,180 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
|
3
|
-
/**
|
4
|
-
* Filesystem cache
|
5
|
-
*
|
6
|
-
* Given a file and a transform function, cache the result into files
|
7
|
-
* or retrieve the previously cached files if the given file is already known.
|
8
|
-
*
|
9
|
-
* @see https://github.com/babel/babel-loader/issues/34
|
10
|
-
* @see https://github.com/babel/babel-loader/pull/41
|
11
|
-
*/
|
12
|
-
var crypto = require("crypto");
|
13
|
-
var mkdirp = require("mkdirp");
|
14
|
-
var findCacheDir = require("find-cache-dir");
|
15
|
-
var fs = require("fs");
|
16
|
-
var os = require("os");
|
17
|
-
var path = require("path");
|
18
|
-
var zlib = require("zlib");
|
19
|
-
|
20
|
-
var defaultCacheDirectory = null; // Lazily instantiated when needed
|
21
|
-
|
22
|
-
/**
|
23
|
-
* Read the contents from the compressed file.
|
24
|
-
*
|
25
|
-
* @async
|
26
|
-
* @params {String} filename
|
27
|
-
* @params {Function} callback
|
28
|
-
*/
|
29
|
-
var read = function read(filename, callback) {
|
30
|
-
return fs.readFile(filename, function (err, data) {
|
31
|
-
if (err) return callback(err);
|
32
|
-
|
33
|
-
return zlib.gunzip(data, function (err, content) {
|
34
|
-
if (err) return callback(err);
|
35
|
-
|
36
|
-
var result = {};
|
37
|
-
|
38
|
-
try {
|
39
|
-
result = JSON.parse(content);
|
40
|
-
} catch (e) {
|
41
|
-
return callback(e);
|
42
|
-
}
|
43
|
-
|
44
|
-
return callback(null, result);
|
45
|
-
});
|
46
|
-
});
|
47
|
-
};
|
48
|
-
|
49
|
-
/**
|
50
|
-
* Write contents into a compressed file.
|
51
|
-
*
|
52
|
-
* @async
|
53
|
-
* @params {String} filename
|
54
|
-
* @params {String} result
|
55
|
-
* @params {Function} callback
|
56
|
-
*/
|
57
|
-
var write = function write(filename, result, callback) {
|
58
|
-
var content = JSON.stringify(result);
|
59
|
-
|
60
|
-
return zlib.gzip(content, function (err, data) {
|
61
|
-
if (err) return callback(err);
|
62
|
-
|
63
|
-
return fs.writeFile(filename, data, callback);
|
64
|
-
});
|
65
|
-
};
|
66
|
-
|
67
|
-
/**
|
68
|
-
* Build the filename for the cached file
|
69
|
-
*
|
70
|
-
* @params {String} source File source code
|
71
|
-
* @params {Object} options Options used
|
72
|
-
*
|
73
|
-
* @return {String}
|
74
|
-
*/
|
75
|
-
var filename = function filename(source, identifier, options) {
|
76
|
-
var hash = crypto.createHash("md4");
|
77
|
-
var contents = JSON.stringify({
|
78
|
-
source: source,
|
79
|
-
options: options,
|
80
|
-
identifier: identifier
|
81
|
-
});
|
82
|
-
|
83
|
-
hash.update(contents);
|
84
|
-
|
85
|
-
return hash.digest("hex") + ".json.gz";
|
86
|
-
};
|
87
|
-
|
88
|
-
/**
|
89
|
-
* Handle the cache
|
90
|
-
*
|
91
|
-
* @params {String} directory
|
92
|
-
* @params {Object} params
|
93
|
-
* @params {Function} callback
|
94
|
-
*/
|
95
|
-
var handleCache = function handleCache(directory, params, callback) {
|
96
|
-
var source = params.source;
|
97
|
-
var options = params.options || {};
|
98
|
-
var transform = params.transform;
|
99
|
-
var identifier = params.identifier;
|
100
|
-
var shouldFallback = typeof params.directory !== "string" && directory !== os.tmpdir();
|
101
|
-
|
102
|
-
// Make sure the directory exists.
|
103
|
-
mkdirp(directory, function (err) {
|
104
|
-
// Fallback to tmpdir if node_modules folder not writable
|
105
|
-
if (err) return shouldFallback ? handleCache(os.tmpdir(), params, callback) : callback(err);
|
106
|
-
|
107
|
-
var file = path.join(directory, filename(source, identifier, options));
|
108
|
-
|
109
|
-
return read(file, function (err, content) {
|
110
|
-
var result = {};
|
111
|
-
// No errors mean that the file was previously cached
|
112
|
-
// we just need to return it
|
113
|
-
if (!err) return callback(null, content);
|
114
|
-
|
115
|
-
// Otherwise just transform the file
|
116
|
-
// return it to the user asap and write it in cache
|
117
|
-
try {
|
118
|
-
result = transform(source, options);
|
119
|
-
} catch (error) {
|
120
|
-
return callback(error);
|
121
|
-
}
|
122
|
-
|
123
|
-
return write(file, result, function (err) {
|
124
|
-
// Fallback to tmpdir if node_modules folder not writable
|
125
|
-
if (err) return shouldFallback ? handleCache(os.tmpdir(), params, callback) : callback(err);
|
126
|
-
|
127
|
-
callback(null, result);
|
128
|
-
});
|
129
|
-
});
|
130
|
-
});
|
131
|
-
};
|
132
|
-
|
133
|
-
/**
|
134
|
-
* Retrieve file from cache, or create a new one for future reads
|
135
|
-
*
|
136
|
-
* @async
|
137
|
-
* @param {Object} params
|
138
|
-
* @param {String} params.directory Directory to store cached files
|
139
|
-
* @param {String} params.identifier Unique identifier to bust cache
|
140
|
-
* @param {String} params.source Original contents of the file to be cached
|
141
|
-
* @param {Object} params.options Options to be given to the transform fn
|
142
|
-
* @param {Function} params.transform Function that will transform the
|
143
|
-
* original file and whose result will be
|
144
|
-
* cached
|
145
|
-
*
|
146
|
-
* @param {Function<err, result>} callback
|
147
|
-
*
|
148
|
-
* @example
|
149
|
-
*
|
150
|
-
* cache({
|
151
|
-
* directory: '.tmp/cache',
|
152
|
-
* identifier: 'babel-loader-cachefile',
|
153
|
-
* source: *source code from file*,
|
154
|
-
* options: {
|
155
|
-
* experimental: true,
|
156
|
-
* runtime: true
|
157
|
-
* },
|
158
|
-
* transform: function(source, options) {
|
159
|
-
* var content = *do what you need with the source*
|
160
|
-
* return content;
|
161
|
-
* }
|
162
|
-
* }, function(err, result) {
|
163
|
-
*
|
164
|
-
* });
|
165
|
-
*/
|
166
|
-
|
167
|
-
module.exports = function (params, callback) {
|
168
|
-
var directory = void 0;
|
169
|
-
|
170
|
-
if (typeof params.directory === "string") {
|
171
|
-
directory = params.directory;
|
172
|
-
} else {
|
173
|
-
if (defaultCacheDirectory === null) {
|
174
|
-
defaultCacheDirectory = findCacheDir({ name: "babel-loader" }) || os.tmpdir();
|
175
|
-
}
|
176
|
-
directory = defaultCacheDirectory;
|
177
|
-
}
|
178
|
-
|
179
|
-
handleCache(directory, params, callback);
|
180
|
-
};
|
package/lib/resolve-rc.js
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
|
3
|
-
var path = require("path");
|
4
|
-
var exists = require("./utils/exists");
|
5
|
-
|
6
|
-
module.exports = function find(fileSystem, start) {
|
7
|
-
var _arr = [".babelrc", "package.json"];
|
8
|
-
|
9
|
-
for (var _i = 0; _i < _arr.length; _i++) {
|
10
|
-
var fileName = _arr[_i];
|
11
|
-
var file = path.join(start, fileName);
|
12
|
-
|
13
|
-
if (exists(fileSystem, file)) {
|
14
|
-
if (fileName !== "package.json" || typeof require(file).babel === "object") {
|
15
|
-
return file;
|
16
|
-
}
|
17
|
-
}
|
18
|
-
}
|
19
|
-
|
20
|
-
var up = path.dirname(start);
|
21
|
-
|
22
|
-
// Reached root
|
23
|
-
if (up !== start) {
|
24
|
-
return find(fileSystem, up);
|
25
|
-
}
|
26
|
-
};
|
package/lib/utils/exists.js
DELETED
package/lib/utils/read.js
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
|
3
|
-
var path = require("path");
|
4
|
-
|
5
|
-
module.exports = function readBabelConfig(fileSystem, filename) {
|
6
|
-
if (path.basename(filename) === "package.json") {
|
7
|
-
var pkg = require(filename);
|
8
|
-
|
9
|
-
return JSON.stringify(pkg.babel);
|
10
|
-
}
|
11
|
-
|
12
|
-
// Webpack `fs` return Buffer
|
13
|
-
return fileSystem.readFileSync(filename).toString("utf8");
|
14
|
-
};
|