Skip to content

Commit

Permalink
Don't execute twice on HMR full reload
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Mar 3, 2019
1 parent b54120a commit ba697ef
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
17 changes: 15 additions & 2 deletions packages/core/integration-tests/test/hmr.js
Expand Up @@ -404,17 +404,30 @@ describe('hmr', function() {
});
let bundle = await b.bundle();

let ctx = await run(bundle, {output() {}}, {require: false});

let outputs = [];
let ctx = await run(
bundle,
{
output(o) {
outputs.push(o);
}
},
{require: false}
);
let spy = sinon.spy(ctx.location, 'reload');

await sleep(50);
assert.deepEqual(outputs, [3]);
assert(spy.notCalled);

await sleep(100);
fs.writeFile(
path.join(__dirname, '/input/local.js'),
'exports.a = 5; exports.b = 5;'
);

await nextEvent(b, 'bundled');
assert.deepEqual(outputs, [3]);
assert(spy.calledOnce);
});

Expand Down
32 changes: 18 additions & 14 deletions packages/core/parcel-bundler/src/builtins/hmr-runtime.js
Expand Up @@ -33,23 +33,27 @@ if ((!parent || !parent.isParcelRequire) && typeof WebSocket !== 'undefined') {
var data = JSON.parse(event.data);

if (data.type === 'update') {
console.clear();

data.assets.forEach(function (asset) {
hmrApply(global.parcelRequire, asset);
});

var handled = false;
data.assets.forEach(function (asset) {
if (!asset.isNew) {
var didAccept = hmrAccept(global.parcelRequire, asset.id);
if (didAccept) {
handled = true;
}
}
data.assets.forEach(function(asset){
handled = handled ||
Boolean(global.parcelRequire.cache[asset.id].hot._acceptCallbacks.length) ||
Boolean(global.parcelRequire.cache[asset.id].hot._disposeCallbacks.length);
});

if(!handled) {
if(handled){
console.clear();

data.assets.forEach(function (asset) {
hmrApply(global.parcelRequire, asset);
});

var handled = false;
data.assets.forEach(function (asset) {
if (!asset.isNew) {
hmrAccept(global.parcelRequire, asset.id);
}
});
} else {
window.location.reload();
}
}
Expand Down

0 comments on commit ba697ef

Please sign in to comment.