From 493ac6cfdb2763568b42e787b95ac3a05ebfd15b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Tue, 1 Nov 2022 22:00:33 +0100 Subject: [PATCH] Pass external dependencies from Babel to Webpack (#971) Backport of 6348e1cb323bb3d57492cc72c964717c2e3699b7 --- src/cache.js | 20 ++++++++++++-------- src/index.js | 3 ++- src/transform.js | 12 ++++++++++-- test/loader.test.js | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 11 deletions(-) diff --git a/src/cache.js b/src/cache.js index 2e80e3e9..2078a058 100644 --- a/src/cache.js +++ b/src/cache.js @@ -121,15 +121,19 @@ const handleCache = async function (directory, params) { // return it to the user asap and write it in cache const result = await transform(source, options); - try { - await write(file, cacheCompression, result); - } catch (err) { - if (fallback) { - // Fallback to tmpdir if node_modules folder not writable - return handleCache(os.tmpdir(), params); + // Do not cache if there are external dependencies, + // since they might change and we cannot control it. + if (!result.externalDependencies.length) { + try { + await write(file, cacheCompression, result); + } catch (err) { + if (fallback) { + // Fallback to tmpdir if node_modules folder not writable + return handleCache(os.tmpdir(), params); + } + + throw err; } - - throw err; } return result; diff --git a/src/index.js b/src/index.js index 41d4d46b..836152be 100644 --- a/src/index.js +++ b/src/index.js @@ -235,8 +235,9 @@ async function loader(source, inputSourceMap, overrides) { }); } - const { code, map, metadata } = result; + const { code, map, metadata, externalDependencies } = result; + externalDependencies?.forEach(dep => this.addDependency(dep)); metadataSubscribers.forEach(subscriber => { subscribe(subscriber, metadata, this); }); diff --git a/src/transform.js b/src/transform.js index 8905eb9f..bc0c88d0 100644 --- a/src/transform.js +++ b/src/transform.js @@ -19,13 +19,21 @@ module.exports = async function (source, options) { // https://github.com/babel/babel/blob/main/packages/babel-core/src/transformation/index.js // For discussion on this topic see here: // https://github.com/babel/babel-loader/pull/629 - const { ast, code, map, metadata, sourceType } = result; + const { ast, code, map, metadata, sourceType, externalDependencies } = result; if (map && (!map.sourcesContent || !map.sourcesContent.length)) { map.sourcesContent = [source]; } - return { ast, code, map, metadata, sourceType }; + return { + ast, + code, + map, + metadata, + sourceType, + // Convert it from a Set to an Array to make it JSON-serializable. + externalDependencies: Array.from(externalDependencies || []), + }; }; module.exports.version = babel.version; diff --git a/test/loader.test.js b/test/loader.test.js index 14ab18a2..a5449985 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -181,3 +181,38 @@ test.cb("should load ESM config files", t => { t.end(); }); }); + +test.cb("should track external dependencies", t => { + const dep = path.join(__dirname, "fixtures/metadata.js"); + const config = Object.assign({}, globalConfig, { + entry: path.join(__dirname, "fixtures/constant.js"), + output: { + path: t.context.directory, + }, + module: { + rules: [ + { + test: /\.js$/, + loader: babelLoader, + options: { + babelrc: false, + configFile: false, + plugins: [ + api => { + api.cache.never(); + api.addExternalDependency(dep); + return { visitor: {} }; + }, + ], + }, + }, + ], + }, + }); + + webpack(config, (err, stats) => { + t.true(stats.compilation.fileDependencies.has(dep)); + t.deepEqual(stats.compilation.warnings, []); + t.end(); + }); +});