Skip to content

Commit 3d09da1

Browse files
fix: hmr in browser extension
1 parent 3c9293b commit 3d09da1

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

src/hmr/hotModuleReplacement.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ function isUrlRequest(url) {
199199
// An URL is not an request if
200200

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

test/HMR.test.js

+27
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,33 @@ describe("HMR", () => {
262262
}, 100);
263263
});
264264

265+
it("should reloads with browser extension protocol", (done) => {
266+
document.head.innerHTML =
267+
'<link rel="stylesheet" href="/dist/main.css" /><link rel="stylesheet" href="chrome-extension://main.css" />';
268+
269+
const update = hotModuleReplacement("./src/style.css", {});
270+
271+
update();
272+
273+
setTimeout(() => {
274+
expect(console.log.mock.calls[0][0]).toMatchSnapshot();
275+
276+
const links = Array.prototype.slice.call(
277+
document.querySelectorAll("link")
278+
);
279+
280+
expect(links[0].visited).toBe(true);
281+
expect(document.head.innerHTML).toMatchSnapshot();
282+
283+
links[1].dispatchEvent(getLoadEvent());
284+
285+
expect(links[1].isLoaded).toBe(true);
286+
expect(links[2].visited).toBeUndefined();
287+
288+
done();
289+
}, 100);
290+
});
291+
265292
it("should reloads with non-file script in the end of page", (done) => {
266293
document.body.appendChild(document.createElement("script"));
267294

test/__snapshots__/HMR.test.js.snap

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ exports[`HMR should reloads with absolute remove url 1`] = `"[HMR] css reload %s
1212
1313
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\\">"`;
1414
15+
exports[`HMR should reloads with browser extension protocol 1`] = `"[HMR] css reload %s"`;
16+
17+
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\\">"`;
18+
1519
exports[`HMR should reloads with link without href 1`] = `"[HMR] css reload %s"`;
1620
1721
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\\">"`;

test/cases/hmr/expected/main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ function isUrlRequest(url) {
220220
// An URL is not an request if
221221

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

0 commit comments

Comments
 (0)