From f8d4c448f60e8e3e71b517634ebfd7e0940ad59e Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Sun, 1 May 2022 07:09:29 +0200 Subject: [PATCH] fix(node-resolve): Respect if other plugins resolve the resolution to a different id --- packages/node-resolve/src/index.js | 5 +++++ packages/node-resolve/test/test.js | 30 +++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/packages/node-resolve/src/index.js b/packages/node-resolve/src/index.js index 8daafcffe..f9895be3c 100644 --- a/packages/node-resolve/src/index.js +++ b/packages/node-resolve/src/index.js @@ -281,6 +281,11 @@ export function nodeResolve(opts = {}) { if (resolvedResolved.external) { return false; } + // Allow other plugins to take over resolution. Rollup core will not + // change the id if it corresponds to an existing file + if (resolvedResolved.id !== resolved.id) { + return resolvedResolved; + } // Pass on meta information added by other plugins return { ...resolved, meta: resolvedResolved.meta }; } diff --git a/packages/node-resolve/test/test.js b/packages/node-resolve/test/test.js index 50cbcac82..9c08ce211 100755 --- a/packages/node-resolve/test/test.js +++ b/packages/node-resolve/test/test.js @@ -1,4 +1,4 @@ -import { join, resolve } from 'path'; +import { join, resolve, dirname } from 'path'; import test from 'ava'; import { rollup } from 'rollup'; @@ -581,3 +581,31 @@ test('passes on meta information from other plugins', async (t) => { ] }); }); + +test('allow other plugins to take over resolution', async (t) => { + await rollup({ + input: 'entry/main.js', + onwarn: failOnWarn(t), + plugins: [ + nodeResolve(), + { + name: 'change-resolution', + resolveId(importee) { + if (importee.endsWith('main.js')) { + return { + id: join(dirname(importee), 'other.js'), + meta: { 'change-resolution': 'changed' } + }; + } + return null; + }, + + load(id) { + const info = this.getModuleInfo(id); + t.is(info.id, join(__dirname, 'fixtures', 'entry', 'other.js')); + t.deepEqual(info.meta, { 'change-resolution': 'changed' }); + } + } + ] + }); +});