fix(pg-wasm): omit default-module-path in web wasm-bindgen glue (#153)#222
Conversation
wasm-pack's `--target web` output emitted an unreachable default-module-path
branch (`module_or_path = new URL('index_bg.wasm', import.meta.url)`) in
__wbg_init. Webpack 5 statically resolves `new URL(..., import.meta.url)` as an
asset import at build time even though the branch never fires for callers that
always pass `module_or_path` (e.g. @e4a/pg-js, which inlines the wasm as
base64), so downstream consumers hit `Module not found: Can't resolve
'index_bg.wasm'`.
Enable wasm-bindgen's --omit-default-module-path via wasm-pack's release-profile
metadata so the branch is never generated. This is the issue's preferred fix and
lives in the crate manifest, so no fragile post-build regex strip is needed. The
bundler target is unaffected (static `import * as wasm from './index_bg.wasm'`,
no init function). Callers that pass module_or_path explicitly are unchanged; a
caller passing nothing now errors at instantiate time, exactly as the flag
intends.
Fixes #153
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
There was a problem hiding this comment.
Rules Dobby 2 gatekeeper review — cycle 1. 0 confirmed findings; clean.
Per-rule Haiku sweep merged with Review Dobby 2's findings (0). The diff is textbook-correct against our own memory (postguard-wasm-pack-omit-default-module-path): wasm-pack 0.15.0 honors omit-default-module-path via [package.metadata.wasm-pack.profile.release.wasm-bindgen], the bundler target correctly needs no change, and cargo ignores package.metadata so the build is the check — verified: --target web now emits 0 index_bg.wasm / new URL(import.meta.url) refs while the explicit module_or_path fetch path is retained.
Two Haiku-raised flags reviewed and dismissed: (1) tests-required — the metadata is cargo-ignored, so there is no testable unit; build-output verification is the correct and completed check; (2) justification-comment — the comment documents a non-obvious webpack static-resolution interaction a maintainer needs to keep the flag, not a rationale-narration of a self-evident deletion.
Submitted as COMMENT (APPROVE is rejected on a self-authored PR); routing on findings = clean → ready-for-review.
Summary
wasm-pack build --target web(shipped as@e4a/pg-wasm) generated an unreachable default-module-path branch in__wbg_init:Webpack 5 statically analyzes
new URL("...", import.meta.url)as an asset import and tries to resolveindex_bg.wasmat build time — even though the branch never fires for callers that always passmodule_or_pathexplicitly (e.g.@e4a/pg-js, which inlines the WASM as base64). Downstream consumers therefore hitModule not found: Can't resolve 'index_bg.wasm'.Fix
Enable wasm-bindgen's
--omit-default-module-path(the issue's preferred option 1) via wasm-pack's release-profile metadata inpg-wasm/Cargo.toml:wasm-pack does support this flag through manifest metadata (confirmed on wasm-pack 0.15.0, the version CI installs), so the branch is never generated in the first place. This keeps the fix in the crate manifest — no workflow change and no fragile post-build regex strip against generated output.
This supersedes #199, which stripped the branch with a post-build
noderegex indelivery.yml. That approach works but (a) is a workflow-only change and (b) matches generated output by pattern. This PR uses the actual--omit-default-module-pathflag instead, so it's robust to wasm-bindgen formatting changes and touches onlyCargo.toml.Verification (locally, wasm-pack 0.15.0)
pg-wasm/pkg/web/index.jscontains thenew URL('index_bg.wasm', import.meta.url)branch (lines 1235–1237).--target webwith the metadata → zeroindex_bg.wasm/new URL(...)references;__wbg_initretains the explicit-module_or_pathpath (object-unwrap +string | Request | URL→fetch). Callers passingmodule_or_pathare unaffected; a caller passing nothing now errors at instantiate time, exactly as--omit-default-module-pathintends.import * as wasm from "./index_bg.wasm"(a legitimate static module import, not theimport.meta.urlasset pattern) with no__wbg_init— so no dead branch exists there and none is introduced. This corrects fix-option 2 in the issue body.cargo metadataparses the manifest;cargo fmt --all -- --checkclean (no Rust changed).package.metadatais ignored by cargo, so the Rust test suite is unaffected; thewasm-pack build --releaseruns for both targets (the exact CI code path) succeed.Downstream
Once a new
@e4a/pg-wasmships with this,@e4a/pg-jsonly needs a dep bump + republish, and theparser: { url: false }webpack workaround in downstream consumers can be removed.Fixes #153
🤖 Generated with Claude Code