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: demote browser export #11

Closed
wants to merge 2 commits into from
Closed

Conversation

penx
Copy link

@penx penx commented Jun 6, 2022

Prefer require, import and types exports over browser.

Exports are resolved in order, i.e. the first match is used.

I'm not sure why the browser export is using the raw src, however when Jest is configured with testEnvironment: "jsdom", it will resolve to this file causing 2 issues:

Also:

"types" - can be used by typing systems to resolve the typing file for the given export. This condition should always be included first.
https://nodejs.org/api/packages.html

@changeset-bot
Copy link

changeset-bot bot commented Jun 6, 2022

🦋 Changeset detected

Latest commit: 9105002

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@remix-run/web-fetch Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Exports are resolved in order, i.e. the first match is used.

I'm not sure why the browser export is using the raw src, however this causes issues with Jest when configured with jsdom, as it will resolve to this file and not be able to transpile it. Jest also throws a type error as discussed on remix-run/remix#3402
packages/fetch/package.json Outdated Show resolved Hide resolved
@MichaelDeBoey
Copy link
Member

CC/ @chaance @jacob-ebey @mjackson
Since I can't add you as a reviewer (I don't have any permissions on this repo), I wanted to tag you guys.

@JRasmusBm
Copy link

JRasmusBm commented Nov 17, 2022

I've been digging for over two hours and this is clearly the fix. Is there any progress on this PR? 😇

EDIT: Solving it with patch-package for now.

@nitrog7
Copy link

nitrog7 commented Jul 19, 2023

nudge

@brophdawg11
Copy link

Exports are resolved in order, i.e. the first match is used.

Is there a source/reference for this?

@penx
Copy link
Author

penx commented Jul 19, 2023

Exports are resolved in order, i.e. the first match is used.

Is there a source/reference for this?

Within the "exports" object, key order is significant. During condition matching, earlier entries have higher priority and take precedence over later entries. The general rule is that conditions should be from most specific to least specific in object order.

https://nodejs.org/api/packages.html#conditional-exports

@jacob-ebey
Copy link
Member

jacob-ebey commented Jul 19, 2023

I don't believe this is "the fix" everyone is after here. Generally, any non-standard conditions like "browser" are put above the "require", "import", and "default" conditions. Reasoning being some bundlers (like esbuild) don't allow you to disable the default conditions, and instead configure conditions to check before the defaults.

This honestly sounds like a Jest issue of loading the wrong conditions for where they are executing. You aren't actually running Jest in the browser, so they should not be attempting to load the browser condition.

Moving browser below the standard conditions will mean that the "browser" condition will never be picked, even when building for / targeting the browser.

@brophdawg11
Copy link

Thanks @penx! I missed that when I looked through those docs.

I agree with @jacob-ebey here - that snippet sounds to me like browser belongs above import/require as it's more specific. Just like node should be above per the docs.

It does feel like Jest either (1) should not pick up the browser entry or (2) should handle ESM due to type:module.

I think we should split off the moving types to the top into a new Pr and merge that since that feels cut and dry. Moving browser around feels wrong and likely requires some more investigation into exactly why jest can't handle it correctly.

@jacob-ebey jacob-ebey self-requested a review July 19, 2023 18:17
@brophdawg11
Copy link

Extracted the lifting of types to the top into #38

@MichaelDeBoey
Copy link
Member

Instead of demoting the browser export, we should provide a CJS version for browser as well, which is exactly what I did in #43

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
6 participants