Skip to content

Unify Node.js WASM import namespace with other targets#4869

Merged
guybedford merged 2 commits into
wasm-bindgen:mainfrom
magic-akari:refactor/node-sharing-wasm
Dec 20, 2025
Merged

Unify Node.js WASM import namespace with other targets#4869
guybedford merged 2 commits into
wasm-bindgen:mainfrom
magic-akari:refactor/node-sharing-wasm

Conversation

@magic-akari

@magic-akari magic-akari commented Dec 18, 2025

Copy link
Copy Markdown
Member

Description

This is a follow-up to PR #4850, which unified WASM import namespaces for web, no-modules, esm, and bundler targets. The Node.js target still used the legacy __wbindgen_placeholder__ namespace, preventing full cross-target WASM sharing.

Node.js Target WASM Import Namespace

Changed the WASM import namespace for --target nodejs from __wbindgen_placeholder__ to ./{name}_bg.js:

;; Before
(import "__wbindgen_placeholder__" "__wbindgen_init_externref_table" (func ...))

;; After
(import "./reference_test_bg.js" "__wbindgen_init_externref_table" (func ...))

JavaScript Import Changes

// Before
let imports = {};
imports['__wbindgen_placeholder__'] = module.exports;

// After
let imports = {};
imports['./reference_test_bg.js'] = module.exports;

Checklist

  • Verified changelog requirement

@magic-akari magic-akari force-pushed the refactor/node-sharing-wasm branch from ac519fb to 03764ce Compare December 18, 2025 14:12
@magic-akari

Copy link
Copy Markdown
Member Author

CI shows a flaky test failure that should have been resolved in #4857 — unclear why it regressed.

@magic-akari magic-akari marked this pull request as ready for review December 18, 2025 14:25
Copilot AI review requested due to automatic review settings December 18, 2025 14:25

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR unifies the WASM import namespace for Node.js targets from __wbindgen_placeholder__ to ./{name}_bg.js, completing the cross-target alignment started in PR #4850. This enables WASM modules to be shared across different build targets (web, bundler, Node.js).

  • Updates Node.js target (both CommonJS and ESM) to use the new ./{name}_bg.js import namespace
  • Modifies both JavaScript import generation and WASM module import mutations
  • Updates all reference test files to reflect the new namespace and resulting import order changes

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated no comments.

Show a summary per file
File Description
crates/cli-support/src/js/mod.rs Core implementation: adds module_name parameter to generate_node_imports() and updates namespace string generation for Node CommonJS mode; adds import module mutation for Node CommonJS to match existing Bundler/ESM pattern
crates/cli/tests/reference/targets-target-nodejs.wat Test reference: updates WASM imports from __wbindgen_placeholder__ to ./reference_test_bg.js
crates/cli/tests/reference/targets-target-nodejs.js Test reference: updates JavaScript imports object key from __wbindgen_placeholder__ to ./reference_test_bg.js
crates/cli/tests/reference/targets-target-nodejs-mvp.wat Test reference: updates WASM imports for MVP target variant
crates/cli/tests/reference/targets-target-nodejs-mvp.js Test reference: updates JavaScript imports for MVP target variant
crates/cli/tests/reference/targets-target-nodejs-atomics.wat Test reference: updates WASM imports for atomics/threading target variant
crates/cli/tests/reference/targets-target-nodejs-atomics.js Test reference: updates JavaScript imports for atomics/threading target variant
crates/cli/tests/reference/import-target-nodejs.wat Test reference: updates WASM imports and reflects natural WASM import reordering due to module name change (. sorts before _)
crates/cli/tests/reference/import-target-nodejs.js Test reference: updates JavaScript imports for complex import scenario
CHANGELOG.md Documents the breaking change under "Changed" section with appropriate context

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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).
@magic-akari magic-akari force-pushed the refactor/node-sharing-wasm branch from 03764ce to d045b4e Compare December 18, 2025 15:36
@guybedford guybedford merged commit 19c1f46 into wasm-bindgen:main Dec 20, 2025
58 checks passed
@magic-akari magic-akari deleted the refactor/node-sharing-wasm branch December 21, 2025 14:19
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.

3 participants