mirror of https://github.com/webpack/webpack.git
add test case for moving modules and chunks between runtimes
This commit is contained in:
parent
62e86f0cd4
commit
cf0c816648
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
export default "chunk";
|
||||
|
|
@ -0,0 +1 @@
|
|||
export default "chunkS";
|
||||
|
|
@ -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();
|
||||
});
|
||||
|
|
@ -0,0 +1 @@
|
|||
export default "module";
|
||||
|
|
@ -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;
|
||||
|
|
@ -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;
|
||||
|
|
@ -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;
|
||||
|
|
@ -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;
|
||||
|
|
@ -0,0 +1 @@
|
|||
export default "moduleS";
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
var supportsWorker = require("../../../helpers/supportsWorker");
|
||||
|
||||
module.exports = function (config) {
|
||||
return supportsWorker();
|
||||
};
|
||||
|
|
@ -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);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
@ -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");
|
||||
|
|
@ -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");
|
||||
Loading…
Reference in New Issue