Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: rename to HMRPayload to HotPayload and remove HMRBroadcaster #16875

Merged
merged 2 commits into from
May 24, 2024
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
4 changes: 2 additions & 2 deletions docs/guide/api-vite-environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class DevEnvironment {
* Communication channel to send and receive messages from the
* associated module runner in the target runtime.
*/
hot: HMRChannel | null
hot: HotChannel | null
/**
* Graph of module nodes, with the imported relationship between
* processed modules and the cached result of the processed code.
Expand Down Expand Up @@ -816,7 +816,7 @@ export interface ModuleRunnerHMRConnection {
* Configure how HMR is handled when this connection triggers an update.
* This method expects that the connection will start listening for HMR updates and call this callback when it's received.
*/
onUpdate(callback: (payload: HMRPayload) => void): void
onUpdate(callback: (payload: HotPayload) => void): void
}
```

Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/client/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ErrorPayload, HMRPayload } from 'types/hmrPayload'
import type { ErrorPayload, HotPayload } from 'types/hotPayload'
import type { ViteHotContext } from 'types/hot'
import type { InferCustomEventPayload } from 'types/customEvent'
import { HMRClient, HMRContext } from '../shared/hmr'
Expand Down Expand Up @@ -172,7 +172,7 @@ const hmrClient = new HMRClient(
},
)

async function handleMessage(payload: HMRPayload) {
async function handleMessage(payload: HotPayload) {
switch (payload.type) {
case 'connected':
console.debug(`[vite] connected.`)
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/client/overlay.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ErrorPayload } from 'types/hmrPayload'
import type { ErrorPayload } from 'types/hotPayload'

// injected by the hmr plugin when served
declare const __BASE__: string
Expand Down
10 changes: 5 additions & 5 deletions packages/vite/src/module-runner/hmrHandler.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import type { HMRPayload } from 'types/hmrPayload'
import type { HotPayload } from 'types/hotPayload'
import { slash, unwrapId } from '../shared/utils'
import type { ModuleRunner } from './runner'

// updates to HMR should go one after another. It is possible to trigger another update during the invalidation for example.
export function createHMRHandler(
runner: ModuleRunner,
): (payload: HMRPayload) => Promise<void> {
): (payload: HotPayload) => Promise<void> {
const queue = new Queue()
return (payload) => queue.enqueue(() => handleHMRPayload(runner, payload))
return (payload) => queue.enqueue(() => handleHotPayload(runner, payload))
}

export async function handleHMRPayload(
export async function handleHotPayload(
runner: ModuleRunner,
payload: HMRPayload,
payload: HotPayload,
): Promise<void> {
const hmrClient = runner.hmrClient
if (!hmrClient || runner.isDestroyed()) return
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/module-runner/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ViteHotContext } from 'types/hot'
import type { HMRPayload } from 'types/hmrPayload'
import type { HotPayload } from 'types/hotPayload'
import type { HMRConnection, HMRLogger } from '../shared/hmr'
import type {
DefineImportMetadata,
Expand All @@ -24,7 +24,7 @@ export interface ModuleRunnerHMRConnection extends HMRConnection {
* Configure how HMR is handled when this connection triggers an update.
* This method expects that connection will start listening for HMR updates and call this callback when it's received.
*/
onUpdate(callback: (payload: HMRPayload) => void): void
onUpdate(callback: (payload: HotPayload) => void): void
}

export interface ModuleRunnerImportMeta extends ImportMeta {
Expand Down
11 changes: 5 additions & 6 deletions packages/vite/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,9 @@ export type {
export type { HmrOptions, HmrContext, HotUpdateContext } from './server/hmr'

export type {
HMRBroadcaster,
HMRChannel,
ServerHMRChannel,
HMRBroadcasterClient,
HotChannel,
ServerHotChannel,
HotChannelClient,
} from './server/hmr'

export type { FetchFunction, FetchResult } from 'vite/module-runner'
Expand All @@ -156,15 +155,15 @@ export type { ServerModuleRunnerOptions } from './ssr/runtime/serverModuleRunner
export type { BindCLIShortcutsOptions, CLIShortcut } from './shortcuts'

export type {
HMRPayload,
HotPayload,
ConnectedPayload,
UpdatePayload,
Update,
FullReloadPayload,
CustomPayload,
PrunePayload,
ErrorPayload,
} from 'types/hmrPayload'
} from 'types/hotPayload'
export type {
CustomEventMap,
InferCustomEventPayload,
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ export async function reloadOnTsconfigChange(
// any json file in the tsconfig cache could have been used to compile ts
if (
path.basename(changedFile) === 'tsconfig.json' ||
changedFile.endsWith('.json') /*
changedFile.endsWith('.json') /*
TODO: the tsconfckCache?.clear() line will make this fail if there are several servers
we may need a cache per server if we don't want all servers to share the reset
leaving it commented for now because it should still work
Expand All @@ -500,7 +500,7 @@ export async function reloadOnTsconfigChange(
tsconfckCache?.clear()

// force full reload
server.hot.send({
server.ws.send({
type: 'full-reload',
path: '*',
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ async function getDevEnvironment(
// @ts-expect-error This plugin requires a ViteDevServer instance.
config.plugins = config.plugins.filter((p) => !p.name.includes('pre-alias'))

const environment = new DevEnvironment('client', config)
const environment = new DevEnvironment('client', config, { hot: false })
await environment.init()

return environment
Expand Down
29 changes: 14 additions & 15 deletions packages/vite/src/node/server/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import {
} from '../optimizer/optimizer'
import { resolveEnvironmentPlugins } from '../plugin'
import { EnvironmentModuleGraph } from './moduleGraph'
import type { HMRChannel } from './hmr'
import { createNoopHMRChannel, getShortName, updateModules } from './hmr'
import type { HotChannel } from './hmr'
import { createNoopHotChannel, getShortName, updateModules } from './hmr'
import type { TransformResult } from './transformRequest'
import { transformRequest } from './transformRequest'
import type { EnvironmentPluginContainer } from './pluginContainer'
Expand All @@ -32,7 +32,7 @@ import {
import type { RemoteEnvironmentTransport } from './environmentTransport'

export interface DevEnvironmentSetup {
hot?: false | HMRChannel
hot: false | HotChannel
watcher?: FSWatcher
options?: EnvironmentOptions
runner?: FetchModuleOptions & {
Expand Down Expand Up @@ -90,24 +90,24 @@ export class DevEnvironment extends BaseEnvironment {
_crawlEndFinder: CrawlEndFinder

/**
* HMR channel for this environment. If not provided or disabled,
* Hot channel for this environment. If not provided or disabled,
* it will be a noop channel that does nothing.
*
* @example
* environment.hot.send({ type: 'full-reload' })
*/
hot: HMRChannel
hot: HotChannel
constructor(
name: string,
config: ResolvedConfig,
setup?: DevEnvironmentSetup,
setup: DevEnvironmentSetup,
) {
let options =
config.environments[name] ?? getDefaultResolvedEnvironmentOptions(config)
if (setup?.options) {
if (setup.options) {
options = mergeConfig(
options,
setup?.options,
setup.options,
) as ResolvedEnvironmentOptions
}
super(name, config, options)
Expand All @@ -118,17 +118,16 @@ export class DevEnvironment extends BaseEnvironment {
this.pluginContainer!.resolveId(url, undefined),
)

this.hot = setup?.hot || createNoopHMRChannel()
this.watcher = setup?.watcher
this.hot = setup.hot || createNoopHotChannel()
this.watcher = setup.watcher

this._onCrawlEndCallbacks = []
this._crawlEndFinder = setupOnCrawlEnd(() => {
this._onCrawlEndCallbacks.forEach((cb) => cb())
})

const ssrRunnerOptions = setup?.runner || {}
this._ssrRunnerOptions = ssrRunnerOptions
setup?.runner?.transport?.register(this)
this._ssrRunnerOptions = setup.runner || {}
setup.runner?.transport?.register(this)

this.hot.on('vite:invalidate', async ({ path, message }) => {
invalidateModule(this, {
Expand All @@ -138,8 +137,8 @@ export class DevEnvironment extends BaseEnvironment {
})

const { optimizeDeps } = this.options.dev
if (setup?.depsOptimizer) {
this.depsOptimizer = setup?.depsOptimizer
if (setup.depsOptimizer) {
this.depsOptimizer = setup.depsOptimizer
} else if (!isDepOptimizationEnabled(optimizeDeps)) {
this.depsOptimizer = undefined
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { asyncFunctionDeclarationPaddingLineCount } from '../../../shared/utils'
export function createNodeDevEnvironment(
name: string,
config: ResolvedConfig,
options?: DevEnvironmentSetup,
options: DevEnvironmentSetup,
): DevEnvironment {
return new DevEnvironment(name, config, {
...options,
Expand Down
Loading