Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(node-resolve): handle browser-mapped paths correctly in nested contexts #920

Merged
merged 1 commit into from Jul 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/node-resolve/src/index.js
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
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
@@ -0,0 +1,4 @@
// test browser mapped imports from the main entrypoint
import s from './dummy-relative';

export default s;
@@ -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
Expand Up @@ -8,6 +8,7 @@
"scripts": {},
"keywords": [],
"browser": {
"./dummy-relative": "component-type",
"dummy-module": "component-type"
}
}