diff --git a/packages/vite/src/client/client.ts b/packages/vite/src/client/client.ts index b376a7bea89421..2bbebbf2608210 100644 --- a/packages/vite/src/client/client.ts +++ b/packages/vite/src/client/client.ts @@ -388,12 +388,7 @@ export function removeStyle(id: string): void { } } -async function fetchUpdate({ - path, - acceptedPath, - timestamp, - explicitImportRequired -}: Update) { +async function fetchUpdate({ path, acceptedPath, timestamp }: Update) { const mod = hotModulesMap.get(path) if (!mod) { // In a code-splitting project, @@ -426,6 +421,7 @@ async function fetchUpdate({ return deps.some((dep) => modulesToUpdate.has(dep)) }) + const needImportQuery = isExplicitImportRequired(path) await Promise.all( Array.from(modulesToUpdate).map(async (dep) => { const disposer = disposeMap.get(dep) @@ -436,7 +432,7 @@ async function fetchUpdate({ /* @vite-ignore */ base + path.slice(1) + - `?${explicitImportRequired ? 'import&' : ''}t=${timestamp}${ + `?${needImportQuery ? 'import&' : ''}t=${timestamp}${ query ? `&${query}` : '' }` ) @@ -588,6 +584,35 @@ export function createHotContext(ownerPath: string): ViteHotContext { return hot } +const cssLangs = `\\.(css|less|sass|scss|styl|stylus|pcss|postcss)($|\\?)` +const cssLangRE = new RegExp(cssLangs) + +const isCSSRequest = (request: string): boolean => cssLangRE.test(request) + +const knownJsSrcRE = /\.((j|t)sx?|m[jt]s|vue|marko|svelte|astro)($|\?)/ + +const pathExtname = (url: string) => { + const parts = url.split('/') + const lastPart = parts[parts.length - 1] + const dotPos = lastPart.lastIndexOf('.') + return dotPos === -1 ? '' : lastPart.slice(dotPos) +} + +const isJSRequest = (url: string): boolean => { + url = cleanUrl(url) + if (knownJsSrcRE.test(url)) { + return true + } + if (!pathExtname(url) && !url.endsWith('/')) { + return true + } + return false +} + +function isExplicitImportRequired(url: string) { + return !isJSRequest(cleanUrl(url)) && !isCSSRequest(url) +} + /** * urls here are dynamic import() urls that couldn't be statically analyzed */ diff --git a/packages/vite/src/node/server/hmr.ts b/packages/vite/src/node/server/hmr.ts index 73e3c11ce468c4..600beed4294c35 100644 --- a/packages/vite/src/node/server/hmr.ts +++ b/packages/vite/src/node/server/hmr.ts @@ -9,7 +9,6 @@ import { createDebugger, normalizePath, unique } from '../utils' import type { ViteDevServer } from '..' import { isCSSRequest } from '../plugins/css' import { getAffectedGlobModules } from '../plugins/importMetaGlob' -import { isExplicitImportRequired } from '../plugins/importAnalysis' import type { ModuleNode } from './moduleGraph' export const debugHmr = createDebugger('vite:hmr') @@ -157,10 +156,6 @@ export function updateModules( type: `${boundary.type}-update` as const, timestamp, path: boundary.url, - explicitImportRequired: - boundary.type === 'js' - ? isExplicitImportRequired(boundary.url) - : undefined, acceptedPath: acceptedVia.url })) ) diff --git a/packages/vite/types/hmrPayload.d.ts b/packages/vite/types/hmrPayload.d.ts index 49d3ce586c6700..2fbed3a821466f 100644 --- a/packages/vite/types/hmrPayload.d.ts +++ b/packages/vite/types/hmrPayload.d.ts @@ -20,7 +20,6 @@ export interface Update { path: string acceptedPath: string timestamp: number - explicitImportRequired: boolean | undefined } export interface PrunePayload {