Align web/no-modules WASM import namespace to enable cross-target WASM sharing#4850
Conversation
CodSpeed Performance ReportMerging #4850 will not alter performanceComparing Summary
|
--target esm to enable cross-target WASM sharing
4c4a54c to
c5f0e70
Compare
There was a problem hiding this comment.
Pull request overview
This PR introduces a new --target esm compilation target to enable WASM file sharing across different JavaScript environments. The key motivation is backward compatibility: the original plan to modify the web target would have broken wasm-pack, which assumes --target web doesn't produce _bg.js files. The solution adds a new ESM target while preserving the original web target behavior.
Key Changes:
- Added new
--target esmthat generates bundler-compatible output with_bg.jsand_bg.wasmfiles - Updated WASM import namespace from
"wbg"to"./{name}_bg.js"for web and no-modules targets - Enhanced
package.jsongeneration to include amainfield for ESM and Node module targets
Reviewed changes
Copilot reviewed 48 out of 56 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
crates/cli/src/wasm_bindgen.rs |
Added Esm enum variant and CLI integration |
crates/cli-support/src/lib.rs |
Added ESM output mode, package.json main field generation, and error message updates |
crates/cli-support/src/js/mod.rs |
Implemented ESM-specific code generation with init functions and import handling |
crates/cli/tests/wasm-bindgen/reference.rs |
Extended test to check _bg.js output for ESM target |
| Reference test files | Added comprehensive test outputs for the new ESM target across multiple test scenarios |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Thank you for this PR - modernizing the targets is a major goal currently, and we've been needing this for a while. I want to review this properly, and will make sure to do that next week, thanks for your patience. |
|
I agree with all of the motivations here. The alignment is a huge step forward. Currently we have the source phase form supported under We had the discussion when we landed that target whether to have Therefore I would like to suggest that we change Let me know if that can work for you here and if that can fit into how you see this coming together? Then as for future composable targets, I wonder if we could separate the concept of the target from the environment and start treating environment hints / feature support as separate fine-grained thing to support to modify the output. In this framing I really appreciate your pushing this forward, will be great to land this with the small adjustment soon. |
|
Thank you for the thoughtful feedback and for planning to review this properly! I'm really glad to see that we are aligned on the goal of modernizing the targets - this has been a pain point for many users and I'm eager to help move it forward. I completely agree with the idea of separating target from environment. The environment-specific outputs (node, deno) exist primarily to provide convenience for runtimes with filesystem access - they can auto-initialize the WASM module without requiring users to manually pass the path. This is indeed an orthogonal concern from the core module format. The Regarding I do have a slight concern here. As I understand it, the current If we make SourcePhase the default for
A few possible paths forward: Option 1: Keep separate target names for now
Option 2: Make
Option 3: Align SourcePhase to bundler/node-module first
Option 4: Implement exactly as suggested
I'm open to any of these approaches - what do you think would work best for the project's direction? |
|
Just following up on my previous comment about Option 3 - I'm planning to start implementing that approach soon (aligning the SourcePhase WASM outputs) since it seems like a good incremental step forward. I'm still waiting for your thoughts on this path vs the others, so I'll hold off until I hear from you. No rush at all - whenever you have a moment to share your preference, that would be really helpful. Thanks again for your guidance and insights on. |
|
I think option 3 makes sense with the current plans, especially given that it is becoming stable across tools and we expect browsers to start supporting that form soon natively. I'm happy to include the instance case here too, but if you want to treat that as a follow-on that is fine by me as well. |
|
I'm assuming this PR depends on the others, but please re-request a review if this is ready. |
--target esm to enable cross-target WASM sharingweb/no-modules WASM import namespace to enable cross-target WASM sharing
80e34cf to
8354354
Compare
Change the Node.js (CommonJS) WASM import namespace from
`__wbindgen_placeholder__` to `./{name}_bg.js`, matching the import
convention used by the `bundler` and `experimental-nodejs-module`
targets.
Changes:
* Update `generate_node_imports` to accept a `module_name` parameter.
* Replace `PLACEHOLDER_MODULE` / `__wbindgen_placeholder__` with
`./{module_name}_bg.js` when emitting Node.js imports.
* Set the `import.module` field for WASM imports in the Node.js target to
the same `./{module_name}_bg.js` value.
Part of the cross-target WASM sharing initiative (#4850).
|
This PR is ready as well. |
Description
This PR updates the WASM import namespace for the
webandno-modulestarget to align with other targets, replacing the placeholder namespace with a relative module path.Changes
WASM Import Namespace Change
Checklist