From aef0374efde8021fc7bf34ec0a688969574c4ee7 Mon Sep 17 00:00:00 2001 From: Alexander Ruliov Date: Fri, 22 May 2020 13:17:44 +0300 Subject: [PATCH 1/2] fix: js exception when reloading async chunk (#444) --- src/hmr/hotModuleReplacement.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/hmr/hotModuleReplacement.js b/src/hmr/hotModuleReplacement.js index 802c0a12..7802749b 100644 --- a/src/hmr/hotModuleReplacement.js +++ b/src/hmr/hotModuleReplacement.js @@ -144,6 +144,10 @@ function getReloadUrl(href, src) { } function reloadStyle(src) { + if (!src) { + return false; + } + const elements = document.querySelectorAll('link'); let loaded = false; From f7e1963ef665a535d00aea6ebcc02c3a5fb0aa67 Mon Sep 17 00:00:00 2001 From: Alexander Ruliov Date: Thu, 28 May 2020 20:42:52 +0300 Subject: [PATCH 2/2] test: hmr with non-file script in the end of page --- src/hmr/hotModuleReplacement.js | 7 ++++++- test/HMR.test.js | 27 +++++++++++++++++++++++++++ test/__snapshots__/HMR.test.js.snap | 4 ++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/hmr/hotModuleReplacement.js b/src/hmr/hotModuleReplacement.js index 7802749b..bc9db1bc 100644 --- a/src/hmr/hotModuleReplacement.js +++ b/src/hmr/hotModuleReplacement.js @@ -7,7 +7,7 @@ const normalizeUrl = require('normalize-url'); -const srcByModuleId = Object.create(null); +let srcByModuleId = Object.create(null); const noDocument = typeof document === 'undefined'; @@ -231,3 +231,8 @@ module.exports = function(moduleId, options) { return debounce(update, 50); }; + +// Used from tests +module.exports.clearCache = function() { + srcByModuleId = Object.create(null); +}; diff --git a/test/HMR.test.js b/test/HMR.test.js index be56e01d..f9a37b8d 100644 --- a/test/HMR.test.js +++ b/test/HMR.test.js @@ -29,6 +29,8 @@ describe('HMR', () => { document.head.innerHTML = ''; document.body.innerHTML = ''; + + hotModuleReplacement.clearCache(); }); afterEach(() => { @@ -259,6 +261,31 @@ describe('HMR', () => { }, 100); }); + it('should reloads with non-file script in the end of page', (done) => { + document.body.appendChild(document.createElement('script')); + + const update = hotModuleReplacement('./src/style.css', {}); + + update(); + + setTimeout(() => { + expect(console.log.mock.calls[0][0]).toMatchSnapshot(); + + const links = Array.prototype.slice.call( + document.querySelectorAll('link') + ); + + expect(links[0].visited).toBe(true); + expect(document.head.innerHTML).toMatchSnapshot(); + + links[1].dispatchEvent(getLoadEvent()); + + expect(links[1].isLoaded).toBe(true); + + done(); + }, 100); + }); + it('should handle error event', (done) => { const update = hotModuleReplacement('./src/style.css', {}); diff --git a/test/__snapshots__/HMR.test.js.snap b/test/__snapshots__/HMR.test.js.snap index 269260d0..06cf0761 100644 --- a/test/__snapshots__/HMR.test.js.snap +++ b/test/__snapshots__/HMR.test.js.snap @@ -24,6 +24,10 @@ exports[`HMR should reloads with non http/https link href 1`] = `"[HMR] css relo exports[`HMR should reloads with non http/https link href 2`] = `""`; +exports[`HMR should reloads with non-file script in the end of page 1`] = `"[HMR] Reload all css"`; + +exports[`HMR should reloads with non-file script in the end of page 2`] = `""`; + exports[`HMR should reloads with reloadAll option 1`] = `"[HMR] Reload all css"`; exports[`HMR should reloads with reloadAll option 2`] = `""`;