Skip to content

Commit 489d178

Browse files
authored
feat(core): devtools config (#173)
1 parent 403cfd3 commit 489d178

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

‎packages/core/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './node/config'
12
export { createDevToolsContext } from './node/context'
23
export { DevTools } from './node/plugins'
34
export { createDevToolsMiddleware } from './node/server'

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import type { StartOptions } from './cli-commands'
2+
import { isObject } from './utils'
3+
4+
export interface DevToolsConfig extends Partial<StartOptions> {
5+
enabled: boolean
6+
/**
7+
* Disable client authentication.
8+
*
9+
* Beware that if you disable client authentication,
10+
* any browsers can connect to the devtools and access to your server and filesystem.
11+
* (including other devices, if you open server `host` option to LAN or WAN)
12+
*
13+
* @default true
14+
*/
15+
clientAuth?: boolean
16+
}
17+
18+
export interface ResolvedDevToolsConfig {
19+
config: Omit<DevToolsConfig, 'enabled'> & { host: string }
20+
enabled: boolean
21+
}
22+
23+
export function normalizeDevToolsConfig(
24+
config: DevToolsConfig | boolean | undefined,
25+
host: string,
26+
): ResolvedDevToolsConfig {
27+
return {
28+
enabled: config === true || !!(config && config.enabled),
29+
config: {
30+
...(isObject(config) ? config : {}),
31+
clientAuth: isObject(config) ? (config.clientAuth ?? true) : true,
32+
host: isObject(config) ? (config.host ?? host) : host,
33+
},
34+
}
35+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function isObject(value: unknown): value is Record<string, any> {
2+
return Object.prototype.toString.call(value) === '[object Object]'
3+
}

‎test/exports/@vitejs/devtools.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
createDevToolsContext: function
33
createDevToolsMiddleware: function
44
DevTools: function
5+
normalizeDevToolsConfig: function
56
./cli-commands:
67
build: function
78
start: function

0 commit comments

Comments
 (0)