Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Dec 1, 2023
1 parent cdb98e4 commit 980ab59
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 55 deletions.
22 changes: 21 additions & 1 deletion packages/vite/src/node/ssr/runtime/__tests__/server-hmr.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { afterEach, describe, expect , it } from 'vitest'
import { afterEach, describe, expect, it } from 'vitest'
import { createTestClient, editFile, resolvePath, waitUntil } from './utils'

describe('vite-server-runetime hmr works as expected', async () => {
Expand All @@ -25,6 +25,26 @@ describe('vite-server-runetime hmr works as expected', async () => {
expect(mod.hmr).toHaveProperty('accept')
})

it('correctly populates hmr client', async () => {
const mod = await runtime.executeId('./fixtures/d')
expect(mod.d).toBe('a')

const fixtureC = resolvePath(import.meta.url, './fixtures/c.ts')
const fixtureD = resolvePath(import.meta.url, './fixtures/d.ts')

expect(runtime.hmrClient!.hotModulesMap.size).toBe(2)
expect(runtime.hmrClient!.dataMap.size).toBe(2)
expect(runtime.hmrClient!.ctxToListenersMap.size).toBe(2)

for (const fixture of [fixtureC, fixtureD]) {
expect(runtime.hmrClient!.hotModulesMap.has(fixture)).toBe(true)
expect(runtime.hmrClient!.dataMap.has(fixture)).toBe(true)
expect(runtime.hmrClient!.ctxToListenersMap.has(fixture)).toBe(true)
}
})

it('correctly invalidates tree when one module is updated')

it('correctly does full-reload', async () => {
const mod = await runtime.executeId(
'./fixtures/circular/circular-index.js',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,4 @@ describe('vite-server-runetime initialization', async () => {
const mod = await runtime.executeId('./fixtures/simple.js')
expect(mod.test).toEqual('I am initialized')
})

it('correctly populates hmr client', async () => {
const mod = await runtime.executeId('./fixtures/d')
expect(mod.d).toBe('a')

const fixtureC = resolvePath(import.meta.url, './fixtures/c.ts')
const fixtureD = resolvePath(import.meta.url, './fixtures/d.ts')

expect(runtime.hmrClient!.hotModulesMap.size).toBe(2)
expect(runtime.hmrClient!.dataMap.size).toBe(2)
expect(runtime.hmrClient!.ctxToListenersMap.size).toBe(2)

for (const fixture of [fixtureC, fixtureD]) {
expect(runtime.hmrClient!.hotModulesMap.has(fixture)).toBe(true)
expect(runtime.hmrClient!.dataMap.has(fixture)).toBe(true)
expect(runtime.hmrClient!.ctxToListenersMap.has(fixture)).toBe(true)
}
})
})
15 changes: 15 additions & 0 deletions packages/vite/src/node/ssr/runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,18 @@ export { ESModulesRunner } from './esmRunner'
export { handleHMRUpdate } from './hmr/hmrHandler'
export { ServerHMRConnector } from './hmr/serverConnector'
export { WorkerPortHMRConnector } from './hmr/workerPortConnector'

export type {
ViteModuleRunner,
ViteRuntimeModuleContext,
ModuleCache,
FetchResult,
ViteServerClientOptions,
} from './types'
export {
ssrDynamicImportKey,
ssrExportAllKey,
ssrImportKey,
ssrImportMetaKey,
ssrModuleExportsKey,
} from './constants'
14 changes: 2 additions & 12 deletions packages/vite/src/node/ssr/runtime/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { posix } from 'node:path'
import { pathToFileURL } from 'node:url'
import type { ViteHotContext } from 'types/hot'
import { HMRClient, HMRContext } from '../../../shared/hmr'
import { ModuleCacheMap } from './moduleCache'
import type {
FetchResult,
HotContext,
ModuleCache,
SSRImportMetadata,
ViteModuleRunner,
Expand All @@ -17,7 +17,6 @@ import {
createImportMetaEnvProxy,
isPrimitive,
normalizeModuleId,
slash,
unwrapId,
} from './utils'
import {
Expand Down Expand Up @@ -63,15 +62,6 @@ export class ViteRuntime {
}
}

public async executeFile<T = any>(
file: string,
entrypoint = false,
): Promise<T> {
const url = `/@fs/${slash(posix.resolve(file))}`
const fetchedModule = await this.cachedModule(url)
return await this.cachedRequest(url, fetchedModule, [], { entrypoint })
}

public async executeId<T = any>(
rawId: string,
entrypoint = false,
Expand Down Expand Up @@ -259,7 +249,7 @@ export class ViteRuntime {

Object.assign(mod, { exports })

let hotContext: HotContext | undefined
let hotContext: ViteHotContext | undefined
if (this.hmrClient) {
Object.defineProperty(meta, 'hot', {
enumerable: true,
Expand Down
23 changes: 1 addition & 22 deletions packages/vite/src/node/ssr/runtime/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { ViteHotContext } from 'types/hot'
import type { HMRConnection } from '../../../shared/hmr'
import type { ModuleCacheMap } from './moduleCache'
import type { ViteRuntime } from './runtime'
import type {
ssrDynamicImportKey,
ssrExportAllKey,
Expand Down Expand Up @@ -34,7 +33,7 @@ export interface SSRImportMetadata extends DefineImportMetadata {
export interface ViteRuntimeImportMeta extends ImportMeta {
url: string
env: ImportMetaEnv
hot?: HotContext
hot?: ViteHotContext
[key: string]: any
}

Expand Down Expand Up @@ -82,31 +81,11 @@ export interface FetchResult {
type?: 'module' | 'commonjs' | 'builtin'
}

export type HotContext = Omit<ViteHotContext, 'acceptDeps' | 'decline'>

export type FetchFunction = (
id: string,
importer?: string,
) => Promise<FetchResult>

export type ResolveIdFunction = (
id: string,
importer?: string,
) => Awaitable<ViteServerClientResolveId | null | undefined | void>

export type CreateHotContextFunction = (
client: ViteRuntime,
url: string,
) => HotContext

export interface ViteServerClientResolveId {
external?: boolean | 'absolute' | 'relative'
id: string
meta?: Record<string, any> | null
moduleSideEffects?: boolean | 'no-treeshake' | null
syntheticNamedExports?: boolean | string | null
}

export interface ViteServerClientOptions {
root: string
fetchModule: FetchFunction
Expand Down
8 changes: 6 additions & 2 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ import {
findNearestPackageData,
resolvePackageData,
} from './packages'
import { slash } from './ssr/runtime/utils'
import type { CommonServerOptions } from '.'

export { wrapId, unwrapId, slash } from './ssr/runtime/utils'
export { wrapId, unwrapId } from './ssr/runtime/utils'

/**
* Inlined to keep `@rollup/pluginutils` in devDependencies
Expand All @@ -56,6 +55,11 @@ export const createFilter = _createFilter as (
options?: { resolve?: string | false | null },
) => (id: string | unknown) => boolean

const windowsSlashRE = /\\/g
export function slash(p: string): string {
return p.replace(windowsSlashRE, '/')
}

const replaceSlashOrColonRE = /[/:]/g
const replaceDotRE = /\./g
const replaceNestedIdRE = /(\s*>\s*)/g
Expand Down

0 comments on commit 980ab59

Please sign in to comment.