Skip to content

Commit

Permalink
fix(hmr): set isSelfAccepting unless it is delayed (#8898)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Jul 3, 2022
1 parent a67b0d3 commit ae34565
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
7 changes: 6 additions & 1 deletion packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,12 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
// its last updated timestamp to force the browser to fetch the most
// up-to-date version of this module.
try {
const depModule = await moduleGraph.ensureEntryFromUrl(url, ssr)
// delay setting `isSelfAccepting` until the file is actually used (#7870)
const depModule = await moduleGraph.ensureEntryFromUrl(
url,
ssr,
canSkipImportAnalysis(url)
)
if (depModule.lastHMRTimestamp > 0) {
url = injectQuery(url, `t=${depModule.lastHMRTimestamp}`)
}
Expand Down
28 changes: 17 additions & 11 deletions packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,24 @@ export function updateModules(
ws.send({
type: 'full-reload'
})
} else {
config.logger.info(
updates
.map(({ path }) => colors.green(`hmr update `) + colors.dim(path))
.join('\n'),
{ clear: true, timestamp: true }
)
ws.send({
type: 'update',
updates
})
return
}

if (updates.length === 0) {
debugHmr(colors.yellow(`no update happened `) + colors.dim(file))
return
}

config.logger.info(
updates
.map(({ path }) => colors.green(`hmr update `) + colors.dim(path))
.join('\n'),
{ clear: true, timestamp: true }
)
ws.send({
type: 'update',
updates
})
}

export async function handleFileAddUnlink(
Expand Down
20 changes: 11 additions & 9 deletions packages/vite/src/node/server/moduleGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ import { extname } from 'node:path'
import { parse as parseUrl } from 'node:url'
import type { ModuleInfo, PartialResolvedId } from 'rollup'
import { isDirectCSSRequest } from '../plugins/css'
import { isHTMLRequest } from '../plugins/html'
import {
cleanUrl,
normalizePath,
removeImportQuery,
removeTimestampQuery
} from '../utils'
import { FS_PREFIX } from '../constants'
import { canSkipImportAnalysis } from '../plugins/importAnalysis'
import type { TransformResult } from './transformRequest'

export class ModuleNode {
Expand Down Expand Up @@ -39,13 +37,13 @@ export class ModuleNode {
lastHMRTimestamp = 0
lastInvalidationTimestamp = 0

constructor(url: string) {
/**
* @param setIsSelfAccepting - set `false` to set `isSelfAccepting` later. e.g. #7870
*/
constructor(url: string, setIsSelfAccepting = true) {
this.url = url
this.type = isDirectCSSRequest(url) ? 'css' : 'js'
// #7870
// The `isSelfAccepting` value is set by importAnalysis, but some
// assets don't go through importAnalysis.
if (isHTMLRequest(url) || canSkipImportAnalysis(url)) {
if (setIsSelfAccepting) {
this.isSelfAccepting = false
}
}
Expand Down Expand Up @@ -182,11 +180,15 @@ export class ModuleGraph {
return noLongerImported
}

async ensureEntryFromUrl(rawUrl: string, ssr?: boolean): Promise<ModuleNode> {
async ensureEntryFromUrl(
rawUrl: string,
ssr?: boolean,
setIsSelfAccepting = true
): Promise<ModuleNode> {
const [url, resolvedId, meta] = await this.resolveUrl(rawUrl, ssr)
let mod = this.urlToModuleMap.get(url)
if (!mod) {
mod = new ModuleNode(url)
mod = new ModuleNode(url, setIsSelfAccepting)
if (meta) mod.meta = meta
this.urlToModuleMap.set(url, mod)
mod.id = resolvedId
Expand Down

0 comments on commit ae34565

Please sign in to comment.