UNPKG

rwsdk

Version:

Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime

26 lines (25 loc) 862 B
import MagicString from "magic-string"; import { normalizeModulePath } from "../lib/normalizeModulePath.mjs"; export const injectVitePreamble = ({ clientEntryPoints, projectRootDir, }) => ({ name: "rwsdk:inject-vite-preamble", apply: "serve", transform(code, id) { if (this.environment.name !== "client") { return; } const normalizedId = normalizeModulePath(id, projectRootDir); if (!clientEntryPoints.has(normalizedId)) { return; } // Only inject preamble in development mode if (process.env.NODE_ENV !== "development") { return; } const s = new MagicString(code); s.prepend(`import 'virtual:vite-preamble';\n`); return { code: s.toString(), map: s.generateMap({ hires: true }), }; }, });