Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jan 18, 2024
1 parent c9e22b1 commit 0ebf086
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 43 deletions.
9 changes: 3 additions & 6 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -733,16 +733,13 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
// normalize and rewrite accepted urls
const normalizedAcceptedUrls = new Set<string>()
for (const { url, start, end } of acceptedUrls) {
// serve url: /file/path.js (absolute to the server)
// fs path: /Users/name/root/file/path.js (absolute to the file system)
const [normalizedServeUrl] = await moduleGraph.resolveUrl(
const [normalized] = await moduleGraph.resolveUrl(
toAbsoluteUrl(url),
ssr,
)

normalizedAcceptedUrls.add(normalizedServeUrl)
// SSR relies on absolute FS paths, not URLs
str().overwrite(start, end, JSON.stringify(normalizedServeUrl), {
normalizedAcceptedUrls.add(normalized)
str().overwrite(start, end, JSON.stringify(normalized), {
contentOnly: true,
})
}
Expand Down
2 changes: 2 additions & 0 deletions packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ export function updateModules(
? isExplicitImportRequired(acceptedVia.url)
: false,
isWithinCircularImport,
// browser modules are invalidated by changing ?t= query,
// but in ssr we control the module system, so we can directly remove them form cache
ssrInvalidates: getSSRInvalidatedImporters(acceptedVia),
}),
),
Expand Down
25 changes: 2 additions & 23 deletions packages/vite/src/node/ssr/runtime/hmrLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import type { Update } from 'types/hmrPayload'
import type { HMRLogger } from '../../../shared/hmr'

export class ViteRuntimeHMRLogger implements HMRLogger {
constructor(private root: string) {}

protected get console(): Console {
return console
}
Expand All @@ -17,29 +15,17 @@ export class ViteRuntimeHMRLogger implements HMRLogger {
}

public invalidated(id: string, message?: string | undefined): void {
this.debug(
`[vite] invalidate ${posixRelativeToRoot(this.root, id)}${
message ? `: ${message}` : ''
}`,
)
this.debug(`[vite] invalidate ${id}${message ? `: ${message}` : ''}`)
}

public updated({ acceptedPath, path }: Update): void {
const relativePath = posixRelativeToRoot(this.root, path)
const relativeAcceptedPath = posixRelativeToRoot(this.root, acceptedPath)
const loggedPath =
path === acceptedPath
? relativePath
: `${relativeAcceptedPath} via ${relativePath}`
path === acceptedPath ? acceptedPath : `${acceptedPath} via ${path}`
this.debug(`[vite] hot updated: ${loggedPath}`)
}
}

export class ViteRuntimeSilentHMRLogger extends ViteRuntimeHMRLogger {
constructor() {
super('/')
}

public override error(): void {
// noop
}
Expand All @@ -48,10 +34,3 @@ export class ViteRuntimeSilentHMRLogger extends ViteRuntimeHMRLogger {
// noop
}
}

function posixRelativeToRoot(root: string, absolutePath: string): string {
if (absolutePath.startsWith(root)) {
return absolutePath.slice(root.length)
}
return absolutePath
}
5 changes: 3 additions & 2 deletions packages/vite/src/node/ssr/runtime/node/mainThreadRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ViteDevServer } from '../../../index'
import { ViteRuntime } from '../runtime'
import { ESModulesRunner } from '../esmRunner'
import { createHMRHandler } from '../hmrHandler'
import type { ViteServerClientOptions } from '../types'
import type { ViteModuleRunner, ViteServerClientOptions } from '../types'
import type { HMRLogger } from '../../../../shared/hmr'
import { ServerHMRConnector } from './serverHmrConnector'

Expand All @@ -13,6 +13,7 @@ interface MainThreadRuntimeOptions
| {
logger?: false | HMRLogger
}
runner?: ViteModuleRunner
}

function createHMROptions(
Expand Down Expand Up @@ -41,7 +42,7 @@ export async function createViteRuntime(
fetchModule: server.ssrFetchModule,
hmr,
},
new ESModulesRunner(),
options.runner || new ESModulesRunner(),
)

if (hmr) {
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/ssr/runtime/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ interface ViteRuntimeDebugger {
export class ViteRuntime {
/**
* Holds the cache of modules
* Keys of the map are filepaths, or plain package names
* Keys of the map are ids
*/
public moduleCache: ModuleCacheMap
public hmrClient?: HMRClient
Expand All @@ -57,7 +57,7 @@ export class ViteRuntime {
this.hmrClient = new HMRClient(
options.hmr.logger === false
? new ViteRuntimeSilentHMRLogger()
: options.hmr.logger || new ViteRuntimeHMRLogger(options.root),
: options.hmr.logger || new ViteRuntimeHMRLogger(),
options.hmr.connection,
({ acceptedPath, ssrInvalidates }) => {
this.moduleCache.delete(acceptedPath)
Expand Down
6 changes: 0 additions & 6 deletions packages/vite/src/node/ssr/runtime/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ export const isWindows =
const NULL_BYTE_PLACEHOLDER = `__x00__`
const VALID_ID_PREFIX = `/@id/`

export function wrapId(id: string): string {
return id.startsWith(VALID_ID_PREFIX)
? id
: VALID_ID_PREFIX + id.replace('\0', NULL_BYTE_PLACEHOLDER)
}

export function unwrapId(id: string): string {
return id.startsWith(VALID_ID_PREFIX)
? id.slice(VALID_ID_PREFIX.length).replace(NULL_BYTE_PLACEHOLDER, '\0')
Expand Down
5 changes: 1 addition & 4 deletions packages/vite/src/node/ssr/ssrFetchModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,11 @@ export async function ssrFetchModule(
importer?: string,
options: FetchModuleOptions = {},
): Promise<FetchResult> {
// node builtins should always be externalized
// builtins should always be externalized
if (rawId.startsWith('data:') || isBuiltin(rawId)) {
return { externalize: rawId, type: 'builtin' }
}

// if there is no importer, it's an entry point, so we know it's not external
// ssr bare imports are always externalized because they are transformed like that on purpose

if (rawId[0] !== '.' && rawId[0] !== '/') {
const {
isProduction,
Expand Down

0 comments on commit 0ebf086

Please sign in to comment.