From fd213e2a01b555ce22b3bb969b34803da08361cb Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Thu, 8 Sep 2022 06:38:45 +0200 Subject: [PATCH] fix(node-resolve): handle circular commonjs --- packages/node-resolve/src/index.js | 2 +- .../test/fixtures/cyclic-commonjs/main.js | 4 ++++ .../test/fixtures/cyclic-commonjs/other.js | 3 +++ packages/node-resolve/test/test.js | 15 +++++++++++++++ 4 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 packages/node-resolve/test/fixtures/cyclic-commonjs/main.js create mode 100644 packages/node-resolve/test/fixtures/cyclic-commonjs/other.js diff --git a/packages/node-resolve/src/index.js b/packages/node-resolve/src/index.js index 637412316..7e717a018 100644 --- a/packages/node-resolve/src/index.js +++ b/packages/node-resolve/src/index.js @@ -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 diff --git a/packages/node-resolve/test/fixtures/cyclic-commonjs/main.js b/packages/node-resolve/test/fixtures/cyclic-commonjs/main.js new file mode 100644 index 000000000..69fa28390 --- /dev/null +++ b/packages/node-resolve/test/fixtures/cyclic-commonjs/main.js @@ -0,0 +1,4 @@ +exports.main = 'main'; +const other = require('./other.js'); + +t.is(other.main, 'main'); diff --git a/packages/node-resolve/test/fixtures/cyclic-commonjs/other.js b/packages/node-resolve/test/fixtures/cyclic-commonjs/other.js new file mode 100644 index 000000000..468201799 --- /dev/null +++ b/packages/node-resolve/test/fixtures/cyclic-commonjs/other.js @@ -0,0 +1,3 @@ +const { main } = require('./main.js'); + +exports.main = main; diff --git a/packages/node-resolve/test/test.js b/packages/node-resolve/test/test.js index d81d13b27..5ab2843d5 100755 --- a/packages/node-resolve/test/test.js +++ b/packages/node-resolve/test/test.js @@ -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',