diff --git a/src/hmr/hotModuleReplacement.js b/src/hmr/hotModuleReplacement.js index 802c0a12..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'; @@ -144,6 +144,10 @@ function getReloadUrl(href, src) { } function reloadStyle(src) { + if (!src) { + return false; + } + const elements = document.querySelectorAll('link'); let loaded = false; @@ -227,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`] = `""`;