Skip to content

Commit de302e0

Browse files
webfansplzclaudeantfu
authored
feat(core): vite integration plugin & environments config (#300)
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Co-authored-by: Anthony Fu <github@antfu.me>
1 parent 071d23e commit de302e0

8 files changed

Lines changed: 84 additions & 0 deletions

File tree

packages/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"./client/webcomponents": "./dist/client/webcomponents.js",
2828
"./config": "./dist/config.js",
2929
"./dirs": "./dist/dirs.js",
30+
"./integration": "./dist/integration.js",
3031
"./internal": "./dist/internal.js",
3132
"./package.json": "./package.json"
3233
},

packages/core/src/integration.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { DevToolsIntegration, runDevTools } from './node/plugins/integration'
2+
export type { DevToolsIntegrationOptions } from './node/plugins/integration'

packages/core/src/node/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { isObject } from 'devframe/node'
33

44
export interface DevToolsConfig extends Partial<StartOptions> {
55
enabled: boolean
6+
/**
7+
* Vite environments to enable DevTools for. Defaults to all environments.
8+
*/
9+
environments?: string[]
610
/**
711
* Disable client authentication.
812
*
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import type { Plugin, ResolvedConfig, ViteBuilder } from 'vite'
2+
import type { ResolvedDevToolsConfig } from '../config'
3+
4+
type DevToolsEnvironment = ResolvedConfig['environments'][string]
5+
6+
export interface DevToolsIntegrationOptions {
7+
config: ResolvedConfig
8+
}
9+
10+
function getDevToolsEnvironments(config: ResolvedConfig): DevToolsEnvironment[] {
11+
const devToolsConfig = config.devtools as ResolvedDevToolsConfig
12+
const environmentNames = devToolsConfig.config.environments ?? Object.keys(config.environments)
13+
const environments: DevToolsEnvironment[] = []
14+
15+
for (const environmentName of environmentNames) {
16+
const environment = config.environments[environmentName]
17+
if (environment) {
18+
environments.push(environment)
19+
}
20+
}
21+
22+
return environments
23+
}
24+
25+
export async function runDevTools(builder: unknown) {
26+
const config = (builder as ViteBuilder).config
27+
for (const _environment of getDevToolsEnvironments(config)) {
28+
try {
29+
const { start } = await import('@vitejs/devtools/cli-commands')
30+
await start(config.devtools.config)
31+
}
32+
catch (error: any) {
33+
config.logger.error(
34+
`Failed to run Vite DevTools: ${error?.message || error?.stack || error}`,
35+
{ error },
36+
)
37+
}
38+
}
39+
}
40+
41+
export function DevToolsIntegration(_options: DevToolsIntegrationOptions): Plugin {
42+
return {
43+
name: 'vite:devtools:integration',
44+
apply: 'build',
45+
configResolved: {
46+
order: 'post',
47+
handler(config) {
48+
// Enable `rolldownOptions.devtools` if the environment is selected, or for all environments by default.
49+
for (const environment of getDevToolsEnvironments(config)) {
50+
environment.build.rolldownOptions.devtools ??= {}
51+
}
52+
},
53+
},
54+
}
55+
}

packages/core/tsdown.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export default defineConfig({
5353
tsconfig: '../../tsconfig.base.json',
5454
entry: {
5555
'index': 'src/index.ts',
56+
'integration': 'src/integration.ts',
5657
'internal': 'src/internal.ts',
5758
'dirs': 'src/dirs.ts',
5859
'cli': 'src/node/cli.ts',

test/__snapshots__/tsnapi/@vitejs/devtools/config.snapshot.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// #region Interfaces
55
export interface DevToolsConfig extends Partial<StartOptions> {
66
enabled: boolean;
7+
environments?: string[];
78
clientAuth?: boolean;
89
clientAuthTokens?: string[];
910
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Generated by tsnapi — public API snapshot of `@vitejs/devtools/integration`
3+
*/
4+
// #region Interfaces
5+
export interface DevToolsIntegrationOptions {
6+
config: ResolvedConfig;
7+
}
8+
// #endregion
9+
10+
// #region Functions
11+
export declare function DevToolsIntegration(_: DevToolsIntegrationOptions): Plugin;
12+
export declare function runDevTools(_: unknown): Promise<void>;
13+
// #endregion
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Generated by tsnapi — public API snapshot of `@vitejs/devtools/integration`
3+
*/
4+
// #region Functions
5+
export function DevToolsIntegration(_) {}
6+
export async function runDevTools(_) {}
7+
// #endregion

0 commit comments

Comments
 (0)