Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(hmr): set isSelfAccepting unless it is delayed #8898

Merged
merged 1 commit into from Jul 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/vite/src/node/plugins/importAnalysis.ts
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is a little confusing. We should move it closer to what it's referencing (above the canSkipImportAnalysis call) and explain why it's necessary.

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
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
}
Comment on lines +175 to +178
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When updates was [] the config.logger.info below was outputing 22:42:33 [vite] .


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
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