Skip to content

Commit 493ac6c

Browse files
Pass external dependencies from Babel to Webpack (#971)
Backport of 6348e1c
1 parent df28fe3 commit 493ac6c

File tree

4 files changed

+59
-11
lines changed

4 files changed

+59
-11
lines changed

src/cache.js

+12-8
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,19 @@ const handleCache = async function (directory, params) {
121121
// return it to the user asap and write it in cache
122122
const result = await transform(source, options);
123123

124-
try {
125-
await write(file, cacheCompression, result);
126-
} catch (err) {
127-
if (fallback) {
128-
// Fallback to tmpdir if node_modules folder not writable
129-
return handleCache(os.tmpdir(), params);
124+
// Do not cache if there are external dependencies,
125+
// since they might change and we cannot control it.
126+
if (!result.externalDependencies.length) {
127+
try {
128+
await write(file, cacheCompression, result);
129+
} catch (err) {
130+
if (fallback) {
131+
// Fallback to tmpdir if node_modules folder not writable
132+
return handleCache(os.tmpdir(), params);
133+
}
134+
135+
throw err;
130136
}
131-
132-
throw err;
133137
}
134138

135139
return result;

src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,9 @@ async function loader(source, inputSourceMap, overrides) {
235235
});
236236
}
237237

238-
const { code, map, metadata } = result;
238+
const { code, map, metadata, externalDependencies } = result;
239239

240+
externalDependencies?.forEach(dep => this.addDependency(dep));
240241
metadataSubscribers.forEach(subscriber => {
241242
subscribe(subscriber, metadata, this);
242243
});

src/transform.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,21 @@ module.exports = async function (source, options) {
1919
// https://github.com/babel/babel/blob/main/packages/babel-core/src/transformation/index.js
2020
// For discussion on this topic see here:
2121
// https://github.com/babel/babel-loader/pull/629
22-
const { ast, code, map, metadata, sourceType } = result;
22+
const { ast, code, map, metadata, sourceType, externalDependencies } = result;
2323

2424
if (map && (!map.sourcesContent || !map.sourcesContent.length)) {
2525
map.sourcesContent = [source];
2626
}
2727

28-
return { ast, code, map, metadata, sourceType };
28+
return {
29+
ast,
30+
code,
31+
map,
32+
metadata,
33+
sourceType,
34+
// Convert it from a Set to an Array to make it JSON-serializable.
35+
externalDependencies: Array.from(externalDependencies || []),
36+
};
2937
};
3038

3139
module.exports.version = babel.version;

test/loader.test.js

+35
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,38 @@ test.cb("should load ESM config files", t => {
181181
t.end();
182182
});
183183
});
184+
185+
test.cb("should track external dependencies", t => {
186+
const dep = path.join(__dirname, "fixtures/metadata.js");
187+
const config = Object.assign({}, globalConfig, {
188+
entry: path.join(__dirname, "fixtures/constant.js"),
189+
output: {
190+
path: t.context.directory,
191+
},
192+
module: {
193+
rules: [
194+
{
195+
test: /\.js$/,
196+
loader: babelLoader,
197+
options: {
198+
babelrc: false,
199+
configFile: false,
200+
plugins: [
201+
api => {
202+
api.cache.never();
203+
api.addExternalDependency(dep);
204+
return { visitor: {} };
205+
},
206+
],
207+
},
208+
},
209+
],
210+
},
211+
});
212+
213+
webpack(config, (err, stats) => {
214+
t.true(stats.compilation.fileDependencies.has(dep));
215+
t.deepEqual(stats.compilation.warnings, []);
216+
t.end();
217+
});
218+
});

0 commit comments

Comments
 (0)