mirror of https://github.com/webpack/webpack.git
test: worker webpack options (#19898)
This commit is contained in:
parent
06f4cb2a08
commit
6b70c4ac52
|
@ -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();
|
||||
});
|
|
@ -0,0 +1,3 @@
|
|||
export function upper(str) {
|
||||
return str.toUpperCase();
|
||||
}
|
|
@ -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)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
|
@ -0,0 +1,5 @@
|
|||
"use strict";
|
||||
|
||||
const supportsWorker = require("../../../helpers/supportsWorker");
|
||||
|
||||
module.exports = () => supportsWorker();
|
|
@ -0,0 +1,11 @@
|
|||
"use strict";
|
||||
|
||||
/** @type {import("../../../../").Configuration} */
|
||||
module.exports = {
|
||||
output: {
|
||||
chunkFilename: "chunk-[name].js"
|
||||
},
|
||||
optimization: {
|
||||
chunkIds: "named"
|
||||
}
|
||||
};
|
|
@ -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`);
|
||||
});
|
|
@ -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();
|
||||
});
|
|
@ -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}.`);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
|
@ -0,0 +1,5 @@
|
|||
"use strict";
|
||||
|
||||
const supportsWorker = require("../../../helpers/supportsWorker");
|
||||
|
||||
module.exports = () => supportsWorker();
|
|
@ -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)
|
||||
);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
|
@ -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`);
|
||||
});
|
Loading…
Reference in New Issue