-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev.js
91 lines (78 loc) · 2.46 KB
/
dev.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* global System */
window.globals = {
module: {
load: (module_id) => System.import(module_id),
},
}
let moduleToDependents = {}
const existingHook = System.constructor.prototype.onload
System.constructor.prototype.onload = (error, module_id, dependencies) =>
Promise.resolve(existingHook.call(this, error, module_id, dependencies)).then(
(existingHookResult) => {
moduleToDependents = {
...moduleToDependents,
...Object.fromEntries(
dependencies.map((dependency) => [
dependency,
moduleToDependents[dependency]
? { [module_id]: module_id, ...moduleToDependents[dependency] }
: { [module_id]: module_id },
]),
),
}
return existingHookResult
},
)
new WebSocket(location.origin.replace(/^http/, 'ws')).onmessage = (payload) => {
const data = JSON.parse(payload.data)
if (
data.path === '/index.html' ||
data.path === '/index.js' ||
data.path === '/dev.js'
) {
console.log('reloading page due to change in: ', data.path)
location.reload()
return
}
window.globals.module.load('app').then((app) => {
app.reset()
const trace = (dependents) =>
Object.values(dependents)
.map((dependent) =>
moduleToDependents[dependent]
? {
...trace(moduleToDependents[dependent]),
[dependent]: dependent,
}
: { [dependent]: dependent },
)
.reduce(
(dependents, nextDependents) => ({
...dependents,
...nextDependents,
}),
{},
)
const changedModuleId = System.resolve(data.path)
const dependents = moduleToDependents[changedModuleId]
if (dependents) {
const transitiveDependents = {
...trace(dependents),
[changedModuleId]: changedModuleId,
}
console.log('reloading modules: ', Object.values(transitiveDependents))
Object.values(transitiveDependents).forEach((module_id) =>
System.delete(module_id),
)
} else {
const internalModules = Array.from(System.entries())
.map(([module_id]) => module_id)
.filter((module_id) => module_id.startsWith('http://localhost'))
console.log('reloading all internal modules: ', internalModules)
internalModules.forEach((module_id) => System.delete(module_id))
}
window.globals.module.load('app').then((app) => {
app.init()
})
})
}