Skip to content

Commit

Permalink
Merge pull request #13450 from webpack/test/hmr-move-between-runtime
Browse files Browse the repository at this point in the history
add test case for moving modules and chunks between runtimes
  • Loading branch information
sokra committed May 27, 2021
2 parents 8da0542 + cf0c816 commit 1131afe
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 5 deletions.
16 changes: 11 additions & 5 deletions test/helpers/createFakeWorker.js
Expand Up @@ -62,14 +62,20 @@ require(${JSON.stringify(path.resolve(outputDirectory, file))});
this.worker = new (require("worker_threads").Worker)(workerBootstrap, {
eval: true
});

this._onmessage = undefined;
}

set onmessage(value) {
this.worker.on("message", data => {
value({
data
});
});
if (this._onmessage) this.worker.off("message", this._onmessage);
this.worker.on(
"message",
(this._onmessage = data => {
value({
data
});
})
);
}

postMessage(data) {
Expand Down
1 change: 1 addition & 0 deletions test/hotCases/worker/move-between-runtime/chunk.js
@@ -0,0 +1 @@
export default "chunk";
1 change: 1 addition & 0 deletions test/hotCases/worker/move-between-runtime/chunkS.js
@@ -0,0 +1 @@
export default "chunkS";
33 changes: 33 additions & 0 deletions test/hotCases/worker/move-between-runtime/index.js
@@ -0,0 +1,33 @@
const update = () =>
new Promise((resolve, reject) => {
NEXT(err => {
if (err) reject(err);
else resolve();
});
});

const expectMessage = (w, msg) =>
new Promise((resolve, reject) => {
w.onmessage = ({ data }) => {
if (data === msg) resolve();
else reject(new Error(data));
};
});

const next = w => {
const p = expectMessage(w, "next");
w.postMessage("next");
return p;
};

it("should support hot module replacement in WebWorkers", async () => {
const a = new Worker(new URL("workerA.js", import.meta.url));
const b = new Worker(new URL("workerB.js", import.meta.url));
for (let i = 0; i < 7; i++) {
await update();
await next(a);
await next(b);
}
await a.terminate();
await b.terminate();
});
1 change: 1 addition & 0 deletions test/hotCases/worker/move-between-runtime/module.js
@@ -0,0 +1 @@
export default "module";
19 changes: 19 additions & 0 deletions test/hotCases/worker/move-between-runtime/moduleA.js
@@ -0,0 +1,19 @@
export default 0;
---
export default 1;
import "./module";
---
export default 2;
import "./module";
---
export default 3;
---
export default 4;
if (Math.random() < 0) import("./chunk");
---
export default 5;
if (Math.random() < 0) import("./chunk");
---
export default 6;
---
export default 7;
19 changes: 19 additions & 0 deletions test/hotCases/worker/move-between-runtime/moduleAs.js
@@ -0,0 +1,19 @@
export default 0;
---
export default 1;
import "./moduleS";
---
export default 2;
import "./moduleS";
---
export default 3;
---
export default 4;
if (Math.random() < 0) import("./chunkS");
---
export default 5;
if (Math.random() < 0) import("./chunkS");
---
export default 6;
---
export default 7;
19 changes: 19 additions & 0 deletions test/hotCases/worker/move-between-runtime/moduleB.js
@@ -0,0 +1,19 @@
export default 0;
---
export default 1;
---
export default 2;
import "./module";
---
export default 3;
import "./module";
---
export default 4;
---
export default 5;
if (Math.random() < 0) import("./chunk");
---
export default 6;
if (Math.random() < 0) import("./chunk");
---
export default 7;
19 changes: 19 additions & 0 deletions test/hotCases/worker/move-between-runtime/moduleBs.js
@@ -0,0 +1,19 @@
export default 0;
---
export default 1;
---
export default 2;
import "./moduleS";
---
export default 3;
import "./moduleS";
---
export default 4;
---
export default 5;
if (Math.random() < 0) import("./chunkS");
---
export default 6;
if (Math.random() < 0) import("./chunkS");
---
export default 7;
1 change: 1 addition & 0 deletions test/hotCases/worker/move-between-runtime/moduleS.js
@@ -0,0 +1 @@
export default "moduleS";
5 changes: 5 additions & 0 deletions test/hotCases/worker/move-between-runtime/test.filter.js
@@ -0,0 +1,5 @@
var supportsWorker = require("../../../helpers/supportsWorker");

module.exports = function (config) {
return supportsWorker();
};
18 changes: 18 additions & 0 deletions test/hotCases/worker/move-between-runtime/worker.js
@@ -0,0 +1,18 @@
export default fn => {
self.onmessage = async ({ data: msg }) => {
try {
switch (msg) {
case "next":
if (!(await import.meta.webpackHot.check(true)))
throw new Error("No update found");
await fn();
self.postMessage("next");
break;
default:
throw new Error("Unexpected message");
}
} catch (e) {
self.postMessage("error: " + e.stack);
}
};
};
5 changes: 5 additions & 0 deletions test/hotCases/worker/move-between-runtime/workerA.js
@@ -0,0 +1,5 @@
import worker from "./worker";
import "./moduleA";
worker(() => import(/* webpackChunkName: "shared" */ "./moduleAs"));
import.meta.webpackHot.accept("./moduleA");
import.meta.webpackHot.accept("./moduleAs");
5 changes: 5 additions & 0 deletions test/hotCases/worker/move-between-runtime/workerB.js
@@ -0,0 +1,5 @@
import worker from "./worker";
import "./moduleB";
worker(() => import(/* webpackChunkName: "shared" */ "./moduleBs"));
import.meta.webpackHot.accept("./moduleB");
import.meta.webpackHot.accept("./moduleBs");

0 comments on commit 1131afe

Please sign in to comment.