forked from choojs/bankai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client-hmr.js
69 lines (59 loc) · 1.99 KB
/
client-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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var nanoajax = require('nanoajax')
// var yo = require('yo-yo')
// script injected in development mode
function updateStream (target, callback) {
var es = new window.EventSource(target)
es.addEventListener('message', function (e) {
nanoajax.ajax({url: target}, function (code, text) {
callback(null, text)
})
})
}
function toArray (list) {
return Array.prototype.slice.call(list)
}
function main () {
var scripts = toArray(document.querySelectorAll('script[data-bankai-hmr]'))
var styles = toArray(document.querySelectorAll('link[data-bankai-hmr]'))
scripts.forEach(function (script) {
updateStream(script.src, function (error, data) {
if (error) {
console.error(error)
}
var el = document.createElement('script')
var prev = document.querySelector('[data-bankai-hmr-copy="' + script.src + '"]')
el.text = data
el.setAttribute('data-bankai-hmr-copy', script.src)
if (prev) {
prev.parentNode.replaceChild(el, prev)
} else {
script.parentNode.insertBefore(el, script.nextSibling)
}
var id = script.dataset.bankaiHmr
var application = require(id)
var app = application(window.__BANKAI_GLOBAL_STATE_HOOK__, {
onStateChange: function (data, state) { window.__BANKAI_GLOBAL_STATE_HOOK__ = state }
})
var tree = app.start()
document.body.replaceChild(tree, document.body.firstChild)
// yo.update(old, tree)
})
})
styles.forEach(function (style) {
updateStream(style.href, function (error, data) {
if (error) {
console.error(error)
}
var el = document.createElement('style')
var prev = document.querySelector('[data-bankai-hmr-copy="' + style.href + '"]')
el.setAttribute('data-bankai-hmr-copy', style.href)
el.appendChild(document.createTextNode(data))
if (prev) {
prev.parentNode.replaceChild(el, prev)
} else {
style.parentNode.insertBefore(el, style.nextSibling)
}
})
})
}
main()