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

[chore]: Standardize package exports in a way that silences pnpm warnings #1590

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

inlined
Copy link
Member

@inlined inlined commented Jan 7, 2025

Fixes #1589 to clean up warnings about our package ordering (which is inconsistent within the repository). Thank you ChatGPT for the suggested ordering, which silences pnpm. I worry but have not verified that the old way might confuse node or typescript on import style.

Checklist (if applicable):

@inlined inlined requested review from mbleigh and pavelgj January 7, 2025 22:25
@inlined
Copy link
Member Author

inlined commented Jan 7, 2025

Note, I ran into issues in multiple PRs that ts-node was not able to use "import" statements because the scripts code was not a module. "tsx" is already installed and is the more modern way to execute a TypeScript script, so I modified the format and lint scripts to use it.

@inlined inlined marked this pull request as draft January 7, 2025 22:53
@inlined
Copy link
Member Author

inlined commented Jan 7, 2025

Looks like jest uses ts-node. Going to look into fixes.

@mbleigh
Copy link
Collaborator

mbleigh commented Jan 7, 2025

IIRC we have explicitly not done this before because it's actually correct and pnpm is wrong to be sending warnings. The ordering of exports is really bizarre (for instance default must be the last one) but certain toolchains we ran into had issues if we didn't have exactly this setup...I think.

@inlined
Copy link
Member Author

inlined commented Jan 8, 2025

If that's the case, why are our orerings not consistent throughout the codebase? Do you have an example of a toolchain which has broken which I can test?

@mbleigh
Copy link
Collaborator

mbleigh commented Jan 8, 2025

I don't remember for sure, I just remember trying to fix this same thing a few months back and going down a rabbit hole where I decided not touching it was the better course...maybe it's fine? I just know that this stuff has been very fragile to mess with previously.

@pavelgj
Copy link
Collaborator

pavelgj commented Jan 8, 2025

I don't remember for sure, I just remember trying to fix this same thing a few months back and going down a rabbit hole where I decided not touching it was the better course...maybe it's fine? I just know that this stuff has been very fragile to mess with previously.

yeah, very fragile... I wouldn't trust our tests to cover these things.

@inlined
Copy link
Member Author

inlined commented Jan 8, 2025

Trying to do some research because I agree this can both be fragile and important. The node docs make it clear that default should be last but only give guidance that you should go “more to less” specific with import vs require (wtf does that even mean?!)

I checked the TypeScript docs and it definitely says “types” must come first. I ran across a bug that noted it works anyway and the team openly considered a breaking change to prevent the proliferation of malformed packages.

The best I can guess from this is that ESM modules should put import first and CSM modules should put require first because that is the naive syntax for the package, but then why would pnpm be giving us warnings since we’re creating CSM modules?

fwiw ChatGPT hallucinates a recommendation for the order here. I’ll add links after my dr apt

@inlined
Copy link
Member Author

inlined commented Jan 8, 2025

There’s an in progress Node docs repo that goes into some of the pitfalls. FWIW, both of its examples have import before require. It also raises another question: given our very high minimum node version, why are we producing CSM instead of the more performant ESM?

@pavelgj
Copy link
Collaborator

pavelgj commented Jan 8, 2025

There’s an in progress Node docs repo that goes into some of the pitfalls. FWIW, both of its examples have import before require. It also raises another question: given our very high minimum node version, why are we producing CSM instead of the more performant ESM?

"high minimum node version" happened organically as we discovered that some things weren't working... and I think originally we just started by copy-pasting the boilerplate from some firebase SDK, and tweaked things until it worked for us... then tweaked a lot more things as we noticed things breaking in various environments.

@inlined
Copy link
Member Author

inlined commented Jan 8, 2025

After more research, I’m starting to think that we should not only make this change, but also change from CSM to ESM (which is a breaking change, so no time like the present!)

ESM is supported in all LTS node versions, loads faster (async I/O), supports tree shaking for smaller deployment sizes, and can be more easily shared in browsers (I know that’s not supported yet, but I’d like to get there). This would require zero code changes on our side because TS syntax is already ESM. I also don’t think it requires any customers to change code because we’re actually creating UMD with our exports annotations.

My somewhat informed assumption is that the change in module type would change what typescript transpiles to. I can verify if you want (once I get my test app working for no-skipLibCheck PR

The only downside is ESM is that exports are no longer dynamic and therefore you can’t replace them with mocks like Sinon.

thoughts @mbleigh @pavelgj ?

@mbleigh
Copy link
Collaborator

mbleigh commented Jan 9, 2025 via email

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

Successfully merging this pull request may close these issues.

[JS] pnpm hints that package exports are in the incorrect order
3 participants