Skip to content

Commit 63c34fa

Browse files
authored
feat(core): support devtools injection in builds (#580)
1 parent d65db12 commit 63c34fa

7 files changed

Lines changed: 66 additions & 4 deletions

File tree

‎docs/errors/DTK0035.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
outline: deep
3+
---
4+
# DTK0035: Build Injection Requires Assets
5+
6+
## Message
7+
> build.injection requires build.withApp: true.
8+
9+
## Cause
10+
`build.injection` is enabled, but `build.withApp` is disabled.
11+
12+
## Example
13+
```ts
14+
export default defineConfig({
15+
devtools: {
16+
build: {
17+
injection: true,
18+
},
19+
},
20+
})
21+
```
22+
23+
## Fix
24+
Set `devtools.build.withApp: true`.
25+
26+
## Source
27+
- [`packages/core/src/node/plugins/injection.ts`](https://github.com/vitejs/devtools/blob/main/packages/core/src/node/plugins/injection.ts) — `configResolved()` validates asset generation.

‎docs/errors/index.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Emitted by `@vitejs/devtools` and `@vitejs/devtools-kit`.
3333
| [DTK0032](./DTK0032) | error | Dock Launch Error |
3434
| [DTK0033](./DTK0033) | warn | DevTools Mode Persist Failed |
3535
| [DTK0034](./DTK0034) | error | Duplicate DevTools Plugin |
36+
| [DTK0035](./DTK0035) | error | Build Injection Requires Assets |
3637
| [DTK0050](./DTK0050) | error | Integration Install Failed |
3738
| [DTK0051](./DTK0051) | warn | Connection Meta Serve Failed |
3839
| [DTK0052](./DTK0052) | error | Launcher Process Exited Before Ready |

‎docs/guide/index.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,21 @@ export default defineConfig({
143143

144144
Open `/__devtools/` for the full-page UI, or load `/__devtools/embedded.js` to embed the dock in the built app.
145145

146+
To show the dock in the built app, enable `build.injection`:
147+
148+
```ts
149+
export default defineConfig({
150+
devtools: {
151+
build: {
152+
withApp: true,
153+
injection: true,
154+
},
155+
},
156+
})
157+
```
158+
159+
Injection defaults to `false` and requires `build.withApp: true`. Ensure the DevTools assets are served under Vite's `base` when deployed.
160+
146161
## What's next
147162

148163
- **Explore the built-in tools** — inspect Vite development with [Vite DevTools](/vite/) and production builds with [DevTools for Rolldown](/rolldown/).

‎packages/core/src/node/diagnostics.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ export const diagnostics = /* #__PURE__ */ defineDiagnostics({
44
docsBase: 'https://devtools.vite.dev/errors',
55
reporters: [createConsoleReporter()],
66
codes: {
7+
DTK0035: {
8+
why: 'build.injection requires build.withApp: true.',
9+
fix: 'Set devtools.build.withApp to true.',
10+
},
711
DTK0008: {
812
why: 'Client authentication is disabled. Any browser can connect to the devtools and access your server and filesystem.',
913
},

‎packages/core/src/node/plugin-options.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ export interface DevToolsUserOptions {
6161
* @default false
6262
*/
6363
withApp?: boolean
64+
/**
65+
* Inject the dock into the built app. Requires `withApp: true`.
66+
* Ensure the DevTools assets are served under Vite's `base` when deployed.
67+
* @default false
68+
*/
69+
injection?: boolean
6470
/** Output directory relative to root. Defaults to Vite's `build.outDir`. */
6571
outDir?: string
6672
}

‎packages/core/src/node/plugins/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export async function createDevToolsPlugins(
6060
const ui = { branding, embeddedVisibility, dockPreferences }
6161

6262
const plugins = [
63-
DevToolsInjection(),
63+
DevToolsInjection(build),
6464
DevToolsServer(ui, resolvedConfig, renderers),
6565
]
6666

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { Plugin } from 'vite'
2+
import type { DevToolsUserOptions } from '../plugin-options'
23
import { DEVTOOLS_MOUNT_PATH } from '@vitejs/devtools-kit/constants'
4+
import { diagnostics } from '../diagnostics'
35

46
/**
57
* Inject the `@devframes/hub-ui` embedded bootstrap into the host app's HTML.
@@ -20,14 +22,21 @@ import { DEVTOOLS_MOUNT_PATH } from '@vitejs/devtools-kit/constants'
2022
* keeps `<base>embedded.js` out of Vite's graph entirely, so the browser
2123
* fetches it straight from the hub with its real URL intact.
2224
*/
23-
export function DevToolsInjection(): Plugin {
24-
const src = `${DEVTOOLS_MOUNT_PATH}embedded.js`
25+
export function DevToolsInjection(build?: DevToolsUserOptions['build']): Plugin {
26+
let src = `${DEVTOOLS_MOUNT_PATH}embedded.js`
2527

2628
return {
2729
name: 'vite:devtools:injection',
2830
enforce: 'post',
2931
apply(_config, env) {
30-
return env.command === 'serve' && !env.isSsrBuild
32+
return !env.isSsrBuild && (env.command === 'serve' || build?.injection === true)
33+
},
34+
configResolved(config) {
35+
if (config.command === 'build') {
36+
if (!build?.withApp)
37+
throw diagnostics.DTK0035({})
38+
src = `${config.base.replace(/\/+$/, '')}${DEVTOOLS_MOUNT_PATH}embedded.js`
39+
}
3140
},
3241
transformIndexHtml: {
3342
order: 'pre',

0 commit comments

Comments
 (0)