From f47952d55072cc62919d0cf5fd5a8fa40c24e460 Mon Sep 17 00:00:00 2001 From: PendingReality Date: Sat, 6 Dec 2025 10:06:07 +0900 Subject: [PATCH 1/2] refactor: use magic-string for proper source maps in WASM fix Improve the WASM data URL fix to use magic-string for accurate source map generation. This eliminates build warnings and ensures proper debugging support. - Replace string.replace() with MagicString.overwrite() - Return both code and source map from renderChunk - Uses existing magic-string dependency (via vite-plugin-dts) --- vite.config.ts | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/vite.config.ts b/vite.config.ts index dd90c0e..64bf316 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,5 +1,6 @@ import fs from "node:fs"; import path from "node:path"; +import MagicString from "magic-string"; import type { Plugin } from "vite"; import { defineConfig } from "vite"; import dts from "vite-plugin-dts"; @@ -15,6 +16,8 @@ import glsl from "vite-plugin-glsl"; * This plugin transforms: * new URL("data:...", import.meta.url) → new URL("data:...") * + * Uses magic-string to ensure proper source map generation. + * * See: https://github.com/sparkjsdev/spark/issues/95 */ function fixWasmDataUrl(): Plugin { @@ -25,8 +28,22 @@ function fixWasmDataUrl(): Plugin { // The data URL can contain any characters including quotes (escaped) const dataUrlPattern = /new\s+URL\(\s*("data:[^"]*")\s*,\s*import\.meta\.url\s*\)/g; - const result = code.replace(dataUrlPattern, "new URL($1)"); - return result !== code ? result : null; + + const matches = [...code.matchAll(dataUrlPattern)]; + if (matches.length === 0) return null; + + const s = new MagicString(code); + for (const match of matches) { + const start = match.index!; + const end = start + match[0].length; + const replacement = `new URL(${match[1]})`; + s.overwrite(start, end, replacement); + } + + return { + code: s.toString(), + map: s.generateMap({ hires: true }), + }; }, }; } From af9950a4b6bca28bab327d96b48a31bad4d59539 Mon Sep 17 00:00:00 2001 From: PendingReality Date: Sat, 6 Dec 2025 10:45:44 +0900 Subject: [PATCH 2/2] fix: remove non-null assertion and fix line endings - Replace match.index! with proper undefined check - Fix CRLF to LF line endings (biome format) --- vite.config.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/vite.config.ts b/vite.config.ts index 64bf316..2d583d3 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -34,7 +34,8 @@ function fixWasmDataUrl(): Plugin { const s = new MagicString(code); for (const match of matches) { - const start = match.index!; + if (match.index === undefined) continue; + const start = match.index; const end = start + match[0].length; const replacement = `new URL(${match[1]})`; s.overwrite(start, end, replacement);