Skip to content

Commit

Permalink
fix: fix HMR propagation when imports not analyzed (#7561)
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Apr 4, 2022
1 parent 2dc0e80 commit 57e7914
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 6 deletions.
34 changes: 34 additions & 0 deletions packages/playground/hmr/__tests__/hmr.spec.ts
Expand Up @@ -160,4 +160,38 @@ if (!isBuild) {
expect(textprev).not.toMatch('direct')
expect(textpost).not.toMatch('direct')
})

test('not loaded dynamic import', async () => {
await page.goto(viteTestUrl + '/dynamic-import/index.html')

let btn = await page.$('button')
expect(await btn.textContent()).toBe('Counter 0')
await btn.click()
expect(await btn.textContent()).toBe('Counter 1')

// Modifying `index.ts` triggers a page reload, as expected
editFile('dynamic-import/index.ts', (code) => code)
await page.waitForNavigation()
btn = await page.$('button')
expect(await btn.textContent()).toBe('Counter 0')

await btn.click()
expect(await btn.textContent()).toBe('Counter 1')

// #7561
// `dep.ts` defines `import.module.hot.accept` and has not been loaded.
// Therefore, modifying it has no effect (doesn't trigger a page reload).
// (Note that, a dynamic import that is never loaded and that does not
// define `accept.module.hot.accept` may wrongfully trigger a full page
// reload, see discussion at #7561.)
editFile('dynamic-import/dep.ts', (code) => code)
try {
await page.waitForNavigation({ timeout: 1000 })
} catch (err) {
const errMsg = 'page.waitForNavigation: Timeout 1000ms exceeded.'
expect(err.message.slice(0, errMsg.length)).toBe(errMsg)
}
btn = await page.$('button')
expect(await btn.textContent()).toBe('Counter 1')
})
}
2 changes: 2 additions & 0 deletions packages/playground/hmr/dynamic-import/dep.ts
@@ -0,0 +1,2 @@
// This file is never loaded
import.meta.hot.accept(() => {})
2 changes: 2 additions & 0 deletions packages/playground/hmr/dynamic-import/index.html
@@ -0,0 +1,2 @@
<button>Counter 0</button>
<script type="module" src="./index.ts"></script>
12 changes: 12 additions & 0 deletions packages/playground/hmr/dynamic-import/index.ts
@@ -0,0 +1,12 @@
const btn = document.querySelector('button')
let count = 0
const update = () => {
btn.textContent = `Counter ${count}`
}
btn.onclick = () => {
count++
update()
}
function neverCalled() {
import('./dep')
}
17 changes: 12 additions & 5 deletions packages/vite/src/node/plugins/importAnalysis.ts
Expand Up @@ -122,6 +122,12 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
},

async transform(source, importer, options) {
// In a real app `server` is always defined, but it is undefined when
// running src/node/server/__tests__/pluginContainer.spec.ts
if (!server) {
return null
}

const ssr = options?.ssr === true
const prettyImporter = prettifyUrl(importer, root)

Expand Down Expand Up @@ -159,7 +165,13 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
)
}

const { moduleGraph } = server
// since we are already in the transform phase of the importer, it must
// have been loaded so its entry is guaranteed in the module graph.
const importerModule = moduleGraph.getModuleById(importer)!

if (!imports.length) {
importerModule.isSelfAccepting = false
isDebug &&
debug(
`${timeFrom(start)} ${colors.dim(`[no imports] ${prettyImporter}`)}`
Expand All @@ -173,11 +185,6 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
let needQueryInjectHelper = false
let s: MagicString | undefined
const str = () => s || (s = new MagicString(source))
// vite-only server context
const { moduleGraph } = server
// since we are already in the transform phase of the importer, it must
// have been loaded so its entry is guaranteed in the module graph.
const importerModule = moduleGraph.getModuleById(importer)!
const importedUrls = new Set<string>()
const staticImportedUrls = new Set<string>()
const acceptedUrls = new Set<{
Expand Down
7 changes: 7 additions & 0 deletions packages/vite/src/node/server/hmr.ts
Expand Up @@ -226,6 +226,13 @@ function propagateUpdate(
}>,
currentChain: ModuleNode[] = [node]
): boolean /* hasDeadEnd */ {
// #7561
// 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) {
return false
}

if (node.isSelfAccepting) {
boundaries.add({
boundary: node,
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/moduleGraph.ts
Expand Up @@ -27,7 +27,7 @@ export class ModuleNode {
importers = new Set<ModuleNode>()
importedModules = new Set<ModuleNode>()
acceptedHmrDeps = new Set<ModuleNode>()
isSelfAccepting = false
isSelfAccepting?: boolean
transformResult: TransformResult | null = null
ssrTransformResult: TransformResult | null = null
ssrModule: Record<string, any> | null = null
Expand Down

0 comments on commit 57e7914

Please sign in to comment.