Skip to content

Refactor HMR: on/off switch (default off) and reload fixes - #20

Open
lschirmbrand wants to merge 1 commit into
masterfrom
feature/HmrRefactor
Open

lschirmbrand wants to merge 1 commit into
masterfrom
feature/HmrRefactor

Conversation

@lschirmbrand

Copy link
Copy Markdown
Contributor
  • Add enabled switch (?hmr=on|off, localStorage, API, showToggle()), default off
  • Restore customElements.define in finally, keep polling after errors
  • Match instances by class identity, patch classes via property descriptors
  • Remove deleted members, fix Function.style pollution
  • Resolve reload paths against the page
  • Swap templates for all components when the template changed, redo bindings/events with the original options, swap styles in place

- Add enabled switch (?hmr=on|off, localStorage, API, showToggle()), default off
- Restore customElements.define in finally, keep polling after errors
- Match instances by class identity, patch classes via property descriptors
- Remove deleted members, fix Function.style pollution
- Resolve reload paths against the page
- Swap templates for all components when the template changed, redo
  bindings/events with the original options, swap styles in place

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Polling bypass, stylesheet update bugs, accessibility, and listener cleanup issues remain.

5 open findings
What changed in this PR

Refactors HMR to support opt-in polling, safer module patching, and improved template/style replacement.

Changes:

  • Adds persistent HMR enable/disable controls and UI toggle.
  • Improves polling resilience, module patching, and path resolution.
  • Rebuilds templates, bindings, events, and styles during reloads.
File Description
src/​HotModuleReplacement.ts Implements controls and reload behavior.
src/​BaseCustomWebComponent.ts Retains binding and event setup state.
README.md Documents HMR controls and path requirements.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

button.setAttribute('aria-checked', String(HotModuleReplacement.enabled));
};
button.addEventListener('click', () => HotModuleReplacement.toggle());
window.addEventListener('hmr-enabled-changed', update);
Comment on lines 147 to +149
public static startPolling(interval = 100) {
setTimeout(() => {
HotModuleReplacement.pollForChanges(interval);
}, interval);
HotModuleReplacement.interval = interval;
HotModuleReplacement.schedule();
Comment on lines +282 to +284
const idx = current.findIndex(x => oldSheets.includes(x));
if (idx < 0)
return;
const newCssModule = await import(url + "?reload=" + newId, { with: { type: 'css' } });
const oldStylesheet: CSSStyleSheet = oldCssModule.default;
const newStylesheet: CSSStyleSheet = newCssModule.default;
oldStylesheet.replace(Array.from(newStylesheet.cssRules).map(rule => rule.cssText).join(''));
Comment on lines +109 to +112
button { all: unset; cursor: pointer; display: flex; align-items: center; gap: 8px; padding: 6px 12px 6px 8px;
font: 12px system-ui, sans-serif; color: #fff; background: #333; border-radius: 16px; opacity: .85;
box-shadow: 0 2px 6px rgba(0,0,0,.35); }
button:hover { opacity: 1; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants