Skip to content
Open
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
46 changes: 33 additions & 13 deletions packages/core/src/node/plugins/injection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,47 @@ 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'))
}
Comment thread
webfansplz marked this conversation as resolved.

export function DevToolsInjection(): Plugin {
return {
name: 'vite:devtools:injection',
enforce: 'post',
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`
Comment thread
webfansplz marked this conversation as resolved.
}
},
}
}
Loading