Skip to content

Commit

Permalink
feat: implement AsyncDisposable (#14648)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Nov 1, 2023
1 parent 1816232 commit 385d580
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 6 deletions.
3 changes: 3 additions & 0 deletions packages/vite/src/node/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,6 @@ export const wildcardHosts = new Set([
export const DEFAULT_DEV_PORT = 5173

export const DEFAULT_PREVIEW_PORT = 4173

export const ASYNC_DISPOSE: typeof Symbol.asyncDispose =
Symbol.asyncDispose || Symbol.for('Symbol.asyncDispose')
2 changes: 1 addition & 1 deletion packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export type ExportsData = {
jsxLoader?: boolean
}

export interface DepsOptimizer {
export interface DepsOptimizer extends AsyncDisposable {
metadata: DepOptimizationMetadata
scanProcessing?: Promise<void>
registerMissingImport: (id: string, resolved: string) => OptimizedDepInfo
Expand Down
5 changes: 5 additions & 0 deletions packages/vite/src/node/optimizer/optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import colors from 'picocolors'
import { createDebugger, getHash } from '../utils'
import { getDepOptimizationConfig } from '../config'
import type { ResolvedConfig, ViteDevServer } from '..'
import { ASYNC_DISPOSE } from '../constants'
import {
addManuallyIncludedOptimizeDeps,
addOptimizedDepInfo,
Expand Down Expand Up @@ -119,6 +120,9 @@ async function createDepsOptimizer(
resetRegisteredIds,
ensureFirstRun,
close,
[ASYNC_DISPOSE]() {
return this.close()
},
options: getDepOptimizationConfig(config, ssr),
}

Expand Down Expand Up @@ -832,6 +836,7 @@ async function createDevSsrDepsOptimizer(
ensureFirstRun: () => {},

close: async () => {},
[ASYNC_DISPOSE]: async () => {},
options: config.ssr.optimizeDeps,
}
devSsrDepsOptimizerMap.set(config, depsOptimizer)
Expand Down
7 changes: 5 additions & 2 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { SourceMap } from 'rollup'
import picomatch from 'picomatch'
import type { Matcher } from 'picomatch'
import type { InvalidatePayload } from 'types/customEvent'
import { ASYNC_DISPOSE, CLIENT_DIR, DEFAULT_DEV_PORT } from '../constants'
import type { CommonServerOptions } from '../http'
import {
httpServerStart,
Expand Down Expand Up @@ -43,7 +44,6 @@ import {
} from '../optimizer'
import { bindCLIShortcuts } from '../shortcuts'
import type { BindCLIShortcutsOptions } from '../shortcuts'
import { CLIENT_DIR, DEFAULT_DEV_PORT } from '../constants'
import type { Logger } from '../logger'
import { printServerUrls } from '../logger'
import { createNoopWatcher, resolveChokidarOptions } from '../watch'
Expand Down Expand Up @@ -182,7 +182,7 @@ export type ServerHook = (
server: ViteDevServer,
) => (() => void) | void | Promise<(() => void) | void>

export interface ViteDevServer {
export interface ViteDevServer extends AsyncDisposable {
/**
* The resolved vite config object
*/
Expand Down Expand Up @@ -535,6 +535,9 @@ export async function _createServer(
}
server.resolvedUrls = null
},
[ASYNC_DISPOSE]() {
return this.close()
},
printUrls() {
if (server.resolvedUrls) {
printServerUrls(
Expand Down
7 changes: 5 additions & 2 deletions packages/vite/src/node/server/pluginContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import type { FSWatcher } from 'chokidar'
import colors from 'picocolors'
import type * as postcss from 'postcss'
import type { Plugin } from '../plugin'
import { ASYNC_DISPOSE, FS_PREFIX } from '../constants'
import {
cleanUrl,
combineSourcemaps,
Expand All @@ -78,7 +79,6 @@ import {
timeFrom,
unwrapId,
} from '../utils'
import { FS_PREFIX } from '../constants'
import type { ResolvedConfig } from '../config'
import { createPluginHookUtils } from '../plugins'
import { buildErrorMessage } from './middlewares/error'
Expand All @@ -105,7 +105,7 @@ export interface PluginContainerOptions {
writeFile?: (name: string, source: string | Uint8Array) => void
}

export interface PluginContainer {
export interface PluginContainer extends AsyncDisposable {
options: InputOptions
getModuleInfo(id: string): ModuleInfo | null
buildStart(options: InputOptions): Promise<void>
Expand Down Expand Up @@ -807,6 +807,9 @@ export async function createPluginContainer(
() => [],
)
},
[ASYNC_DISPOSE]() {
return this.close()
},
}

return container
Expand Down
6 changes: 5 additions & 1 deletion packages/vite/src/node/server/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { WebSocketServer as WebSocketServerRaw_ } from 'ws'
import type { WebSocket as WebSocketTypes } from 'dep-types/ws'
import type { CustomPayload, ErrorPayload, HMRPayload } from 'types/hmrPayload'
import type { InferCustomEventPayload } from 'types/customEvent'
import { ASYNC_DISPOSE } from '../constants'
import type { ResolvedConfig } from '..'
import { isObject } from '../utils'

Expand All @@ -29,7 +30,7 @@ export type WebSocketCustomListener<T> = (
client: WebSocketClient,
) => void

export interface WebSocketServer {
export interface WebSocketServer extends AsyncDisposable {
/**
* Listen on port and host
*/
Expand Down Expand Up @@ -308,5 +309,8 @@ export function createWebSocketServer(
})
})
},
[ASYNC_DISPOSE]() {
return this.close()
},
}
}

0 comments on commit 385d580

Please sign in to comment.