diff --git a/src/vite.ts b/src/vite.ts index 8b2cacf..d6d130a 100644 --- a/src/vite.ts +++ b/src/vite.ts @@ -18,9 +18,12 @@ * }); */ -import type { Plugin, ResolvedConfig } from "vite"; +import type { Plugin, ResolvedConfig, ViteDevServer } from "vite"; import type { SolidGrabPluginOptions } from "./types.js"; +const VIRTUAL_INIT = "virtual:solid-grab-init"; +const RESOLVED_VIRTUAL_INIT = "\0" + VIRTUAL_INIT; + // ── Regex-based JSX transform ──────────────────────────────────────── // // We intentionally avoid a full AST parse here for speed. Solid JSX is @@ -191,6 +194,17 @@ export default function solidGrab( isDev = config.command === "serve" || config.mode === "development"; }, + // Virtual module that imports the runtime — resolved by Vite's pipeline + resolveId(id) { + if (id === VIRTUAL_INIT) return RESOLVED_VIRTUAL_INIT; + }, + + load(id) { + if (id === RESOLVED_VIRTUAL_INIT) { + return `import "solid-grab";`; + } + }, + transform(code, id) { // Only transform in dev mode if (!isDev) return null; @@ -217,17 +231,27 @@ export default function solidGrab( }; }, - // Auto-inject the runtime script in dev mode - transformIndexHtml(html) { - if (!autoImport) return html; - if (!isDev) return html; - - // Inject a module script that imports solid-grab - const script = ``; + // Serve the virtual init module through Vite's transform pipeline + configureServer(server: ViteDevServer) { + server.middlewares.use((req, _res, next) => { + // Rewrite the URL so Vite's built-in module serving handles it + if (req.url === "/@solid-grab/init") { + req.url = `/@id/${VIRTUAL_INIT}`; + } + next(); + }); + }, - return html.replace("", `${script}\n`); + // Inject a