diff --git a/examples/react-server/src/features/test/plugin.ts b/examples/react-server/src/features/test/plugin.ts index d5fa68e6..676bf1d4 100644 --- a/examples/react-server/src/features/test/plugin.ts +++ b/examples/react-server/src/features/test/plugin.ts @@ -1,3 +1,4 @@ +import MagicString from "magic-string"; import type { Plugin } from "vite"; import { $__global } from "../../global"; @@ -6,6 +7,7 @@ export function vitePluginTestReactServerStream(): Plugin { return { name: vitePluginTestReactServerStream.name, + enforce: "pre", resolveId(source, _importer, _options) { if (source.startsWith(prefix)) { return "\0" + source; @@ -37,6 +39,21 @@ export function vitePluginTestReactServerStream(): Plugin { } return; }, + transform(code) { + // Workaround module runner `import.meta.env` usage inside Vitest + // Error: [module runner] Dynamic access of "import.meta.env" is not supported. Please, use "import.meta.env.DEV" instead. + if (code.includes("import.meta.env.DEV")) { + const output = new MagicString(code); + for (const match of code.matchAll(/\bimport\.meta\.env\.DEV\b/dg)) { + const [start, end] = match.indices![0]; + output.update(start, end, "true"); + } + if (output.hasChanged()) { + return { code: output.toString(), map: output.generateMap() }; + } + } + return; + }, }; }