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

Add node module output format #4027

Merged
merged 16 commits into from
Aug 4, 2024
Merged

Conversation

Systemcluster
Copy link
Contributor

@Systemcluster Systemcluster commented Jul 30, 2024

This PR adds a node module output target. Node stabilized node modules with release 12, a lot of the JS ecosystem moved towards publishing modules, and the other targets apart from nodejs all generate modules.

There were already code-paths for this target, but it wasn't hooked up or finished. I changed the naming away from "experimental" and finished the implementation.

Support for the format would also need to be added to wasm-pack after this is merged.

@Systemcluster Systemcluster force-pushed the node-module branch 3 times, most recently from d67e885 to 52afb2f Compare July 30, 2024 06:51
Copy link
Collaborator

@daxpedda daxpedda left a comment

Choose a reason for hiding this comment

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

Even though the changes are very small, personally I can't review this as I have no clue about Node.

Best case scenario I can take a look at it later when I have more time to learn whats needed here, but even then I would keep it experimental until a maintainer who knows whats going on here can review this.

crates/cli-support/src/lib.rs Show resolved Hide resolved
@daxpedda daxpedda added the needs review Needs a review by a maintainer. label Jul 30, 2024
@Systemcluster
Copy link
Contributor Author

Thanks for checking, I'll annotate some of the changes later to make it easier to review.

@Systemcluster Systemcluster force-pushed the node-module branch 6 times, most recently from 4685879 to d23a4b8 Compare July 31, 2024 04:55
Copy link
Contributor Author

@Systemcluster Systemcluster left a comment

Choose a reason for hiding this comment

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

I've split up the changes into separate commits, so it should be more obvious what changes what. The core changes are in 4d63628. In general, the new target mostly uses the bundler target paths, plus the existing node import logic as noted below.

crates/cli-support/src/js/mod.rs Outdated Show resolved Hide resolved
crates/cli-support/src/js/mod.rs Show resolved Hide resolved
@Systemcluster
Copy link
Contributor Author

Systemcluster commented Aug 1, 2024

My earlier question was resolved after gaining some more familiarity with the codebase, I've updated my comments where I was being unclear.

@daxpedda I think you should be able to review this in this state.

Copy link
Collaborator

@daxpedda daxpedda left a comment

Choose a reason for hiding this comment

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

LGTM!

I'm happy to move this forward with the following changes:

  • Name the option experimental-nodejs-module.
    • I don't feel entirely comfortable otherwise, considering my lack of knowledge with this tech.
    • After some digging I do believe we want to produce .mjs files, so until we figure that out we shouldn't stabilize the option.
  • Make sure its being tested in CI.
    The CI is a bit of a mess and I have no guidance on where to add it, unfortunately you will have to dig into it yourself.

@Systemcluster
Copy link
Contributor Author

I don't feel entirely comfortable otherwise, considering my lack of knowledge with this tech.

I'm fine with experimental as long as it's merged. 🙂

After some digging I do believe we want to produce .mjs files, so until we figure that out we shouldn't stabilize the option.

That will require a few more changes than 4d63628 if I've seen right, so far I was trying to change as little as possible. If I go through and generalize the extension already, I'll also add a command line option for overwriting the default.

Just to make clear, currently there are the following targets:

Target Type Extension
bundler, deno, web ESM .js
nodejs CJS .js
no-modules Integrated .js

Changing it for this target would add another row:

Target Type Extension
nodejs-module ESM .mjs

While I don't disagree that a .mjs extension would make sense for the target itself, it would be surprising with the other targets being as they are. Consider if an option to manually set the extension and leaving the default consistent with the others it isn't better here at this point.

In either case, since this remains experimental for now we can do that in a follow-up PR.

The CI is a bit of a mess and I have no guidance on where to add it, unfortunately you will have to dig into it yourself.

I didn't find any relevant tests on first glance and was hoping for more info, but I'll take digging in. 😄

@daxpedda
Copy link
Collaborator

daxpedda commented Aug 1, 2024

While I don't disagree that a .mjs extension would make sense for the target itself, it would be surprising with the other targets being as they are. Consider if an option to manually set the extension and leaving the default consistent with the others it isn't better here at this point.

Is there a reason file extensions should be consistent across targets?
I certainly would not have expected so, because targets are in no way equal, they have to be handled very differently.

In either case, since this remains experimental for now we can do that in a follow-up PR.

Agreed.


Your work and input so far is really appreciated, it has been very valuable and you put a lot of effort into making this as easy to review as possible.

Thank you!

@daxpedda daxpedda added waiting for author Waiting for author to respond and removed needs review Needs a review by a maintainer. labels Aug 1, 2024
@Systemcluster
Copy link
Contributor Author

Systemcluster commented Aug 1, 2024

Is there a reason file extensions should be consistent across targets?

Principle of least surprise is the main one, but I don't have reasons beyond that.

because targets are in no way equal, they have to be handled very differently.

Technically yes, but only to some degree. If you want to distribute your wasm-bindgen generated files on NPM you always have to package it with a package.json, and resolution through package.json by default goes through Node. TypeScript and other tools¹ also adhere to the Node resolution strategy, either by default or as an option, and that resolution will consider the "type" field in the package.json to decide whether .js files are ESM or CJS. That part is the same with all targets.

I'll think about it more until the follow-up. I actually don't mind using .mjs here, I just wish we could make the nodejs target output .cjs instead (or both).

¹With exceptions, Webpack states it autodetects the format, some other bundlers might do the same.

@daxpedda daxpedda added needs review Needs a review by a maintainer. and removed waiting for author Waiting for author to respond labels Aug 4, 2024
@Systemcluster Systemcluster force-pushed the node-module branch 4 times, most recently from 18fdcad to 3eafd91 Compare August 4, 2024 06:23
@Systemcluster
Copy link
Contributor Author

It looks like for use-cases without a package.json the explicit extension is really useful 😄

Test is running now, I added a package.json temporarily to make the test work. It can be removed with the extension change.

crates/test/src/lib.rs Outdated Show resolved Hide resolved
.github/workflows/main.yml Outdated Show resolved Hide resolved
crates/cli-support/src/js/mod.rs Show resolved Hide resolved
crates/cli/src/bin/wasm-bindgen.rs Show resolved Hide resolved
crates/js-sys/tests/headless_no_threads.rs Outdated Show resolved Hide resolved
.github/workflows/main.yml Outdated Show resolved Hide resolved
@Systemcluster Systemcluster force-pushed the node-module branch 3 times, most recently from 6e2b397 to 78d3338 Compare August 4, 2024 20:03
Copy link
Collaborator

@daxpedda daxpedda left a comment

Choose a reason for hiding this comment

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

Great work, thank you!

@Systemcluster
Copy link
Contributor Author

It took a bit of back and forth but we got there, thanks for the review and guidance!

@daxpedda daxpedda merged commit 09e0e6d into rustwasm:main Aug 4, 2024
27 checks passed
@Systemcluster Systemcluster deleted the node-module branch August 4, 2024 21:04
@magic-akari
Copy link
Contributor

I think it might be necessary to mention #3790

@Systemcluster
Copy link
Contributor Author

Thanks for linking that issue, that's a feature I want too. I wanted to experiment with some approaches during the week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs review Needs a review by a maintainer.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants