Skip to content

Commit 945b7ed

Browse files
authored
Backport #1037 to 8.x (#1039)
1 parent 55eb044 commit 945b7ed

File tree

7 files changed

+394
-848
lines changed

7 files changed

+394
-848
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
- uses: actions/checkout@v2
1111
- uses: actions/setup-node@v1
1212
with:
13-
node-version: "*"
13+
node-version: "18.x"
1414
- name: Install dependencies
1515
run: yarn
1616
- name: Lint

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"react-intl-webpack-plugin": "^0.3.0",
4545
"rimraf": "^3.0.0",
4646
"semver": "7.3.2",
47-
"webpack": "^5.34.0"
47+
"webpack": "^5.61.0"
4848
},
4949
"scripts": {
5050
"clean": "rimraf lib/",

src/cache.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,27 @@ const handleCache = async function (directory, params) {
9393
cacheIdentifier,
9494
cacheDirectory,
9595
cacheCompression,
96+
logger,
9697
} = params;
9798

9899
const file = path.join(directory, filename(source, cacheIdentifier, options));
99100

100101
try {
101102
// No errors mean that the file was previously cached
102103
// we just need to return it
104+
logger.debug(`reading cache file '${file}'`);
103105
return await read(file, cacheCompression);
104-
} catch (err) {}
106+
} catch (err) {
107+
// conitnue if cache can't be read
108+
logger.debug(`discarded cache as it can not be read`);
109+
}
105110

106111
const fallback =
107112
typeof cacheDirectory !== "string" && directory !== os.tmpdir();
108113

