-
Notifications
You must be signed in to change notification settings - Fork 61
/
reloading.js
449 lines (393 loc) · 12 KB
/
reloading.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*eslint semi: "error"*/
/**
* This is modified version of Browserify's original module loader function,
* made to support LiveReactLoad's reloading functionality.
*
*
* @param mappings
* An object containing modules and their metadata, created by
* LiveReactLoad plugin. The structure of the mappings object is:
* {
* [module_id]: [
* "...module_source...",
* {
* "module": target_id,
* "./another/module": target_id,
* ...
* },
* {
* hash: "32bit_hash_from_source",
* isEntry: true|false
* }
* ],
* ...
* }
*
* @param entryPoints
* List of bundle's entry point ids. At the moment, only one entry point
* is supported by LiveReactLoad
* @param options
* LiveReactLoad options passed from the CLI/plugin params
*/
function loader(mappings, entryPoints, options) {
if (entryPoints.length > 1) {
throw new Error(
"LiveReactLoad supports only one entry point at the moment"
)
}
var entryId = entryPoints[0];
var scope = {
mappings: mappings,
cache: {},
reloading: false,
reloadHooks: {},
reload: function (fn) {
scope.reloading = true;
try {
fn();
} finally {
scope.reloading = false;
}
}
};
function startClient() {
if (!options.clientEnabled) {
return;
}
if (typeof window.WebSocket === "undefined") {
warn("WebSocket API not available, reloading is disabled");
return;
}
var protocol = window.location.protocol === "https:" ? "wss" : "ws";
var url = protocol + "://" + (options.host || window.location.hostname);
if (options.port != 80) {
url = url + ":" + options.port;
}
var ws = new WebSocket(url);
ws.onopen = function () {
info("WebSocket client listening for changes...");
};
ws.onmessage = function (m) {
var msg = JSON.parse(m.data);
if (msg.type === "change") {
handleBundleChange(msg.data);
} else if (msg.type === "bundle_error") {
handleBundleError(msg.data);
}
}
}
function compile(mapping) {
var body = mapping[0];
if (typeof body !== "function") {
debug("Compiling module", mapping[2])
var compiled = compileModule(body, mapping[2].sourcemap);
mapping[0] = compiled;
mapping[2].source = body;
}
}
function compileModule(source, sourcemap) {
var toModule = new Function(
"__livereactload_source", "__livereactload_sourcemap",
"return eval('function __livereactload_module(require, module, exports){\\n' + __livereactload_source + '\\n}; __livereactload_module;' + (__livereactload_sourcemap || ''));"
);
return toModule(source, sourcemap)
}
function unknownUseCase() {
throw new Error(
"Unknown use-case encountered! Please raise an issue: " +
"https://github.com/milankinen/livereactload/issues"
)
}
// returns loaded module from cache or if not found, then
// loads it from the source and caches it
function load(id, recur) {
var mappings = scope.mappings;
var cache = scope.cache;
if (!cache[id]) {
if (!mappings[id]) {
var req = typeof require == "function" && require;
if (req) return req(id);
var error = new Error("Cannot find module '" + id + "'");
error.code = "MODULE_NOT_FOUND";
throw error;
}
var hook = scope.reloadHooks[id];
var module = cache[id] = {
exports: {},
__accepted: false,
onReload: function (hook) {
scope.reloadHooks[id] = hook;
}
};
mappings[id][0].call(module.exports, function require(path) {
var targetId = mappings[id][1][path];
return load(targetId ? targetId : path);
}, module, module.exports, unknownUseCase, mappings, cache, entryPoints);
if (scope.reloading && typeof hook === "function") {
// it's important **not** to assign to module.__accepted because it would point
// to the old module object during the reload event!
cache[id].__accepted = hook()
}
}
return cache[id].exports;
}
/**
* Patches the existing modules with new sources and returns a list of changes
* (module id and old mapping. ATTENTION: This function does not do any reloading yet.
*
* @param mappings
* New mappings
* @returns {Array}
* List of changes
*/
function patch(mappings) {
var compile = scope.compile;
var changes = [];
keys(mappings).forEach(function (id) {
var old = scope.mappings[id];
var mapping = mappings[id];
var meta = mapping[2];
if (!old || old[2].hash !== meta.hash) {
compile(mapping);
scope.mappings[id] = mapping;
changes.push([id, old]);
}
});
return changes;
}
/**
* Reloads modules based on the given changes. If reloading fails, this function
* tries to restore old implementation.
*
* @param changes
* Changes array received from "patch" function
*/
function reload(changes) {
var changedModules = changes.map(function (c) {
return c[0];
});
var newMods = changes.filter(function (c) {
return !c[1];
}).map(function (c) {
return c[0];
});
scope.reload(function () {
try {
info("Applying changes...");
debug("Changed modules", changedModules);
debug("New modules", newMods);
evaluate(entryId, {});
info("Reload complete!");
} catch (e) {
error("Error occurred while reloading changes. Restoring old implementation...");
console.error(e);
console.error(e.stack);
try {
restore();
evaluate(entryId, {});
info("Restored!");
} catch (re) {
error("Restore failed. You may need to refresh your browser... :-/");
console.error(re);
console.error(re.stack);
}
}
})
function evaluate(id, changeCache) {
if (id in changeCache) {
debug("Circular dependency detected for module", id, "not traversing any further...");
return changeCache[id];
}
if (isExternalModule(id)) {
debug("Module", id, "is an external module. Do not reload");
return false;
}
var module = getModule(id);
debug("Evaluate module details", module);
// initially mark change status to follow module's change status
// TODO: how to propagate change status from children to this without causing infinite recursion?
var meChanged = contains(changedModules, id);
changeCache[id] = meChanged;
if (id in scope.cache) {
delete scope.cache[id];
}
var deps = module.deps.filter(isLocalModule);
var depsChanged = deps.map(function (dep) {
return evaluate(dep, changeCache);
});
// In the case of circular dependencies, the module evaluation stops because of the
// changeCache check above. Also module cache should be clear. However, if some circular
// dependency (or its descendant) gets reloaded, it (re)loads new version of this
// module back to cache. That's why we need to ensure that we're not
// 1) reloading module twice (so that we don't break cross-refs)
// 2) reload any new version if there is no need for reloading
//
// Hence the complex "scope.cache" stuff...
//
var isReloaded = module.cached !== undefined && id in scope.cache;
var depChanged = any(depsChanged);
if (isReloaded || depChanged || meChanged) {
debug("Module changed", id, isReloaded, depChanged, meChanged);
if (!isReloaded) {
var msg = contains(newMods, id) ? " > Add new module ::" : " > Reload module ::";
console.log(msg, id);
load(id);
} else {
console.log(" > Already reloaded ::", id);
}
changeCache[id] = !allExportsProxies(id) && !isAccepted(id);
return changeCache[id];
} else {
// restore old version of the module
if (module.cached !== undefined) {
scope.cache[id] = module.cached;
}
return false;
}
}
function allExportsProxies(id) {
var e = scope.cache[id].exports;
return isProxy(e) || (isPlainObj(e) && all(vals(e), isProxy));
function isProxy(x) {
return x && !!x.__$$LiveReactLoadable;
}
}
function isAccepted(id) {
var accepted = scope.cache[id].__accepted;
scope.cache[id].__accepted = false;
if (accepted === true) {
console.log(" > Manually accepted")
}
return accepted === true;
}
function restore() {
changes.forEach(function (c) {
var id = c[0], mapping = c[1];
if (mapping) {
debug("Restore old mapping", id);
scope.mappings[id] = mapping;
} else {
debug("Delete new mapping", id);
delete scope.mappings[id];
}
})
}
}
function getModule(id) {
return {
deps: vals(scope.mappings[id][1]),
meta: scope.mappings[id][2],
cached: scope.cache[id]
};
}
function handleBundleChange(newMappings) {
info("Bundle changed");
var changes = patch(newMappings);
if (changes.length > 0) {
reload(changes);
} else {
info("Nothing to reload");
}
}
function handleBundleError(data) {
error("Bundling error occurred");
error(data.error);
}
// prepare mappings before starting the app
forEachValue(scope.mappings, compile);
if (options.babel) {
if (isReactTransformEnabled(scope.mappings)) {
info("LiveReactLoad Babel transform detected. Ready to rock!");
} else {
warn(
"Could not detect LiveReactLoad transform (livereactload/babel-transform). " +
"Please see instructions how to setup the transform:\n\n" +
"https://github.com/milankinen/livereactload#installation"
);
}
}
scope.compile = compile;
scope.load = load;
debug("Options:", options);
debug("Entries:", entryPoints, entryId);
startClient();
// standalone bundles may need the exports from entry module
return load(entryId);
// this function is stringified in browserify process and appended to the bundle
// so these helper functions must be inlined into this function, otherwise
// the function is not working
function isReactTransformEnabled(mappings) {
return any(vals(mappings), function (mapping) {
var source = mapping[2].source;
return source && source.indexOf("__$$LiveReactLoadable") !== -1;
});
}
function isLocalModule(id) {
return id.indexOf(options.nodeModulesRoot) === -1
}
function isExternalModule(id) {
return !(id in scope.mappings);
}
function keys(obj) {
return obj ? Object.keys(obj) : [];
}
function vals(obj) {
return keys(obj).map(function (key) {
return obj[key];
});
}
function contains(col, val) {
for (var i = 0; i < col.length; i++) {
if (col[i] === val) return true;
}
return false;
}
function all(col, f) {
if (!f) {
f = function (x) {
return x;
};
}
for (var i = 0; i < col.length; i++) {
if (!f(col[i])) return false;
}
return true;
}
function any(col, f) {
if (!f) {
f = function (x) {
return x;
};
}
for (var i = 0; i < col.length; i++) {
if (f(col[i])) return true;
}
return false;
}
function forEachValue(obj, fn) {
keys(obj).forEach(function (key) {
if (obj.hasOwnProperty(key)) {
fn(obj[key]);
}
});
}
function isPlainObj(x) {
return typeof x == 'object' && x.constructor == Object;
}
function debug() {
if (options.debug) {
console.log.apply(console, ["LiveReactload [DEBUG] ::"].concat(Array.prototype.slice.call(arguments)));
}
}
function info(msg) {
console.info("LiveReactload ::", msg);
}
function warn(msg) {
console.warn("LiveReactload ::", msg);
}
function error(msg) {
console.error("LiveReactload ::", msg);
}
}
module.exports = loader;
module.exports["default"] = loader;