Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: json HMR (fixes #9521) #9610

Merged
merged 2 commits into from Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/importAnalysis.ts
Expand Up @@ -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)

Expand Down
5 changes: 5 additions & 0 deletions packages/vite/src/node/server/hmr.ts
Expand Up @@ -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
}

Expand Down
16 changes: 15 additions & 1 deletion 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)
Expand Down Expand Up @@ -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 changes: 3 additions & 0 deletions playground/json/hmr.json
@@ -0,0 +1,3 @@
{
"hmr": "this is hmr json"
}
6 changes: 6 additions & 0 deletions playground/json/index.html
Expand Up @@ -25,6 +25,9 @@ <h2>JSON Module</h2>
<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'
Expand Down Expand Up @@ -58,6 +61,9 @@ <h2>Has BOM Tag</h2>
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
}
Expand Down