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: move types condition to the front #763

Merged
merged 1 commit into from Apr 30, 2023

Conversation

Andarist
Copy link
Contributor

I moved types condition to the front. package.json#exports are order-sensitive - they are always matched from the top to the bottom. When a match is found then it should be used and no further matching should occur.

Right now, the current setup works in TypeScript but it's considered a bug and it should not be relied upon, see the thread and the comment here. For that reason, I would like to fix all popular packages that misconfigured their exports this way so the bug can be fixed in TypeScript.

⚠️ this PR focuses solely on fixing "🐛 Used fallback condition" problem but the "🎭 Masquerading as CJS" remains here. You can check the reported errors here

@vercel
Copy link

vercel bot commented Apr 30, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
discord-api-types ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 30, 2023 10:14pm

@vladfrangu
Copy link
Member

I understand that this is supposed to fix the linked issue, but doesn't this actually break esm imports now? We only generate cjs output via tsc, and an esm wrapper that re-exports from cjs, which doesn't have its own types 👀

@vladfrangu
Copy link
Member

Disregard, turns out I'm blind, the types line has been moved to be first in the object (although I fail to see how that matters since objects don't necessarily have an order) 🫥

@kyranet
Copy link
Member

kyranet commented Apr 30, 2023

It does if you use Object.entries, as object entries are insert-size, or in this case, however they're ordered in the source.

@vladfrangu vladfrangu merged commit 9dce6ed into discordjs:main Apr 30, 2023
6 checks passed
@Andarist
Copy link
Contributor Author

Andarist commented May 1, 2023

Disregard, turns out I'm blind, the types line has been moved to be first in the object (although I fail to see how that matters since objects don't necessarily have an order) 🫥

I can expand a little bit on what @kyranet mentioned. Since es2015 the keys in objects have defined order. It's an insertion order with the exception that number-like keys are at the beginning and those are sorted:

var foo = {}
foo.b = null
foo.a = null
foo[10] = null
foo[1] = null

Object.keys(foo) // ['1', '10', 'b', 'a']

This fact is leveraged by the defined algorithm for package.json#exports resolution. You can find it in this section: "PACKAGE_TARGET_RESOLVE(packageURL, target, patternMatch,
isImports, conditions)":

  1. For each property p of target, in object insertion order as,

@iCrawl
Copy link
Member

iCrawl commented May 1, 2023

@Andarist Do you know how this affects top-level keys? We do have some modules that basically just do this:
image
With no explicit exports field.

@Andarist
Copy link
Contributor Author

Andarist commented May 1, 2023

Those are not order-sensitive. It's up to the bundler~ (and runtimes) to pick up the correct field. Note that most of those fields are overriden if exports are present (to the best of my knowledge only "alias" fields, such as package.json#browser, are still considered when resolving the file when exports are present). However, in TypeScript you must opt-into reading exports with the new moduleResolution: node16/nodenext - without that exports are ignored.

Does this clarify it for you? I can answer some extra questions if you have regarding this.

@iCrawl
Copy link
Member

iCrawl commented May 1, 2023

That answers all my questions, thank you!

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

Successfully merging this pull request may close these issues.

None yet

4 participants