From 0ebf086265f3ef249e539a913e1c13a2935596ae Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Thu, 18 Jan 2024 16:12:14 +0100 Subject: [PATCH] chore: cleanup --- .../vite/src/node/plugins/importAnalysis.ts | 9 +++---- packages/vite/src/node/server/hmr.ts | 2 ++ .../vite/src/node/ssr/runtime/hmrLogger.ts | 25 ++----------------- .../ssr/runtime/node/mainThreadRuntime.ts | 5 ++-- packages/vite/src/node/ssr/runtime/runtime.ts | 4 +-- packages/vite/src/node/ssr/runtime/utils.ts | 6 ----- packages/vite/src/node/ssr/ssrFetchModule.ts | 5 +--- 7 files changed, 13 insertions(+), 43 deletions(-) diff --git a/packages/vite/src/node/plugins/importAnalysis.ts b/packages/vite/src/node/plugins/importAnalysis.ts index 08a6e5eaa31038..5dfccfebfc5953 100644 --- a/packages/vite/src/node/plugins/importAnalysis.ts +++ b/packages/vite/src/node/plugins/importAnalysis.ts @@ -733,16 +733,13 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { // normalize and rewrite accepted urls const normalizedAcceptedUrls = new Set() 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, }) } diff --git a/packages/vite/src/node/server/hmr.ts b/packages/vite/src/node/server/hmr.ts index 7f702df3cdcd54..a87d902ae0a35d 100644 --- a/packages/vite/src/node/server/hmr.ts +++ b/packages/vite/src/node/server/hmr.ts @@ -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), }), ), diff --git a/packages/vite/src/node/ssr/runtime/hmrLogger.ts b/packages/vite/src/node/ssr/runtime/hmrLogger.ts index f2728a11f32a7b..d6421af1f3802f 100644 --- a/packages/vite/src/node/ssr/runtime/hmrLogger.ts +++ b/packages/vite/src/node/ssr/runtime/hmrLogger.ts @@ -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 } @@ -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 } @@ -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 -} diff --git a/packages/vite/src/node/ssr/runtime/node/mainThreadRuntime.ts b/packages/vite/src/node/ssr/runtime/node/mainThreadRuntime.ts index 22b873da9ccc43..7ba05c3b7618d5 100644 --- a/packages/vite/src/node/ssr/runtime/node/mainThreadRuntime.ts +++ b/packages/vite/src/node/ssr/runtime/node/mainThreadRuntime.ts @@ -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' @@ -13,6 +13,7 @@ interface MainThreadRuntimeOptions | { logger?: false | HMRLogger } + runner?: ViteModuleRunner } function createHMROptions( @@ -41,7 +42,7 @@ export async function createViteRuntime( fetchModule: server.ssrFetchModule, hmr, }, - new ESModulesRunner(), + options.runner || new ESModulesRunner(), ) if (hmr) { diff --git a/packages/vite/src/node/ssr/runtime/runtime.ts b/packages/vite/src/node/ssr/runtime/runtime.ts index ba665611afe486..45acd20a2419b1 100644 --- a/packages/vite/src/node/ssr/runtime/runtime.ts +++ b/packages/vite/src/node/ssr/runtime/runtime.ts @@ -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 @@ -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) diff --git a/packages/vite/src/node/ssr/runtime/utils.ts b/packages/vite/src/node/ssr/runtime/utils.ts index f83b88cbdab5bc..87d599d4d581ce 100644 --- a/packages/vite/src/node/ssr/runtime/utils.ts +++ b/packages/vite/src/node/ssr/runtime/utils.ts @@ -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') diff --git a/packages/vite/src/node/ssr/ssrFetchModule.ts b/packages/vite/src/node/ssr/ssrFetchModule.ts index 59e9028d49275f..9981becc2647dd 100644 --- a/packages/vite/src/node/ssr/ssrFetchModule.ts +++ b/packages/vite/src/node/ssr/ssrFetchModule.ts @@ -27,14 +27,11 @@ export async function ssrFetchModule( importer?: string, options: FetchModuleOptions = {}, ): Promise { - // 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,