Skip to content

Commit

Permalink
Pass external dependencies from Babel to Webpack (#971)
Browse files Browse the repository at this point in the history
Backport of 6348e1c
  • Loading branch information
nicolo-ribaudo committed Nov 3, 2022
1 parent df28fe3 commit 493ac6c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
20 changes: 12 additions & 8 deletions src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
12 changes: 10 additions & 2 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
35 changes: 35 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

0 comments on commit 493ac6c

Please sign in to comment.