You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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-*.ts files
Related RxMap implementation
Update HMR implementation:
Modify plugin-hmr.ts to 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
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
The text was updated successfully, but these errors were encountered:
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:
content_security_policy
w3c/webextensions#98)Proposed Solution
We should replace our custom filesystem-based HMR with a direct connection to Vite's dev server WebSocket. This would:
Technical Details
Current Implementation
Our current approach involves:
fileWriter.ts
,fileWriter-*.ts
)fileWriter-rxjs.ts
)fileWriter-hmr.ts
)Describe the proposed solution
Proposed Implementation
We can replace this with:
Code Changes Required
Remove file writer modules:
fileWriter.ts
fileWriter-*.ts
filesUpdate HMR implementation:
plugin-hmr.ts
to use Vite's WebSocket directlyCSP Handling:
Tests:
Benefits
Verification Steps
Before implementation, we should verify:
Implementation Plan
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
The text was updated successfully, but these errors were encountered: