From d0fa8e075abeabe40bd253074b327f7a56004801 Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Wed, 10 Aug 2022 21:39:03 +0900 Subject: [PATCH 1/2] fix: json HMR --- packages/vite/src/node/plugins/importAnalysis.ts | 2 +- packages/vite/src/node/server/hmr.ts | 5 +++++ playground/json/__tests__/json.spec.ts | 16 +++++++++++++++- playground/json/hmr.json | 3 +++ playground/json/index.html | 6 ++++++ 5 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 playground/json/hmr.json diff --git a/packages/vite/src/node/plugins/importAnalysis.ts b/packages/vite/src/node/plugins/importAnalysis.ts index f04cb8625f864b..003ca552985378 100644 --- a/packages/vite/src/node/plugins/importAnalysis.ts +++ b/packages/vite/src/node/plugins/importAnalysis.ts @@ -363,7 +363,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { const depModule = await moduleGraph.ensureEntryFromUrl( url, ssr, - canSkipImportAnalysis(url) + canSkipImportAnalysis(removeImportQuery(url)) ) if (depModule.lastHMRTimestamp > 0) { url = injectQuery(url, `t=${depModule.lastHMRTimestamp}`) diff --git a/packages/vite/src/node/server/hmr.ts b/packages/vite/src/node/server/hmr.ts index f96e9353ce0450..a4de1284a7e050 100644 --- a/packages/vite/src/node/server/hmr.ts +++ b/packages/vite/src/node/server/hmr.ts @@ -231,6 +231,11 @@ function propagateUpdate( // if the imports of `node` have not been analyzed, then `node` has not // been loaded in the browser and we should stop propagation. if (node.id && node.isSelfAccepting === undefined) { + debugHmr( + `[propagate update] stop propagation because not analyzed: ${colors.dim( + node.id + )}` + ) return false } diff --git a/playground/json/__tests__/json.spec.ts b/playground/json/__tests__/json.spec.ts index fb55d363a077c5..09530627a580f0 100644 --- a/playground/json/__tests__/json.spec.ts +++ b/playground/json/__tests__/json.spec.ts @@ -1,10 +1,12 @@ import { readFileSync } from 'node:fs' import testJson from '../test.json' -import { isBuild, page } from '~utils' +import hmrJson from '../hmr.json' +import { editFile, isBuild, isServe, page, untilUpdated } from '~utils' const deepJson = require('vue/package.json') const stringified = JSON.stringify(testJson) const deepStringified = JSON.stringify(deepJson) +const hmrStringified = JSON.stringify(hmrJson) test('default import', async () => { expect(await page.textContent('.full')).toBe(stringified) @@ -45,3 +47,15 @@ test('?raw', async () => { readFileSync(require.resolve('../test.json'), 'utf-8') ) }) + +test.runIf(isServe)('should full reload', async () => { + expect(await page.textContent('.hmr')).toBe(hmrStringified) + + editFile('hmr.json', (code) => + code.replace('"this is hmr json"', '"this is hmr update json"') + ) + await untilUpdated( + () => page.textContent('.hmr'), + '"this is hmr update json"' + ) +}) diff --git a/playground/json/hmr.json b/playground/json/hmr.json new file mode 100644 index 00000000000000..2dc497c5321ac4 --- /dev/null +++ b/playground/json/hmr.json @@ -0,0 +1,3 @@ +{ + "hmr": "this is hmr json" +} diff --git a/playground/json/index.html b/playground/json/index.html index 4c93436522e6c8..46ed94ab0463f7 100644 --- a/playground/json/index.html +++ b/playground/json/index.html @@ -25,6 +25,9 @@

JSON Module

Has BOM Tag


 
+

HMR

+

+