From 720efaee80d8fd56e95266dec2256ba9b15e06ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 27 Aug 2024 13:48:40 -0400 Subject: [PATCH] feat: add babel-loader debug logger --- src/cache.js | 10 ++++++++++ src/index.js | 18 +++++++++++++++++- test/cache.test.js | 31 +++++++++++++++++++++++++++++++ test/loader.test.js | 17 +++++++++++++++++ 4 files changed, 75 insertions(+), 1 deletion(-) diff --git a/src/cache.js b/src/cache.js index 868268c2..4a703088 100644 --- a/src/cache.js +++ b/src/cache.js @@ -111,6 +111,7 @@ const handleCache = async function (directory, params) { cacheCompression, hash, getFileTimestamp, + logger, } = params; const file = path.join( @@ -121,6 +122,7 @@ 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}'`); const result = await read(file, cacheCompression); if ( !(await areExternalDependenciesModified( @@ -128,10 +130,15 @@ const handleCache = async function (directory, params) { getFileTimestamp, )) ) { + logger.debug(`validated cache file '${file}'`); return result; } + logger.debug( + `discarded cache file '${file}' due to changes in external dependencies`, + ); } catch { // conitnue if cache can't be read + logger.debug(`discarded cache as it can not be read`); } const fallback = @@ -140,6 +147,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) { @@ -151,10 +159,12 @@ 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); await addTimestamps(result.externalDependencies, getFileTimestamp); 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 d140323b..96e5fd48 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(schema); @@ -70,17 +71,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, @@ -109,6 +113,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. @@ -145,12 +150,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, @@ -177,6 +184,7 @@ async function loader(source, inputSourceMap, overrides) { let result; if (cacheDirectory) { + logger.debug("cache is enabled"); const getFileTimestamp = promisify((path, cb) => { this._compilation.fileSystemInfo.getFileTimestamp(path, cb); }); @@ -192,15 +200,21 @@ async function loader(source, inputSourceMap, overrides) { cacheCompression, hash, getFileTimestamp, + 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, @@ -214,9 +228,11 @@ async function loader(source, inputSourceMap, overrides) { 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 a8b6f067..eb7b4efe 100644 --- a/test/cache.test.js +++ b/test/cache.test.js @@ -400,3 +400,34 @@ test("should cache result when there are external dependencies", async () => { assert.ok(stats.compilation.fileDependencies.has(dep)); assert.strictEqual(counter, 2); }); + +test("should output debug logs when stats.loggingDebug includes babel-loader", async () => { + const config = Object.assign({}, globalConfig, { + output: { + path: 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); + + assert.match( + 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.+\n\s+applying Babel transform\n\s+writing result to cache file.+\n\s+added '.+babel.config.json' to webpack dependencies/, + ); +}); diff --git a/test/loader.test.js b/test/loader.test.js index cc22e165..d672daac 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -199,3 +199,20 @@ test("should track external dependencies", async () => { assert.ok(stats.compilation.fileDependencies.has(dep)); assert.deepEqual(stats.compilation.warnings, []); }); + +test("should output debug logs when stats.loggingDebug includes babel-loader", async () => { + const config = Object.assign({}, globalConfig, { + output: { + path: context.directory, + }, + stats: { + loggingDebug: ["babel-loader"], + }, + }); + + const stats = await webpackAsync(config); + assert.match( + stats.toString(config.stats), + /normalizing loader options\n\s+resolving Babel configs\n\s+cache is disabled, applying Babel transform/, + ); +});