fix: json HMR (fixes #9521) (#9610)

This commit is contained in:
翠 / green 2022-08-10 22:32:50 +09:00 committed by GitHub
parent ee7f78faa1
commit e45d95f864
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 2 deletions

View File

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

View File

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

View File

@ -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"'
)
})

3
playground/json/hmr.json Normal file
View File

@ -0,0 +1,3 @@
{
"hmr": "this is hmr json"
}

View File

@ -25,6 +25,9 @@
<h2>Has BOM Tag</h2>
<pre class="bom"></pre>
<h2>HMR</h2>
<pre class="hmr"></pre>
<script type="module">
import json, { hello } from './test.json'
import deepJson, { name } from 'vue/package.json'
@ -58,6 +61,9 @@
import hasBomJson from './json-bom/has-bom.json'
text('.bom', JSON.stringify(hasBomJson))
import hmrJSON from './hmr.json'
text('.hmr', JSON.stringify(hmrJSON))
function text(sel, text) {
document.querySelector(sel).textContent = text
}