From ba697ef5907f83719be2eb4782e3a34d4b686acd Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig Date: Sun, 3 Mar 2019 23:14:05 +0100 Subject: [PATCH] Don't execute twice on HMR full reload --- packages/core/integration-tests/test/hmr.js | 17 ++++++++-- .../src/builtins/hmr-runtime.js | 32 +++++++++++-------- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/packages/core/integration-tests/test/hmr.js b/packages/core/integration-tests/test/hmr.js index eb3b2dfff50..bf4aa9ffedd 100644 --- a/packages/core/integration-tests/test/hmr.js +++ b/packages/core/integration-tests/test/hmr.js @@ -404,10 +404,22 @@ 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'), @@ -415,6 +427,7 @@ describe('hmr', function() { ); await nextEvent(b, 'bundled'); + assert.deepEqual(outputs, [3]); assert(spy.calledOnce); }); diff --git a/packages/core/parcel-bundler/src/builtins/hmr-runtime.js b/packages/core/parcel-bundler/src/builtins/hmr-runtime.js index da4199c7c77..49c12e8ba84 100644 --- a/packages/core/parcel-bundler/src/builtins/hmr-runtime.js +++ b/packages/core/parcel-bundler/src/builtins/hmr-runtime.js @@ -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(); } }