From a629a1fa7929c4b203f47472731fb9b7fec8fbe2 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Fri, 13 Mar 2026 09:02:12 -0700 Subject: [PATCH] Fix wasm-pack output for Vite 8/rolldown production builds Rewrite generated yaak_templates.js to use Vite's ?init import style instead of the ES Module Integration style that rolldown doesn't support. Co-Authored-By: Claude Opus 4.6 --- crates/yaak-templates/build-wasm.cjs | 19 +++++++++++++++++++ crates/yaak-templates/pkg/yaak_templates.js | 9 +++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/crates/yaak-templates/build-wasm.cjs b/crates/yaak-templates/build-wasm.cjs index 4f86941b..23611d0d 100644 --- a/crates/yaak-templates/build-wasm.cjs +++ b/crates/yaak-templates/build-wasm.cjs @@ -1,4 +1,6 @@ const { execSync } = require('node:child_process'); +const fs = require('node:fs'); +const path = require('node:path'); if (process.env.SKIP_WASM_BUILD === '1') { console.log('Skipping wasm-pack build (SKIP_WASM_BUILD=1)'); @@ -6,3 +8,20 @@ if (process.env.SKIP_WASM_BUILD === '1') { } execSync('wasm-pack build --target bundler', { stdio: 'inherit' }); + +// Rewrite the generated entry to use Vite's ?init import style instead of +// the ES Module Integration style that wasm-pack generates, which Vite/rolldown +// does not support in production builds. +const entry = path.join(__dirname, 'pkg', 'yaak_templates.js'); +fs.writeFileSync( + entry, + [ + 'import init from "./yaak_templates_bg.wasm?init";', + 'export * from "./yaak_templates_bg.js";', + 'import * as bg from "./yaak_templates_bg.js";', + 'const instance = await init({ "./yaak_templates_bg.js": bg });', + 'bg.__wbg_set_wasm(instance.exports);', + 'instance.exports.__wbindgen_start();', + '', + ].join('\n'), +); diff --git a/crates/yaak-templates/pkg/yaak_templates.js b/crates/yaak-templates/pkg/yaak_templates.js index 8d2a7738..aa78c841 100644 --- a/crates/yaak-templates/pkg/yaak_templates.js +++ b/crates/yaak-templates/pkg/yaak_templates.js @@ -1,5 +1,6 @@ -import * as wasm from "./yaak_templates_bg.wasm"; +import init from "./yaak_templates_bg.wasm?init"; export * from "./yaak_templates_bg.js"; -import { __wbg_set_wasm } from "./yaak_templates_bg.js"; -__wbg_set_wasm(wasm); -wasm.__wbindgen_start(); +import * as bg from "./yaak_templates_bg.js"; +const instance = await init({ "./yaak_templates_bg.js": bg }); +bg.__wbg_set_wasm(instance.exports); +instance.exports.__wbindgen_start();