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 splitbrain between .js being ESM & CJS inconsistently #189

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

t3chguy
Copy link
Member

@t3chguy t3chguy commented Jan 8, 2025

Fixes #188

no actual changes to the js/cjs/mjs files barring some ts annotations

Switches the package type to ESM as that is what wasm-pack is outputting
Convinces the wasm bindgen test to use an ESM runner
Uses explicit extensions on the entrypoints between CJS/ESM

@richvdh
Copy link
Member

richvdh commented Jan 8, 2025

would it be better to stop using .js altogether, and just call everything .mjs / .cjs, to avoid this sort of confusion?

@t3chguy
Copy link
Member Author

t3chguy commented Jan 8, 2025

That would also be possible but I somehow hate the idea of having to rename the output files of wasm-pack s/js/mjs - "type": "module" is also a good thing going forward. I can imagine also imagine issues going forward if the tests/entrypoints were ts-ified and needing mts/cts and the issues typescript has around those

Edit: ended up being explicit on the checked in files, still added "type": "module" for wasm-pack generated files and for ts downstream to be happier

@t3chguy t3chguy force-pushed the t3chguy/fix-cjs-esm branch from 42eae8c to 5d2f778 Compare January 8, 2025 12:52
@t3chguy t3chguy marked this pull request as ready for review January 8, 2025 13:13
@t3chguy t3chguy requested a review from a team as a code owner January 8, 2025 13:13
@t3chguy t3chguy requested a review from andybalaam January 8, 2025 13:13
@andybalaam andybalaam requested review from richvdh and removed request for andybalaam January 8, 2025 14:20
@andybalaam
Copy link
Member

@richvdh I assigned this to you since you seem to understand it more than me. Feel free to assign another one to me :-)

Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no actual changes to the js/cjs/mjs files

This doesn't seem quite right? Could you give the description of the PR an update, please, to give an overview of the approach? (Looks like we're now standardising on .js meaning ESM, and .cjs meaning CommonJS?)

Also, please can you add an entry to the changelog?

@@ -59,7 +59,7 @@ let initialised = false;
*
* It will throw if there is an attempt to load the module asynchronously running
*
* @returns {typeof import("./pkg/matrix_sdk_crypto_wasm_bg.wasm.d")}
* @returns {typeof import("./pkg/matrix_sdk_crypto_wasm_bg.wasm.d", { with: { "resolution-mode": "require" } })}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you help me understand what exactly this is doing? I get that it's to do with https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-3.html?#stable-support-resolution-mode-in-import-types, but what's the actual outcome/why do we need this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Its what the error told me to do :D

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm, fair enough. I assume the commandline yarn lint:types/tsc --noEmit showed the same error? Or was this specific to your IDE?

I'd be interested to understand why this is a problem now when it wasn't before.

tsc is now detecting node.cjs as CommonJs where it wasn't before? (which seems odd, why would it have done that?)

Or tsc now considers ./pkg/matrix_sdk_crypto_wasm_bg.wasm.d.ts as ESMish where it wasn't before? (seems odd, it's typescript)

Copy link
Member Author

@t3chguy t3chguy Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm, fair enough. I assume the commandline yarn lint:types/tsc --noEmit showed the same error? Or was this specific to your IDE?

Same error

/opt/homebrew/bin/yarn run lint:types
yarn run v1.22.22
$ tsc --noEmit
node.cjs:83:36 - error TS1542: Type import of an ECMAScript module from a CommonJS module must have a 'resolution-mode' attribute.

83  * @returns {Promise<typeof import("./pkg/matrix_sdk_crypto_wasm_bg.wasm.d")>}
                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Found 1 error in node.cjs:83

error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Process finished with exit code 2

Or tsc now considers ./pkg/matrix_sdk_crypto_wasm_bg.wasm.d.ts as ESMish where it wasn't before? (seems odd, it's typescript)

Yes exactly that, because .mts and .cts exist, .ts esm/cjs-ness also depends on package.json type field.

@t3chguy
Copy link
Member Author

t3chguy commented Jan 9, 2025

This doesn't seem quite right? Could you give the description of the PR an update, please, to give an overview of the approach? (Looks like we're now standardising on .js meaning ESM, and .cjs meaning CommonJS?)

Have updated the description

Also, please can you add an entry to the changelog?

Could you please point me to any guidance on doing so? Not seeing anything in the contributing doc

@t3chguy t3chguy requested a review from richvdh January 9, 2025 13:01
@richvdh
Copy link
Member

richvdh commented Jan 9, 2025

Also, please can you add an entry to the changelog?
Could you please point me to any guidance on doing so? Not seeing anything in the contributing doc

https://github.com/matrix-org/matrix-rust-sdk/blob/main/CONTRIBUTING.md#writing-changelog-entries is the most relevant guide.

TL;DR though: if you could just add an entry under the "UNRELEASED" heading, following the same style as previous entries, that would be be great.

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

Successfully merging this pull request may close these issues.

SyntaxError: Unexpected token 'export'
3 participants