Skip to content

Commit

Permalink
backport #1034 to 9.x (#1037)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung authored Sep 16, 2024
1 parent e449287 commit a1a24ca
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,28 @@ const handleCache = async function (directory, params) {
cacheIdentifier,
cacheDirectory,
cacheCompression,
logger,
} = params;

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

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();

// 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 @@ -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) {
Expand Down
22 changes: 20 additions & 2 deletions 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();
validateOptions(schema, loaderOptions, {
Expand All @@ -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,
Expand All @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -185,22 +192,29 @@ async function loader(source, inputSourceMap, overrides) {

let result;
if (cacheDirectory) {
logger.debug("cache is enabled");
result = await cache({
source,
options,
transform,
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,
Expand All @@ -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];
Expand Down
31 changes: 31 additions & 0 deletions test/cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.+/,
);
});
17 changes: 17 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/,
);
});

0 comments on commit a1a24ca

Please sign in to comment.