Skip to content

Commit a1a24ca

Browse files
authored
backport #1034 to 9.x (#1037)
1 parent e449287 commit a1a24ca

File tree

4 files changed

+77
-3
lines changed

4 files changed

+77
-3
lines changed

src/cache.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,22 +90,28 @@ const handleCache = async function (directory, params) {
9090
cacheIdentifier,
9191
cacheDirectory,
9292
cacheCompression,
93+
logger,
9394
} = params;
9495

9596
const file = path.join(directory, filename(source, cacheIdentifier, options));
9697

9798
try {
9899
// No errors mean that the file was previously cached
99100
// we just need to return it
101+
logger.debug(`reading cache file '${file}'`);
100102
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+
}
102107

103108
const fallback =
104109
typeof cacheDirectory !== "string" && directory !== os.tmpdir();
105110

106111
// Make sure the directory exists.
107112
try {
108113
// overwrite directory if exists
114+
logger.debug(`creating cache folder '${directory}'`);
109115
await mkdir(directory, { recursive: true });
110116
} catch (err) {
111117
if (fallback) {
@@ -117,12 +123,14 @@ const handleCache = async function (directory, params) {
117123

118124
// Otherwise just transform the file
119125
// return it to the user asap and write it in cache
126+
logger.debug(`applying Babel transform`);
120127
const result = await transform(source, options);
121128

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

src/index.js

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

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

5758
let loaderOptions = this.getOptions();
5859
validateOptions(schema, loaderOptions, {
@@ -78,17 +79,20 @@ async function loader(source, inputSourceMap, overrides) {
7879
);
7980
}
8081

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

8486
if (typeof override !== "function") {
8587
throw new Error("Custom overrides must be functions.");
8688
}
89+
logger.debug("applying customize override to @babel/core");
8790
overrides = override(babel);
8891
}
8992

9093
let customOptions;
9194
if (overrides && overrides.customOptions) {
95+
logger.debug("applying overrides customOptions() to loader options");
9296
const result = await overrides.customOptions.call(this, loaderOptions, {
9397
source,
9498
map: inputSourceMap,
@@ -113,6 +117,7 @@ async function loader(source, inputSourceMap, overrides) {
113117
);
114118
}
115119

120+
logger.debug("normalizing loader options");
116121
// Standardize on 'sourceMaps' as the key passed through to Webpack, so that
117122
// users may safely use either one alongside our default use of
118123
// 'this.sourceMap' below without getting error about conflicting aliases.
@@ -149,12 +154,14 @@ async function loader(source, inputSourceMap, overrides) {
149154
delete programmaticOptions.cacheCompression;
150155
delete programmaticOptions.metadataSubscribers;
151156

157+
logger.debug("resolving Babel configs");
152158
const config = await babel.loadPartialConfigAsync(
153159
injectCaller(programmaticOptions, this.target),
154160
);
155161
if (config) {
156162
let options = config.options;
157163
if (overrides && overrides.config) {
164+
logger.debug("applying overrides config() to Babel config");
158165
options = await overrides.config.call(this, config, {
159166
source,
160167
map: inputSourceMap,
@@ -185,22 +192,29 @@ async function loader(source, inputSourceMap, overrides) {
185192

186193
let result;
187194
if (cacheDirectory) {
195+
logger.debug("cache is enabled");
188196
result = await cache({
189197
source,
190198
options,
191199
transform,
192200
cacheDirectory,
193201
cacheIdentifier,
194202
cacheCompression,
203+
logger,
195204
});
196205
} else {
206+
logger.debug("cache is disabled, applying Babel transform");
197207
result = await transform(source, options);
198208
}
199209

200-
config.files.forEach(configFile => this.addDependency(configFile));
210+
config.files.forEach(configFile => {
211+
this.addDependency(configFile);
212+
logger.debug(`added '${configFile}' to webpack dependencies`);
213+
});
201214

202215
if (result) {
203216
if (overrides && overrides.result) {
217+
logger.debug("applying overrides result() to Babel transform results");
204218
result = await overrides.result.call(this, result, {
205219
source,
206220
map: inputSourceMap,
@@ -212,9 +226,13 @@ async function loader(source, inputSourceMap, overrides) {
212226

213227
const { code, map, metadata, externalDependencies } = result;
214228

215-
externalDependencies?.forEach(dep => this.addDependency(dep));
229+
externalDependencies?.forEach(dep => {
230+
this.addDependency(dep);
231+
logger.debug(`added '${dep}' to webpack dependencies`);
232+
});
216233
metadataSubscribers.forEach(subscriber => {
217234
subscribe(subscriber, metadata, this);
235+
logger.debug(`invoked metadata subscriber '${String(subscriber)}'`);
218236
});
219237

220238
return [code, map];

test/cache.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,34 @@ test("should allow to specify the .babelrc file", async t => {
325325
const files = fs.readdirSync(t.context.cacheDirectory);
326326
t.true(files.length === 2);
327327
});
328+
329+
test("should output debug logs when stats.loggingDebug includes babel-loader", async t => {
330+
const config = Object.assign({}, globalConfig, {
331+
output: {
332+
path: t.context.directory,
333+
},
334+
module: {
335+
rules: [
336+
{
337+
test: /\.jsx?/,
338+
loader: babelLoader,
339+
exclude: /node_modules/,
340+
options: {
341+
cacheDirectory: true,
342+
presets: ["@babel/preset-env"],
343+
},
344+
},
345+
],
346+
},
347+
stats: {
348+
loggingDebug: ["babel-loader"],
349+
},
350+
});
351+
352+
const stats = await webpackAsync(config);
353+
354+
t.regex(
355+
stats.toString(config.stats),
356+
/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.+/,
357+
);
358+
});

test/loader.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,20 @@ test("should track external dependencies", async t => {
194194
t.true(stats.compilation.fileDependencies.has(dep));
195195
t.deepEqual(stats.compilation.warnings, []);
196196
});
197+
198+
test("should output debug logs when stats.loggingDebug includes babel-loader", async t => {
199+
const config = Object.assign({}, globalConfig, {
200+
output: {
201+
path: t.context.directory,
202+
},
203+
stats: {
204+
loggingDebug: ["babel-loader"],
205+
},
206+
});
207+
208+
const stats = await webpackAsync(config);
209+
t.regex(
210+
stats.toString(config.stats),
211+
/normalizing loader options\n\s+resolving Babel configs\n\s+cache is disabled, applying Babel transform/,
212+
);
213+
});

0 commit comments

Comments
 (0)