$ show defstudio/vite-livewire-plugin
This repository provides a Vite plugin (`@defstudio/vite-livewire-plugin`) that enables hot reload for Laravel Livewire components without a full page refresh. It watches for changes in Blade templates and Livewire PHP classes, then sends a custom event to the browser. The injected client-side script listens for this event and calls `$refresh` on all Livewire components, preserving their state. The plugin also supports optional CSS/JS refresh, an opt-in checkbox for toggling hot reload, and configurable watch patterns. It is a single-file TypeScript plugin with a robust configuration parser that accepts strings, arrays, or objects. The code is well-structured, includes comprehensive tests, and follows modern Vite plugin conventions. The repository is actively maintained with recent releases and CI workflows, though the last commit date appears anomalous (future date).
The LLM identified 2 files worth highlighting for reuse:
The core plugin implementation. It exports a default function `livewire(config?)` that returns a Vite plugin with hooks for `resolveId`, `load`, and `hotUpdate`. The `resolvePluginConfig` function demonstrates a reusable pattern for normalizing configuration from multiple input types (undefined, string, array, object). The `hotUpdate` hook uses `minimatch` to match file changes against watch patterns and triggers custom events and CSS/JS updates. The `load` hook injects client-side JavaScript that handles the `livewire-update` event and refreshes Livewire components. This module is self-contained and could be extracted as a reference for building similar Vite plugins.
A comprehensive test suite using Vitest that covers configuration parsing (all input variants) and hot update behavior (pattern matching, event triggering, CSS refresh). It demonstrates a reusable pattern for testing Vite plugins by creating a fake `HotUpdateOptions` context and spying on the server's `send` method. The tests are well-organized, use `vi.spyOn` and `vi.setSystemTime`, and cover edge cases like overriding default watches. This test file can serve as a template for testing other Vite plugins with similar hooks.