-
Notifications
You must be signed in to change notification settings - Fork 150
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: Handle the scenario where a package.json#browser field could be false
#427
Conversation
…set to {'./some-path': false}
593191b
to
b8174ce
Compare
Co-authored-by: Steven <[email protected]>
@@ -259,8 +259,18 @@ async function resolveRemappings( | |||
): Promise<void> { | |||
if (job.conditions?.includes('browser')) { | |||
const { browser: pkgBrowser } = pkgCfg; | |||
if (!pkgBrowser) { | |||
return; | |||
} |
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.
Actually now that I think about it, isn't there a case where browser can be a string?
We should add a test for this case as well
https://gist.github.com/defunctzombie/4339901/49493836fb873ddaa4b8a7aa0ef2352119f69211
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.
It can be, but I saw this test and now I think that maybe we should update its implementation (and rename). It's ensuring that the field specified in the browser field is not in the output, but it's not imported anywhere in the other modules so I think it's a false positive (as you pointed out when we were pairing).
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.
@jeffsee55 That is very strange.
Maybe talk to @dvoytenko to see if that is a mistake since I would expect to see import-main-browser.js in the output.js.
Also, that test seems very oddly written since pkg
is its own package boundary and pkg/subdir
is another package boundary, but the browser
field is referencing files from one to the other. I don't think thats common (or even allowed?)
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.
Thanks @styfle , will follow up with Dima.
For posterity, here are my thoughts on the current state of these tests, it seems like the output.js
should expect only files that are traceable from nft
, but in the tests the browser-specific files aren't imported or required by the paths the relate to the input entrypoint. So in my test I required it specifically. So it seemed like the other tests aren't asserting the right behavior because those browser files just aren't part of the dependency graph.
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 it could be a string, as in "main export file for browser". TBH, I haven't seen those in the wild and not what I've been trying to support in one of my previous PRs. But I think it's a use case in its own right and probably should be supported too. It's a relatively under-defined and legacy area - most of folk moved away from browser
, but I found the best way to debug them is by trying out to build examples using esbuild
- it understands browser
correctly.
false
This one is failing on Windows only:
|
Confirmed this has nothing to do with this PR, tests are failing here too |
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.
Is there a test for a string instead of object? For example, "browser": "./foo.js"
?
@styfle there was already one here https://github.com/vercel/nft/blob/main/test/unit/browser-remappings-malformed/node_modules/pkg/package.json. However I was hoping to revisit this after we land this to ensure that these tests are actually asserting what we think they are. That test, for example doesn't actually require the file mentioned in the |
@jeffsee55 I'm not suggesting that you change the existing test. I want a new test that tests for Something like:
And then That would prove that your comment is correct when it says "Downstream processing is expected to handle this case, and it should remain in the mapping result" |
🎉 This PR is included in version 0.27.3 🎉 The release is available on: Your semantic-release bot 📦🚀 |
…ort (#11841) See [nft](vercel/nft#427) for details
We recently shipped support for the
browser
field, but we discovered an edge case where instead of mapping to another string, the value could befalse
(eg,object-inspect
), which indicates that this module should be treated as an empty object ({}
) (evidenced by its usage here).The code was previously erroring on the string's
startsWith
checks, causing the entire package to be omitted from the file mapping result. This ensures that if the value isfalse
we'll continue on.