test: worker webpack options (#19898)

This commit is contained in:
Alexander Akait 2025-09-10 16:49:49 +03:00 committed by GitHub
parent 06f4cb2a08
commit 6b70c4ac52
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 192 additions and 0 deletions

View File

@ -0,0 +1,18 @@
import { Worker } from "worker_threads";
import * as fs from "fs";
import * as path from "path";
it("should respect entry options", async () => {
const worker = new Worker(
/* webpackEntryOptions: { filename: "my-[name].js", name: "custom-worker", asyncChunks: false } */
new URL("./worker.js", import.meta.url)
);
worker.postMessage("ok");
const result = await new Promise(resolve => {
worker.on("message", data => {
resolve(data);
});
});
expect(result).toBe("data: OK, thanks");
await worker.terminate();
});

View File

@ -0,0 +1,3 @@
export function upper(str) {
return str.toUpperCase();
}

View File

@ -0,0 +1,19 @@
"use strict";
const fs = require("fs");
module.exports = {
afterExecute(options) {
const outputPath = options.output.path;
const files = fs.readdirSync(outputPath);
const expected = ["bundle0.js", "my-custom-worker.js"];
const intersection = expected.filter((value) => files.includes(value));
if (intersection.length !== 2) {
throw new Error(
`Not all files were generated by webpack, expected ${JSON.stringify(expected)}, got ${JSON.stringify(files)}`
);
}
}
};

View File

@ -0,0 +1,5 @@
"use strict";
const supportsWorker = require("../../../helpers/supportsWorker");
module.exports = () => supportsWorker();

View File

@ -0,0 +1,11 @@
"use strict";
/** @type {import("../../../../").Configuration} */
module.exports = {
output: {
chunkFilename: "chunk-[name].js"
},
optimization: {
chunkIds: "named"
}
};

View File

@ -0,0 +1,6 @@
import { parentPort } from "worker_threads";
parentPort.on("message", async data => {
const { upper } = await import("./module");
parentPort.postMessage(`data: ${upper(data)}, thanks`);
});

View File

@ -0,0 +1,48 @@
import { Worker } from "worker_threads";
it("should allow to ignore worker construction", async () => {
const worker = new Worker(
/* webpackIgnore: true */
new URL("./worker.js", import.meta.url),
{ type: "module" }
);
worker.postMessage("ok");
const result = await new Promise(resolve => {
worker.on("message", data => {
resolve(data);
});
});
expect(result).toBe("data: OK, thanks");
await worker.terminate();
});
it("should allow to ignore URL construction", async () => {
const worker = new Worker(
new URL(/* webpackIgnore: true */ "./worker.mjs", import.meta.url),
{ type: "module" }
);
worker.postMessage("ok");
const result = await new Promise(resolve => {
worker.on("message", data => {
resolve(data);
});
});
expect(result).toBe("data: OK, thanks");
await worker.terminate();
});
it("should allow to ignore worker and URL constructions", async () => {
const worker = new Worker(
/* webpackIgnore: true */
new URL(/* webpackIgnore: true */ "./worker.mjs", import.meta.url),
{ type: "module" }
);
worker.postMessage("ok");
const result = await new Promise(resolve => {
worker.on("message", data => {
resolve(data);
});
});
expect(result).toBe("data: OK, thanks");
await worker.terminate();
});

View File

@ -0,0 +1,29 @@
"use strict";
const fs = require("fs");
const path = require("path");
module.exports = {
afterExecute(options) {
const outputPath = options.output.path;
const files = fs.readdirSync(outputPath);
const expected = ["bundle0.js", "worker.mjs", "worker-worker.mjs"];
const intersection = expected.filter((value) => files.includes(value));
if (intersection.length !== 3) {
throw new Error(
`Not all files were generated by webpack, expected ${JSON.stringify(expected)}, got ${JSON.stringify(files)}`
);
}
for (const file of files) {
const filename = path.resolve(outputPath, file);
const source = fs.readFileSync(filename, "utf8");
if (file === "bundle0.js" && source.includes("worker import")) {
throw new Error(`Found an worker import in ${filename}.`);
}
}
}
};

View File

@ -0,0 +1,5 @@
"use strict";
const supportsWorker = require("../../../helpers/supportsWorker");
module.exports = () => supportsWorker();

View File

@ -0,0 +1,39 @@
"use strict";
const fs = require("fs");
const path = require("path");
/** @type {import("../../../../").Configuration} */
module.exports = {
output: {
assetModuleFilename: "worker-[name].mjs",
environment: {
nodePrefixForCoreModules: false
}
},
plugins: [
{
apply(compiler) {
compiler.hooks.compilation.tap("Test", (compilation) => {
compilation.hooks.processAssets.tap(
{
name: "copy-webpack-plugin",
stage:
compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL
},
() => {
const data = fs.readFileSync(
path.resolve(__dirname, "./worker.js")
);
compilation.emitAsset(
"worker.mjs",
new compiler.webpack.sources.RawSource(data)
);
}
);
});
}
}
]
};

View File

@ -0,0 +1,9 @@
import { parentPort } from "worker_threads";
function upper(str) {
return str.toUpperCase();
}
parentPort.on("message", async data => {
parentPort.postMessage(`data: ${upper(data)}, thanks`);
});