Skip to content

pg-wasm: web target ships dead new URL("index_bg.wasm", import.meta.url) that breaks bundler consumers #153

Description

@rubenhensen

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

  1. Fresh project, webpack 5, npm i @e4a/pg-js@0.10.0
  2. Import PostGuard from @e4a/pg-js anywhere
  3. webpack --mode development → ENOENT on index_bg.wasm

Fix options

  1. 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.
  2. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingjavascriptPull requests that update javascript code

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions