Skip to content

Commit

Permalink
hmr: tests and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jan 8, 2024
1 parent 8f05bb3 commit eb8ca8d
Show file tree
Hide file tree
Showing 22 changed files with 249 additions and 301 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"test-serve": "vitest run -c vitest.config.e2e.ts",
"test-build": "VITE_TEST_BUILD=1 vitest run -c vitest.config.e2e.ts",
"test-build-without-plugin-commonjs": "VITE_TEST_WITHOUT_PLUGIN_COMMONJS=1 pnpm test-build",
"test-unit": "vitest",
"test-unit": "vitest run",
"test-docs": "pnpm run docs-build",
"debug-serve": "VITE_DEBUG_SERVE=1 vitest run -c vitest.config.e2e.ts",
"debug-build": "VITE_TEST_BUILD=1 VITE_PRESERVE_BUILD_ARTIFACTS=1 vitest run -c vitest.config.e2e.ts",
Expand Down
15 changes: 9 additions & 6 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -733,12 +733,15 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
// normalize and rewrite accepted urls
const normalizedAcceptedUrls = new Set<string>()
for (const { url, start, end } of acceptedUrls) {
const [normalized] = await moduleGraph.resolveUrl(
toAbsoluteUrl(url),
ssr,
)
normalizedAcceptedUrls.add(normalized)
str().overwrite(start, end, JSON.stringify(normalized), {
// serve url: /file/path.js (absolute to the server)
// fs path: /Users/name/root/file/path.js (absolute to the file system)
const [normalizedServeUrl, normalizedFsPath] =
await moduleGraph.resolveUrl(toAbsoluteUrl(url), ssr)

normalizedAcceptedUrls.add(normalizedServeUrl)
// SSR relies on absolute FS paths, not URLs
const replaceUrl = ssr ? cleanUrl(normalizedFsPath) : normalizedServeUrl
str().overwrite(start, end, JSON.stringify(replaceUrl), {
contentOnly: true,
})
}
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/ssr/runtime/hmrHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export async function handleHMRUpdate(
break
}
case 'full-reload':
hmrClient.logger.debug(`[vite] program reload`)
await hmrClient.notifyListeners('vite:beforeFullReload', payload)
Array.from(runtime.moduleCache.keys()).forEach((id) => {
if (!id.includes('node_modules')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export async function createViteRuntime(
new ESModulesRunner(),
)

// TODO: just implement a simple queue instead of using o-limit
// process hmr updates one by one - it's possible to trigger one update inside another
// for example, triggering .invalidate will send update events while update event is in process
const limiter = limit(1)
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/ssr/runtime/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class ViteRuntime {
this.envProxy = createImportMetaEnvProxy(options.environmentVariables)
if (typeof options.hmr === 'object') {
this.hmrClient = new HMRClient(
// TODO: use debug for this instead?
console,
options.hmr,
({ acceptedPath, ssrInvalidates }) => {
Expand Down
Loading

0 comments on commit eb8ca8d

Please sign in to comment.