Skip to content

Commit e45d95f

Browse files
authored
fix: json HMR (fixes #9521) (#9610)
1 parent ee7f78f commit e45d95f

File tree

5 files changed

+30
-2
lines changed

5 files changed

+30
-2
lines changed

packages/vite/src/node/plugins/importAnalysis.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ const debug = createDebugger('vite:import-analysis')
6969

7070
const clientDir = normalizePath(CLIENT_DIR)
7171

72-
const skipRE = /\.(map|json)$/
72+
const skipRE = /\.(map|json)($|\?)/
7373
export const canSkipImportAnalysis = (id: string): boolean =>
7474
skipRE.test(id) || isDirectCSSRequest(id)
7575

packages/vite/src/node/server/hmr.ts

+5
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,11 @@ function propagateUpdate(
231231
// if the imports of `node` have not been analyzed, then `node` has not
232232
// been loaded in the browser and we should stop propagation.
233233
if (node.id && node.isSelfAccepting === undefined) {
234+
debugHmr(
235+
`[propagate update] stop propagation because not analyzed: ${colors.dim(
236+
node.id
237+
)}`
238+
)
234239
return false
235240
}
236241

playground/json/__tests__/json.spec.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { readFileSync } from 'node:fs'
22
import testJson from '../test.json'
3-
import { isBuild, page } from '~utils'
3+
import hmrJson from '../hmr.json'
4+
import { editFile, isBuild, isServe, page, untilUpdated } from '~utils'
45

56
const deepJson = require('vue/package.json')
67
const stringified = JSON.stringify(testJson)
78
const deepStringified = JSON.stringify(deepJson)
9+
const hmrStringified = JSON.stringify(hmrJson)
810

911
test('default import', async () => {
1012
expect(await page.textContent('.full')).toBe(stringified)
@@ -45,3 +47,15 @@ test('?raw', async () => {
4547
readFileSync(require.resolve('../test.json'), 'utf-8')
4648
)
4749
})
50+
51+
test.runIf(isServe)('should full reload', async () => {
52+
expect(await page.textContent('.hmr')).toBe(hmrStringified)
53+
54+
editFile('hmr.json', (code) =>
55+
code.replace('"this is hmr json"', '"this is hmr update json"')
56+
)
57+
await untilUpdated(
58+
() => page.textContent('.hmr'),
59+
'"this is hmr update json"'
60+
)
61+
})

playground/json/hmr.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"hmr": "this is hmr json"
3+
}

playground/json/index.html

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ <h2>JSON Module</h2>
2525
<h2>Has BOM Tag</h2>
2626
<pre class="bom"></pre>
2727

28+
<h2>HMR</h2>
29+
<pre class="hmr"></pre>
30+
2831
<script type="module">
2932
import json, { hello } from './test.json'
3033
import deepJson, { name } from 'vue/package.json'
@@ -58,6 +61,9 @@ <h2>Has BOM Tag</h2>
5861
import hasBomJson from './json-bom/has-bom.json'
5962
text('.bom', JSON.stringify(hasBomJson))
6063

64+
import hmrJSON from './hmr.json'
65+
text('.hmr', JSON.stringify(hmrJSON))
66+
6167
function text(sel, text) {
6268
document.querySelector(sel).textContent = text
6369
}

0 commit comments

Comments
 (0)