Skip to content

Commit

Permalink
feat: add babel-loader debug logger
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Aug 27, 2024
1 parent 0854dab commit 720efae
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ const handleCache = async function (directory, params) {
cacheCompression,
hash,
getFileTimestamp,
logger,
} = params;

const file = path.join(
Expand All @@ -121,17 +122,23 @@ 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(
result.externalDependencies,
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 =
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
18 changes: 17 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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);
});
Expand All @@ -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,
Expand All @@ -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];
Expand Down
31 changes: 31 additions & 0 deletions test/cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/,
);
});
17 changes: 17 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/,
);
});

0 comments on commit 720efae

Please sign in to comment.