diff --git a/packages/vite/src/node/plugins/importMetaGlob.ts b/packages/vite/src/node/plugins/importMetaGlob.ts index 4d097cdea86967..157d7a91ec8e4f 100644 --- a/packages/vite/src/node/plugins/importMetaGlob.ts +++ b/packages/vite/src/node/plugins/importMetaGlob.ts @@ -21,12 +21,12 @@ import fg from 'fast-glob' import { stringifyQuery } from 'ufo' import type { GeneralImportGlobOptions } from 'types/importGlob' import type { Plugin } from '../plugin' -import type { ViteDevServer } from '../server' import type { EnvironmentModuleNode } from '../server/moduleGraph' import type { ResolvedConfig } from '../config' import { evalValue, normalizePath, transformStableResult } from '../utils' import type { Logger } from '../logger' import { slash } from '../../shared/utils' +import type { Environment } from '../environment' const { isMatch, scan } = micromatch @@ -44,43 +44,16 @@ interface ParsedGeneralImportGlobOptions extends GeneralImportGlobOptions { query?: string } -export function getAffectedGlobModules( - file: string, - server: ViteDevServer, -): EnvironmentModuleNode[] { - const modules: EnvironmentModuleNode[] = [] - // TODO: properly support other runtimes. Changing _importGlobMap breaks VitePress - // https://github.com/vuejs/vitepress/blob/28989df83446923a9e7c8ada345b0778119ed66f/src/node/plugins/staticDataPlugin.ts#L128 - for (const [id, allGlobs] of server._importGlobMap!) { - // (glob1 || glob2) && !glob3 && !glob4... - if ( - allGlobs.some( - ({ affirmed, negated }) => - (!affirmed.length || affirmed.some((glob) => isMatch(file, glob))) && - (!negated.length || negated.every((glob) => isMatch(file, glob))), - ) - ) { - const mod = server.environments.client.moduleGraph.getModuleById(id) - - if (mod) { - if (mod.file) { - server.environments.client.moduleGraph.onFileChange(mod.file) - } - modules.push(mod) - } - } - } - return modules -} - export function importGlobPlugin(config: ResolvedConfig): Plugin { - let server: ViteDevServer | undefined + const importGlobMaps = new Map< + Environment, + Map + >() return { name: 'vite:import-glob', - configureServer(_server) { - server = _server - server._importGlobMap.clear() + configureServer() { + importGlobMaps.clear() }, async transform(code, id, options) { if (!code.includes('import.meta.glob')) return @@ -94,9 +67,12 @@ export function importGlobPlugin(config: ResolvedConfig): Plugin { config.logger, ) if (result) { - if (server) { + if (this.environment) { const allGlobs = result.matches.map((i) => i.globsResolved) - server._importGlobMap.set( + if (!importGlobMaps.has(this.environment)) { + importGlobMaps.set(this.environment, new Map()) + } + importGlobMaps.get(this.environment)!.set( id, allGlobs.map((globs) => { const affirmed: string[] = [] @@ -112,6 +88,29 @@ export function importGlobPlugin(config: ResolvedConfig): Plugin { return transformStableResult(result.s, id, config) } }, + hotUpdate({ type, file, modules: oldModules, environment }) { + if (type === 'update') return + + const importGlobMap = importGlobMaps.get(environment) + if (!importGlobMap) return + + const modules: EnvironmentModuleNode[] = [] + for (const [id, allGlobs] of importGlobMap) { + // (glob1 || glob2) && !glob3 && !glob4... + if ( + allGlobs.some( + ({ affirmed, negated }) => + (!affirmed.length || + affirmed.some((glob) => isMatch(file, glob))) && + (!negated.length || negated.every((glob) => isMatch(file, glob))), + ) + ) { + const mod = environment.moduleGraph.getModuleById(id) + if (mod) modules.push(mod) + } + } + return modules.length > 0 ? [...oldModules, ...modules] : undefined + }, } } diff --git a/packages/vite/src/node/server/hmr.ts b/packages/vite/src/node/server/hmr.ts index 099d329b36d5aa..55fde029202281 100644 --- a/packages/vite/src/node/server/hmr.ts +++ b/packages/vite/src/node/server/hmr.ts @@ -11,7 +11,6 @@ import { createDebugger, normalizePath } from '../utils' import type { InferCustomEventPayload, ViteDevServer } from '..' import { getHookHandler } from '../plugins' import { isCSSRequest } from '../plugins/css' -import { getAffectedGlobModules } from '../plugins/importMetaGlob' import { isExplicitImportRequired } from '../plugins/importAnalysis' import { getEnvFilesForMode } from '../env' import { withTrailingSlash, wrapId } from '../../shared/utils' @@ -42,6 +41,7 @@ export interface HmrOptions { } export interface HotUpdateContext { + type: 'create' | 'update' | 'delete' file: string timestamp: number modules: Array @@ -238,15 +238,11 @@ export async function handleHMRUpdate( async function applyHMR(environment: DevEnvironment) { const mods = environment.moduleGraph.getModulesByFile(file) || new Set() - if (type === 'create' || type === 'delete') { - for (const mod of getAffectedGlobModules(file, server)) { - mods.add(mod) - } - } // check if any plugin wants to perform custom HMR handling const timestamp = Date.now() const hotContext: HotUpdateContext = { + type, file, timestamp, modules: [...mods], @@ -276,6 +272,7 @@ export async function handleHMRUpdate( modules: hotContext.modules.map((mod) => server.moduleGraph.getBackwardCompatibleModuleNode(mod), ), + type: undefined, } as HmrContext const filteredModules = await getHookHandler(plugin.handleHotUpdate!)( hmrContext, diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 65c80f8052b479..b3133e612799aa 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -379,10 +379,6 @@ export interface ViteDevServer { * @internal */ _setInternalServer(server: ViteDevServer): void - /** - * @internal - */ - _importGlobMap: Map /** * @internal */ @@ -739,7 +735,6 @@ export async function _createServer( server = _server }, _restartPromise: null, - _importGlobMap: new Map(), _forceOptimizeOnRestart: false, _pendingRequests: new Map(), _safeModulesPath: new Set(),