Skip to content

Commit

Permalink
fix: error when reloading async chunk (#633)
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Oct 21, 2020
1 parent 19ec95b commit 89e7a0a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/hmr/hotModuleReplacement.js
Expand Up @@ -143,6 +143,10 @@ function getReloadUrl(href, src) {
}

function reloadStyle(src) {
if (!src) {
return false;
}

const elements = document.querySelectorAll('link');
let loaded = false;

Expand Down
25 changes: 25 additions & 0 deletions test/HMR.test.js
Expand Up @@ -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', {});

Expand Down
4 changes: 4 additions & 0 deletions test/__snapshots__/HMR.test.js.snap
Expand Up @@ -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\\">"`;
Expand Down
4 changes: 4 additions & 0 deletions test/cases/hmr/expected/webpack-4/main.js
Expand Up @@ -1022,6 +1022,10 @@ function getReloadUrl(href, src) {
}

function reloadStyle(src) {
if (!src) {
return false;
}

const elements = document.querySelectorAll('link');
let loaded = false;

Expand Down
4 changes: 4 additions & 0 deletions test/cases/hmr/expected/webpack-5/main.js
Expand Up @@ -164,6 +164,10 @@ function getReloadUrl(href, src) {
}

function reloadStyle(src) {
if (!src) {
return false;
}

const elements = document.querySelectorAll('link');
let loaded = false;

Expand Down

0 comments on commit 89e7a0a

Please sign in to comment.