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: declare ES module in package.json #1061

Merged
merged 6 commits into from Nov 14, 2023

Conversation

gthb
Copy link
Contributor

@gthb gthb commented Sep 23, 2021

In bundler mode, generate package.json with "type": "module" and use the "main" attribute instead of the "module" attribute.

This change makes the built ES module palatable to Node.js (when run with --experimental-vm-modules --experimental-wasm-modules), while it remains also palatable to webpack as illustrated in webpack/webpack#14313 (where the pkg subfolder is generated with wasm-pack built with this change). This resolves the headache of using a wasm-pack-built package in a library that one needs to both run directly in Node and include in a webpack build.

Fixes #1039

  • You have the latest version of rustfmt installed
$ rustup component add rustfmt
  • You ran cargo fmt on the code base before submitting
  • You reference which issue is being closed in the PR text

In bundler mode, generate the package.json with "type": "module" and use the
"main" attribute instead of the "module" attribute.

This change makes the built ES module palatable to node in
--experimental-wasm-modules mode as well as to webpack as illustrated in
webpack/webpack#14313
I noticed that this specific evasion of the "type" keyword is already used in
utils::manifest::NpmPackage in the tests, so just match that.
gthb added a commit to gthb/try-to-use-wasm-in-next.js that referenced this pull request Sep 24, 2021
The build-wasm script was failing if you had wasm-pack built with the patch in
rustwasm/wasm-pack#1061 because the "module" attribute
would not be there --- so fix that by just hardcoding the filename.
Update test case that landed after this PR was created, in
rustwasm#1061, to expect main attribute in
package.json instead of module attribute.
@gthb
Copy link
Contributor Author

gthb commented Oct 20, 2021

Sorry to push, but any chance of getting this merged and released (or else a comment on why it shouldn't be)?

I'm about to start jumping through hoops in CI to postprocess wasm-pack output to make it usable in a library that gets used both directly in Node.js and in a webpack build... and it would be so nice not to have to. :)

@josephg
Copy link

josephg commented Mar 2, 2022

Ping - I really want this feature merged too. I've settled for writing a post-processing script which inserts "type": "module" myself, but its quite annoying!

@mkermani144
Copy link

mkermani144 commented Oct 3, 2022

I don't know if this is going to be merged that soon, but isn't it better if we use the new exports field of package.json instead of providing main field?

mkermani144 added a commit to mkermani144/sigma-rust that referenced this pull request Oct 3, 2022
When {wasm-pack} is run with browser target, it generates a {package.json}
in which there is a {module} field, but no {main}, {type} or {exports}
field is present. This causes some of the modern tools which run in Node
be unable to use the package (Vitest is an example).

Although it may seem irrational to use the browser package in Node, but
it makes sense when we want to write tests. Many test tools such as Jest
and Vitest run in Node.

Add a {set_exports} script to be run after {wasm-pack}, updating the
generated {package.json} as follows:
- Add an {exports} field to enable new Node entry resolution feature
- Add {"type": "module"} to indicate that this is an ESM module
- Remove {module}, as it's no longer needed

{wasm-pack} may fix the issue in the future or new module entry resolution
mechanisms may be introduced in the future. Until then, this script will
enable the use of the package without getting browser environment related
issues when run in Node.

Related {wasm-pack} PR:
rustwasm/wasm-pack#1061
@josephg
Copy link

josephg commented Oct 3, 2022

I think exports or main would be fine, though I’m curious why you think exports is better @mkermani144 other than it’s newer.

It doesn’t matter to me, just so long as package.json isn’t broken out of the gate. (It’s broken because it claims the module is a cjs module when it’s actually an esm module). I’m disappointed this has been in a bad state so long. What do we need to do to fix this?

@mkermani144
Copy link

mkermani144 commented Oct 4, 2022

I just think it's more modern than the main field and nothing more. From Node docs:

For new packages targeting the currently supported versions of Node.js, the "exports"field is recommended. For packages supporting Node.js 10 and below, the "main" field is required.

Anyway, I agree with you. Merging this PR is far more important than using either of the fields 😅

@hanayashiki
Copy link

when is this out?

@hanayashiki
Copy link

For myself, this library is not sufficient for package publishing. I use a wrapper typescript file and rollup to publish npm package that is usable out of box. I'm using target --web and i think that is the only way.

@kachkaev
Copy link

kachkaev commented Oct 14, 2022

It’d be great to see this PR merged! I’ve crafted a demo where I use --targer=bundler and then manually patch package.json. It confirms that the proposed solution works in Node.js, Jest and Next.js 🎉

@tdelmas
Copy link

tdelmas commented Nov 14, 2023

#1039 (comment) :

Yes, need to rebase master into the current #1061 in order to get that merged.

Copy link
Member

@drager drager left a comment

Choose a reason for hiding this comment

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

Thank you!

@drager drager merged commit 77b8ced into rustwasm:master Nov 14, 2023
5 checks passed
@gthb gthb deleted the esm-type-and-main-in-package-json branch November 14, 2023 20:04
@dukejones
Copy link

So stoked about this!! Any idea of when the next release is? Last release looks like Jun 27.

I'm writing a plugin to integrate wasm-pack with Vite & Sveltekit, & I'd love to not have to manually rewrite package.json with "type":"module"

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.

Add type = "module" in package.json to support nodejs in bundler mode
8 participants