From 1f7855c1f691b588854d295d3dbffff759826c51 Mon Sep 17 00:00:00 2001 From: Rom Date: Mon, 25 Apr 2022 11:55:13 +0200 Subject: [PATCH] fix: HMR propagation of HTML changes (fix #7870) (#7895) --- packages/playground/hmr/__tests__/hmr.spec.ts | 18 +++++++++++++++--- .../hmr/{dynamic-import => counter}/dep.ts | 0 .../hmr/{dynamic-import => counter}/index.html | 0 .../hmr/{dynamic-import => counter}/index.ts | 0 packages/vite/src/node/server/moduleGraph.ts | 7 +++++++ 5 files changed, 22 insertions(+), 3 deletions(-) rename packages/playground/hmr/{dynamic-import => counter}/dep.ts (100%) rename packages/playground/hmr/{dynamic-import => counter}/index.html (100%) rename packages/playground/hmr/{dynamic-import => counter}/index.ts (100%) diff --git a/packages/playground/hmr/__tests__/hmr.spec.ts b/packages/playground/hmr/__tests__/hmr.spec.ts index 7325c9fe47943a..40b2bdf31b7956 100644 --- a/packages/playground/hmr/__tests__/hmr.spec.ts +++ b/packages/playground/hmr/__tests__/hmr.spec.ts @@ -162,7 +162,7 @@ if (!isBuild) { }) test('not loaded dynamic import', async () => { - await page.goto(viteTestUrl + '/dynamic-import/index.html') + await page.goto(viteTestUrl + '/counter/index.html') let btn = await page.$('button') expect(await btn.textContent()).toBe('Counter 0') @@ -170,7 +170,7 @@ if (!isBuild) { expect(await btn.textContent()).toBe('Counter 1') // Modifying `index.ts` triggers a page reload, as expected - editFile('dynamic-import/index.ts', (code) => code) + editFile('counter/index.ts', (code) => code) await page.waitForNavigation() btn = await page.$('button') expect(await btn.textContent()).toBe('Counter 0') @@ -184,7 +184,7 @@ if (!isBuild) { // (Note that, a dynamic import that is never loaded and that does not // define `accept.module.hot.accept` may wrongfully trigger a full page // reload, see discussion at #7561.) - editFile('dynamic-import/dep.ts', (code) => code) + editFile('counter/dep.ts', (code) => code) try { await page.waitForNavigation({ timeout: 1000 }) } catch (err) { @@ -194,4 +194,16 @@ if (!isBuild) { btn = await page.$('button') expect(await btn.textContent()).toBe('Counter 1') }) + + test('HTML', async () => { + await page.goto(viteTestUrl + '/counter/index.html') + let btn = await page.$('button') + expect(await btn.textContent()).toBe('Counter 0') + editFile('counter/index.html', (code) => + code.replace('Counter', 'Compteur') + ) + await page.waitForNavigation() + btn = await page.$('button') + expect(await btn.textContent()).toBe('Compteur 0') + }) } diff --git a/packages/playground/hmr/dynamic-import/dep.ts b/packages/playground/hmr/counter/dep.ts similarity index 100% rename from packages/playground/hmr/dynamic-import/dep.ts rename to packages/playground/hmr/counter/dep.ts diff --git a/packages/playground/hmr/dynamic-import/index.html b/packages/playground/hmr/counter/index.html similarity index 100% rename from packages/playground/hmr/dynamic-import/index.html rename to packages/playground/hmr/counter/index.html diff --git a/packages/playground/hmr/dynamic-import/index.ts b/packages/playground/hmr/counter/index.ts similarity index 100% rename from packages/playground/hmr/dynamic-import/index.ts rename to packages/playground/hmr/counter/index.ts diff --git a/packages/vite/src/node/server/moduleGraph.ts b/packages/vite/src/node/server/moduleGraph.ts index e470fafb05d8fd..a6c5bfa0ab121f 100644 --- a/packages/vite/src/node/server/moduleGraph.ts +++ b/packages/vite/src/node/server/moduleGraph.ts @@ -2,6 +2,7 @@ import { extname } from 'path' import type { ModuleInfo, PartialResolvedId } from 'rollup' import { parse as parseUrl } from 'url' import { isDirectCSSRequest } from '../plugins/css' +import { isHTMLRequest } from '../plugins/html' import { cleanUrl, normalizePath, @@ -37,6 +38,12 @@ export class ModuleNode { constructor(url: string) { this.url = url this.type = isDirectCSSRequest(url) ? 'css' : 'js' + // #7870 + // The `isSelfAccepting` value is set by importAnalysis, but HTML + // assets don't go through importAnalysis. + if (isHTMLRequest(url)) { + this.isSelfAccepting = false + } } }