109114
// Make sure the directory exists.
110115
try {
116+
logger.debug(`creating cache folder '${directory}'`);
111117
await makeDir(directory);
112118
} catch (err) {
113119
if (fallback) {
@@ -119,12 +125,14 @@ const handleCache = async function (directory, params) {
119125

120126
// Otherwise just transform the file
121127
// return it to the user asap and write it in cache
128+
logger.debug(`applying Babel transform`);
122129
const result = await transform(source, options);
123130

124131
// Do not cache if there are external dependencies,
125132
// since they might change and we cannot control it.
126133
if (!result.externalDependencies.length) {
127134
try {
135+
logger.debug(`writing result to cache file '${file}'`);
128136
await write(file, cacheCompression, result);
129137
} catch (err) {
130138
if (fallback) {

src/index.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ function makeLoader(callback) {
5454

5555
async function loader(source, inputSourceMap, overrides) {
5656
const filename = this.resourcePath;
57+
const logger = this.getLogger("babel-loader");
5758

5859
let loaderOptions = loaderUtils.getOptions(this);
5960

@@ -80,17 +81,20 @@ async function loader(source, inputSourceMap, overrides) {
8081
);
8182
}
8283

84+
logger.debug(`loading customize override: '${loaderOptions.customize}'`);
8385
let override = require(loaderOptions.customize);
8486
if (override.__esModule) override = override.default;
8587

8688
if (typeof override !== "function") {
8789
throw new Error("Custom overrides must be functions.");
8890
}
91+
logger.debug("applying customize override to @babel/core");
8992
overrides = override(babel);
9093
}
9194

9295
let customOptions;
9396
if (overrides && overrides.customOptions) {
97+
logger.debug("applying overrides customOptions() to loader options");
9498
const result = await overrides.customOptions.call(this, loaderOptions, {
9599
source,
96100
map: inputSourceMap,
@@ -115,6 +119,7 @@ async function loader(source, inputSourceMap, overrides) {
115119
);
116120
}
117121

122+
logger.debug("normalizing loader options");
118123
// Standardize on 'sourceMaps' as the key passed through to Webpack, so that
119124
// users may safely use either one alongside our default use of
120125
// 'this.sourceMap' below without getting error about conflicting aliases.
@@ -161,12 +166,14 @@ async function loader(source, inputSourceMap, overrides) {
161166

162167
// babel.loadPartialConfigAsync is available in v7.8.0+
163168
const { loadPartialConfigAsync = babel.loadPartialConfig } = babel;
169+
logger.debug("resolving Babel configs");
164170
const config = await loadPartialConfigAsync(
165171
injectCaller(programmaticOptions, this.target),
166172
);
167173
if (config) {
168174
let options = config.options;
169175
if (overrides && overrides.config) {
176+
logger.debug("applying overrides config() to Babel config");
170177
options = await overrides.config.call(this, config, {
171178
source,
172179
map: inputSourceMap,
@@ -197,35 +204,44 @@ async function loader(source, inputSourceMap, overrides) {
197204

198205
let result;
199206
if (cacheDirectory) {
207+
logger.debug("cache is enabled");
200208
result = await cache({
201209
source,
202210
options,
203211
transform,
204212
cacheDirectory,
205213
cacheIdentifier,
206214
cacheCompression,
215+
logger,
207216
});
208217
} else {
218+
logger.debug("cache is disabled, applying Babel transform");
209219
result = await transform(source, options);
210220
}
211221

212222
// Availabe since Babel 7.12
213223
// https://github.com/babel/babel/pull/11907
214224
if (config.files) {
215-
config.files.forEach(configFile => this.addDependency(configFile));
225+
config.files.forEach(configFile => {
226+
this.addDependency(configFile);
227+
logger.debug(`added '${configFile}' to webpack dependencies`);
228+
});
216229
} else {
217230
// .babelrc.json
218231
if (typeof config.babelrc === "string") {
219232
this.addDependency(config.babelrc);
233+
logger.debug(`added '${config.babelrc}' to webpack dependencies`);
220234
}
221235
// babel.config.js
222236
if (config.config) {
223237
this.addDependency(config.config);
238+
logger.debug(`added '${config.config}' to webpack dependencies`);
224239
}
225240
}
226241

227242
if (result) {
228243
if (overrides && overrides.result) {
244+
logger.debug("applying overrides result() to Babel transform results");
229245
result = await overrides.result.call(this, result, {
230246
source,
231247
map: inputSourceMap,
@@ -237,9 +253,13 @@ async function loader(source, inputSourceMap, overrides) {
237253

238254
const { code, map, metadata, externalDependencies } = result;
239255

240-
externalDependencies?.forEach(dep => this.addDependency(dep));
256+
externalDependencies?.forEach(dep => {
257+
this.addDependency(dep);
258+
logger.debug(`added '${dep}' to webpack dependencies`);
259+
});
241260
metadataSubscribers.forEach(subscriber => {
242261
subscribe(subscriber, metadata, this);
262+
logger.debug(`invoked metadata subscriber '${String(subscriber)}'`);
243263
});
244264

245265
return [code, map];

test/cache.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,3 +385,39 @@ test.cb("should allow to specify the .babelrc file", t => {
385385
});
386386
});
387387
});
388+
389+
test.cb(
390+
"should output debug logs when stats.loggingDebug includes babel-loader",
391+
t => {
392+
const config = Object.assign({}, globalConfig, {
393+
output: {
394+
path: t.context.directory,
395+
},
396+
module: {
397+
rules: [
398+
{
399+
test: /\.jsx?/,
400+
loader: babelLoader,
401+
exclude: /node_modules/,
402+
options: {
403+
cacheDirectory: true,
404+
presets: ["@babel/preset-env"],
405+
},
406+
},
407+
],
408+
},
409+
stats: {
410+
loggingDebug: ["babel-loader"],
411+
},
412+
});
413+
414+
webpack(config, (err, stats) => {
415+
t.is(err, null);
416+
t.regex(
417+
stats.toString(config.stats),
418+
/normalizing loader options\n\s+resolving Babel configs\n\s+cache is enabled\n\s+reading cache file.+\n\s+discarded cache as it can not be read\n\s+creating cache folder.+\n\s+applying Babel transform\n\s+writing result to cache file.+\n\s+added '.+babel.config.json' to webpack dependencies/,
419+
);
420+
t.end();
421+
});
422+
},
423+
);

test/loader.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,26 @@ test.cb("should track external dependencies", t => {
216216
t.end();
217217
});
218218
});
219+
220+
test.cb(
221+
"should output debug logs when stats.loggingDebug includes babel-loader",
222+
t => {
223+
const config = Object.assign({}, globalConfig, {
224+
output: {
225+
path: t.context.directory,
226+
},
227+
stats: {
228+
loggingDebug: ["babel-loader"],
229+
},
230+
});
231+
232+
webpack(config, (err, stats) => {
233+
t.is(err, null);
234+
t.regex(
235+
stats.toString(config.stats),
236+
/normalizing loader options\n\s+resolving Babel configs\n\s+cache is disabled, applying Babel transform/,
237+
);
238+
t.end();
239+
});
240+
},
241+
);

0 commit comments

Comments
 (0)