Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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 {
Expand All @@ -25,8 +28,23 @@ 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no single character variable names. can you come up with something else?

for (const match of matches) {
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);
}

return {
code: s.toString(),
map: s.generateMap({ hires: true }),
};
},
};
}
Expand Down