livereload-morph 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,10 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Noah A.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10
+
package/README.md ADDED
@@ -0,0 +1,160 @@
1
+ # livereload-morph
2
+
3
+ A livereload-js replacement that uses **idiomorph** for intelligent DOM morphing. Instead of full page reloads, livereload-morph preserves page state by morphing HTML changes directly into the DOM.
4
+
5
+ ## Features
6
+
7
+ - **HTML Morphing**: HTML file changes update the DOM without full page reload, preserving:
8
+ - Input field values and cursor position
9
+ - Scroll position
10
+ - Active elements and focus
11
+ - Component state
12
+
13
+ - **CSS Live Reload**: CSS changes update without flash using clone-and-replace strategy
14
+
15
+ - **LiveReload Protocol 7 Compatible**: Works with existing LiveReload servers (guard-livereload, browser-sync, etc.)
16
+
17
+ - **Minimal & Fast**: Small bundle (includes idiomorph), vanilla JavaScript, no dependencies
18
+
19
+ ## Quick Start
20
+
21
+ ### Installation
22
+
23
+ ```bash
24
+ bun install
25
+ ```
26
+
27
+ ### Build
28
+
29
+ ```bash
30
+ bun run build
31
+ ```
32
+
33
+ ### Testing
34
+
35
+ Start the test server with LiveReload:
36
+
37
+ ```bash
38
+ bun run test
39
+ ```
40
+
41
+ Then open http://localhost:3000/test/ in your browser.
42
+
43
+ **Test HTML Morphing:**
44
+ 1. Type in the input field
45
+ 2. Edit `test/index.html` (change text, add elements, etc.)
46
+ 3. Watch the page update without losing your input!
47
+
48
+ **Test CSS Reload:**
49
+ 1. Edit `test/styles.css` (change `.color-box` background color)
50
+ 2. Watch styles update with no flash
51
+
52
+ ## Usage
53
+
54
+ Add to your HTML page:
55
+
56
+ ```html
57
+ <script type="module">
58
+ window.LiveMorphOptions = {
59
+ host: 'localhost',
60
+ port: 35729,
61
+ verbose: true, // Enable console logging
62
+ morphHTML: true // Enable HTML morphing (default: true)
63
+ };
64
+ </script>
65
+ <script type="module" src="http://localhost:35729/livereload-morph.js"></script>
66
+ ```
67
+
68
+ Or use query string parameters:
69
+
70
+ ```html
71
+ <script type="module" src="http://localhost:35729/livereload-morph.js?host=localhost&verbose=true"></script>
72
+ ```
73
+
74
+ ## Configuration Options
75
+
76
+ | Option | Type | Default | Description |
77
+ |--------|------|---------|-------------|
78
+ | `host` | string | (required) | LiveReload server hostname |
79
+ | `port` | number | 35729 | LiveReload server port |
80
+ | `path` | string | 'livereload' | WebSocket path |
81
+ | `https` | boolean | false | Use secure WebSocket (wss://) |
82
+ | `morphHTML` | boolean | true | Enable HTML morphing |
83
+ | `verbose` | boolean | false | Enable console logging |
84
+ | `importCacheWaitPeriod` | number | 200 | Enable legacy WebKit @import workaround. Set to any value > 0 to enable, 0 to disable |
85
+ | `mindelay` | number | 1000 | Min reconnection delay (ms) |
86
+ | `maxdelay` | number | 60000 | Max reconnection delay (ms) |
87
+ | `handshake_timeout` | number | 5000 | Handshake timeout (ms) |
88
+
89
+ ## How It Works
90
+
91
+ 1. **WebSocket Connection**: Connects to LiveReload server on port 35729
92
+ 2. **File Change Detection**: Server sends `reload` command with changed file path
93
+ 3. **Smart Routing**:
94
+ - `.css` / `.css.map` files → Clone-and-replace for `<link>` tags, CSSOM rule replacement for `@import`
95
+ - Images (`.jpg`, `.png`, `.gif`, `.svg`, `.webp`, `.ico`) → Cache-bust `<img>` src and CSS backgrounds
96
+ - `.js` / `.mjs` files → Full page reload (no safe hot-reload)
97
+ - Everything else → Morph with idiomorph (re-fetches page HTML)
98
+ 4. **State Preservation**: idiomorph intelligently merges changes while preserving DOM state
99
+
100
+ ### CSS Reload Details
101
+
102
+ Live-morph supports both `<link>` tags and `@import` rules:
103
+
104
+ - **`<link>` tags**: Clone with cache-busted URL, wait for load, remove original (prevents FOUC)
105
+ - **`@import` rules**: Replace rule in CSSOM with cache-busted URL
106
+ - By default uses legacy WebKit workaround (pre-cache with temp `<link>` tag to trigger browser fetch)
107
+ - Prevents flicker when updating `@import` rules (still needed in modern browsers!)
108
+ - Set `importCacheWaitPeriod: 0` to disable workaround (will cause brief flicker)
109
+ - **Cross-origin CSS**: CORS-protected stylesheets are handled gracefully (can't inspect `@import` rules)
110
+
111
+ ## State Preservation
112
+
113
+ **What's preserved automatically:**
114
+ - Input values (text, textarea, select)
115
+ - Checkbox/radio checked state
116
+ - `<details>` open/closed state
117
+
118
+ **For best results, add IDs to form elements.** Without IDs, idiomorph may recreate elements instead of morphing them, which can lose state in edge cases.
119
+
120
+ ## vs livereload-js
121
+
122
+ | Feature | livereload-morph | livereload-js |
123
+ |---------|------------------|---------------|
124
+ | HTML updates | ✅ Morph (preserves state) | ❌ Full reload |
125
+ | CSS updates | ✅ Clone-replace | ✅ Clone-replace |
126
+ | Input state | ✅ Preserved | ❌ Lost on reload |
127
+ | Scroll position | ✅ Preserved | ❌ Lost on reload |
128
+ | Focus state | ✅ Preserved | ❌ Lost on reload |
129
+
130
+ ## Development
131
+
132
+ ```bash
133
+ # Build once
134
+ bun run build
135
+
136
+ # Build and watch for changes
137
+ bun run build:watch
138
+
139
+ # Run test server (builds + starts server)
140
+ bun run test
141
+
142
+ # Development mode (watch build + server)
143
+ bun run dev
144
+ ```
145
+
146
+ ## Browser Support
147
+
148
+ Modern browsers with ES6+ support:
149
+ - Chrome/Edge 60+
150
+ - Firefox 60+
151
+ - Safari 12+
152
+
153
+ ## License
154
+
155
+ MIT
156
+
157
+ ## Credits
158
+
159
+ - Built with [idiomorph](https://github.com/bigskysoftware/idiomorph) for DOM morphing
160
+ - Compatible with [LiveReload Protocol 7](github.com/livereload/livereload-js)