Skip to content

Commit

Permalink
fix: hmr in browser extension
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Aug 4, 2021
1 parent 3c9293b commit 3d09da1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/hmr/hotModuleReplacement.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ function isUrlRequest(url) {
// An URL is not an request if

// It is not http or https
if (!/^https?:/i.test(url)) {
if (!/^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(url)) {
return false;
}

Expand Down
27 changes: 27 additions & 0 deletions test/HMR.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,33 @@ describe("HMR", () => {
}, 100);
});

it("should reloads with browser extension protocol", (done) => {
document.head.innerHTML =
'<link rel="stylesheet" href="/dist/main.css" /><link rel="stylesheet" href="chrome-extension://main.css" />';

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);
expect(links[2].visited).toBeUndefined();

done();
}, 100);
});

it("should reloads with non-file script in the end of page", (done) => {
document.body.appendChild(document.createElement("script"));

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 @@ -12,6 +12,10 @@ exports[`HMR should reloads with absolute remove url 1`] = `"[HMR] css reload %s
exports[`HMR should reloads with absolute remove url 2`] = `"<link rel=\\"stylesheet\\" href=\\"/dist/main.css\\"><link rel=\\"stylesheet\\" href=\\"http://localhost/dist/main.css?1479427200000\\"><link rel=\\"stylesheet\\" href=\\"http://dev.com/dist/main.css\\">"`;
exports[`HMR should reloads with browser extension protocol 1`] = `"[HMR] css reload %s"`;
exports[`HMR should reloads with browser extension protocol 2`] = `"<link rel=\\"stylesheet\\" href=\\"/dist/main.css\\"><link rel=\\"stylesheet\\" href=\\"http://localhost/dist/main.css?1479427200000\\"><link rel=\\"stylesheet\\" href=\\"chrome-extension://main.css\\">"`;
exports[`HMR should reloads with link without href 1`] = `"[HMR] css reload %s"`;
exports[`HMR should reloads with link without href 2`] = `"<link rel=\\"stylesheet\\" href=\\"/dist/main.css\\"><link rel=\\"stylesheet\\" href=\\"http://localhost/dist/main.css?1479427200000\\"><link rel=\\"shortcut icon\\">"`;
Expand Down
2 changes: 1 addition & 1 deletion test/cases/hmr/expected/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ function isUrlRequest(url) {
// An URL is not an request if

// It is not http or https
if (!/^https?:/i.test(url)) {
if (!/^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(url)) {
return false;
}

Expand Down

0 comments on commit 3d09da1

Please sign in to comment.