Summary
The web/ target generated by wasm-pack build --target web (and shipped to npm as @e4a/pg-wasm) contains a wasm-bindgen-generated default-value branch in the __wbg_init function that breaks downstream bundlers (most notably webpack 5).
In @e4a/pg-wasm@0.5.x, web/index.js lines 1237–1239:
if (module_or_path === undefined) {
module_or_path = new URL('index_bg.wasm', import.meta.url);
}
This branch only fires when a caller invokes init() with no argument. Callers that always pass init({ module_or_path: <ArrayBuffer> }) — e.g. @e4a/pg-js, which inlines the WASM as base64 — never hit it.
Problem
Webpack 5 statically analyzes new URL("...", import.meta.url) as an asset import and tries to resolve index_bg.wasm next to the importing module at build time, regardless of whether the branch is reachable. Consumers of pg-js who don't have a separate index_bg.wasm next to pg-js/dist/ therefore see:
Module not found: Error: Can't resolve 'index_bg.wasm' in '.../node_modules/@e4a/pg-js/dist'
Repro
- Fresh project, webpack 5,
npm i @e4a/pg-js@0.10.0
- Import
PostGuard from @e4a/pg-js anywhere
webpack --mode development → ENOENT on index_bg.wasm
Fix options
- Pass
--omit-default-module-path to wasm-bindgen (preferred). wasm-pack does not expose this flag directly, but it can be applied by either:
- Calling
wasm-bindgen manually after wasm-pack build with the --omit-default-module-path flag, or
- Adding a post-build script that regex-strips lines 1237–1239 from
pkg/web/index.js.
- The
bundler/ target carries the same dead branch and should get the same treatment if it's still published.
Downstream
@e4a/pg-js consumers are currently working around this with a webpack parser: { url: false } rule on the pg-js module. See encryption4all/postguard-js (issue to be filed referencing this one). Once pg-wasm ships the fix, pg-js only needs a dep bump + republish, and the workaround in downstream consumers can be removed.
Summary
The
web/target generated bywasm-pack build --target web(and shipped to npm as@e4a/pg-wasm) contains a wasm-bindgen-generated default-value branch in the__wbg_initfunction that breaks downstream bundlers (most notably webpack 5).In
@e4a/pg-wasm@0.5.x,web/index.jslines 1237–1239:This branch only fires when a caller invokes
init()with no argument. Callers that always passinit({ module_or_path: <ArrayBuffer> })— e.g.@e4a/pg-js, which inlines the WASM as base64 — never hit it.Problem
Webpack 5 statically analyzes
new URL("...", import.meta.url)as an asset import and tries to resolveindex_bg.wasmnext to the importing module at build time, regardless of whether the branch is reachable. Consumers ofpg-jswho don't have a separateindex_bg.wasmnext topg-js/dist/therefore see:Repro
npm i @e4a/pg-js@0.10.0PostGuardfrom@e4a/pg-jsanywherewebpack --mode development→ ENOENT onindex_bg.wasmFix options
--omit-default-module-pathto wasm-bindgen (preferred). wasm-pack does not expose this flag directly, but it can be applied by either:wasm-bindgenmanually afterwasm-pack buildwith the--omit-default-module-pathflag, orpkg/web/index.js.bundler/target carries the same dead branch and should get the same treatment if it's still published.Downstream
@e4a/pg-jsconsumers are currently working around this with a webpackparser: { url: false }rule on the pg-js module. See encryption4all/postguard-js (issue to be filed referencing this one). Once pg-wasm ships the fix,pg-jsonly needs a dep bump + republish, and the workaround in downstream consumers can be removed.