Skip to content

Commit

Permalink
fix(node-resolve): handle browser-mapped paths correctly in nested co…
Browse files Browse the repository at this point in the history
…ntexts (#920)
  • Loading branch information
eemeli authored Jul 24, 2021
1 parent 4bd1beb commit 1e2363c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/node-resolve/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`];
Expand Down
35 changes: 34 additions & 1 deletion packages/node-resolve/test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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',
Expand Down
4 changes: 4 additions & 0 deletions packages/node-resolve/test/fixtures/browser-local-relative.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// test browser mapped imports from the main entrypoint
import s from './dummy-relative';

export default s;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// test browser mapped imports from the main entrypoint
import s from './dummy-relative';

export default s;
1 change: 1 addition & 0 deletions packages/node-resolve/test/fixtures/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"scripts": {},
"keywords": [],
"browser": {
"./dummy-relative": "component-type",
"dummy-module": "component-type"
}
}

0 comments on commit 1e2363c

Please sign in to comment.