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
30 changes: 30 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
import fs from "node:fs";
import path from "node:path";
import type { Plugin } from "vite";
import { defineConfig } from "vite";
import dts from "vite-plugin-dts";
import glsl from "vite-plugin-glsl";

/**
* Vite plugin to fix WASM data URL compatibility with webpack/Next.js.
*
* wasm-pack generates code like: new URL("data:...", import.meta.url)
* The import.meta.url argument is unnecessary for data: URLs and causes
* webpack/Vite to incorrectly try to rewrite the URL as a file path.
*
* This plugin transforms:
* new URL("data:...", import.meta.url) → new URL("data:...")
*
* See: https://github.com/sparkjsdev/spark/issues/95
*/
function fixWasmDataUrl(): Plugin {
return {
name: "fix-wasm-data-url",
renderChunk(code) {
// Match: new URL("data:...", import.meta.url)
// 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 assetsDirectory = "examples/assets";
const localAssetsDirectoryExist = fs.existsSync(assetsDirectory);
if (!localAssetsDirectoryExist) {
Expand Down Expand Up @@ -32,6 +59,9 @@ export default defineConfig(({ mode }) => {
}),

dts({ outDir: "dist/types" }),

// Fix webpack/Next.js compatibility for WASM data URLs
fixWasmDataUrl(),
{
name: "serve-node-modules-alias",
configureServer(server) {
Expand Down