-
-
Notifications
You must be signed in to change notification settings - Fork 594
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): fix "browser" and "exports.browser" field mappings #843
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,10 +86,25 @@ async function resolvePackageTarget(context, { target, subpath, pattern, interna | |
} | ||
|
||
if (target && typeof target === 'object') { | ||
const { packageBrowserField, useBrowserOverrides } = context; | ||
let browserTarget = null; | ||
|
||
if (useBrowserOverrides) { | ||
if (Object.keys(target).includes('browser')) { | ||
browserTarget = target.browser; | ||
} else { | ||
for (const [, value] of Object.entries(target)) { | ||
if (packageBrowserField[value]) { | ||
browserTarget = value; | ||
} | ||
} | ||
} | ||
} | ||
|
||
for (const [key, value] of Object.entries(target)) { | ||
if (key === 'default' || context.conditions.includes(key)) { | ||
const resolved = await resolvePackageTarget(context, { | ||
target: value, | ||
target: browserTarget || value, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
subpath, | ||
pattern, | ||
internal | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -199,3 +199,33 @@ test('pkg.browser with mapping to prevent bundle by specifying a value of false' | |
|
||
t.is(module.exports, 'ok'); | ||
}); | ||
|
||
test('pkg.browser can override the export map result', async (t) => { | ||
const bundle = await rollup({ | ||
input: 'browser-override-exports.js', | ||
plugins: [nodeResolve({ browser: true }), commonjs()] | ||
}); | ||
const { module } = await testBundle(t, bundle); | ||
|
||
t.is(module.exports, 'browser'); | ||
}); | ||
|
||
test('exports.browser takes precedence over export map result, when browser:true', async (t) => { | ||
const bundle = await rollup({ | ||
input: 'browser-exports-browser.js', | ||
plugins: [nodeResolve({ browser: true }), commonjs()] | ||
}); | ||
const { module } = await testBundle(t, bundle); | ||
|
||
t.is(module.exports, 'browser'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is incorrect - the correct result is the The reason for this is that the exports object matches the first condition that is valid in object ordering. So the |
||
}); | ||
|
||
test('exports.browser does not take precedence over export map result, when browser:false', async (t) => { | ||
const bundle = await rollup({ | ||
input: 'browser-exports-browser.js', | ||
plugins: [nodeResolve(), commonjs()] | ||
}); | ||
const { module } = await testBundle(t, bundle); | ||
|
||
t.is(module.exports, 'require'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Somewhat suprised to see this is returning There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it's because I included the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure, perhaps @LarsDenBakker can clarify what the expectation is for this case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW, removing the commonjs plugin made no difference. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense, it's about the top-level resolver conditions, not the transforms. |
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const b = require('exports-browser'); | ||
|
||
module.exports = b; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const Nanoid = require('nanoid'); | ||
|
||
module.exports = Nanoid; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these mappings are supposed to apply for all resolutions not just exports targets. Eg
require('./index.js')
/import './index.js'
should also be rewritten but aren't here.In theory such a bug fix is an extension of this bug fix, so if you'd rather do it as a follow up I'm open to that - but strictly speaking it would be incomplete without it.