babel-loader 8.0.0-beta.2 → 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 +78 -2
- package/lib/Error.js +17 -38
- package/lib/cache.js +114 -80
- package/lib/index.js +130 -99
- package/lib/transform.js +33 -12
- package/lib/utils/relative.js +5 -3
- package/package.json +9 -8
- package/lib/config.js +0 -24
- package/lib/utils/exists.js +0 -11
- package/lib/utils/read.js +0 -12
package/README.md
CHANGED
@@ -25,7 +25,7 @@ __Notes:__ Issues with the output should be reported on the babel [issue tracker
|
|
25
25
|
> webpack 3.x | babel-loader 8.x | babel 7.x
|
26
26
|
|
27
27
|
```bash
|
28
|
-
npm install babel-loader
|
28
|
+
npm install "babel-loader@^8.0.0-beta" @babel/core @babel/preset-env webpack
|
29
29
|
```
|
30
30
|
|
31
31
|
> webpack 3.x babel-loader 7.x | babel 6.x
|
@@ -131,7 +131,7 @@ rules: [
|
|
131
131
|
loader: 'babel-loader',
|
132
132
|
options: {
|
133
133
|
presets: ['@babel/preset-env'],
|
134
|
-
plugins: ['@babel/transform-runtime']
|
134
|
+
plugins: ['@babel/plugin-transform-runtime']
|
135
135
|
}
|
136
136
|
}
|
137
137
|
}
|
@@ -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
CHANGED
@@ -1,20 +1,8 @@
|
|
1
|
-
|
1
|
+
"use strict";
|
2
2
|
|
3
|
-
|
3
|
+
const STRIP_FILENAME_RE = /^[^:]+: /;
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
var _sPO = Object.setPrototypeOf || function _sPO(o, p) { o.__proto__ = p; return o; };
|
8
|
-
|
9
|
-
var _construct = typeof Reflect === "object" && Reflect.construct || function _construct(Parent, args, Class) { var Constructor, a = [null]; a.push.apply(a, args); Constructor = Parent.bind.apply(Parent, a); return _sPO(new Constructor(), Class.prototype); };
|
10
|
-
|
11
|
-
var _cache = typeof Map === "function" && new Map();
|
12
|
-
|
13
|
-
function _wrapNativeSuper(Class) { if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() {} Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writeable: true, configurable: true } }); return _sPO(Wrapper, _sPO(function Super() { return _construct(Class, arguments, _gPO(this).constructor); }, Class)); }
|
14
|
-
|
15
|
-
var STRIP_FILENAME_RE = /^[^:]+: /;
|
16
|
-
|
17
|
-
var format = function format(err) {
|
5
|
+
const format = err => {
|
18
6
|
if (err instanceof SyntaxError) {
|
19
7
|
err.name = "SyntaxError";
|
20
8
|
err.message = err.message.replace(STRIP_FILENAME_RE, "");
|
@@ -28,30 +16,21 @@ var format = function format(err) {
|
|
28
16
|
return err;
|
29
17
|
};
|
30
18
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
codeFrame = _format.codeFrame,
|
45
|
-
hideStack = _format.hideStack;
|
46
|
-
|
47
|
-
_this.name = "BabelLoaderError";
|
48
|
-
_this.message = `${name ? `${name}: ` : ""}${message}\n\n${codeFrame}\n`;
|
49
|
-
_this.hideStack = hideStack;
|
50
|
-
Error.captureStackTrace(_assertThisInitialized(_this), _this.constructor);
|
51
|
-
return _this;
|
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);
|
52
32
|
}
|
53
33
|
|
54
|
-
|
55
|
-
}(_wrapNativeSuper(Error));
|
34
|
+
}
|
56
35
|
|
57
36
|
module.exports = LoaderError;
|
package/lib/cache.js
CHANGED
@@ -1,3 +1,7 @@
|
|
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
|
+
|
1
5
|
/**
|
2
6
|
* Filesystem Cache
|
3
7
|
*
|
@@ -7,66 +11,73 @@
|
|
7
11
|
* @see https://github.com/babel/babel-loader/issues/34
|
8
12
|
* @see https://github.com/babel/babel-loader/pull/41
|
9
13
|
*/
|
10
|
-
|
14
|
+
const fs = require("fs");
|
15
|
+
|
16
|
+
const os = require("os");
|
11
17
|
|
12
|
-
|
18
|
+
const path = require("path");
|
13
19
|
|
14
|
-
|
20
|
+
const zlib = require("zlib");
|
15
21
|
|
16
|
-
|
22
|
+
const crypto = require("crypto");
|
17
23
|
|
18
|
-
|
24
|
+
const mkdirpOrig = require("mkdirp");
|
19
25
|
|
20
|
-
|
26
|
+
const findCacheDir = require("find-cache-dir");
|
21
27
|
|
22
|
-
|
28
|
+
const promisify = require("util.promisify");
|
23
29
|
|
24
|
-
|
30
|
+
const transform = require("./transform"); // Lazily instantiated when needed
|
25
31
|
|
26
32
|
|
27
|
-
|
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);
|
28
39
|
/**
|
29
40
|
* Read the contents from the compressed file.
|
30
41
|
*
|
31
42
|
* @async
|
32
43
|
* @params {String} filename
|
33
|
-
* @params {Function} callback
|
34
44
|
*/
|
35
45
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
try {
|
44
|
-
result = JSON.parse(content);
|
45
|
-
} catch (e) {
|
46
|
-
return callback(e);
|
47
|
-
}
|
48
|
-
|
49
|
-
return callback(null, result);
|
50
|
-
});
|
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);
|
51
53
|
});
|
52
|
-
|
54
|
+
|
55
|
+
return function read(_x) {
|
56
|
+
return _ref.apply(this, arguments);
|
57
|
+
};
|
58
|
+
}();
|
53
59
|
/**
|
54
60
|
* Write contents into a compressed file.
|
55
61
|
*
|
56
62
|
* @async
|
57
63
|
* @params {String} filename
|
58
64
|
* @params {String} result
|
59
|
-
* @params {Function} callback
|
60
65
|
*/
|
61
66
|
|
62
67
|
|
63
|
-
|
64
|
-
|
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);
|
68
75
|
});
|
69
|
-
|
76
|
+
|
77
|
+
return function write(_x2, _x3) {
|
78
|
+
return _ref2.apply(this, arguments);
|
79
|
+
};
|
80
|
+
}();
|
70
81
|
/**
|
71
82
|
* Build the filename for the cached file
|
72
83
|
*
|
@@ -77,9 +88,9 @@ var write = function write(filename, result, callback) {
|
|
77
88
|
*/
|
78
89
|
|
79
90
|
|
80
|
-
|
81
|
-
|
82
|
-
|
91
|
+
const filename = function (source, identifier, options) {
|
92
|
+
const hash = crypto.createHash("SHA1");
|
93
|
+
const contents = JSON.stringify({
|
83
94
|
source,
|
84
95
|
options,
|
85
96
|
identifier
|
@@ -92,44 +103,61 @@ var filename = function filename(source, identifier, options) {
|
|
92
103
|
*
|
93
104
|
* @params {String} directory
|
94
105
|
* @params {Object} params
|
95
|
-
* @params {Function} callback
|
96
106
|
*/
|
97
107
|
|
98
108
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
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
|
+
}
|
105
127
|
|
106
|
-
|
107
|
-
// Fallback to tmpdir if node_modules folder not writable
|
108
|
-
if (err) {
|
109
|
-
return fallback ? handleCache(os.tmpdir(), params, callback) : callback(err);
|
128
|
+
throw err;
|
110
129
|
}
|
111
130
|
|
112
|
-
|
113
|
-
|
131
|
+
const file = path.join(directory, filename(source, cacheIdentifier, options));
|
132
|
+
|
133
|
+
try {
|
114
134
|
// No errors mean that the file was previously cached
|
115
135
|
// we just need to return it
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
}
|
130
|
-
|
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;
|
131
155
|
});
|
132
|
-
|
156
|
+
|
157
|
+
return function handleCache(_x4, _x5) {
|
158
|
+
return _ref3.apply(this, arguments);
|
159
|
+
};
|
160
|
+
}();
|
133
161
|
/**
|
134
162
|
* Retrieve file from cache, or create a new one for future reads
|
135
163
|
*
|
@@ -143,8 +171,6 @@ var handleCache = function handleCache(directory, params, callback) {
|
|
143
171
|
* original file and whose result will be
|
144
172
|
* cached
|
145
173
|
*
|
146
|
-
* @param {Function<err, result>} callback
|
147
|
-
*
|
148
174
|
* @example
|
149
175
|
*
|
150
176
|
* cache({
|
@@ -165,20 +191,28 @@ var handleCache = function handleCache(directory, params, callback) {
|
|
165
191
|
*/
|
166
192
|
|
167
193
|
|
168
|
-
module.exports =
|
169
|
-
|
194
|
+
module.exports =
|
195
|
+
/*#__PURE__*/
|
196
|
+
function () {
|
197
|
+
var _ref4 = _asyncToGenerator(function* (params) {
|
198
|
+
let directory;
|
170
199
|
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
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;
|
178
210
|
}
|
179
211
|
|
180
|
-
directory
|
181
|
-
}
|
212
|
+
return yield handleCache(directory, params);
|
213
|
+
});
|
182
214
|
|
183
|
-
|
184
|
-
|
215
|
+
return function (_x6) {
|
216
|
+
return _ref4.apply(this, arguments);
|
217
|
+
};
|
218
|
+
}();
|
package/lib/index.js
CHANGED
@@ -1,22 +1,18 @@
|
|
1
|
-
|
1
|
+
"use strict";
|
2
2
|
|
3
|
-
var
|
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
4
|
|
5
|
-
|
5
|
+
const babel = require("@babel/core");
|
6
6
|
|
7
|
-
|
7
|
+
const pkg = require("../package.json");
|
8
8
|
|
9
|
-
|
9
|
+
const cache = require("./cache");
|
10
10
|
|
11
|
-
|
11
|
+
const transform = require("./transform");
|
12
12
|
|
13
|
-
|
13
|
+
const relative = require("./utils/relative");
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
var relative = require("./utils/relative.js");
|
18
|
-
|
19
|
-
var loaderUtils = require("loader-utils");
|
15
|
+
const loaderUtils = require("loader-utils");
|
20
16
|
|
21
17
|
function subscribe(subscriber, metadata, context) {
|
22
18
|
if (context[subscriber]) {
|
@@ -24,95 +20,130 @@ function subscribe(subscriber, metadata, context) {
|
|
24
20
|
}
|
25
21
|
}
|
26
22
|
|
27
|
-
module.exports =
|
28
|
-
|
29
|
-
|
30
|
-
var filename = this.resourcePath;
|
31
|
-
var options = loaderUtils.getOptions(this) || {}; // Use memoryFS (webpack) if available
|
32
|
-
|
33
|
-
fs = this.fs ? this.fs : fs;
|
34
|
-
var babelrc = null; // Deprecation handling
|
35
|
-
|
36
|
-
if ("forceEnv" in options) {
|
37
|
-
console.warn("The option `forceEnv` has been removed in favor of `envName` in Babel 7.");
|
38
|
-
}
|
39
|
-
|
40
|
-
if (typeof options.babelrc === "string") {
|
41
|
-
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");
|
42
|
-
}
|
43
|
-
|
44
|
-
if (options.babelrc !== false) {
|
45
|
-
babelrc = options.extends && exists(fs, options.extends) ? options.extends : rc(fs, path.dirname(filename));
|
46
|
-
}
|
23
|
+
module.exports = makeLoader();
|
24
|
+
module.exports.custom = makeLoader;
|
47
25
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
inputSourceMap: inputSourceMap || undefined,
|
55
|
-
sourceRoot: process.cwd(),
|
56
|
-
cacheIdentifier: JSON.stringify({
|
57
|
-
env: options.envName || process.env.BABEL_ENV || process.env.NODE_ENV || "development",
|
58
|
-
options,
|
59
|
-
babelrc: babelrc ? read(fs, babelrc) : null,
|
60
|
-
"@babel/core": transform.version,
|
61
|
-
"@babel/loader": pkg.version
|
62
|
-
}),
|
63
|
-
metadataSubscribers: []
|
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));
|
64
32
|
};
|
65
|
-
|
66
|
-
|
67
|
-
if (options.sourceMap === undefined) {
|
68
|
-
options.sourceMap = this.sourceMap;
|
69
|
-
}
|
70
|
-
|
71
|
-
if (options.sourceFileName === undefined) {
|
72
|
-
options.sourceFileName = relative(options.sourceRoot, options.filename);
|
73
|
-
}
|
33
|
+
}
|
74
34
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
delete options.cacheDirectory;
|
80
|
-
delete options.cacheIdentifier;
|
81
|
-
delete options.metadataSubscribers; // Make the loader async
|
82
|
-
|
83
|
-
var callback = this.async();
|
84
|
-
|
85
|
-
if (cacheDirectory) {
|
86
|
-
return cache({
|
87
|
-
source,
|
88
|
-
options,
|
89
|
-
transform,
|
90
|
-
cacheDirectory,
|
91
|
-
cacheIdentifier
|
92
|
-
}, function (err, _temp) {
|
93
|
-
var _ref = _temp === void 0 ? {} : _temp,
|
94
|
-
code = _ref.code,
|
95
|
-
map = _ref.map,
|
96
|
-
metadata = _ref.metadata;
|
97
|
-
|
98
|
-
if (err) return callback(err);
|
99
|
-
metadataSubscribers.forEach(function (subscriber) {
|
100
|
-
subscribe(subscriber, metadata, _this);
|
101
|
-
});
|
102
|
-
return callback(null, code, map);
|
103
|
-
});
|
104
|
-
}
|
35
|
+
function loader(_x, _x2, _x3) {
|
36
|
+
return _loader.apply(this, arguments);
|
37
|
+
}
|
105
38
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
if (
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
39
|
+
function _loader() {
|
40
|
+
_loader = _asyncToGenerator(function* (source, inputSourceMap, overrides) {
|
41
|
+
const filename = this.resourcePath;
|
42
|
+
let loaderOptions = loaderUtils.getOptions(this) || {};
|
43
|
+
let customOptions;
|
44
|
+
|
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
|
50
|
+
|
51
|
+
|
52
|
+
if ("forceEnv" in loaderOptions) {
|
53
|
+
console.warn("The option `forceEnv` has been removed in favor of `envName` in Babel 7.");
|
54
|
+
}
|
55
|
+
|
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
|
+
}
|
81
|
+
|
82
|
+
const config = babel.loadPartialConfig(programmaticOptions);
|
83
|
+
|
84
|
+
if (config) {
|
85
|
+
let options = config.options;
|
86
|
+
|
87
|
+
if (overrides && overrides.config) {
|
88
|
+
options = yield overrides.config.call(this, config, {
|
89
|
+
source,
|
90
|
+
customOptions
|
91
|
+
});
|
92
|
+
}
|
93
|
+
|
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
|
+
}
|
122
|
+
|
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.
|
144
|
+
|
145
|
+
|
146
|
+
return [source, inputSourceMap];
|
117
147
|
});
|
118
|
-
|
148
|
+
return _loader.apply(this, arguments);
|
149
|
+
}
|
package/lib/transform.js
CHANGED
@@ -1,27 +1,48 @@
|
|
1
|
-
|
1
|
+
"use strict";
|
2
2
|
|
3
|
-
var
|
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
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
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;
|
9
23
|
}
|
10
24
|
|
11
|
-
|
12
|
-
|
13
|
-
|
25
|
+
if (!result) return null;
|
26
|
+
const {
|
27
|
+
code,
|
28
|
+
map,
|
29
|
+
metadata
|
30
|
+
} = result;
|
14
31
|
|
15
32
|
if (map && (!map.sourcesContent || !map.sourcesContent.length)) {
|
16
33
|
map.sourcesContent = [source];
|
17
34
|
}
|
18
35
|
|
19
|
-
return
|
36
|
+
return {
|
20
37
|
code,
|
21
38
|
map,
|
22
39
|
metadata
|
23
|
-
}
|
40
|
+
};
|
24
41
|
});
|
25
|
-
|
42
|
+
|
43
|
+
return function (_x, _x2) {
|
44
|
+
return _ref.apply(this, arguments);
|
45
|
+
};
|
46
|
+
}();
|
26
47
|
|
27
48
|
module.exports.version = babel.version;
|
package/lib/utils/relative.js
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
-
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
const path = require("path");
|
2
4
|
|
3
5
|
module.exports = function relative(root, file) {
|
4
|
-
|
5
|
-
|
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
|
6
8
|
// use the absolute path of the file
|
7
9
|
|
8
10
|
if (rootPath && rootPath !== filePath) {
|
package/package.json
CHANGED
@@ -1,27 +1,28 @@
|
|
1
1
|
{
|
2
2
|
"name": "babel-loader",
|
3
|
-
"version": "8.0.0-beta.
|
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/core": "^7.0.0 || ^7.0.0-rc || ^7.0.0-beta.
|
19
|
+
"@babel/core": "^7.0.0 || ^7.0.0-rc || ^7.0.0-beta.41",
|
19
20
|
"webpack": ">=2"
|
20
21
|
},
|
21
22
|
"devDependencies": {
|
22
|
-
"@babel/cli": "^7.0.0-beta.
|
23
|
-
"@babel/core": "^7.0.0-beta.
|
24
|
-
"@babel/preset-env": "^7.0.0-beta.
|
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",
|
25
26
|
"ava": "0.25.0",
|
26
27
|
"babel-eslint": "^8.0.0",
|
27
28
|
"babel-plugin-istanbul": "^4.0.0",
|
@@ -32,7 +33,7 @@
|
|
32
33
|
"eslint-plugin-flowtype": "^2.25.0",
|
33
34
|
"eslint-plugin-prettier": "^2.1.2",
|
34
35
|
"husky": "^0.14.0",
|
35
|
-
"lint-staged": "^7.
|
36
|
+
"lint-staged": "^7.1.0",
|
36
37
|
"nyc": "^11.0.1",
|
37
38
|
"prettier": "^1.2.2",
|
38
39
|
"react": "^16.0.0",
|
package/lib/config.js
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
var path = require("path");
|
2
|
-
|
3
|
-
var exists = require("./utils/exists");
|
4
|
-
|
5
|
-
var configs = [".babelrc", ".babelrc.js", "package.json"];
|
6
|
-
|
7
|
-
module.exports = function find(fs, start) {
|
8
|
-
for (var _i = 0; _i < configs.length; _i++) {
|
9
|
-
var config = configs[_i];
|
10
|
-
config = path.join(start, config);
|
11
|
-
|
12
|
-
if (exists(fs, config)) {
|
13
|
-
if (path.basename(config) !== "package.json" || typeof require(config).babel === "object") {
|
14
|
-
return config;
|
15
|
-
}
|
16
|
-
}
|
17
|
-
}
|
18
|
-
|
19
|
-
var up = path.dirname(start); // Reached root
|
20
|
-
|
21
|
-
if (up !== start) {
|
22
|
-
return find(fs, up);
|
23
|
-
}
|
24
|
-
};
|
package/lib/utils/exists.js
DELETED
package/lib/utils/read.js
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
var path = require("path");
|
2
|
-
|
3
|
-
module.exports = function (fs, config) {
|
4
|
-
if (path.basename(config) === "package.json") {
|
5
|
-
var pkg = require(config);
|
6
|
-
|
7
|
-
return JSON.stringify(pkg.babel);
|
8
|
-
} // memoryFS (webpack) returns a {Buffer}
|
9
|
-
|
10
|
-
|
11
|
-
return fs.readFileSync(config).toString("utf-8");
|
12
|
-
};
|