From e45d95f864c76a00408b5f7d7e49a7503d78400f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Wed, 10 Aug 2022 22:32:50 +0900 Subject: [PATCH] fix: json HMR (fixes #9521) (#9610) --- 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 f04cb8625..5041812a9 100644 --- a/packages/vite/src/node/plugins/importAnalysis.ts +++ b/packages/vite/src/node/plugins/importAnalysis.ts @@ -69,7 +69,7 @@ const debug = createDebugger('vite:import-analysis') const clientDir = normalizePath(CLIENT_DIR) -const skipRE = /\.(map|json)$/ +const skipRE = /\.(map|json)($|\?)/ export const canSkipImportAnalysis = (id: string): boolean => skipRE.test(id) || isDirectCSSRequest(id) diff --git a/packages/vite/src/node/server/hmr.ts b/packages/vite/src/node/server/hmr.ts index f96e9353c..a4de1284a 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 fb55d363a..09530627a 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 000000000..2dc497c53 --- /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 4c9343652..46ed94ab0 100644 --- a/playground/json/index.html +++ b/playground/json/index.html @@ -25,6 +25,9 @@

Has BOM Tag


 
+

HMR

+

+