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(runtime): runtime HMR affects only imported files #15898

Merged
merged 15 commits into from
Feb 22, 2024
Merged
Prev Previous commit
Next Next commit
test: add tests for no full reload
  • Loading branch information
sheremet-va committed Feb 13, 2024
commit 823df65768bc3b511d81c105ed650eed71d0e6bd
2 changes: 1 addition & 1 deletion packages/vite/src/node/ssr/runtime/moduleCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class ModuleCacheMap extends Map<string, ModuleCache> {

const fileModule = this.getByModuleId(importedId)
const importers = fileModule?.importers
// console.log(this, { importedId, importers: fileModule?.importers, imports: fileModule?.imports })

if (!importers) return false

if (importers.has(importedBy)) return true
Expand Down
56 changes: 56 additions & 0 deletions playground/hmr-ssr/__tests__/hmr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,54 @@ describe('acceptExports', () => {
)
})
})

describe("doesn't reload if files not in the the entrypoint importers chain is changed", async () => {
const testFile = 'non-tested/index.js'

beforeAll(async () => {
// so it's in the module graph
await server.transformRequest(testFile, { ssr: true })
await server.transformRequest('non-tested/dep.js', { ssr: true })
})

test('does not full reload', async () => {
editFile(
testFile,
(code) => code + '\n\nexport const query5 = "query5"',
)
const start = Date.now()
// for 2 seconds check that there is no log about the file being reloaded
while (Date.now() - start < 2000) {
if (
clientLogs.some(
(log) =>
log.match(PROGRAM_RELOAD) ||
log.includes('non-tested/index.js'),
)
) {
throw new Error('File was reloaded')
}
await new Promise((r) => setTimeout(r, 100))
}
}, 5_000)

test('does not update', async () => {
editFile('non-tested/dep.js', (code) => code + '//comment')
const start = Date.now()
// for 2 seconds check that there is no log about the file being reloaded
while (Date.now() - start < 2000) {
if (
clientLogs.some(
(log) =>
log.match(PROGRAM_RELOAD) || log.includes('non-tested/dep.js'),
)
) {
throw new Error('File was updated')
}
await new Promise((r) => setTimeout(r, 100))
}
}, 5_000)
})
})

test('accepts itself when imported for side effects only (no bindings imported)', async () => {
Expand Down Expand Up @@ -1049,6 +1097,14 @@ async function setupViteRuntime(
noDiscovery: true,
include: [],
},
plugins: [
{
name: 'tessss',
handleHotUpdate({ file }) {
console.log('hot', file)
},
},
],
...serverOptions,
})

Expand Down
3 changes: 3 additions & 0 deletions playground/hmr-ssr/non-tested/dep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const test = 'true'

import.meta.hot.accept()
9 changes: 9 additions & 0 deletions playground/hmr-ssr/non-tested/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { test } from './dep.js'

function main() {
test()
}

main()

import.meta.hot.accept('./dep.js')
Loading