Convert CRXJS HMR from filesystem-based to WebSocket-based
Describe the problem
Convert CRXJS HMR from filesystem-based to WebSocket-based
Problem
Currently, CRXJS implements a complex filesystem-based Hot Module Replacement (HMR) system. This approach was necessary because Chrome's Content Security Policy (CSP) for content scripts historically didn't support files served from localhost. However, this restriction has been relaxed in recent Chrome versions, making our current implementation unnecessarily complex.
References:
Proposed Solution
We should replace our custom filesystem-based HMR with a direct connection to Vite's dev server WebSocket. This would:
- Simplify our codebase significantly
- Improve development performance
- Better align with Vite's native HMR implementation
- Reduce maintenance burden
Technical Details
Current Implementation
Our current approach involves:
- A complex file writer system that writes files to disk (
fileWriter.ts,fileWriter-*.ts) - An RxJS-based event system for tracking file changes (
fileWriter-rxjs.ts) - Custom HMR payloads and transformations (
fileWriter-hmr.ts) - A background service worker that acts as a bridge between content scripts and the dev server
Describe the proposed solution
Proposed Implementation
We can replace this with:
- Direct WebSocket connections from content scripts to Vite's dev server
- Utilizing Vite's native HMR protocol
- Modifying the content script CSP as needed to allow connections to the dev server
Code Changes Required
-
Remove file writer modules:
-
fileWriter.ts -
fileWriter-*.tsfiles - Related RxMap implementation
-
-
Update HMR implementation:
- Modify
plugin-hmr.tsto use Vite's WebSocket directly - Simplify the background script bridge or remove it entirely
- Update the content script client to connect directly to Vite's server
- Modify
-
CSP Handling:
- Ensure content script manifest includes necessary CSP for WebSocket connections
- Add appropriate connect-src directives for development mode
-
Tests:
- Update all HMR-related tests to verify WebSocket-based functionality
- Add specific tests for CSP configurations
Benefits
- Code simplification: Remove ~1000+ lines of complex code
- Maintenance: Fewer custom systems to maintain
- Performance: No filesystem operations during development
- Developer experience: Faster, more reliable HMR
- Future-proofing: Better alignment with browser standards and Vite's architecture
Verification Steps
Before implementation, we should verify:
- That Chrome's content script CSP can be extended to allow WebSocket connections to localhost
- The minimum Chrome version required for this feature
- Firefox compatibility with this approach
- Any edge cases where the filesystem approach might still be needed
Implementation Plan
- Create a proof-of-concept branch demonstrating direct WebSocket connection
- Document CSP requirements and browser compatibility
- Implement core WebSocket-based HMR functionality
- Update tests
- Create migration documentation for any breaking changes
- Release as a major version update
Additional Context
This change represents a significant simplification of our architecture. The file writer code is one of the most complex parts of CRXJS, and removing it would make the codebase more maintainable.
Key files to examine:
-
packages/vite-plugin/src/node/fileWriter.ts -
packages/vite-plugin/src/node/fileWriter-hmr.ts -
packages/vite-plugin/src/node/fileWriter-rxjs.ts -
packages/vite-plugin/src/node/plugin-hmr.ts
Alternatives considered
Leave it as-is, but the ad hoc nature of the file writer solution leaves CRXJS open to regressions. Fixing this closes #971
Importance
nice to have
⚠️ Important Notice: CRXJS is seeking new maintainers.
- New issues and PRs may not receive immediate attention
- A new maintenance team must establish itself by March 31, 2025 or this repository will be archived on June 1, 2025
- Learn more about the transition
This is an automated message. Please do not reply to this comment.
Thanks for contributing to CRXJS! This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs within 7 days.
Thanks for contributing to CRXJS! This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs within 7 days.