From a1a24cac780ee13d91064342aa70921863c1462a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 16 Sep 2024 07:10:19 -0400 Subject: [PATCH] backport #1034 to 9.x (#1037) --- src/cache.js | 10 +++++++++- src/index.js | 22 ++++++++++++++++++++-- test/cache.test.js | 31 +++++++++++++++++++++++++++++++ test/loader.test.js | 17 +++++++++++++++++ 4 files changed, 77 insertions(+), 3 deletions(-) diff --git a/src/cache.js b/src/cache.js index 2444bb12..b0de55e0 100644 --- a/src/cache.js +++ b/src/cache.js @@ -90,6 +90,7 @@ const handleCache = async function (directory, params) { cacheIdentifier, cacheDirectory, cacheCompression, + logger, } = params; const file = path.join(directory, filename(source, cacheIdentifier, options)); @@ -97,8 +98,12 @@ const handleCache = async function (directory, params) { try { // No errors mean that the file was previously cached // we just need to return it + logger.debug(`reading cache file '${file}'`); return await read(file, cacheCompression); - } catch {} + } catch { + // conitnue if cache can't be read + logger.debug(`discarded cache as it can not be read`); + } const fallback = typeof cacheDirectory !== "string" && directory !== os.tmpdir(); @@ -106,6 +111,7 @@ const handleCache = async function (directory, params) { // Make sure the directory exists. try { // overwrite directory if exists + logger.debug(`creating cache folder '${directory}'`); await mkdir(directory, { recursive: true }); } catch (err) { if (fallback) { @@ -117,12 +123,14 @@ const handleCache = async function (directory, params) { // Otherwise just transform the file // return it to the user asap and write it in cache + logger.debug(`applying Babel transform`); const result = await transform(source, options); // Do not cache if there are external dependencies, // since they might change and we cannot control it. if (!result.externalDependencies.length) { try { + logger.debug(`writing result to cache file '${file}'`); await write(file, cacheCompression, result); } catch (err) { if (fallback) { diff --git a/src/index.js b/src/index.js index 5ba55e3e..05a23088 100644 --- a/src/index.js +++ b/src/index.js @@ -53,6 +53,7 @@ function makeLoader(callback) { async function loader(source, inputSourceMap, overrides) { const filename = this.resourcePath; + const logger = this.getLogger("babel-loader"); let loaderOptions = this.getOptions(); validateOptions(schema, loaderOptions, { @@ -78,17 +79,20 @@ async function loader(source, inputSourceMap, overrides) { ); } + logger.debug(`loading customize override: '${loaderOptions.customize}'`); let override = require(loaderOptions.customize); if (override.__esModule) override = override.default; if (typeof override !== "function") { throw new Error("Custom overrides must be functions."); } + logger.debug("applying customize override to @babel/core"); overrides = override(babel); } let customOptions; if (overrides && overrides.customOptions) { + logger.debug("applying overrides customOptions() to loader options"); const result = await overrides.customOptions.call(this, loaderOptions, { source, map: inputSourceMap, @@ -113,6 +117,7 @@ async function loader(source, inputSourceMap, overrides) { ); } + logger.debug("normalizing loader options"); // Standardize on 'sourceMaps' as the key passed through to Webpack, so that // users may safely use either one alongside our default use of // 'this.sourceMap' below without getting error about conflicting aliases. @@ -149,12 +154,14 @@ async function loader(source, inputSourceMap, overrides) { delete programmaticOptions.cacheCompression; delete programmaticOptions.metadataSubscribers; + logger.debug("resolving Babel configs"); const config = await babel.loadPartialConfigAsync( injectCaller(programmaticOptions, this.target), ); if (config) { let options = config.options; if (overrides && overrides.config) { + logger.debug("applying overrides config() to Babel config"); options = await overrides.config.call(this, config, { source, map: inputSourceMap, @@ -185,6 +192,7 @@ async function loader(source, inputSourceMap, overrides) { let result; if (cacheDirectory) { + logger.debug("cache is enabled"); result = await cache({ source, options, @@ -192,15 +200,21 @@ async function loader(source, inputSourceMap, overrides) { cacheDirectory, cacheIdentifier, cacheCompression, + logger, }); } else { + logger.debug("cache is disabled, applying Babel transform"); result = await transform(source, options); } - config.files.forEach(configFile => this.addDependency(configFile)); + config.files.forEach(configFile => { + this.addDependency(configFile); + logger.debug(`added '${configFile}' to webpack dependencies`); + }); if (result) { if (overrides && overrides.result) { + logger.debug("applying overrides result() to Babel transform results"); result = await overrides.result.call(this, result, { source, map: inputSourceMap, @@ -212,9 +226,13 @@ async function loader(source, inputSourceMap, overrides) { const { code, map, metadata, externalDependencies } = result; - externalDependencies?.forEach(dep => this.addDependency(dep)); + externalDependencies?.forEach(dep => { + this.addDependency(dep); + logger.debug(`added '${dep}' to webpack dependencies`); + }); metadataSubscribers.forEach(subscriber => { subscribe(subscriber, metadata, this); + logger.debug(`invoked metadata subscriber '${String(subscriber)}'`); }); return [code, map]; diff --git a/test/cache.test.js b/test/cache.test.js index 71290b36..ed38c035 100644 --- a/test/cache.test.js +++ b/test/cache.test.js @@ -325,3 +325,34 @@ test("should allow to specify the .babelrc file", async t => { const files = fs.readdirSync(t.context.cacheDirectory); t.true(files.length === 2); }); + +test("should output debug logs when stats.loggingDebug includes babel-loader", async t => { + const config = Object.assign({}, globalConfig, { + output: { + path: t.context.directory, + }, + module: { + rules: [ + { + test: /\.jsx?/, + loader: babelLoader, + exclude: /node_modules/, + options: { + cacheDirectory: true, + presets: ["@babel/preset-env"], + }, + }, + ], + }, + stats: { + loggingDebug: ["babel-loader"], + }, + }); + + const stats = await webpackAsync(config); + + t.regex( + stats.toString(config.stats), + /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.+/, + ); +}); diff --git a/test/loader.test.js b/test/loader.test.js index b622bb5f..49285e5d 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -194,3 +194,20 @@ test("should track external dependencies", async t => { t.true(stats.compilation.fileDependencies.has(dep)); t.deepEqual(stats.compilation.warnings, []); }); + +test("should output debug logs when stats.loggingDebug includes babel-loader", async t => { + const config = Object.assign({}, globalConfig, { + output: { + path: t.context.directory, + }, + stats: { + loggingDebug: ["babel-loader"], + }, + }); + + const stats = await webpackAsync(config); + t.regex( + stats.toString(config.stats), + /normalizing loader options\n\s+resolving Babel configs\n\s+cache is disabled, applying Babel transform/, + ); +});