[bug] `--target bundler`: namespaced exports (`js_namespace`) are missing from the entry module's re-export list
Describe the bug
With js_namespace on an exported function (supported since 0.2.106, #4744), the bundler target generates the namespace object in index_bg.js but does not re-export it from the entry index.js, so the namespace is invisible to consumers importing the package. The web and nodejs targets handle the same code correctly.
Reproduction
wasm-bindgen 0.2.126, wasm-pack 0.15.0, rustc 1.97.1, node 23.5.0.
use wasm_bindgen::prelude::*;
#[wasm_bindgen(js_namespace = testns, js_name = ping)]
pub fn test_ns_ping() -> u32 { 42 }wasm-pack build --out-name index --target bundlerpkg/index_bg.js (correct — lines 342-348 of our build):
function testns__ping() {
const ret = wasm.testns__ping();
return ret >>> 0;
}
export const testns = {};
testns.ping = testns__ping;pkg/index.js — this is the complete file; testns appears nowhere in it (grep -c testns pkg/index.js → 0):
/* @ts-self-types="./index.d.ts" */
import * as wasm from "./index_bg.wasm";
import { __wbg_set_wasm } from "./index_bg.js";
__wbg_set_wasm(wasm);
wasm.__wbindgen_start();
export {
Coordinate, EllipsoidInverseFlattening, EllipsoidSemiMinorAxis, Sphere, init_panic_hook, nested, parseEllipsoid, ring, zuniq
} from "./index_bg.js";(The other names are the exports of the crate the probe was added to, GRID4EARTH/healpix-geo js/; the point is that the list is complete and testns is not in it.)
import { testns } from "pkg" therefore fails to resolve, even though pkg/index.d.ts declares it:
declare function testns__ping(): number;
export let testns: {
ping: typeof testns__ping,
};Expected
index.js should include testns in its re-export list, as the other two targets do for the same input:
--target nodejs—index.js:359:exports.testns = testns;--target web—index.js:349:export const testns = {}in the single-file glue, i.e. directly in the entry.
Notes
- Possibly related to "Ensure Node (CJS/ESM) and bundler entrypoints only expose public exports (no internal import shims)" (0.2.107) and to the
js_namespace-onimplwork in #5154 (0.2.122): the entry re-export list looks like it is built from classes and free functions only, with namespace objects never added to it. - Workaround we're using: emulate namespaces with unit structs (
#[wasm_bindgen(js_name = ns)] pub struct Ns;+ static methods), which works on all three targets.
Source: wasm-bindgen/wasm-bindgen