forked from vitejs/vite
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ssrInvalidates to HMR payload
- Loading branch information
1 parent
27d5788
commit 2793ced
Showing
9 changed files
with
169 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 102 additions & 80 deletions
182
packages/vite/src/node/ssr/runtime/__tests__/server-hmr.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,108 @@ | ||
import { afterEach, describe, expect, it } from 'vitest' | ||
import { describe, expect } from 'vitest' | ||
import { createTestClient, editFile, resolvePath, waitUntil } from './utils' | ||
|
||
describe('vite-server-runetime hmr works as expected', async () => { | ||
const { runtime, stdout, waitForWatcher } = await createTestClient( | ||
{ | ||
describe( | ||
'vite-server-runetime hmr works as expected', | ||
async () => { | ||
const it = await createTestClient({ | ||
server: { | ||
// override watch options because it's disabled by default | ||
watch: {}, | ||
}, | ||
}, | ||
// { printLog: true }, | ||
) | ||
await waitForWatcher() | ||
|
||
afterEach(() => { | ||
runtime.clearCache() | ||
}) | ||
|
||
it('hmr options are defined', async () => { | ||
expect(runtime.hmrClient).toBeDefined() | ||
|
||
const mod = await runtime.executeId('./fixtures/hmr.js') | ||
expect(mod).toHaveProperty('hmr') | ||
expect(mod.hmr).toHaveProperty('accept') | ||
}) | ||
|
||
it('correctly populates hmr client', async () => { | ||
const mod = await runtime.executeId('./fixtures/d') | ||
expect(mod.d).toBe('a') | ||
|
||
const fixtureC = resolvePath(import.meta.url, './fixtures/c.ts') | ||
const fixtureD = resolvePath(import.meta.url, './fixtures/d.ts') | ||
|
||
expect(runtime.hmrClient!.hotModulesMap.size).toBe(2) | ||
expect(runtime.hmrClient!.dataMap.size).toBe(2) | ||
expect(runtime.hmrClient!.ctxToListenersMap.size).toBe(2) | ||
|
||
for (const fixture of [fixtureC, fixtureD]) { | ||
expect(runtime.hmrClient!.hotModulesMap.has(fixture)).toBe(true) | ||
expect(runtime.hmrClient!.dataMap.has(fixture)).toBe(true) | ||
expect(runtime.hmrClient!.ctxToListenersMap.has(fixture)).toBe(true) | ||
} | ||
}) | ||
|
||
it('correctly invalidates tree when one module is updated') | ||
|
||
it('correctly does full-reload', async () => { | ||
const mod = await runtime.executeId( | ||
'./fixtures/circular/circular-index.js', | ||
true, | ||
) | ||
expect(mod.a).toBe('a') | ||
expect(mod.b).toBe('b') | ||
|
||
expect( | ||
runtime.hmrClient?.customListenersMap.has('vite:beforeFullReload'), | ||
).toBe(true) | ||
|
||
editFile( | ||
resolvePath(import.meta.url, './fixtures/circular/circular-b.js'), | ||
(content) => content.replace("b = 'b'", "b = 'bb'"), | ||
) | ||
await waitUntil(() => stdout().includes('full reload')) | ||
|
||
const mod2 = await runtime.executeId( | ||
'./fixtures/circular/circular-index.js', | ||
) | ||
expect(mod2.b).toBe('bb') | ||
}) | ||
|
||
it('correctly reruns accepted ssr code', async () => { | ||
const [mod] = await runtime.executeEntrypoints(['./fixtures/simple.js']) | ||
expect(mod.test).toEqual('I am initialized') | ||
expect(stdout()).toMatch('I am initialized') | ||
|
||
editFile(resolvePath(import.meta.url, './fixtures/simple.js'), (content) => | ||
content.replace(/initialized/g, 'changed'), | ||
) | ||
|
||
await waitUntil(() => stdout().includes('I am accepted')) | ||
const mod2 = await runtime.executeId('./fixtures/simple.js') | ||
expect(mod2.test).toEqual('I am changed') | ||
expect(stdout()).toMatch('I am changed') | ||
}) | ||
}, 10_000) | ||
}) | ||
|
||
it('hmr options are defined', async ({ runtime }) => { | ||
expect(runtime.hmrClient).toBeDefined() | ||
|
||
const mod = await runtime.executeId('./fixtures/hmr.js') | ||
expect(mod).toHaveProperty('hmr') | ||
expect(mod.hmr).toHaveProperty('accept') | ||
}) | ||
|
||
it('correctly populates hmr client', async ({ runtime }) => { | ||
const mod = await runtime.executeId('./fixtures/d') | ||
expect(mod.d).toBe('a') | ||
|
||
const fixtureC = resolvePath(import.meta.url, './fixtures/c.ts') | ||
const fixtureD = resolvePath(import.meta.url, './fixtures/d.ts') | ||
|
||
expect(runtime.hmrClient!.hotModulesMap.size).toBe(2) | ||
expect(runtime.hmrClient!.dataMap.size).toBe(2) | ||
expect(runtime.hmrClient!.ctxToListenersMap.size).toBe(2) | ||
|
||
for (const fixture of [fixtureC, fixtureD]) { | ||
expect(runtime.hmrClient!.hotModulesMap.has(fixture)).toBe(true) | ||
expect(runtime.hmrClient!.dataMap.has(fixture)).toBe(true) | ||
expect(runtime.hmrClient!.ctxToListenersMap.has(fixture)).toBe(true) | ||
} | ||
}) | ||
|
||
it('correctly invalidates tree when one module is updated', async ({ | ||
runtime, | ||
stdout, | ||
}) => { | ||
const mod = await runtime.executeId( | ||
// "c" only depends on "a" | ||
'./fixtures/c.ts', | ||
true, | ||
) | ||
|
||
expect(mod.c).toBe('a') | ||
|
||
editFile(resolvePath(import.meta.url, './fixtures/a.ts'), (content) => | ||
content.replace("a = 'a'", "a = 'aa'"), | ||
) | ||
|
||
await waitUntil(() => stdout().includes('hot update')) | ||
|
||
const mod2 = await runtime.executeId( | ||
// "c" only depends on "a" | ||
'./fixtures/c.ts', | ||
true, | ||
) | ||
|
||
expect(mod2.c).toBe('aa') | ||
}) | ||
|
||
it('correctly does full-reload', async ({ runtime, stdout }) => { | ||
const mod = await runtime.executeId( | ||
'./fixtures/circular/circular-index.js', | ||
true, | ||
) | ||
expect(mod.a).toBe('a') | ||
expect(mod.b).toBe('b') | ||
|
||
expect( | ||
runtime.hmrClient?.customListenersMap.has('vite:beforeFullReload'), | ||
).toBe(true) | ||
|
||
editFile( | ||
resolvePath(import.meta.url, './fixtures/circular/circular-b.js'), | ||
(content) => content.replace("b = 'b'", "b = 'bb'"), | ||
) | ||
await waitUntil(() => stdout().includes('full reload')) | ||
|
||
const mod2 = await runtime.executeId( | ||
'./fixtures/circular/circular-index.js', | ||
) | ||
expect(mod2.b).toBe('bb') | ||
}) | ||
|
||
it('correctly reruns accepted ssr code', async ({ runtime, stdout }) => { | ||
const [mod] = await runtime.executeEntrypoints(['./fixtures/simple.js']) | ||
expect(mod.test).toEqual('I am initialized') | ||
expect(stdout()).toMatch('I am initialized') | ||
|
||
editFile( | ||
resolvePath(import.meta.url, './fixtures/simple.js'), | ||
(content) => content.replace(/initialized/g, 'changed'), | ||
) | ||
|
||
await waitUntil(() => stdout().includes('I am accepted')) | ||
const mod2 = await runtime.executeId('./fixtures/simple.js') | ||
expect(mod2.test).toEqual('I am changed') | ||
expect(stdout()).toMatch('I am changed') | ||
}) | ||
}, | ||
process.env.CI ? 30_00 : 5_000, | ||
) |
9 changes: 3 additions & 6 deletions
9
packages/vite/src/node/ssr/runtime/__tests__/server-no-hmr.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 4 additions & 13 deletions
17
packages/vite/src/node/ssr/runtime/__tests__/server-runtime.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.