Skip to content

Commit 8fe4030

Browse files
committed
feat(core): introduce @vitejs/devtools/preamble for explicit devtools client injection
1 parent 13352a8 commit 8fe4030

6 files changed

Lines changed: 31 additions & 5 deletions

File tree

docs/guide/index.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,18 @@ pnpm dev
110110

111111
Then open your app in the browser and open the DevTools panel.
112112

113+
#### Projects without an HTML entry
114+
115+
The embedded DevTools client is usually injected through Vite's `transformIndexHtml` hook. If your app does not start from an HTML entry, keep the `DevTools()` plugin enabled and import the client injector manually in your client entry instead:
116+
117+
```ts twoslash
118+
import '@vitejs/devtools/client/inject'
119+
```
120+
121+
This loads the same DevTools client that would normally be added to `index.html`. Put it in a browser entry such as `main.ts` or `entry.client.ts`, not in server-only files or shared SSR entry files.
122+
123+
If your project does have an HTML entry, avoid importing `@vitejs/devtools/client/inject` in addition to the HTML injection, as that would inject the client twice and create duplicate dock elements.
124+
113125
#### Building with the App
114126

115127
You can also generate a static DevTools build alongside your app's build output by enabling the `build.withApp` option:

packages/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"vite": "*"
5656
},
5757
"dependencies": {
58+
"@rolldown/pluginutils": "catalog:deps",
5859
"@vitejs/devtools-kit": "workspace:*",
5960
"@vitejs/devtools-rolldown": "workspace:*",
6061
"@vitejs/devtools-rpc": "workspace:*",

packages/core/src/node/plugins/injection.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,26 @@ import process from 'node:process'
33
import { join, normalize } from 'pathe'
44
import { dirDist } from '../../dirs'
55

6+
export function getClientInjectUrl() {
7+
const fileUrl = process.env.VITE_DEVTOOLS_LOCAL_DEV
8+
? normalize(join(dirDist, '..', 'src/client/inject/index.ts'))
9+
: normalize(join(dirDist, 'client/inject.js'))
10+
11+
return `/@fs/${fileUrl}`
12+
}
13+
614
export function DevToolsInjection(): Plugin {
715
return {
816
name: 'vite:devtools:injection',
917
enforce: 'post',
18+
apply: 'serve',
1019
transformIndexHtml() {
11-
const fileUrl = process.env.VITE_DEVTOOLS_LOCAL_DEV
12-
? normalize(join(dirDist, '..', 'src/client/inject/index.ts'))
13-
: normalize(join(dirDist, 'client/inject.js'))
20+
const fileUrl = getClientInjectUrl()
1421
return [
1522
{
1623
tag: 'script',
1724
attrs: {
18-
src: `/@fs/${fileUrl}`,
25+
src: fileUrl,
1926
type: 'module',
2027
},
2128
injectTo: 'body',

packages/core/tsdown.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const define = {
88
}
99

1010
export default defineConfig({
11-
exports: true,
1211
plugins: [
1312
Vue({
1413
isProduction: true,

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ catalogs:
5252
deps:
5353
'@pnpm/read-project-manifest': ^1001.2.5
5454
'@rolldown/debug': ^1.0.0-rc.11
55+
'@rolldown/pluginutils': ^1.0.0-rc.11
5556
ansis: ^4.2.0
5657
birpc: ^4.0.0
5758
cac: ^7.0.0

0 commit comments

Comments
 (0)