babel-loader 9.1.3 → 9.2.1
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 +7 -1
- package/lib/cache.js +11 -3
- package/lib/index.js +23 -4
- package/lib/injectCaller.js +1 -25
- package/package.json +21 -30
package/README.md
CHANGED
@@ -82,11 +82,17 @@ module: {
|
|
82
82
|
}
|
83
83
|
```
|
84
84
|
|
85
|
+
The `options` passed here will be [merged](https://babeljs.io/docs/configuration#how-babel-merges-config-items) with Babel config files, e.g. `babel.config.js` or `.babelrc`.
|
86
|
+
|
85
87
|
This loader also supports the following loader-specific option:
|
86
88
|
|
87
89
|
* `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 set to `true` in options (`{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
90
|
|
89
|
-
* `cacheIdentifier`: Default is a string composed by
|
91
|
+
* `cacheIdentifier`: Default is a string composed by
|
92
|
+
- the `@babel/core`'s version and the `babel-loader`'s version
|
93
|
+
- the [merged](https://babeljs.io/docs/configuration#how-babel-merges-config-items) [Babel config](https://babeljs.io/docs/config-files), including options passed to `babel-loader` and the contents of `babel.config.js` or `.babelrc` file if they exist
|
94
|
+
- the value of the environment variable `BABEL_ENV` with a fallback to the `NODE_ENV` environment variable.
|
95
|
+
This can be set to a custom value to force cache busting if the identifier changes.
|
90
96
|
|
91
97
|
* `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.
|
92
98
|
|
package/lib/cache.js
CHANGED
@@ -27,7 +27,7 @@ let hashType = "sha256";
|
|
27
27
|
// use md5 hashing if sha256 is not available
|
28
28
|
try {
|
29
29
|
crypto.createHash(hashType);
|
30
|
-
} catch
|
30
|
+
} catch {
|
31
31
|
hashType = "md5";
|
32
32
|
}
|
33
33
|
const gunzip = promisify(zlib.gunzip);
|
@@ -91,19 +91,25 @@ const handleCache = async function (directory, params) {
|
|
91
91
|
options = {},
|
92
92
|
cacheIdentifier,
|
93
93
|
cacheDirectory,
|
94
|
-
cacheCompression
|
94
|
+
cacheCompression,
|
95
|
+
logger
|
95
96
|
} = params;
|
96
97
|
const file = path.join(directory, filename(source, cacheIdentifier, options));
|
97
98
|
try {
|
98
99
|
// No errors mean that the file was previously cached
|
99
100
|
// we just need to return it
|
101
|
+
logger.debug(`reading cache file '${file}'`);
|
100
102
|
return await read(file, cacheCompression);
|
101
|
-
} catch
|
103
|
+
} catch {
|
104
|
+
// conitnue if cache can't be read
|
105
|
+
logger.debug(`discarded cache as it can not be read`);
|
106
|
+
}
|
102
107
|
const fallback = typeof cacheDirectory !== "string" && directory !== os.tmpdir();
|
103
108
|
|
104
109
|
// Make sure the directory exists.
|
105
110
|
try {
|
106
111
|
// overwrite directory if exists
|
112
|
+
logger.debug(`creating cache folder '${directory}'`);
|
107
113
|
await mkdir(directory, {
|
108
114
|
recursive: true
|
109
115
|
});
|
@@ -116,12 +122,14 @@ const handleCache = async function (directory, params) {
|
|
116
122
|
|
117
123
|
// Otherwise just transform the file
|
118
124
|
// return it to the user asap and write it in cache
|
125
|
+
logger.debug(`applying Babel transform`);
|
119
126
|
const result = await transform(source, options);
|
120
127
|
|
121
128
|
// Do not cache if there are external dependencies,
|
122
129
|
// since they might change and we cannot control it.
|
123
130
|
if (!result.externalDependencies.length) {
|
124
131
|
try {
|
132
|
+
logger.debug(`writing result to cache file '${file}'`);
|
125
133
|
await write(file, cacheCompression, result);
|
126
134
|
} catch (err) {
|
127
135
|
if (fallback) {
|
package/lib/index.js
CHANGED
@@ -41,6 +41,9 @@ function makeLoader(callback) {
|
|
41
41
|
}
|
42
42
|
async function loader(source, inputSourceMap, overrides) {
|
43
43
|
const filename = this.resourcePath;
|
44
|
+
const logger = typeof this.getLogger === "function" ? this.getLogger("babel-loader") : {
|
45
|
+
debug: () => {}
|
46
|
+
};
|
44
47
|
let loaderOptions = this.getOptions();
|
45
48
|
validateOptions(schema, loaderOptions, {
|
46
49
|
name: "Babel loader"
|
@@ -55,15 +58,18 @@ async function loader(source, inputSourceMap, overrides) {
|
|
55
58
|
if (overrides) {
|
56
59
|
throw new Error("babel-loader's 'customize' option is not available when already " + "using a customized babel-loader wrapper.");
|
57
60
|
}
|
61
|
+
logger.debug(`loading customize override: '${loaderOptions.customize}'`);
|
58
62
|
let override = require(loaderOptions.customize);
|
59
63
|
if (override.__esModule) override = override.default;
|
60
64
|
if (typeof override !== "function") {
|
61
65
|
throw new Error("Custom overrides must be functions.");
|
62
66
|
}
|
67
|
+
logger.debug("applying customize override to @babel/core");
|
63
68
|
overrides = override(babel);
|
64
69
|
}
|
65
70
|
let customOptions;
|
66
71
|
if (overrides && overrides.customOptions) {
|
72
|
+
logger.debug("applying overrides customOptions() to loader options");
|
67
73
|
const result = await overrides.customOptions.call(this, loaderOptions, {
|
68
74
|
source,
|
69
75
|
map: inputSourceMap
|
@@ -79,7 +85,7 @@ async function loader(source, inputSourceMap, overrides) {
|
|
79
85
|
if (typeof loaderOptions.babelrc === "string") {
|
80
86
|
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");
|
81
87
|
}
|
82
|
-
|
88
|
+
logger.debug("normalizing loader options");
|
83
89
|
// Standardize on 'sourceMaps' as the key passed through to Webpack, so that
|
84
90
|
// users may safely use either one alongside our default use of
|
85
91
|
// 'this.sourceMap' below without getting error about conflicting aliases.
|
@@ -106,10 +112,12 @@ async function loader(source, inputSourceMap, overrides) {
|
|
106
112
|
delete programmaticOptions.cacheIdentifier;
|
107
113
|
delete programmaticOptions.cacheCompression;
|
108
114
|
delete programmaticOptions.metadataSubscribers;
|
115
|
+
logger.debug("resolving Babel configs");
|
109
116
|
const config = await babel.loadPartialConfigAsync(injectCaller(programmaticOptions, this.target));
|
110
117
|
if (config) {
|
111
118
|
let options = config.options;
|
112
119
|
if (overrides && overrides.config) {
|
120
|
+
logger.debug("applying overrides config() to Babel config");
|
113
121
|
options = await overrides.config.call(this, config, {
|
114
122
|
source,
|
115
123
|
map: inputSourceMap,
|
@@ -137,20 +145,27 @@ async function loader(source, inputSourceMap, overrides) {
|
|
137
145
|
} = loaderOptions;
|
138
146
|
let result;
|
139
147
|
if (cacheDirectory) {
|
148
|
+
logger.debug("cache is enabled");
|
140
149
|
result = await cache({
|
141
150
|
source,
|
142
151
|
options,
|
143
152
|
transform,
|
144
153
|
cacheDirectory,
|
145
154
|
cacheIdentifier,
|
146
|
-
cacheCompression
|
155
|
+
cacheCompression,
|
156
|
+
logger
|
147
157
|
});
|
148
158
|
} else {
|
159
|
+
logger.debug("cache is disabled, applying Babel transform");
|
149
160
|
result = await transform(source, options);
|
150
161
|
}
|
151
|
-
config.files.forEach(configFile =>
|
162
|
+
config.files.forEach(configFile => {
|
163
|
+
this.addDependency(configFile);
|
164
|
+
logger.debug(`added '${configFile}' to webpack dependencies`);
|
165
|
+
});
|
152
166
|
if (result) {
|
153
167
|
if (overrides && overrides.result) {
|
168
|
+
logger.debug("applying overrides result() to Babel transform results");
|
154
169
|
result = await overrides.result.call(this, result, {
|
155
170
|
source,
|
156
171
|
map: inputSourceMap,
|
@@ -165,9 +180,13 @@ async function loader(source, inputSourceMap, overrides) {
|
|
165
180
|
metadata,
|
166
181
|
externalDependencies
|
167
182
|
} = result;
|
168
|
-
externalDependencies
|
183
|
+
externalDependencies?.forEach(dep => {
|
184
|
+
this.addDependency(dep);
|
185
|
+
logger.debug(`added '${dep}' to webpack dependencies`);
|
186
|
+
});
|
169
187
|
metadataSubscribers.forEach(subscriber => {
|
170
188
|
subscribe(subscriber, metadata, this);
|
189
|
+
logger.debug(`invoked metadata subscriber '${String(subscriber)}'`);
|
171
190
|
});
|
172
191
|
return [code, map];
|
173
192
|
}
|
package/lib/injectCaller.js
CHANGED
@@ -1,6 +1,4 @@
|
|
1
|
-
const babel = require("@babel/core");
|
2
1
|
module.exports = function injectCaller(opts, target) {
|
3
|
-
if (!supportsCallerOption()) return opts;
|
4
2
|
return Object.assign({}, opts, {
|
5
3
|
caller: Object.assign({
|
6
4
|
name: "babel-loader",
|
@@ -16,26 +14,4 @@ module.exports = function injectCaller(opts, target) {
|
|
16
14
|
supportsTopLevelAwait: true
|
17
15
|
}, opts.caller)
|
18
16
|
});
|
19
|
-
};
|
20
|
-
|
21
|
-
// TODO: We can remove this eventually, I'm just adding it so that people have
|
22
|
-
// a little time to migrate to the newer RCs of @babel/core without getting
|
23
|
-
// hard-to-diagnose errors about unknown 'caller' options.
|
24
|
-
let supportsCallerOptionFlag = undefined;
|
25
|
-
function supportsCallerOption() {
|
26
|
-
if (supportsCallerOptionFlag === undefined) {
|
27
|
-
try {
|
28
|
-
// Rather than try to match the Babel version, we just see if it throws
|
29
|
-
// when passed a 'caller' flag, and use that to decide if it is supported.
|
30
|
-
babel.loadPartialConfig({
|
31
|
-
caller: undefined,
|
32
|
-
babelrc: false,
|
33
|
-
configFile: false
|
34
|
-
});
|
35
|
-
supportsCallerOptionFlag = true;
|
36
|
-
} catch (err) {
|
37
|
-
supportsCallerOptionFlag = false;
|
38
|
-
}
|
39
|
-
}
|
40
|
-
return supportsCallerOptionFlag;
|
41
|
-
}
|
17
|
+
};
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "babel-loader",
|
3
|
-
"version": "9.1
|
3
|
+
"version": "9.2.1",
|
4
4
|
"description": "babel module loader for webpack",
|
5
5
|
"files": [
|
6
6
|
"lib"
|
@@ -19,41 +19,34 @@
|
|
19
19
|
},
|
20
20
|
"devDependencies": {
|
21
21
|
"@ava/babel": "^1.0.1",
|
22
|
-
"@babel/cli": "^7.
|
23
|
-
"@babel/core": "^7.
|
24
|
-
"@babel/
|
22
|
+
"@babel/cli": "^7.23.0",
|
23
|
+
"@babel/core": "^7.23.3",
|
24
|
+
"@babel/eslint-parser": "^7.23.3",
|
25
|
+
"@babel/preset-env": "^7.23.3",
|
25
26
|
"ava": "^3.13.0",
|
26
|
-
"
|
27
|
-
"
|
28
|
-
"
|
29
|
-
"
|
30
|
-
"
|
31
|
-
"
|
32
|
-
"
|
33
|
-
"
|
34
|
-
"
|
35
|
-
"husky": "^4.3.0",
|
36
|
-
"lint-staged": "^10.5.1",
|
37
|
-
"nyc": "^15.1.0",
|
38
|
-
"pnp-webpack-plugin": "^1.6.4",
|
39
|
-
"prettier": "^2.1.2",
|
40
|
-
"react": "^17.0.1",
|
41
|
-
"react-intl": "^5.9.4",
|
42
|
-
"react-intl-webpack-plugin": "^0.3.0",
|
43
|
-
"rimraf": "^3.0.0",
|
44
|
-
"semver": "7.5.2",
|
45
|
-
"webpack": "^5.74.0"
|
27
|
+
"c8": "^8.0.0",
|
28
|
+
"eslint": "^9.6.0",
|
29
|
+
"eslint-config-prettier": "^9.1.0",
|
30
|
+
"eslint-plugin-prettier": "^5.1.3",
|
31
|
+
"globals": "^15.8.0",
|
32
|
+
"husky": "^8.0.3",
|
33
|
+
"lint-staged": "^13.2.3",
|
34
|
+
"prettier": "^3.0.0",
|
35
|
+
"webpack": "^5.89.0"
|
46
36
|
},
|
47
37
|
"scripts": {
|
48
|
-
"clean": "rimraf lib
|
38
|
+
"clean": "node ./scripts/rimraf.mjs lib",
|
49
39
|
"build": "babel src/ --out-dir lib/ --copy-files",
|
50
40
|
"format": "prettier --write --trailing-comma all 'src/**/*.js' 'test/**/*.test.js' 'test/helpers/*.js' && prettier --write --trailing-comma es5 'scripts/*.js'",
|
51
41
|
"lint": "eslint src test",
|
52
42
|
"precommit": "lint-staged",
|
53
43
|
"prepublish": "yarn run clean && yarn run build",
|
54
44
|
"preversion": "yarn run test",
|
55
|
-
"test": "yarn run lint &&
|
56
|
-
"test-only": "
|
45
|
+
"test": "yarn run lint && yarn run build --source-maps && c8 yarn run test-only",
|
46
|
+
"test-only": "ava"
|
47
|
+
},
|
48
|
+
"resolutions": {
|
49
|
+
"minipass": "6.0.2"
|
57
50
|
},
|
58
51
|
"repository": {
|
59
52
|
"type": "git",
|
@@ -119,7 +112,5 @@
|
|
119
112
|
"git add yarn.lock"
|
120
113
|
]
|
121
114
|
},
|
122
|
-
"
|
123
|
-
"nyc/node-preload": "0.2.0"
|
124
|
-
}
|
115
|
+
"packageManager": "yarn@3.6.4"
|
125
116
|
}
|