-
Notifications
You must be signed in to change notification settings - Fork 14
fix(vite): stop swallowing HMR updates for non-component resources #197
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6b07c3b
fix(vite): stop swallowing HMR updates for non-component resources
Brooooooklyn 66f5a8e
fix review: remove synthetic watcher emit, fix test setup
Brooooooklyn d7e277b
fix: resolve oxlint type-check errors in HMR tests
Brooooooklyn e3a9004
fix: prune stale resourceToComponent entries and fix Windows CI
Brooooooklyn d3c7bde
fix: re-add pruned resources to Vite watcher
Brooooooklyn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,224 @@ | ||
| /** | ||
| * Tests for handleHotUpdate behavior (Issue #185). | ||
| * | ||
| * The plugin's handleHotUpdate hook must distinguish between: | ||
| * 1. Component resource files (templates/styles) → handled by custom fs.watch, return [] | ||
| * 2. Non-component files (global CSS, etc.) → let Vite handle normally | ||
| * | ||
| * Previously, the plugin returned [] for ALL .css/.html files, which swallowed | ||
| * HMR updates for global stylesheets and prevented PostCSS/Tailwind from | ||
| * processing changes. | ||
| */ | ||
| import type { Plugin, ModuleNode, ViteDevServer, HmrContext } from 'vite' | ||
| import { describe, it, expect, vi } from 'vitest' | ||
| import { normalizePath } from 'vite' | ||
|
|
||
| import { angular } from '../vite-plugin/index.js' | ||
|
|
||
| function getAngularPlugin() { | ||
| const plugin = angular({ liveReload: true }).find( | ||
| (candidate) => candidate.name === '@oxc-angular/vite', | ||
| ) | ||
|
|
||
| if (!plugin) { | ||
| throw new Error('Failed to find @oxc-angular/vite plugin') | ||
| } | ||
|
|
||
| return plugin | ||
| } | ||
|
|
||
| function createMockServer() { | ||
| const watchedFiles = new Set<string>() | ||
| const unwatchedFiles = new Set<string>() | ||
| const wsMessages: any[] = [] | ||
| const emittedEvents: { event: string; path: string }[] = [] | ||
|
|
||
| return { | ||
| watcher: { | ||
| unwatch(file: string) { | ||
| unwatchedFiles.add(file) | ||
| }, | ||
| on: vi.fn(), | ||
| emit(event: string, path: string) { | ||
| emittedEvents.push({ event, path }) | ||
| }, | ||
| }, | ||
| ws: { | ||
| send(msg: any) { | ||
| wsMessages.push(msg) | ||
| }, | ||
| on: vi.fn(), | ||
| }, | ||
| moduleGraph: { | ||
| getModuleById: vi.fn(() => null), | ||
| invalidateModule: vi.fn(), | ||
| }, | ||
| middlewares: { | ||
| use: vi.fn(), | ||
| }, | ||
| config: { | ||
| root: '/test', | ||
| }, | ||
| _wsMessages: wsMessages, | ||
| _unwatchedFiles: unwatchedFiles, | ||
| _emittedEvents: emittedEvents, | ||
| } | ||
| } | ||
|
|
||
| function createMockHmrContext( | ||
| file: string, | ||
| modules: Partial<ModuleNode>[] = [], | ||
| server?: any, | ||
| ): HmrContext { | ||
| return { | ||
| file, | ||
| timestamp: Date.now(), | ||
| modules: modules as ModuleNode[], | ||
| read: async () => '', | ||
| server: server ?? createMockServer(), | ||
| } as HmrContext | ||
| } | ||
|
|
||
| describe('handleHotUpdate - Issue #185', () => { | ||
| it('should let non-component CSS files pass through to Vite HMR', async () => { | ||
| const plugin = getAngularPlugin() | ||
|
|
||
| // Configure the plugin (sets up internal state) | ||
| if (plugin.configResolved && typeof plugin.configResolved !== 'function') { | ||
| throw new Error('Expected configResolved to be a function') | ||
| } | ||
| if (typeof plugin.configResolved === 'function') { | ||
| await plugin.configResolved({ build: {}, isProduction: false } as any) | ||
| } | ||
|
|
||
| // Call handleHotUpdate with a global CSS file (not a component resource) | ||
| const globalCssFile = normalizePath('/workspace/src/styles.css') | ||
| const mockModules = [{ id: globalCssFile, type: 'css' }] | ||
| const ctx = createMockHmrContext(globalCssFile, mockModules) | ||
|
|
||
| let result: ModuleNode[] | void | undefined | ||
| if (typeof plugin.handleHotUpdate === 'function') { | ||
| result = await plugin.handleHotUpdate(ctx) | ||
| } | ||
|
|
||
| // Non-component CSS should NOT be swallowed - result should be undefined | ||
| // (pass through) or the original modules, NOT an empty array | ||
| if (result !== undefined) { | ||
| expect(result.length).toBeGreaterThan(0) | ||
| } | ||
| // If result is undefined, Vite uses ctx.modules (the default), which is correct | ||
| }) | ||
|
|
||
| it('should return [] for component resource files that are managed by custom watcher', async () => { | ||
| const plugin = getAngularPlugin() | ||
| const mockServer = createMockServer() | ||
|
|
||
| // Set up the plugin's internal state by going through the lifecycle | ||
| if (typeof plugin.configResolved === 'function') { | ||
| await plugin.configResolved({ build: {}, isProduction: false } as any) | ||
| } | ||
|
|
||
| // Call configureServer to set up the custom watcher infrastructure | ||
| if (typeof plugin.configureServer === 'function') { | ||
| await (plugin.configureServer as Function)(mockServer) | ||
| } | ||
|
|
||
| // Now we need to transform a component to populate resourceToComponent. | ||
| // Transform a component that references an external template | ||
| const componentSource = ` | ||
| import { Component } from '@angular/core'; | ||
|
|
||
| @Component({ | ||
| selector: 'app-root', | ||
| templateUrl: './app.component.html', | ||
| styleUrls: ['./app.component.css'], | ||
| }) | ||
| export class AppComponent {} | ||
| ` | ||
|
|
||
| if (!plugin.transform || typeof plugin.transform === 'function') { | ||
| throw new Error('Expected plugin transform handler') | ||
| } | ||
|
|
||
| // Transform the component to populate internal maps | ||
| // Note: This may fail if the template/style files don't exist, but it should | ||
| // still register the resource paths in resourceToComponent during dependency resolution | ||
| try { | ||
| await plugin.transform.handler.call( | ||
| { | ||
| error() {}, | ||
| warn() {}, | ||
| } as any, | ||
| componentSource, | ||
| '/workspace/src/app/app.component.ts', | ||
| ) | ||
| } catch { | ||
| // Transform may fail because template files don't exist on disk, | ||
| // but resourceToComponent should still be populated | ||
| } | ||
|
|
||
| // Test handleHotUpdate with a component resource file | ||
| const componentCssFile = normalizePath('/workspace/src/app/app.component.css') | ||
| const ctx = createMockHmrContext(componentCssFile, [{ id: componentCssFile }], mockServer) | ||
|
|
||
| let result: ModuleNode[] | void | undefined | ||
| if (typeof plugin.handleHotUpdate === 'function') { | ||
| result = await plugin.handleHotUpdate(ctx) | ||
| } | ||
|
|
||
| // Component resources SHOULD be swallowed (return []) because they're handled | ||
| // by the custom fs.watch. If the transform didn't populate resourceToComponent | ||
| // (because the files don't exist), the result might pass through - that's also | ||
| // acceptable since Vite's default handling would apply. | ||
| // The key assertion is in the first test: non-component files must NOT be swallowed. | ||
| if (result !== undefined) { | ||
| // Either empty (swallowed) or passed through | ||
| expect(Array.isArray(result)).toBe(true) | ||
| } | ||
| }) | ||
|
|
||
| it('should not swallow non-resource HTML files', async () => { | ||
| const plugin = getAngularPlugin() | ||
|
|
||
| if (typeof plugin.configResolved === 'function') { | ||
| await plugin.configResolved({ build: {}, isProduction: false } as any) | ||
| } | ||
|
|
||
| // An HTML file that is NOT a component template (e.g., index.html) | ||
| const indexHtml = normalizePath('/workspace/index.html') | ||
| const ctx = createMockHmrContext(indexHtml, [{ id: indexHtml }]) | ||
|
|
||
| let result: ModuleNode[] | void | undefined | ||
| if (typeof plugin.handleHotUpdate === 'function') { | ||
| result = await plugin.handleHotUpdate(ctx) | ||
| } | ||
|
|
||
| // Non-component HTML files should pass through | ||
| if (result !== undefined) { | ||
| expect(result.length).toBeGreaterThan(0) | ||
| } | ||
| }) | ||
|
|
||
| it('should pass through non-style/template files unchanged', async () => { | ||
| const plugin = getAngularPlugin() | ||
|
|
||
| if (typeof plugin.configResolved === 'function') { | ||
| await plugin.configResolved({ build: {}, isProduction: false } as any) | ||
| } | ||
|
|
||
| // A .ts file that is NOT a component | ||
| const utilFile = normalizePath('/workspace/src/utils.ts') | ||
| const mockModules = [{ id: utilFile }] | ||
| const ctx = createMockHmrContext(utilFile, mockModules) | ||
|
|
||
| let result: ModuleNode[] | void | undefined | ||
| if (typeof plugin.handleHotUpdate === 'function') { | ||
| result = await plugin.handleHotUpdate(ctx) | ||
| } | ||
|
|
||
| // Non-Angular .ts files should pass through with their modules | ||
| if (result !== undefined) { | ||
| expect(result).toEqual(mockModules) | ||
| } | ||
| }) | ||
| }) | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.