From d46c19e1b61f99c31bb71dff17ce00738477c545 Mon Sep 17 00:00:00 2001 From: arlo Date: Thu, 25 Jun 2026 17:34:35 +0800 Subject: [PATCH] fix(core): use virtual module instead of fs URL for client injection --- packages/core/src/node/plugins/injection.ts | 46 +++++++++++++++------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/packages/core/src/node/plugins/injection.ts b/packages/core/src/node/plugins/injection.ts index ee95a210..76ee93f1 100644 --- a/packages/core/src/node/plugins/injection.ts +++ b/packages/core/src/node/plugins/injection.ts @@ -3,6 +3,16 @@ import process from 'node:process' import { join, normalize } from 'pathe' import { dirDist } from '../../dirs' +const DEVTOOLS_INJECTION_VIRTUAL_ID = 'virtual:vite-devtools-injection' + +const RESOLVED_DEVTOOLS_INJECTION_VIRTUAL_ID = `\0${DEVTOOLS_INJECTION_VIRTUAL_ID}` + +function resolveDevToolsInjectionEntry(): string { + return process.env.VITE_DEVTOOLS_LOCAL_DEV + ? normalize(join(dirDist, '..', 'src/client/inject/index.ts')) + : normalize(join(dirDist, 'client/inject.js')) +} + export function DevToolsInjection(): Plugin { return { name: 'vite:devtools:injection', @@ -10,20 +20,30 @@ export function DevToolsInjection(): Plugin { apply(_config, env) { return env.command === 'serve' && !env.isSsrBuild }, - transformIndexHtml() { - const fileUrl = process.env.VITE_DEVTOOLS_LOCAL_DEV - ? normalize(join(dirDist, '..', 'src/client/inject/index.ts')) - : normalize(join(dirDist, 'client/inject.js')) - return [ - { - tag: 'script', - attrs: { - src: `/@fs/${fileUrl}`, - type: 'module', + transformIndexHtml: { + order: 'pre', + handler() { + return [ + { + tag: 'script', + attrs: { + type: 'module', + }, + children: `import ${JSON.stringify(DEVTOOLS_INJECTION_VIRTUAL_ID)}`, + injectTo: 'body', }, - injectTo: 'body', - }, - ] + ] + }, + }, + resolveId(id) { + if (id === DEVTOOLS_INJECTION_VIRTUAL_ID) { + return RESOLVED_DEVTOOLS_INJECTION_VIRTUAL_ID + } + }, + load(id) { + if (id === RESOLVED_DEVTOOLS_INJECTION_VIRTUAL_ID) { + return `import(${JSON.stringify(resolveDevToolsInjectionEntry())})\n` + } }, } }