From f5a31fef1888ab4a93d45a8619e13e7a59899087 Mon Sep 17 00:00:00 2001
From: cap-Bernardito <cap-Bernardito@yandex.ru>
Date: Wed, 21 Oct 2020 13:19:56 +0300
Subject: [PATCH] fix: error when reloading async chunk

---
 src/hmr/hotModuleReplacement.js           |  4 ++++
 test/HMR.test.js                          | 25 +++++++++++++++++++++++
 test/__snapshots__/HMR.test.js.snap       |  4 ++++
 test/cases/hmr/expected/webpack-4/main.js |  4 ++++
 test/cases/hmr/expected/webpack-5/main.js |  4 ++++
 5 files changed, 41 insertions(+)

diff --git a/src/hmr/hotModuleReplacement.js b/src/hmr/hotModuleReplacement.js
index 4b980611..c5b1bbfd 100644
--- a/src/hmr/hotModuleReplacement.js
+++ b/src/hmr/hotModuleReplacement.js
@@ -143,6 +143,10 @@ function getReloadUrl(href, src) {
 }
 
 function reloadStyle(src) {
+  if (!src) {
+    return false;
+  }
+
   const elements = document.querySelectorAll('link');
   let loaded = false;
 
diff --git a/test/HMR.test.js b/test/HMR.test.js
index c8d51093..6a2f0067 100644
--- a/test/HMR.test.js
+++ b/test/HMR.test.js
@@ -259,6 +259,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/non_file_styles.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 9f4c8542..bf306c32 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`] = `"<link rel=\\"stylesheet\\" href=\\"/dist/main.css\\"><link rel=\\"stylesheet\\" href=\\"http://localhost/dist/main.css?1479427200000\\"><link rel=\\"shortcut icon\\" href=\\"data:;base64,=\\">"`;
 
+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`] = `"<link rel=\\"stylesheet\\" href=\\"/dist/main.css\\"><link rel=\\"stylesheet\\" href=\\"http://localhost/dist/main.css?1479427200000\\">"`;
+
 exports[`HMR should work reload all css 1`] = `"[HMR] Reload all css"`;
 
 exports[`HMR should work reload all css 2`] = `"<link rel=\\"stylesheet\\" href=\\"/dist/main.css\\"><link rel=\\"stylesheet\\" href=\\"http://localhost/dist/main.css?1479427200000\\">"`;
diff --git a/test/cases/hmr/expected/webpack-4/main.js b/test/cases/hmr/expected/webpack-4/main.js
index d96f1332..693d6d26 100644
--- a/test/cases/hmr/expected/webpack-4/main.js
+++ b/test/cases/hmr/expected/webpack-4/main.js
@@ -1022,6 +1022,10 @@ function getReloadUrl(href, src) {
 }
 
 function reloadStyle(src) {
+  if (!src) {
+    return false;
+  }
+
   const elements = document.querySelectorAll('link');
   let loaded = false;
 
diff --git a/test/cases/hmr/expected/webpack-5/main.js b/test/cases/hmr/expected/webpack-5/main.js
index 7441653f..661f7fdb 100644
--- a/test/cases/hmr/expected/webpack-5/main.js
+++ b/test/cases/hmr/expected/webpack-5/main.js
@@ -164,6 +164,10 @@ function getReloadUrl(href, src) {
 }
 
 function reloadStyle(src) {
+  if (!src) {
+    return false;
+  }
+
   const elements = document.querySelectorAll('link');
   let loaded = false;