From 1e2363c07b37ec671a570f0ac7e8522a61ee8c36 Mon Sep 17 00:00:00 2001 From: Eemeli Aro Date: Sat, 24 Jul 2021 19:13:57 +0300 Subject: [PATCH] fix(node-resolve): handle browser-mapped paths correctly in nested contexts (#920) --- packages/node-resolve/src/index.js | 2 +- packages/node-resolve/test/browser.js | 35 ++++++++++++++++++- .../test/fixtures/browser-local-relative.js | 4 +++ .../fixtures/nested/browser-local-relative.js | 4 +++ .../node-resolve/test/fixtures/package.json | 1 + 5 files changed, 44 insertions(+), 2 deletions(-) create mode 100755 packages/node-resolve/test/fixtures/browser-local-relative.js create mode 100755 packages/node-resolve/test/fixtures/nested/browser-local-relative.js diff --git a/packages/node-resolve/src/index.js b/packages/node-resolve/src/index.js index 5bd18c1e9..a8ba3da29 100644 --- a/packages/node-resolve/src/index.js +++ b/packages/node-resolve/src/index.js @@ -88,7 +88,7 @@ export function nodeResolve(opts = {}) { return { id: ES6_BROWSER_EMPTY }; } const browserImportee = - browser[importee] || + (importee[0] !== '.' && browser[importee]) || browser[resolvedImportee] || browser[`${resolvedImportee}.js`] || browser[`${resolvedImportee}.json`]; diff --git a/packages/node-resolve/test/browser.js b/packages/node-resolve/test/browser.js index 67897ce99..b62f17249 100644 --- a/packages/node-resolve/test/browser.js +++ b/packages/node-resolve/test/browser.js @@ -146,7 +146,7 @@ test('allows use of object browser field, resolving nested directories', async ( t.is(module.exports.test, 43); }); -test('respects local browser field', async (t) => { +test('respects local browser field for external dependencies', async (t) => { const bundle = await rollup({ input: 'browser-local.js', onwarn: () => t.fail('No warnings were expected'), @@ -161,6 +161,39 @@ test('respects local browser field', async (t) => { t.is(module.exports, 'component-type'); }); +test('respects local browser field for internal dependencies', async (t) => { + const bundle = await rollup({ + input: 'browser-local-relative.js', + onwarn: () => t.fail('No warnings were expected'), + plugins: [ + nodeResolve({ + mainFields: ['browser', 'main'] + }) + ] + }); + const { module } = await testBundle(t, bundle); + + t.is(module.exports, 'component-type'); +}); + +test('does not apply local browser field for matching imports in nested paths', async (t) => { + try { + await rollup({ + input: 'nested/browser-local-relative.js', + onwarn: () => t.fail('No warnings were expected'), + plugins: [ + nodeResolve({ + mainFields: ['browser', 'main'] + }) + ] + }); + } catch (e) { + t.is(e.code, 'UNRESOLVED_IMPORT'); + return; + } + t.fail('expecting error'); +}); + test('allows use of object browser field, resolving to nested node_modules', async (t) => { const bundle = await rollup({ input: 'browser-entry-points-to-node-module.js', diff --git a/packages/node-resolve/test/fixtures/browser-local-relative.js b/packages/node-resolve/test/fixtures/browser-local-relative.js new file mode 100755 index 000000000..6a83ab6a5 --- /dev/null +++ b/packages/node-resolve/test/fixtures/browser-local-relative.js @@ -0,0 +1,4 @@ +// test browser mapped imports from the main entrypoint +import s from './dummy-relative'; + +export default s; diff --git a/packages/node-resolve/test/fixtures/nested/browser-local-relative.js b/packages/node-resolve/test/fixtures/nested/browser-local-relative.js new file mode 100755 index 000000000..6a83ab6a5 --- /dev/null +++ b/packages/node-resolve/test/fixtures/nested/browser-local-relative.js @@ -0,0 +1,4 @@ +// test browser mapped imports from the main entrypoint +import s from './dummy-relative'; + +export default s; diff --git a/packages/node-resolve/test/fixtures/package.json b/packages/node-resolve/test/fixtures/package.json index a58c25b80..28bd9c9b7 100755 --- a/packages/node-resolve/test/fixtures/package.json +++ b/packages/node-resolve/test/fixtures/package.json @@ -8,6 +8,7 @@ "scripts": {}, "keywords": [], "browser": { + "./dummy-relative": "component-type", "dummy-module": "component-type" } }