forked from roxiness/routify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhmr.js
34 lines (30 loc) · 1.24 KB
/
hmr.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* mounts app to target element
*
* @export
* @param {object} Component Svelte component
* @param {object} [options={ target: document.body }] Options for the Svelte component
* @param {string} [id='hmr'] ID for the component container
* @param {string} [eventName='app-loaded'] Name of the event that triggers replacement of previous component
* @returns
*/
export default function HMR(Component, options = { target: document.body }, id = 'hmr', eventName = 'app-loaded') {
const prerenderedHtmlElement = document.getElementById(id)
// Create a hidden target element to contain our app
const target = document.createElement("div")
target.style.visibility = 'hidden'
options.target.appendChild(target)
if (!prerenderedHtmlElement)
showApp()
else
// Wait for the app to load before replacing the prerendered HTML
addEventListener(eventName, showApp)
function showApp() {
removeEventListener(eventName, showApp)
if (prerenderedHtmlElement) prerenderedHtmlElement.remove()
// Show our component and take over the ID of the old container
target.style.visibility = null
target.setAttribute('id', id)
}
return new Component({ ...options, target });
}