babel-loader 8.0.0-beta.0 → 8.0.0-beta.4
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 +88 -11
- package/lib/Error.js +36 -0
- package/lib/cache.js +218 -0
- package/lib/index.js +122 -161
- package/lib/transform.js +57 -0
- package/lib/utils/relative.js +10 -7
- package/package.json +16 -12
- package/lib/fs-cache.js +0 -180
- package/lib/resolve-rc.js +0 -24
- package/lib/utils/exists.js +0 -11
- package/lib/utils/read.js +0 -12
package/README.md
CHANGED
@@ -22,16 +22,16 @@ __Notes:__ Issues with the output should be reported on the babel [issue tracker
|
|
22
22
|
|
23
23
|
<h2 align="center">Install</h2>
|
24
24
|
|
25
|
-
> webpack 3.x | babel-loader
|
25
|
+
> webpack 3.x | babel-loader 8.x | babel 7.x
|
26
26
|
|
27
27
|
```bash
|
28
|
-
|
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
|
32
32
|
|
33
33
|
```bash
|
34
|
-
npm install
|
34
|
+
npm install babel-loader babel-core babel-preset-env webpack
|
35
35
|
```
|
36
36
|
|
37
37
|
<h2 align="center">Usage</h2>
|
@@ -49,7 +49,7 @@ module: {
|
|
49
49
|
use: {
|
50
50
|
loader: 'babel-loader',
|
51
51
|
options: {
|
52
|
-
presets: ['@babel/env']
|
52
|
+
presets: ['@babel/preset-env']
|
53
53
|
}
|
54
54
|
}
|
55
55
|
}
|
@@ -73,8 +73,8 @@ module: {
|
|
73
73
|
use: {
|
74
74
|
loader: 'babel-loader',
|
75
75
|
options: {
|
76
|
-
presets: ['@babel/env'],
|
77
|
-
plugins: [require('@babel/plugin-
|
76
|
+
presets: ['@babel/preset-env'],
|
77
|
+
plugins: [require('@babel/plugin-proposal-object-rest-spread')]
|
78
78
|
}
|
79
79
|
}
|
80
80
|
}
|
@@ -88,7 +88,8 @@ This loader also supports the following loader-specific option:
|
|
88
88
|
|
89
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.
|
90
90
|
|
91
|
-
* `
|
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.
|
92
93
|
|
93
94
|
__Note:__ The `sourceMap` option is ignored, instead sourceMaps are automatically enabled when webpack is configured to use them (via the `devtool` config option).
|
94
95
|
|
@@ -117,7 +118,7 @@ requiring `babel-plugin-transform-runtime` and making all helper references use
|
|
117
118
|
|
118
119
|
See the [docs](http://babeljs.io/docs/plugins/transform-runtime/) for more information.
|
119
120
|
|
120
|
-
**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`.
|
121
122
|
|
122
123
|
```javascript
|
123
124
|
rules: [
|
@@ -129,8 +130,8 @@ rules: [
|
|
129
130
|
use: {
|
130
131
|
loader: 'babel-loader',
|
131
132
|
options: {
|
132
|
-
presets: ['@babel/env'],
|
133
|
-
plugins: ['@babel/transform-runtime']
|
133
|
+
presets: ['@babel/preset-env'],
|
134
|
+
plugins: ['@babel/plugin-transform-runtime']
|
134
135
|
}
|
135
136
|
}
|
136
137
|
}
|
@@ -204,4 +205,80 @@ In the case one of your dependencies is installing `babel` and you cannot uninst
|
|
204
205
|
}
|
205
206
|
```
|
206
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
|
+
|
207
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,188 +1,149 @@
|
|
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
|
-
|
15
|
+
const loaderUtils = require("loader-utils");
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
/**
|
21
|
-
* Error thrown by Babel formatted to conform to Webpack reporting.
|
22
|
-
*/
|
23
|
-
|
24
|
-
|
25
|
-
function BabelLoaderError(name, message, codeFrame, hideStack, error) {
|
26
|
-
Error.call(this);
|
27
|
-
this.name = "BabelLoaderError";
|
28
|
-
this.message = formatMessage(name, message, codeFrame);
|
29
|
-
this.hideStack = hideStack;
|
30
|
-
this.error = error;
|
31
|
-
Error.captureStackTrace(this, BabelLoaderError);
|
32
|
-
}
|
33
|
-
|
34
|
-
BabelLoaderError.prototype = Object.create(Error.prototype);
|
35
|
-
BabelLoaderError.prototype.constructor = BabelLoaderError;
|
36
|
-
var STRIP_FILENAME_RE = /^[^:]+: /;
|
37
|
-
|
38
|
-
var formatMessage = function formatMessage(name, message, codeFrame) {
|
39
|
-
return (name ? name + ": " : "") + message + "\n\n" + codeFrame + "\n";
|
40
|
-
};
|
41
|
-
|
42
|
-
var transpile = function transpile(source, options) {
|
43
|
-
var forceEnv = options.forceEnv;
|
44
|
-
var tmpEnv;
|
45
|
-
delete options.forceEnv;
|
46
|
-
|
47
|
-
if (forceEnv) {
|
48
|
-
tmpEnv = process.env.BABEL_ENV;
|
49
|
-
process.env.BABEL_ENV = forceEnv;
|
17
|
+
function subscribe(subscriber, metadata, context) {
|
18
|
+
if (context[subscriber]) {
|
19
|
+
context[subscriber](metadata);
|
50
20
|
}
|
21
|
+
}
|
51
22
|
|
52
|
-
|
53
|
-
|
54
|
-
try {
|
55
|
-
result = babel.transform(source, options);
|
56
|
-
} catch (error) {
|
57
|
-
if (forceEnv) restoreBabelEnv(tmpEnv);
|
58
|
-
|
59
|
-
if (error.message && error.codeFrame) {
|
60
|
-
var message = error.message;
|
61
|
-
var name;
|
62
|
-
var hideStack;
|
63
|
-
|
64
|
-
if (error instanceof SyntaxError) {
|
65
|
-
message = message.replace(STRIP_FILENAME_RE, "");
|
66
|
-
name = "SyntaxError";
|
67
|
-
hideStack = true;
|
68
|
-
} else if (error instanceof TypeError) {
|
69
|
-
message = message.replace(STRIP_FILENAME_RE, "");
|
70
|
-
hideStack = true;
|
71
|
-
}
|
72
|
-
|
73
|
-
throw new BabelLoaderError(name, message, error.codeFrame, hideStack, error);
|
74
|
-
} else {
|
75
|
-
throw error;
|
76
|
-
}
|
77
|
-
}
|
78
|
-
|
79
|
-
var code = result.code;
|
80
|
-
var map = result.map;
|
81
|
-
var metadata = result.metadata;
|
82
|
-
|
83
|
-
if (map && (!map.sourcesContent || !map.sourcesContent.length)) {
|
84
|
-
map.sourcesContent = [source];
|
85
|
-
}
|
23
|
+
module.exports = makeLoader();
|
24
|
+
module.exports.custom = makeLoader;
|
86
25
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
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));
|
92
32
|
};
|
93
|
-
};
|
94
|
-
|
95
|
-
function restoreBabelEnv(prevValue) {
|
96
|
-
if (prevValue === undefined) {
|
97
|
-
delete process.env.BABEL_ENV;
|
98
|
-
} else {
|
99
|
-
process.env.BABEL_ENV = prevValue;
|
100
|
-
}
|
101
33
|
}
|
102
34
|
|
103
|
-
function
|
104
|
-
|
105
|
-
context[s](metadata);
|
106
|
-
}
|
35
|
+
function loader(_x, _x2, _x3) {
|
36
|
+
return _loader.apply(this, arguments);
|
107
37
|
}
|
108
38
|
|
109
|
-
|
110
|
-
|
39
|
+
function _loader() {
|
40
|
+
_loader = _asyncToGenerator(function* (source, inputSourceMap, overrides) {
|
41
|
+
const filename = this.resourcePath;
|
42
|
+
let loaderOptions = loaderUtils.getOptions(this) || {};
|
43
|
+
let customOptions;
|
111
44
|
|
112
|
-
|
113
|
-
|
114
|
-
|
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
|
115
50
|
|
116
|
-
var loaderOptions = loaderUtils.getOptions(this) || {};
|
117
|
-
var fileSystem = this.fs ? this.fs : fs;
|
118
|
-
var babelrcPath = null;
|
119
51
|
|
120
|
-
|
121
|
-
|
122
|
-
|
52
|
+
if ("forceEnv" in loaderOptions) {
|
53
|
+
console.warn("The option `forceEnv` has been removed in favor of `envName` in Babel 7.");
|
54
|
+
}
|
123
55
|
|
124
|
-
|
125
|
-
|
126
|
-
|
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
|
+
}
|
127
81
|
|
128
|
-
|
129
|
-
metadataSubscribers: [],
|
130
|
-
inputSourceMap: inputSourceMap,
|
131
|
-
sourceRoot: process.cwd(),
|
132
|
-
filename: filename,
|
133
|
-
cacheIdentifier: JSON.stringify({
|
134
|
-
"@babel/loader": pkg.version,
|
135
|
-
"@babel/core": babel.version,
|
136
|
-
babelrc: babelrcPath ? read(fileSystem, babelrcPath) : null,
|
137
|
-
env: loaderOptions.forceEnv || process.env.BABEL_ENV || process.env.NODE_ENV || "development"
|
138
|
-
})
|
139
|
-
};
|
140
|
-
var options = Object.assign({}, defaultOptions, loaderOptions);
|
82
|
+
const config = babel.loadPartialConfig(programmaticOptions);
|
141
83
|
|
142
|
-
|
143
|
-
|
144
|
-
}
|
84
|
+
if (config) {
|
85
|
+
let options = config.options;
|
145
86
|
|
146
|
-
|
147
|
-
|
148
|
-
|
87
|
+
if (overrides && overrides.config) {
|
88
|
+
options = yield overrides.config.call(this, config, {
|
89
|
+
source,
|
90
|
+
customOptions
|
91
|
+
});
|
92
|
+
}
|
149
93
|
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
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.
|
178
144
|
|
179
|
-
var _transpile = transpile(source, options),
|
180
|
-
code = _transpile.code,
|
181
|
-
map = _transpile.map,
|
182
|
-
metadata = _transpile.metadata;
|
183
145
|
|
184
|
-
|
185
|
-
return passMetadata(s, _this, metadata);
|
146
|
+
return [source, inputSourceMap];
|
186
147
|
});
|
187
|
-
|
188
|
-
}
|
148
|
+
return _loader.apply(this, arguments);
|
149
|
+
}
|
package/lib/transform.js
ADDED
@@ -0,0 +1,57 @@
|
|
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; // We don't return the full result here because some entries are not
|
26
|
+
// really serializable. For a full list of properties see here:
|
27
|
+
// https://github.com/babel/babel/blob/master/packages/babel-core/src/transformation/index.js
|
28
|
+
// For discussion on this topic see here:
|
29
|
+
// https://github.com/babel/babel-loader/pull/629
|
30
|
+
|
31
|
+
const {
|
32
|
+
ast,
|
33
|
+
code,
|
34
|
+
map,
|
35
|
+
metadata,
|
36
|
+
sourceType
|
37
|
+
} = result;
|
38
|
+
|
39
|
+
if (map && (!map.sourcesContent || !map.sourcesContent.length)) {
|
40
|
+
map.sourcesContent = [source];
|
41
|
+
}
|
42
|
+
|
43
|
+
return {
|
44
|
+
ast,
|
45
|
+
code,
|
46
|
+
map,
|
47
|
+
metadata,
|
48
|
+
sourceType
|
49
|
+
};
|
50
|
+
});
|
51
|
+
|
52
|
+
return function (_x, _x2) {
|
53
|
+
return _ref.apply(this, arguments);
|
54
|
+
};
|
55
|
+
}();
|
56
|
+
|
57
|
+
module.exports.version = babel.version;
|
package/lib/utils/relative.js
CHANGED
@@ -1,12 +1,15 @@
|
|
1
|
-
|
1
|
+
"use strict";
|
2
2
|
|
3
|
-
|
4
|
-
var rootPath = sourceRoot.replace(/\\/g, "/").split("/")[1];
|
5
|
-
var fileRootPath = filename.replace(/\\/g, "/").split("/")[1]; // If the file is in a completely different root folder use the absolute path of file.
|
3
|
+
const path = require("path");
|
6
4
|
|
7
|
-
|
8
|
-
|
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
|
9
|
+
|
10
|
+
if (rootPath && rootPath !== filePath) {
|
11
|
+
return file;
|
9
12
|
}
|
10
13
|
|
11
|
-
return path.relative(
|
14
|
+
return path.relative(root, file);
|
12
15
|
};
|
package/package.json
CHANGED
@@ -1,28 +1,29 @@
|
|
1
1
|
{
|
2
2
|
"name": "babel-loader",
|
3
|
-
"version": "8.0.0-beta.
|
3
|
+
"version": "8.0.0-beta.4",
|
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 || ^7.0.0-
|
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
|
-
"@babel/cli": "7.0.0-beta.
|
23
|
-
"@babel/core": "7.0.0-beta.
|
24
|
-
"@babel/preset-env": "7.0.0-beta.
|
25
|
-
"ava": "0.
|
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",
|
26
27
|
"babel-eslint": "^8.0.0",
|
27
28
|
"babel-plugin-istanbul": "^4.0.0",
|
28
29
|
"babel-plugin-react-intl": "^2.1.3",
|
@@ -32,19 +33,19 @@
|
|
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": "^
|
36
|
+
"lint-staged": "^7.1.0",
|
36
37
|
"nyc": "^11.0.1",
|
37
38
|
"prettier": "^1.2.2",
|
38
39
|
"react": "^16.0.0",
|
39
40
|
"react-intl": "^2.1.2",
|
40
41
|
"react-intl-webpack-plugin": "^0.0.3",
|
41
42
|
"rimraf": "^2.4.3",
|
42
|
-
"webpack": "^
|
43
|
+
"webpack": "^4.0.0"
|
43
44
|
},
|
44
45
|
"scripts": {
|
45
46
|
"clean": "rimraf lib/",
|
46
47
|
"build": "babel src/ --out-dir lib/",
|
47
|
-
"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'",
|
48
49
|
"lint": "eslint src test",
|
49
50
|
"precommit": "lint-staged",
|
50
51
|
"prepublish": "yarn run clean && yarn run build",
|
@@ -52,6 +53,9 @@
|
|
52
53
|
"test": "yarn run lint && cross-env BABEL_ENV=test yarn run build && yarn run test-only",
|
53
54
|
"test-only": "nyc ava"
|
54
55
|
},
|
56
|
+
"publishConfig": {
|
57
|
+
"tag": "next"
|
58
|
+
},
|
55
59
|
"repository": {
|
56
60
|
"type": "git",
|
57
61
|
"url": "https://github.com/babel/babel-loader.git"
|
package/lib/fs-cache.js
DELETED
@@ -1,180 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Filesystem cache
|
3
|
-
*
|
4
|
-
* Given a file and a transform function, cache the result into files
|
5
|
-
* or retrieve the previously cached files if the given file is already known.
|
6
|
-
*
|
7
|
-
* @see https://github.com/babel/babel-loader/issues/34
|
8
|
-
* @see https://github.com/babel/babel-loader/pull/41
|
9
|
-
*/
|
10
|
-
var crypto = require("crypto");
|
11
|
-
|
12
|
-
var mkdirp = require("mkdirp");
|
13
|
-
|
14
|
-
var findCacheDir = require("find-cache-dir");
|
15
|
-
|
16
|
-
var fs = require("fs");
|
17
|
-
|
18
|
-
var os = require("os");
|
19
|
-
|
20
|
-
var path = require("path");
|
21
|
-
|
22
|
-
var zlib = require("zlib");
|
23
|
-
|
24
|
-
var defaultCacheDirectory = null; // Lazily instantiated when needed
|
25
|
-
|
26
|
-
/**
|
27
|
-
* Read the contents from the compressed file.
|
28
|
-
*
|
29
|
-
* @async
|
30
|
-
* @params {String} filename
|
31
|
-
* @params {Function} callback
|
32
|
-
*/
|
33
|
-
|
34
|
-
var read = function read(filename, callback) {
|
35
|
-
return fs.readFile(filename, function (err, data) {
|
36
|
-
if (err) return callback(err);
|
37
|
-
return zlib.gunzip(data, function (err, content) {
|
38
|
-
if (err) return callback(err);
|
39
|
-
var result = {};
|
40
|
-
|
41
|
-
try {
|
42
|
-
result = JSON.parse(content);
|
43
|
-
} catch (e) {
|
44
|
-
return callback(e);
|
45
|
-
}
|
46
|
-
|
47
|
-
return callback(null, result);
|
48
|
-
});
|
49
|
-
});
|
50
|
-
};
|
51
|
-
/**
|
52
|
-
* Write contents into a compressed file.
|
53
|
-
*
|
54
|
-
* @async
|
55
|
-
* @params {String} filename
|
56
|
-
* @params {String} result
|
57
|
-
* @params {Function} callback
|
58
|
-
*/
|
59
|
-
|
60
|
-
|
61
|
-
var write = function write(filename, result, callback) {
|
62
|
-
var content = JSON.stringify(result);
|
63
|
-
return zlib.gzip(content, function (err, data) {
|
64
|
-
if (err) return callback(err);
|
65
|
-
return fs.writeFile(filename, data, callback);
|
66
|
-
});
|
67
|
-
};
|
68
|
-
/**
|
69
|
-
* Build the filename for the cached file
|
70
|
-
*
|
71
|
-
* @params {String} source File source code
|
72
|
-
* @params {Object} options Options used
|
73
|
-
*
|
74
|
-
* @return {String}
|
75
|
-
*/
|
76
|
-
|
77
|
-
|
78
|
-
var filename = function filename(source, identifier, options) {
|
79
|
-
var hash = crypto.createHash("SHA1");
|
80
|
-
var contents = JSON.stringify({
|
81
|
-
source: source,
|
82
|
-
options: options,
|
83
|
-
identifier: identifier
|
84
|
-
});
|
85
|
-
hash.end(contents);
|
86
|
-
return hash.read().toString("hex") + ".json.gz";
|
87
|
-
};
|
88
|
-
/**
|
89
|
-
* Handle the cache
|
90
|
-
*
|
91
|
-
* @params {String} directory
|
92
|
-
* @params {Object} params
|
93
|
-
* @params {Function} callback
|
94
|
-
*/
|
95
|
-
|
96
|
-
|
97
|
-
var handleCache = function handleCache(directory, params, callback) {
|
98
|
-
var source = params.source;
|
99
|
-
var options = params.options || {};
|
100
|
-
var transform = params.transform;
|
101
|
-
var identifier = params.identifier;
|
102
|
-
var shouldFallback = typeof params.directory !== "string" && directory !== os.tmpdir(); // Make sure the directory exists.
|
103
|
-
|
104
|
-
mkdirp(directory, function (err) {
|
105
|
-
// Fallback to tmpdir if node_modules folder not writable
|
106
|
-
if (err) return shouldFallback ? handleCache(os.tmpdir(), params, callback) : callback(err);
|
107
|
-
var file = path.join(directory, filename(source, identifier, options));
|
108
|
-
return read(file, function (err, content) {
|
109
|
-
var result = {}; // No errors mean that the file was previously cached
|
110
|
-
// we just need to return it
|
111
|
-
|
112
|
-
if (!err) return callback(null, content); // Otherwise just transform the file
|
113
|
-
// return it to the user asap and write it in cache
|
114
|
-
|
115
|
-
try {
|
116
|
-
result = transform(source, options);
|
117
|
-
} catch (error) {
|
118
|
-
return callback(error);
|
119
|
-
}
|
120
|
-
|
121
|
-
return write(file, result, function (err) {
|
122
|
-
// Fallback to tmpdir if node_modules folder not writable
|
123
|
-
if (err) return shouldFallback ? handleCache(os.tmpdir(), params, callback) : callback(err);
|
124
|
-
callback(null, result);
|
125
|
-
});
|
126
|
-
});
|
127
|
-
});
|
128
|
-
};
|
129
|
-
/**
|
130
|
-
* Retrieve file from cache, or create a new one for future reads
|
131
|
-
*
|
132
|
-
* @async
|
133
|
-
* @param {Object} params
|
134
|
-
* @param {String} params.directory Directory to store cached files
|
135
|
-
* @param {String} params.identifier Unique identifier to bust cache
|
136
|
-
* @param {String} params.source Original contents of the file to be cached
|
137
|
-
* @param {Object} params.options Options to be given to the transform fn
|
138
|
-
* @param {Function} params.transform Function that will transform the
|
139
|
-
* original file and whose result will be
|
140
|
-
* cached
|
141
|
-
*
|
142
|
-
* @param {Function<err, result>} callback
|
143
|
-
*
|
144
|
-
* @example
|
145
|
-
*
|
146
|
-
* cache({
|
147
|
-
* directory: '.tmp/cache',
|
148
|
-
* identifier: 'babel-loader-cachefile',
|
149
|
-
* source: *source code from file*,
|
150
|
-
* options: {
|
151
|
-
* experimental: true,
|
152
|
-
* runtime: true
|
153
|
-
* },
|
154
|
-
* transform: function(source, options) {
|
155
|
-
* var content = *do what you need with the source*
|
156
|
-
* return content;
|
157
|
-
* }
|
158
|
-
* }, function(err, result) {
|
159
|
-
*
|
160
|
-
* });
|
161
|
-
*/
|
162
|
-
|
163
|
-
|
164
|
-
module.exports = function (params, callback) {
|
165
|
-
var directory;
|
166
|
-
|
167
|
-
if (typeof params.directory === "string") {
|
168
|
-
directory = params.directory;
|
169
|
-
} else {
|
170
|
-
if (defaultCacheDirectory === null) {
|
171
|
-
defaultCacheDirectory = findCacheDir({
|
172
|
-
name: "babel-loader"
|
173
|
-
}) || os.tmpdir();
|
174
|
-
}
|
175
|
-
|
176
|
-
directory = defaultCacheDirectory;
|
177
|
-
}
|
178
|
-
|
179
|
-
handleCache(directory, params, callback);
|
180
|
-
};
|
package/lib/resolve-rc.js
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
var path = require("path");
|
2
|
-
|
3
|
-
var exists = require("./utils/exists");
|
4
|
-
|
5
|
-
module.exports = function find(fileSystem, start) {
|
6
|
-
var _arr = [".babelrc", ".babelrc.js", "package.json"];
|
7
|
-
|
8
|
-
for (var _i = 0; _i < _arr.length; _i++) {
|
9
|
-
var fileName = _arr[_i];
|
10
|
-
var file = path.join(start, fileName);
|
11
|
-
|
12
|
-
if (exists(fileSystem, file)) {
|
13
|
-
if (fileName !== "package.json" || typeof require(file).babel === "object") {
|
14
|
-
return file;
|
15
|
-
}
|
16
|
-
}
|
17
|
-
}
|
18
|
-
|
19
|
-
var up = path.dirname(start); // Reached root
|
20
|
-
|
21
|
-
if (up !== start) {
|
22
|
-
return find(fileSystem, 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 readBabelConfig(fileSystem, filename) {
|
4
|
-
if (path.basename(filename) === "package.json") {
|
5
|
-
var pkg = require(filename);
|
6
|
-
|
7
|
-
return JSON.stringify(pkg.babel);
|
8
|
-
} // Webpack `fs` return Buffer
|
9
|
-
|
10
|
-
|
11
|
-
return fileSystem.readFileSync(filename).toString("utf8");
|
12
|
-
};
|