Skip to content

Commit 5cf48e9

Browse files
authored
fix(node-resolve): handle circular commonjs (#1259)
1 parent 81e2985 commit 5cf48e9

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

packages/node-resolve/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ export function nodeResolve(opts = {}) {
285285
// `moduleSideEffects` information.
286286
const resolvedResolved = await this.resolve(resolved.id, importer, {
287287
...resolveOptions,
288-
custom: { ...custom, 'node-resolve': { resolved } }
288+
custom: { ...custom, 'node-resolve': { ...custom['node-resolve'], resolved } }
289289
});
290290
if (resolvedResolved) {
291291
// Handle plugins that manually make the result external
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
exports.main = 'main';
2+
const other = require('./other.js');
3+
4+
t.is(other.main, 'main');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const { main } = require('./main.js');
2+
3+
exports.main = main;

packages/node-resolve/test/test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ test('finds and converts a basic CommonJS module', async (t) => {
4444
t.is(module.exports, 'It works!');
4545
});
4646

47+
test('handles cyclic CommonJS modules', async (t) => {
48+
const bundle = await rollup({
49+
input: 'cyclic-commonjs/main.js',
50+
onwarn(warning) {
51+
if (warning.code !== 'CIRCULAR_DEPENDENCY') {
52+
t.fail(`Unexpected warning:\n${warning.code}\n${warning.message}`);
53+
}
54+
},
55+
plugins: [nodeResolve(), commonjs()]
56+
});
57+
const { module } = await testBundle(t, bundle);
58+
59+
t.is(module.exports.main, 'main');
60+
});
61+
4762
test('handles a trailing slash', async (t) => {
4863
const bundle = await rollup({
4964
input: 'trailing-slash.js',

0 commit comments

Comments
 (0)