babel-loader 8.0.0 → 8.0.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 +61 -46
- package/lib/cache.js +20 -16
- package/lib/index.js +87 -15
- package/lib/injectCaller.js +39 -0
- package/lib/transform.js +5 -39
- package/package.json +5 -5
- package/lib/utils/relative.js +0 -15
package/README.md
CHANGED
@@ -1,42 +1,43 @@
|
|
1
|
-
> This
|
1
|
+
> This README is for babel-loader v8 + Babel v7
|
2
2
|
> Check the [7.x branch](https://github.com/babel/babel-loader/tree/7.x) for docs with Babel v6
|
3
3
|
|
4
4
|
[](https://www.npmjs.com/package/babel-loader)
|
5
5
|
[](https://travis-ci.org/babel/babel-loader)
|
6
|
-
[](https://ci.appveyor.com/project/babel/babel-loader/branch/master)
|
7
7
|
[](https://codecov.io/gh/babel/babel-loader)
|
8
8
|
|
9
9
|
<div align="center">
|
10
|
-
<a href="https://github.com/babel/babel
|
11
|
-
<img
|
10
|
+
<a href="https://github.com/babel/babel">
|
11
|
+
<img src="https://rawgit.com/babel/logo/master/babel.svg" alt="Babel logo" width="200" height="200">
|
12
12
|
</a>
|
13
13
|
<a href="https://github.com/webpack/webpack">
|
14
|
-
<img
|
14
|
+
<img src="https://webpack.js.org/assets/icon-square-big.svg" alt="webpack logo" width="200" height="200">
|
15
15
|
</a>
|
16
|
-
<h1>Babel Loader</h1>
|
17
16
|
</div>
|
18
17
|
|
18
|
+
<h1 align="center">Babel Loader</h1>
|
19
|
+
|
19
20
|
This package allows transpiling JavaScript files using [Babel](https://github.com/babel/babel) and [webpack](https://github.com/webpack/webpack).
|
20
21
|
|
21
|
-
|
22
|
+
**Note**: Issues with the output should be reported on the Babel [Issues](https://github.com/babel/babel/issues) tracker.
|
22
23
|
|
23
24
|
<h2 align="center">Install</h2>
|
24
25
|
|
25
26
|
> webpack 4.x | babel-loader 8.x | babel 7.x
|
26
27
|
|
27
28
|
```bash
|
28
|
-
npm install babel-loader @babel/core @babel/preset-env webpack
|
29
|
+
npm install -D babel-loader @babel/core @babel/preset-env webpack
|
29
30
|
```
|
30
31
|
|
31
32
|
> webpack 4.x | babel-loader 7.x | babel 6.x
|
32
33
|
|
33
34
|
```bash
|
34
|
-
npm install babel-loader@7 babel-core babel-preset-env webpack
|
35
|
+
npm install -D babel-loader@7 babel-core babel-preset-env webpack
|
35
36
|
```
|
36
37
|
|
37
38
|
<h2 align="center">Usage</h2>
|
38
39
|
|
39
|
-
|
40
|
+
webpack documentation: [Loaders](https://webpack.js.org/loaders/)
|
40
41
|
|
41
42
|
Within your webpack configuration object, you'll need to add the babel-loader to the list of modules, like so:
|
42
43
|
|
@@ -44,7 +45,7 @@ Within your webpack configuration object, you'll need to add the babel-loader to
|
|
44
45
|
module: {
|
45
46
|
rules: [
|
46
47
|
{
|
47
|
-
test: /\.js$/,
|
48
|
+
test: /\.m?js$/,
|
48
49
|
exclude: /(node_modules|bower_components)/,
|
49
50
|
use: {
|
50
51
|
loader: 'babel-loader',
|
@@ -59,16 +60,15 @@ module: {
|
|
59
60
|
|
60
61
|
### Options
|
61
62
|
|
62
|
-
See the `babel` [options](https://babeljs.io/docs/
|
63
|
-
|
63
|
+
See the `babel` [options](https://babeljs.io/docs/en/options).
|
64
64
|
|
65
|
-
You can pass options to the loader by using the [options
|
65
|
+
You can pass options to the loader by using the [`options`](https://webpack.js.org/configuration/module/#rule-options-rule-query) property:
|
66
66
|
|
67
67
|
```javascript
|
68
68
|
module: {
|
69
69
|
rules: [
|
70
70
|
{
|
71
|
-
test: /\.js$/,
|
71
|
+
test: /\.m?js$/,
|
72
72
|
exclude: /(node_modules|bower_components)/,
|
73
73
|
use: {
|
74
74
|
loader: 'babel-loader',
|
@@ -84,48 +84,44 @@ module: {
|
|
84
84
|
|
85
85
|
This loader also supports the following loader-specific option:
|
86
86
|
|
87
|
-
* `cacheDirectory`: Default `false`. When set, the given directory will be used to cache the results of the loader. Future webpack builds will attempt to read from the cache to avoid needing to run the potentially expensive Babel recompilation process on each run. If the value is blank (`loader: 'babel-loader?cacheDirectory'`) or `true` (`loader: babel-loader?cacheDirectory=true`) the loader will use the default cache directory in `node_modules/.cache/babel-loader` or fallback to the default OS temporary file directory if no `node_modules` folder could be found in any root directory.
|
87
|
+
* `cacheDirectory`: Default `false`. When set, the given directory will be used to cache the results of the loader. Future webpack builds will attempt to read from the cache to avoid needing to run the potentially expensive Babel recompilation process on each run. If the value is blank (`loader: 'babel-loader?cacheDirectory'`) or `true` (`loader: 'babel-loader?cacheDirectory=true'`), the loader will use the default cache directory in `node_modules/.cache/babel-loader` or fallback to the default OS temporary file directory if no `node_modules` folder could be found in any root directory.
|
88
|
+
|
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.
|
88
90
|
|
89
|
-
* `
|
91
|
+
* `cacheCompression`: Default `true`. When set, each Babel transform output will be compressed with Gzip. If you want to opt-out of cache compression, set it to `false` -- your project may benefit from this if it transpiles thousands of files.
|
90
92
|
|
91
|
-
* `
|
92
|
-
`babel-loader` will be used.
|
93
|
+
* `customize`: Default `null`. The path of a module that exports a `custom` callback [like the one that you'd pass to `.custom()`](#customized-loader). Since you already have to make a new file to use this, it is recommended that you instead use `.custom` to create a wrapper loader. Only use this is you _must_ continue using `babel-loader` directly, but still want to customize.
|
93
94
|
|
94
|
-
|
95
|
+
**Note**: The `sourceMap` option is ignored. Instead, source maps are automatically enabled when webpack is configured to use them (via the [`devtool`](https://webpack.js.org/configuration/devtool/#devtool) config option).
|
95
96
|
|
96
97
|
## Troubleshooting
|
97
98
|
|
98
99
|
### babel-loader is slow!
|
99
100
|
|
100
|
-
Make sure you are transforming as few files as possible. Because you are probably
|
101
|
-
matching `/\.js$/`, you might be transforming the `node_modules` folder or other unwanted
|
102
|
-
source.
|
101
|
+
Make sure you are transforming as few files as possible. Because you are probably matching `/\.m?js$/`, you might be transforming the `node_modules` folder or other unwanted source.
|
103
102
|
|
104
103
|
To exclude `node_modules`, see the `exclude` option in the `loaders` config as documented above.
|
105
104
|
|
106
|
-
You can also speed up babel-loader by as much as 2x by using the `cacheDirectory` option.
|
107
|
-
This will cache transformations to the filesystem.
|
105
|
+
You can also speed up babel-loader by as much as 2x by using the `cacheDirectory` option. This will cache transformations to the filesystem.
|
108
106
|
|
109
|
-
###
|
107
|
+
### Babel is injecting helpers into each file and bloating my code!
|
110
108
|
|
111
|
-
|
112
|
-
this will be added to every file that requires it.
|
109
|
+
Babel uses very small helpers for common functions such as `_extend`. By default, this will be added to every file that requires it.
|
113
110
|
|
114
|
-
You can instead require the
|
111
|
+
You can instead require the Babel runtime as a separate module to avoid the duplication.
|
115
112
|
|
116
|
-
The following configuration disables automatic per-file runtime injection in
|
117
|
-
requiring `babel-plugin-transform-runtime` and making all helper references use it.
|
113
|
+
The following configuration disables automatic per-file runtime injection in Babel, requiring `babel-plugin-transform-runtime` instead and making all helper references use it.
|
118
114
|
|
119
115
|
See the [docs](https://babeljs.io/docs/plugins/transform-runtime/) for more information.
|
120
116
|
|
121
|
-
**NOTE
|
117
|
+
**NOTE**: You must run `npm install -D @babel/plugin-transform-runtime` to include this in your project and `babel-runtime` itself as a dependency with `npm install @babel/runtime`.
|
122
118
|
|
123
119
|
```javascript
|
124
120
|
rules: [
|
125
|
-
// the 'transform-runtime' plugin tells
|
126
|
-
// instead of inlining it.
|
121
|
+
// the 'transform-runtime' plugin tells Babel to
|
122
|
+
// require the runtime instead of inlining it.
|
127
123
|
{
|
128
|
-
test: /\.js$/,
|
124
|
+
test: /\.m?js$/,
|
129
125
|
exclude: /(node_modules|bower_components)/,
|
130
126
|
use: {
|
131
127
|
loader: 'babel-loader',
|
@@ -138,9 +134,9 @@ rules: [
|
|
138
134
|
]
|
139
135
|
```
|
140
136
|
|
141
|
-
#### **NOTE
|
137
|
+
#### **NOTE**: transform-runtime & custom polyfills (e.g. Promise library)
|
142
138
|
|
143
|
-
Since [babel-plugin-transform-runtime](https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-runtime) includes a polyfill that includes a custom [regenerator
|
139
|
+
Since [babel-plugin-transform-runtime](https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-runtime) includes a polyfill that includes a custom [regenerator-runtime](https://github.com/facebook/regenerator/blob/master/packages/regenerator-runtime/runtime.js) and [core-js](https://github.com/zloirock/core-js), the following usual shimming method using `webpack.ProvidePlugin` will not work:
|
144
140
|
|
145
141
|
```javascript
|
146
142
|
// ...
|
@@ -184,23 +180,23 @@ require('@babel/runtime/core-js/promise').default = require('bluebird');
|
|
184
180
|
require('./app');
|
185
181
|
```
|
186
182
|
|
187
|
-
### The
|
183
|
+
### The Node.js API for `babel` has been moved to `babel-core`.
|
188
184
|
|
189
|
-
If you receive this message it means that you have the npm package `babel` installed and
|
190
|
-
```
|
185
|
+
If you receive this message, it means that you have the npm package `babel` installed and are using the short notation of the loader in the webpack config (which is not valid anymore as of webpack 2.x):
|
186
|
+
```javascript
|
191
187
|
{
|
192
|
-
test: /\.js$/,
|
188
|
+
test: /\.m?js$/,
|
193
189
|
loader: 'babel',
|
194
190
|
}
|
195
191
|
```
|
196
192
|
|
197
|
-
|
193
|
+
webpack then tries to load the `babel` package instead of the `babel-loader`.
|
198
194
|
|
199
|
-
To fix this you should uninstall the npm package `babel
|
195
|
+
To fix this, you should uninstall the npm package `babel`, as it is deprecated in Babel v6. (Instead, install `babel-cli` or `babel-core`.)
|
200
196
|
In the case one of your dependencies is installing `babel` and you cannot uninstall it yourself, use the complete name of the loader in the webpack config:
|
201
|
-
```
|
197
|
+
```javascript
|
202
198
|
{
|
203
|
-
test: /\.js$/,
|
199
|
+
test: /\.m?js$/,
|
204
200
|
loader: 'babel-loader',
|
205
201
|
}
|
206
202
|
```
|
@@ -214,9 +210,14 @@ of Babel's configuration for each file that it processes.
|
|
214
210
|
`babel` so that tooling can ensure that it using exactly the same `@babel/core`
|
215
211
|
instance as the loader itself.
|
216
212
|
|
213
|
+
In cases where you want to customize without actually having a file to call `.custom`, you
|
214
|
+
may also pass the `customize` option with a string pointing at a file that exports
|
215
|
+
your `custom` callback function.
|
216
|
+
|
217
217
|
### Example
|
218
218
|
|
219
219
|
```js
|
220
|
+
// Export from "./my-custom-loader.js" or whatever you want.
|
220
221
|
module.exports = require("babel-loader").custom(babel => {
|
221
222
|
function myPlugin() {
|
222
223
|
return {
|
@@ -236,7 +237,7 @@ module.exports = require("babel-loader").custom(babel => {
|
|
236
237
|
};
|
237
238
|
},
|
238
239
|
|
239
|
-
// Passed Babel's 'PartialConfig' object.
|
240
|
+
// Passed Babel's 'PartialConfig' object.
|
240
241
|
config(cfg) {
|
241
242
|
if (cfg.hasFilesystemConfig()) {
|
242
243
|
// Use the normal config
|
@@ -264,6 +265,20 @@ module.exports = require("babel-loader").custom(babel => {
|
|
264
265
|
});
|
265
266
|
```
|
266
267
|
|
268
|
+
```js
|
269
|
+
// And in your Webpack config
|
270
|
+
module.exports = {
|
271
|
+
// ..
|
272
|
+
module: {
|
273
|
+
rules: [{
|
274
|
+
// ...
|
275
|
+
loader: path.join(__dirname, 'my-custom-loader.js'),
|
276
|
+
// ...
|
277
|
+
}]
|
278
|
+
}
|
279
|
+
};
|
280
|
+
```
|
281
|
+
|
267
282
|
### `customOptions(options: Object): { custom: Object, loader: Object }`
|
268
283
|
|
269
284
|
Given the loader's options, split custom options out of `babel-loader`'s
|
package/lib/cache.js
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
"use strict";
|
2
2
|
|
3
|
-
function
|
3
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, 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); } }
|
4
|
+
|
5
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
4
6
|
|
5
7
|
/**
|
6
8
|
* Filesystem Cache
|
@@ -46,13 +48,13 @@ const mkdirp = promisify(mkdirpOrig);
|
|
46
48
|
const read =
|
47
49
|
/*#__PURE__*/
|
48
50
|
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
|
+
var _ref = _asyncToGenerator(function* (filename, compress) {
|
52
|
+
const data = yield readFile(filename + (compress ? ".gz" : ""));
|
53
|
+
const content = compress ? yield gunzip(data) : data;
|
54
|
+
return JSON.parse(content.toString());
|
53
55
|
});
|
54
56
|
|
55
|
-
return function read(_x) {
|
57
|
+
return function read(_x, _x2) {
|
56
58
|
return _ref.apply(this, arguments);
|
57
59
|
};
|
58
60
|
}();
|
@@ -68,13 +70,13 @@ function () {
|
|
68
70
|
const write =
|
69
71
|
/*#__PURE__*/
|
70
72
|
function () {
|
71
|
-
var _ref2 = _asyncToGenerator(function* (filename, result) {
|
73
|
+
var _ref2 = _asyncToGenerator(function* (filename, compress, result) {
|
72
74
|
const content = JSON.stringify(result);
|
73
|
-
const data = yield gzip(content);
|
74
|
-
return yield writeFile(filename, data);
|
75
|
+
const data = compress ? yield gzip(content) : content;
|
76
|
+
return yield writeFile(filename + (compress ? ".gz" : ""), data);
|
75
77
|
});
|
76
78
|
|
77
|
-
return function write(
|
79
|
+
return function write(_x3, _x4, _x5) {
|
78
80
|
return _ref2.apply(this, arguments);
|
79
81
|
};
|
80
82
|
}();
|
@@ -96,7 +98,7 @@ const filename = function (source, identifier, options) {
|
|
96
98
|
identifier
|
97
99
|
});
|
98
100
|
hash.update(contents);
|
99
|
-
return hash.digest("hex") + ".json
|
101
|
+
return hash.digest("hex") + ".json";
|
100
102
|
};
|
101
103
|
/**
|
102
104
|
* Handle the cache
|
@@ -114,14 +116,15 @@ function () {
|
|
114
116
|
source,
|
115
117
|
options = {},
|
116
118
|
cacheIdentifier,
|
117
|
-
cacheDirectory
|
119
|
+
cacheDirectory,
|
120
|
+
cacheCompression
|
118
121
|
} = params;
|
119
122
|
const file = path.join(directory, filename(source, cacheIdentifier, options));
|
120
123
|
|
121
124
|
try {
|
122
125
|
// No errors mean that the file was previously cached
|
123
126
|
// we just need to return it
|
124
|
-
return yield read(file);
|
127
|
+
return yield read(file, cacheCompression);
|
125
128
|
} catch (err) {}
|
126
129
|
|
127
130
|
const fallback = typeof cacheDirectory !== "string" && directory !== os.tmpdir(); // Make sure the directory exists.
|
@@ -141,7 +144,7 @@ function () {
|
|
141
144
|
const result = yield transform(source, options);
|
142
145
|
|
143
146
|
try {
|
144
|
-
yield write(file, result);
|
147
|
+
yield write(file, cacheCompression, result);
|
145
148
|
} catch (err) {
|
146
149
|
if (fallback) {
|
147
150
|
// Fallback to tmpdir if node_modules folder not writable
|
@@ -154,7 +157,7 @@ function () {
|
|
154
157
|
return result;
|
155
158
|
});
|
156
159
|
|
157
|
-
return function handleCache(
|
160
|
+
return function handleCache(_x6, _x7) {
|
158
161
|
return _ref3.apply(this, arguments);
|
159
162
|
};
|
160
163
|
}();
|
@@ -176,6 +179,7 @@ function () {
|
|
176
179
|
* cache({
|
177
180
|
* directory: '.tmp/cache',
|
178
181
|
* identifier: 'babel-loader-cachefile',
|
182
|
+
* cacheCompression: false,
|
179
183
|
* source: *source code from file*,
|
180
184
|
* options: {
|
181
185
|
* experimental: true,
|
@@ -212,7 +216,7 @@ function () {
|
|
212
216
|
return yield handleCache(directory, params);
|
213
217
|
});
|
214
218
|
|
215
|
-
return function (
|
219
|
+
return function (_x8) {
|
216
220
|
return _ref4.apply(this, arguments);
|
217
221
|
};
|
218
222
|
}();
|
package/lib/index.js
CHANGED
@@ -1,8 +1,26 @@
|
|
1
1
|
"use strict";
|
2
2
|
|
3
|
-
function
|
3
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, 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); } }
|
4
4
|
|
5
|
-
|
5
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
6
|
+
|
7
|
+
let babel;
|
8
|
+
|
9
|
+
try {
|
10
|
+
babel = require("@babel/core");
|
11
|
+
} catch (err) {
|
12
|
+
if (err.code === "MODULE_NOT_FOUND") {
|
13
|
+
err.message += "\n babel-loader@8 requires Babel 7.x (the package '@babel/core'). " + "If you'd like to use Babel 6.x ('babel-core'), you should install 'babel-loader@7'.";
|
14
|
+
}
|
15
|
+
|
16
|
+
throw err;
|
17
|
+
} // Since we've got the reverse bridge package at @babel/core@6.x, give
|
18
|
+
// people useful feedback if they try to use it alongside babel-loader.
|
19
|
+
|
20
|
+
|
21
|
+
if (/^6\./.test(babel.version)) {
|
22
|
+
throw new Error("\n babel-loader@8 will not work with the '@babel/core@6' bridge package. " + "If you want to use Babel 6.x, install 'babel-loader@7'.");
|
23
|
+
}
|
6
24
|
|
7
25
|
const pkg = require("../package.json");
|
8
26
|
|
@@ -10,7 +28,9 @@ const cache = require("./cache");
|
|
10
28
|
|
11
29
|
const transform = require("./transform");
|
12
30
|
|
13
|
-
const
|
31
|
+
const injectCaller = require("./injectCaller");
|
32
|
+
|
33
|
+
const path = require("path");
|
14
34
|
|
15
35
|
const loaderUtils = require("loader-utils");
|
16
36
|
|
@@ -40,10 +60,38 @@ function _loader() {
|
|
40
60
|
_loader = _asyncToGenerator(function* (source, inputSourceMap, overrides) {
|
41
61
|
const filename = this.resourcePath;
|
42
62
|
let loaderOptions = loaderUtils.getOptions(this) || {};
|
63
|
+
|
64
|
+
if (loaderOptions.customize != null) {
|
65
|
+
if (typeof loaderOptions.customize !== "string") {
|
66
|
+
throw new Error("Customized loaders must be implemented as standalone modules.");
|
67
|
+
}
|
68
|
+
|
69
|
+
if (!path.isAbsolute(loaderOptions.customize)) {
|
70
|
+
throw new Error("Customized loaders must be passed as absolute paths, since " + "babel-loader has no way to know what they would be relative to.");
|
71
|
+
}
|
72
|
+
|
73
|
+
if (overrides) {
|
74
|
+
throw new Error("babel-loader's 'customize' option is not available when already " + "using a customized babel-loader wrapper.");
|
75
|
+
}
|
76
|
+
|
77
|
+
let override = require(loaderOptions.customize);
|
78
|
+
|
79
|
+
if (override.__esModule) override = override.default;
|
80
|
+
|
81
|
+
if (typeof override !== "function") {
|
82
|
+
throw new Error("Custom overrides must be functions.");
|
83
|
+
}
|
84
|
+
|
85
|
+
overrides = override(babel);
|
86
|
+
}
|
87
|
+
|
43
88
|
let customOptions;
|
44
89
|
|
45
90
|
if (overrides && overrides.customOptions) {
|
46
|
-
const result = yield overrides.customOptions.call(this, loaderOptions
|
91
|
+
const result = yield overrides.customOptions.call(this, loaderOptions, {
|
92
|
+
source,
|
93
|
+
map: inputSourceMap
|
94
|
+
});
|
47
95
|
customOptions = result.custom;
|
48
96
|
loaderOptions = result.loader;
|
49
97
|
} // Deprecation handling
|
@@ -55,31 +103,41 @@ function _loader() {
|
|
55
103
|
|
56
104
|
if (typeof loaderOptions.babelrc === "string") {
|
57
105
|
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
|
-
} //
|
106
|
+
} // Standardize on 'sourceMaps' as the key passed through to Webpack, so that
|
107
|
+
// users may safely use either one alongside our default use of
|
108
|
+
// 'this.sourceMap' below without getting error about conflicting aliases.
|
109
|
+
|
59
110
|
|
111
|
+
if (Object.prototype.hasOwnProperty.call(loaderOptions, "sourceMap") && !Object.prototype.hasOwnProperty.call(loaderOptions, "sourceMaps")) {
|
112
|
+
loaderOptions = Object.assign({}, loaderOptions, {
|
113
|
+
sourceMaps: loaderOptions.sourceMap
|
114
|
+
});
|
115
|
+
delete loaderOptions.sourceMap;
|
116
|
+
}
|
60
117
|
|
61
|
-
const {
|
62
|
-
sourceRoot = process.cwd(),
|
63
|
-
sourceMap = this.sourceMap,
|
64
|
-
sourceFileName = relative(sourceRoot, filename)
|
65
|
-
} = loaderOptions;
|
66
118
|
const programmaticOptions = Object.assign({}, loaderOptions, {
|
67
119
|
filename,
|
68
120
|
inputSourceMap: inputSourceMap || undefined,
|
69
|
-
|
70
|
-
|
71
|
-
|
121
|
+
// Set the default sourcemap behavior based on Webpack's mapping flag,
|
122
|
+
// but allow users to override if they want.
|
123
|
+
sourceMaps: loaderOptions.sourceMaps === undefined ? this.sourceMap : loaderOptions.sourceMaps,
|
124
|
+
// Ensure that Webpack will get a full absolute path in the sourcemap
|
125
|
+
// so that it can properly map the module back to its internal cached
|
126
|
+
// modules.
|
127
|
+
sourceFileName: filename
|
72
128
|
}); // Remove loader related options
|
73
129
|
|
130
|
+
delete programmaticOptions.customize;
|
74
131
|
delete programmaticOptions.cacheDirectory;
|
75
132
|
delete programmaticOptions.cacheIdentifier;
|
133
|
+
delete programmaticOptions.cacheCompression;
|
76
134
|
delete programmaticOptions.metadataSubscribers;
|
77
135
|
|
78
136
|
if (!babel.loadPartialConfig) {
|
79
137
|
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
138
|
}
|
81
139
|
|
82
|
-
const config = babel.loadPartialConfig(programmaticOptions);
|
140
|
+
const config = babel.loadPartialConfig(injectCaller(programmaticOptions));
|
83
141
|
|
84
142
|
if (config) {
|
85
143
|
let options = config.options;
|
@@ -87,10 +145,21 @@ function _loader() {
|
|
87
145
|
if (overrides && overrides.config) {
|
88
146
|
options = yield overrides.config.call(this, config, {
|
89
147
|
source,
|
148
|
+
map: inputSourceMap,
|
90
149
|
customOptions
|
91
150
|
});
|
92
151
|
}
|
93
152
|
|
153
|
+
if (options.sourceMaps === "inline") {
|
154
|
+
// Babel has this weird behavior where if you set "inline", we
|
155
|
+
// inline the sourcemap, and set 'result.map = null'. This results
|
156
|
+
// in bad behavior from Babel since the maps get put into the code,
|
157
|
+
// which Webpack does not expect, and because the map we return to
|
158
|
+
// Webpack is null, which is also bad. To avoid that, we override the
|
159
|
+
// behavior here so "inline" just behaves like 'true'.
|
160
|
+
options.sourceMaps = true;
|
161
|
+
}
|
162
|
+
|
94
163
|
const {
|
95
164
|
cacheDirectory = null,
|
96
165
|
cacheIdentifier = JSON.stringify({
|
@@ -98,6 +167,7 @@ function _loader() {
|
|
98
167
|
"@babel/core": transform.version,
|
99
168
|
"@babel/loader": pkg.version
|
100
169
|
}),
|
170
|
+
cacheCompression = true,
|
101
171
|
metadataSubscribers = []
|
102
172
|
} = loaderOptions;
|
103
173
|
let result;
|
@@ -108,7 +178,8 @@ function _loader() {
|
|
108
178
|
options,
|
109
179
|
transform,
|
110
180
|
cacheDirectory,
|
111
|
-
cacheIdentifier
|
181
|
+
cacheIdentifier,
|
182
|
+
cacheCompression
|
112
183
|
});
|
113
184
|
} else {
|
114
185
|
result = yield transform(source, options);
|
@@ -124,6 +195,7 @@ function _loader() {
|
|
124
195
|
if (overrides && overrides.result) {
|
125
196
|
result = yield overrides.result.call(this, result, {
|
126
197
|
source,
|
198
|
+
map: inputSourceMap,
|
127
199
|
customOptions,
|
128
200
|
config,
|
129
201
|
options
|
@@ -0,0 +1,39 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
const babel = require("@babel/core");
|
4
|
+
|
5
|
+
module.exports = function injectCaller(opts) {
|
6
|
+
if (!supportsCallerOption()) return opts;
|
7
|
+
return Object.assign({}, opts, {
|
8
|
+
caller: Object.assign({
|
9
|
+
name: "babel-loader",
|
10
|
+
// Webpack >= 2 supports ESM and dynamic import.
|
11
|
+
supportsStaticESM: true,
|
12
|
+
supportsDynamicImport: true
|
13
|
+
}, opts.caller)
|
14
|
+
});
|
15
|
+
}; // TODO: We can remove this eventually, I'm just adding it so that people have
|
16
|
+
// a little time to migrate to the newer RCs of @babel/core without getting
|
17
|
+
// hard-to-diagnose errors about unknown 'caller' options.
|
18
|
+
|
19
|
+
|
20
|
+
let supportsCallerOptionFlag = undefined;
|
21
|
+
|
22
|
+
function supportsCallerOption() {
|
23
|
+
if (supportsCallerOptionFlag === undefined) {
|
24
|
+
try {
|
25
|
+
// Rather than try to match the Babel version, we just see if it throws
|
26
|
+
// when passed a 'caller' flag, and use that to decide if it is supported.
|
27
|
+
babel.loadPartialConfig({
|
28
|
+
caller: undefined,
|
29
|
+
babelrc: false,
|
30
|
+
configFile: false
|
31
|
+
});
|
32
|
+
supportsCallerOptionFlag = true;
|
33
|
+
} catch (err) {
|
34
|
+
supportsCallerOptionFlag = false;
|
35
|
+
}
|
36
|
+
}
|
37
|
+
|
38
|
+
return supportsCallerOptionFlag;
|
39
|
+
}
|
package/lib/transform.js
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
"use strict";
|
2
2
|
|
3
|
-
function
|
3
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, 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); } }
|
4
|
+
|
5
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
4
6
|
|
5
7
|
const babel = require("@babel/core");
|
6
8
|
|
@@ -17,7 +19,7 @@ function () {
|
|
17
19
|
let result;
|
18
20
|
|
19
21
|
try {
|
20
|
-
result = yield transform(source,
|
22
|
+
result = yield transform(source, options);
|
21
23
|
} catch (err) {
|
22
24
|
throw err.message && err.codeFrame ? new LoaderError(err) : err;
|
23
25
|
}
|
@@ -54,40 +56,4 @@ function () {
|
|
54
56
|
};
|
55
57
|
}();
|
56
58
|
|
57
|
-
module.exports.version = babel.version;
|
58
|
-
|
59
|
-
function injectCaller(opts) {
|
60
|
-
if (!supportsCallerOption()) return opts;
|
61
|
-
return Object.assign({}, opts, {
|
62
|
-
caller: Object.assign({
|
63
|
-
name: "babel-loader",
|
64
|
-
// Webpack >= 2 supports ESM and dynamic import.
|
65
|
-
supportsStaticESM: true,
|
66
|
-
supportsDynamicImport: true
|
67
|
-
}, opts.caller)
|
68
|
-
});
|
69
|
-
} // TODO: We can remove this eventually, I'm just adding it so that people have
|
70
|
-
// a little time to migrate to the newer RCs of @babel/core without getting
|
71
|
-
// hard-to-diagnose errors about unknown 'caller' options.
|
72
|
-
|
73
|
-
|
74
|
-
let supportsCallerOptionFlag = undefined;
|
75
|
-
|
76
|
-
function supportsCallerOption() {
|
77
|
-
if (supportsCallerOptionFlag === undefined) {
|
78
|
-
try {
|
79
|
-
// Rather than try to match the Babel version, we just see if it throws
|
80
|
-
// when passed a 'caller' flag, and use that to decide if it is supported.
|
81
|
-
babel.loadPartialConfig({
|
82
|
-
caller: undefined,
|
83
|
-
babelrc: false,
|
84
|
-
configFile: false
|
85
|
-
});
|
86
|
-
supportsCallerOptionFlag = true;
|
87
|
-
} catch (err) {
|
88
|
-
supportsCallerOptionFlag = false;
|
89
|
-
}
|
90
|
-
}
|
91
|
-
|
92
|
-
return supportsCallerOptionFlag;
|
93
|
-
}
|
59
|
+
module.exports.version = babel.version;
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "babel-loader",
|
3
|
-
"version": "8.0.
|
3
|
+
"version": "8.0.4",
|
4
4
|
"description": "babel module loader for webpack",
|
5
5
|
"files": [
|
6
6
|
"lib"
|
@@ -20,16 +20,16 @@
|
|
20
20
|
"webpack": ">=2"
|
21
21
|
},
|
22
22
|
"devDependencies": {
|
23
|
-
"@babel/cli": "^7.0.0
|
24
|
-
"@babel/core": "^7.0.0
|
25
|
-
"@babel/preset-env": "^7.0.0
|
23
|
+
"@babel/cli": "^7.0.0",
|
24
|
+
"@babel/core": "^7.0.0",
|
25
|
+
"@babel/preset-env": "^7.0.0",
|
26
26
|
"ava": "0.25.0",
|
27
27
|
"babel-eslint": "^8.0.0",
|
28
28
|
"babel-plugin-istanbul": "^4.0.0",
|
29
29
|
"babel-plugin-react-intl": "^2.1.3",
|
30
30
|
"cross-env": "^5.0.0",
|
31
31
|
"eslint": "^4.1.0",
|
32
|
-
"eslint-config-babel": "^
|
32
|
+
"eslint-config-babel": "^8.0.0",
|
33
33
|
"eslint-plugin-flowtype": "^2.25.0",
|
34
34
|
"eslint-plugin-prettier": "^2.1.2",
|
35
35
|
"husky": "^0.14.0",
|
package/lib/utils/relative.js
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
|
3
|
-
const path = require("path");
|
4
|
-
|
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;
|
12
|
-
}
|
13
|
-
|
14
|
-
return path.relative(root, file);
|
15
|
-
};
|