From 8cbbb3cab76a35bf7389169b39acd8a3d9d00000 Mon Sep 17 00:00:00 2001 From: naoki <0512.naoki.job@gmail.com> Date: Thu, 26 Nov 2026 22:41:42 +0900 Subject: [PATCH] fix(vite-plugin): respect Vite's server.hmr setting when compiling components --- .../test/plugin-fast-paths.test.ts | 24 +++++++++++++++++-- napi/angular-compiler/vite-plugin/index.ts | 3 ++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/napi/angular-compiler/test/plugin-fast-paths.test.ts b/napi/angular-compiler/test/plugin-fast-paths.test.ts index 045feeabd..e475a9e26 100644 --- a/napi/angular-compiler/test/plugin-fast-paths.test.ts +++ b/napi/angular-compiler/test/plugin-fast-paths.test.ts @@ -104,12 +104,16 @@ async function callPluginHook(hook: any, ...args: unknown[]) { } /** Run the plugin through `config`/`configResolved`/`configureServer`. */ -async function setupPlugin(plugin: Plugin, command: 'serve' | 'build' = 'serve') { +async function setupPlugin( + plugin: Plugin, + command: 'serve' | 'build' = 'serve', + serverHmr?: boolean, +) { const mockServer = createMockServer() await callPluginHook(plugin.config, {}, { command, mode: 'development' }) // A real resolved config: preprocessCSS needs one to run Sass. const resolved = await resolveConfig( - { configFile: false, root: tempDir, logLevel: 'silent' }, + { configFile: false, root: tempDir, logLevel: 'silent', server: { hmr: serverHmr } }, command, ) await callPluginHook(plugin.configResolved, resolved) @@ -180,6 +184,22 @@ function preprocessCallsFor(path: string): number { ).length } +it('omits the component update import when Vite disables HMR (e.g. Vitest)', async () => { + const plugin = getAngularPlugin({}) + await setupPlugin(plugin, 'serve', false) + const { code, errors } = await transform( + plugin, + `import { Component } from '@angular/core'; + @Component({ selector: 'app-root', template: 'Hello' }) + export class AppComponent {}`, + join(appDir, 'app.component.ts'), + ) + + expect(errors).toEqual([]) + expect(code).toContain('ɵɵdefineComponent') + expect(code).not.toContain('ɵɵreplaceMetadata') +}) + describe('transform skips the resource URL parse when no URL is spelled', () => { it('does not call extractComponentUrls for an inline-only component', async () => { const plugin = getAngularPlugin() diff --git a/napi/angular-compiler/vite-plugin/index.ts b/napi/angular-compiler/vite-plugin/index.ts index f7f2e700e..2ce963611 100644 --- a/napi/angular-compiler/vite-plugin/index.ts +++ b/napi/angular-compiler/vite-plugin/index.ts @@ -661,7 +661,8 @@ export function angular(options: PluginOptions = {}): Plugin[] { // modules, as those are served via HTTP middleware only. This matches // Angular's official behavior where _enableHmr is only set for browser // bundles (see @angular/build application-code-bundle.js). - hmr: pluginOptions.liveReload && watchMode && !isSSR, + // Vitest uses client transforms for DOM tests but disables Vite's HMR server. + hmr: pluginOptions.liveReload && watchMode && !isSSR && resolvedConfig?.server?.hmr !== false, angularVersion: pluginOptions.angularVersion, minifyComponentStyles: getMinifyComponentStyles(context), emitClassMetadata: pluginOptions.emitClassMetadata,