Skip to content

Commit

Permalink
fix(node-resolve): handle circular commonjs (#1259)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert authored Sep 8, 2022
1 parent 81e2985 commit 5cf48e9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/node-resolve/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ export function nodeResolve(opts = {}) {
// `moduleSideEffects` information.
const resolvedResolved = await this.resolve(resolved.id, importer, {
...resolveOptions,
custom: { ...custom, 'node-resolve': { resolved } }
custom: { ...custom, 'node-resolve': { ...custom['node-resolve'], resolved } }
});
if (resolvedResolved) {
// Handle plugins that manually make the result external
Expand Down
4 changes: 4 additions & 0 deletions packages/node-resolve/test/fixtures/cyclic-commonjs/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
exports.main = 'main';
const other = require('./other.js');

t.is(other.main, 'main');
3 changes: 3 additions & 0 deletions packages/node-resolve/test/fixtures/cyclic-commonjs/other.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { main } = require('./main.js');

exports.main = main;
15 changes: 15 additions & 0 deletions packages/node-resolve/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ test('finds and converts a basic CommonJS module', async (t) => {
t.is(module.exports, 'It works!');
});

test('handles cyclic CommonJS modules', async (t) => {
const bundle = await rollup({
input: 'cyclic-commonjs/main.js',
onwarn(warning) {
if (warning.code !== 'CIRCULAR_DEPENDENCY') {
t.fail(`Unexpected warning:\n${warning.code}\n${warning.message}`);
}
},
plugins: [nodeResolve(), commonjs()]
});
const { module } = await testBundle(t, bundle);

t.is(module.exports.main, 'main');
});

test('handles a trailing slash', async (t) => {
const bundle = await rollup({
input: 'trailing-slash.js',
Expand Down

0 comments on commit 5cf48e9

Please sign in to comment.