Skip to content

Commit 6348e1c

Browse files
Pass external dependencies from Babel to Webpack (#971)
1 parent 457b9ad commit 6348e1c

File tree

4 files changed

+59
-11
lines changed

4 files changed

+59
-11
lines changed

src/cache.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,19 @@ const handleCache = async function (directory, params) {
119119
// return it to the user asap and write it in cache
120120
const result = await transform(source, options);
121121

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

133137
return result;

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,9 @@ async function loader(source, inputSourceMap, overrides) {
210210
});
211211
}
212212

213-
const { code, map, metadata } = result;
213+
const { code, map, metadata, externalDependencies } = result;
214214

215+
externalDependencies?.forEach(dep => this.addDependency(dep));
215216
metadataSubscribers.forEach(subscriber => {
216217
subscribe(subscriber, metadata, this);
217218
});

src/transform.js

Lines changed: 10 additions & 2 deletions
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

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

0 commit comments

Comments
 (0)