Skip to content

Commit

Permalink
Make hmr-runtime builtin ES3-compatible (#169)
Browse files Browse the repository at this point in the history
* Make hmr-runtime builtin ES3-compatible

* use a regular for loop for loop in hmr-runtime
  • Loading branch information
chee authored and devongovett committed Dec 11, 2017
1 parent 70663cc commit d17dccc
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/builtins/hmr-runtime.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var global = (1,eval)('this');
var global = (1, eval)('this');
var OldModule = module.bundle.Module;
function Module() {
OldModule.call(this);
Expand All @@ -16,15 +16,14 @@ module.bundle.Module = Module;

if (!module.bundle.parent) {
var ws = new WebSocket('ws://localhost:{{HMR_PORT}}/');
ws.onmessage = (e) => {
var data = JSON.parse(e.data);
ws.onmessage = function(event) {
var data = JSON.parse(event.data);
var index, asset;

if (data.type === 'update') {
for (let asset of data.assets) {
for (index = 0; index < data.assets.length; index++) {
asset = data.assets[index];
hmrApply(global.require, asset);
}

for (let asset of data.assets) {
if (!asset.isNew) {
hmrAccept(global.require, asset.id);
}
Expand All @@ -51,10 +50,12 @@ function getParents(bundle, id) {
return [];
}

let parents = [];
for (let k in modules) {
for (let d in modules[k][1]) {
let dep = modules[k][1][d];
var parents = [];
var k, d, dep;

for (k in modules) {
for (d in modules[k][1]) {
dep = modules[k][1][d];
if (dep === id || (Array.isArray(dep) && dep[dep.length - 1] === id)) {
parents.push(+k);
}
Expand All @@ -75,7 +76,7 @@ function hmrApply(bundle, asset) {
}

if (modules[asset.id] || !bundle.parent) {
let fn = new Function('require', 'module', 'exports', asset.generated.js);
var fn = new Function('require', 'module', 'exports', asset.generated.js);
asset.isNew = !modules[asset.id];
modules[asset.id] = [fn, asset.deps];
} else if (bundle.parent) {
Expand All @@ -93,7 +94,7 @@ function hmrAccept(bundle, id) {
return hmrAccept(bundle.parent, id);
}

let cached = bundle.cache[id];
var cached = bundle.cache[id];
if (cached && cached.hot._disposeCallback) {
cached.hot._disposeCallback();
}
Expand All @@ -107,5 +108,7 @@ function hmrAccept(bundle, id) {
return true;
}

return getParents(global.require, id).some(id => hmrAccept(global.require, id));
return getParents(global.require, id).some(function (id) {
return hmrAccept(global.require, id)
});
}

0 comments on commit d17dccc

Please sign in to comment.