How to use the Worker filename generated by multi-compiler mode in the second stage? #17244
|
Similar to #14349 but I want to be able to use the output of the first stage of the multi-compiler mode in the second stage. Let's say I have a simplified webpack config: [
{
name: "worker",
target: "webworker",
entry: "./worker.js",
output: {
path: path.join(__dirname, "dist"),
filename: "worker-[contenthash].js"
},
},
{
name: "app",
target: "browser",
entry: "./app",
output: {
path: path.join(__dirname, "dist"),
filename: "index.js"
},
dependencies: ["worker"],
},
]Then when we build, the first output, "worker" will be built and generate a file called And inside of index.js I have: new Worker("<INSERT WORKER OUTPUT FILE HERE>")How do I make it such that index.js will produce To be honest, ignore the "dist" part if not relevant. I'm only really interested in passing the output filename of one into another. It's not even really about workers. Is this possible? WorkaroundsCurrently I have some ideas for workarounds, namely having a plugin that searches the |
Replies: 1 comment 1 reply
|
Here example: const path = require("path");
const webpack = require("webpack");
let workerEntry;
module.exports = [
{
devtool: false,
mode: "development",
name: "worker",
target: "webworker",
entry: "./src/worker.js",
output: {
path: path.join(__dirname, "dist-temp"),
filename: "worker-[contenthash].js"
},
plugins: [
{
apply: compiler => {
compiler.hooks.done.tap('MyWebpackPlugin', function(stats) {
const entrypoints = stats.toJson().entrypoints;
Object.keys(entrypoints).forEach((entryName) => {
const entryFiles = entrypoints[entryName].assets;
const entryFilename = entryFiles[0].name;
if (!workerEntry && entryName === "main") {
workerEntry = path.join(stats.compilation.outputOptions.path, entryFilename);
}
});
});
}
}
]
},
{
devtool: false,
mode: "development",
name: "app",
target: "web",
entry: "./src/app.js",
output: {
path: path.join(__dirname, "dist"),
filename: "index.js"
},
dependencies: ["worker"],
plugins: [
{
apply: compiler => {
compiler.hooks.beforeRun.tap('MyWebpackPlugin', function(stats) {
new webpack.DefinePlugin({
WORKER_URL: JSON.stringify(workerEntry)
}).apply(compiler);
});
}
}
]
}
];and code: |
Here example: