Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"./client/webcomponents": "./dist/client/webcomponents.js",
"./config": "./dist/config.js",
"./dirs": "./dist/dirs.js",
"./integration": "./dist/integration.js",
"./internal": "./dist/internal.js",
"./package.json": "./package.json"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/integration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { DevToolsIntegration, runDevTools } from './node/plugins/integration'
export type { DevToolsIntegrationOptions } from './node/plugins/integration'
4 changes: 4 additions & 0 deletions packages/core/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { isObject } from 'devframe/node'

export interface DevToolsConfig extends Partial<StartOptions> {
enabled: boolean
/**
* Vite environments to enable DevTools for. Defaults to all environments.
*/
environments?: string[]
/**
* Disable client authentication.
*
Expand Down
55 changes: 55 additions & 0 deletions packages/core/src/node/plugins/integration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import type { Plugin, ResolvedConfig, ViteBuilder } from 'vite'
import type { ResolvedDevToolsConfig } from '../config'

type DevToolsEnvironment = ResolvedConfig['environments'][string]

export interface DevToolsIntegrationOptions {
config: ResolvedConfig
}

function getDevToolsEnvironments(config: ResolvedConfig): DevToolsEnvironment[] {
const devToolsConfig = config.devtools as ResolvedDevToolsConfig
const environmentNames = devToolsConfig.config.environments ?? Object.keys(config.environments)
const environments: DevToolsEnvironment[] = []

for (const environmentName of environmentNames) {
const environment = config.environments[environmentName]
if (environment) {
environments.push(environment)
}
}

return environments
}

export async function runDevTools(builder: unknown) {
const config = (builder as ViteBuilder).config
for (const _environment of getDevToolsEnvironments(config)) {
try {
const { start } = await import('@vitejs/devtools/cli-commands')
await start(config.devtools.config)
}
catch (error: any) {
config.logger.error(
`Failed to run Vite DevTools: ${error?.message || error?.stack || error}`,
{ error },
)
}
}
}

export function DevToolsIntegration(_options: DevToolsIntegrationOptions): Plugin {
return {
name: 'vite:devtools:integration',
apply: 'build',
configResolved: {
order: 'post',
handler(config) {
// Enable `rolldownOptions.devtools` if the environment is selected, or for all environments by default.
for (const environment of getDevToolsEnvironments(config)) {
environment.build.rolldownOptions.devtools ??= {}
}
},
},
}
}
1 change: 1 addition & 0 deletions packages/core/tsdown.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export default defineConfig({
tsconfig: '../../tsconfig.base.json',
entry: {
'index': 'src/index.ts',
'integration': 'src/integration.ts',
'internal': 'src/internal.ts',
'dirs': 'src/dirs.ts',
'cli': 'src/node/cli.ts',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// #region Interfaces
export interface DevToolsConfig extends Partial<StartOptions> {
enabled: boolean;
environments?: string[];
clientAuth?: boolean;
clientAuthTokens?: string[];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Generated by tsnapi — public API snapshot of `@vitejs/devtools/integration`
*/
// #region Interfaces
export interface DevToolsIntegrationOptions {
config: ResolvedConfig;
}
// #endregion

// #region Functions
export declare function DevToolsIntegration(_: DevToolsIntegrationOptions): Plugin;
export declare function runDevTools(_: unknown): Promise<void>;
// #endregion
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Generated by tsnapi — public API snapshot of `@vitejs/devtools/integration`
*/
// #region Functions
export function DevToolsIntegration(_) {}
export async function runDevTools(_) {}
// #endregion
Loading