Skip to content

Commit 1c16a2b

Browse files
committed
fix(commonjs): handle external dependencies when using the cache (#1038)
1 parent c583aaf commit 1c16a2b

File tree

5 files changed

+62
-3
lines changed

5 files changed

+62
-3
lines changed

packages/commonjs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
## Requirements
1515

16-
This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v8.0.0+) and Rollup v2.38.3+.
16+
This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v12.0.0+) and Rollup v2.68.0+. If you are using [`@rollup/plugin-node-resolve`](https://github.com/rollup/plugins/tree/master/packages/node-resolve), it should be v13.0.6+.
1717

1818
## Install
1919

packages/commonjs/src/resolve-require-sources.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export function getRequireResolver(extensions, detectCyclesAndConditional) {
100100
resolvedSources,
101101
meta: { commonjs: parentMeta }
102102
}) {
103-
// We explicitly track ES modules to handle ciruclar imports
103+
// We explicitly track ES modules to handle circular imports
104104
if (!(parentMeta && parentMeta.isCommonJS)) knownCjsModuleTypes[parentId] = false;
105105
if (isWrappedId(parentId, ES_IMPORT_SUFFIX)) return false;
106106
const parentRequires = parentMeta && parentMeta.requires;
@@ -135,7 +135,7 @@ export function getRequireResolver(extensions, detectCyclesAndConditional) {
135135
await Promise.all(
136136
Object.keys(resolvedSources)
137137
.map((source) => resolvedSources[source])
138-
.filter(({ id }) => !parentRequireSet.has(id))
138+
.filter(({ id, external }) => !(external || parentRequireSet.has(id)))
139139
.map(async (resolved) => {
140140
if (isWrappedId(resolved.id, ES_IMPORT_SUFFIX)) {
141141
return (

packages/commonjs/test/snapshots/test.js.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,22 @@ Generated by [AVA](https://avajs.dev).
316316
317317
console.log('main');␊
318318
`
319+
320+
## handles external dependencies when using the cache
321+
322+
> Snapshot 1
323+
324+
`'use strict';␊
325+
326+
var require$$0 = require('external');␊
327+
328+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }␊
329+
330+
var require$$0__default = /*#__PURE__*/_interopDefaultLegacy(require$$0);␊
331+
332+
var second = require$$0__default["default"].second;␊
333+
334+
var main = require$$0.first + second;␊
335+
336+
module.exports = main;␊
337+
`
144 Bytes
Binary file not shown.

packages/commonjs/test/test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,46 @@ test('handles ESM cycles when using the cache', async (t) => {
11471147
t.snapshot(await getCodeFromBundle(bundle));
11481148
});
11491149

1150+
test('handles external dependencies when using the cache', async (t) => {
1151+
const modules = {};
1152+
const resetModules = () => {
1153+
modules['main.js'] =
1154+
"import first from 'first.js';import second from 'second.js';export default first + second;";
1155+
modules['first.js'] = "export {first as default} from 'external';";
1156+
modules['second.js'] = "module.exports = require('external').second;";
1157+
};
1158+
const options = {
1159+
input: 'main.js',
1160+
external: ['external'],
1161+
plugins: [commonjs(), loader(modules)],
1162+
onwarn
1163+
};
1164+
1165+
resetModules();
1166+
let bundle = await rollup(options);
1167+
t.is(
1168+
(
1169+
await executeBundle(bundle, t, {
1170+
context: {
1171+
require(id) {
1172+
if (id === 'external') {
1173+
return { first: 'first', second: 'second' };
1174+
}
1175+
throw new Error(`Unexpected require "${id}"`);
1176+
}
1177+
}
1178+
})
1179+
).exports,
1180+
'firstsecond'
1181+
);
1182+
const code = await getCodeFromBundle(bundle);
1183+
t.snapshot(code);
1184+
1185+
options.cache = bundle.cache;
1186+
bundle = await rollup(options);
1187+
t.is(await getCodeFromBundle(bundle), code);
1188+
});
1189+
11501190
test('allows the config to be reused', async (t) => {
11511191
const config = {
11521192
preserveModules: true,

0 commit comments

Comments
 (0)