Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: js exception when reloading async chunk (#444) #531

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/hmr/hotModuleReplacement.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

const normalizeUrl = require('normalize-url');

const srcByModuleId = Object.create(null);
let srcByModuleId = Object.create(null);

const noDocument = typeof document === 'undefined';

Expand Down Expand Up @@ -144,6 +144,10 @@ function getReloadUrl(href, src) {
}

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

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

Expand Down Expand Up @@ -227,3 +231,8 @@ module.exports = function(moduleId, options) {

return debounce(update, 50);
};

// Used from tests
module.exports.clearCache = function() {
srcByModuleId = Object.create(null);
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use mock for test it, no need it in runtime code

Copy link
Author

@Rulexec Rulexec Jul 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How to mock variable inside module?

Currently value inside srcByModuleId persists between tests and my situation happens only if it's clear.

27 changes: 27 additions & 0 deletions test/HMR.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ describe('HMR', () => {

document.head.innerHTML = '<link rel="stylesheet" href="/dist/main.css" />';
document.body.innerHTML = '<script src="/dist/main.js"></script>';

hotModuleReplacement.clearCache();
});

afterEach(() => {
Expand Down Expand Up @@ -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', {});

Expand Down
4 changes: 4 additions & 0 deletions test/__snapshots__/HMR.test.js.snap
Original file line number Diff line number Diff line change
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 reloads with reloadAll option 1`] = `"[HMR] Reload all css"`;

exports[`HMR should reloads with reloadAll option 2`] = `"<link rel=\\"stylesheet\\" href=\\"/dist/main.css\\"><link rel=\\"stylesheet\\" href=\\"http://localhost/dist/main.css?1479427200000\\">"`;
Expand Down