Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/247-run-with-dependencies-result.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"webpack": patch
---

Report no result from `runWithDependencies` instead of nested empty arrays.
13 changes: 6 additions & 7 deletions lib/MultiCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,13 +497,14 @@ module.exports = class MultiCompiler {
return readyCompilers;
};
/**
* Processes the provided stat.
* @param {Callback<Stats[]>} callback callback
* Runs every compiler whose dependencies are fulfilled, then the ones
* they unblock, until none remain.
* @param {(err?: Error | null) => void} callback called when all are done, or with the first error
* @returns {void}
*/
const runCompilers = (callback) => {
if (remainingCompilers.length === 0) return callback(null);
asyncLib.map(
asyncLib.each(
getReadyCompilers(),
(compiler, callback) => {
fn(compiler, (err) => {
Expand All @@ -512,12 +513,10 @@ module.exports = class MultiCompiler {
runCompilers(callback);
});
},
(/** @type {Error | null | undefined} */ err, results) => {
callback(/** @type {Error | null} */ (err), results);
}
callback
);
};
runCompilers(callback);
runCompilers((err) => callback(err || null));
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

/**
Expand Down
74 changes: 74 additions & 0 deletions test/unitCases/MultiCompiler.unittest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1059,3 +1059,77 @@ describe("MultiCompiler deprecated 'done' hook call", () => {
tracker();
});
});

describe("MultiCompiler runWithDependencies", () => {
/**
* @param {import("../../").MultiCompiler} compiler compiler
* @returns {import("../../").Compiler[]} child compilers
*/
const childrenOf = (compiler) => compiler.compilers;

it("should run a compiler after the ones it depends on and report no result", (done) => {
const compiler = /** @type {import("../../").MultiCompiler} */ (
webpack([
{ name: "a", context: path.join(testDirectory, "fixtures"), entry: "./a.js" },
{
name: "b",
dependencies: ["a"],
context: path.join(testDirectory, "fixtures"),
entry: "./b.js"
}
])
);
/** @type {string[]} */
const order = [];
compiler.runWithDependencies(
childrenOf(compiler),
(child, callback) => {
order.push(/** @type {string} */ (child.name));
setImmediate(() => callback(null));
},
(err, results) => {
try {
expect(err).toBeNull();
expect(results).toBeUndefined();
expect(order).toEqual(["a", "b"]);
done();
} catch (assertionError) {
done(assertionError);
}
}
);
});

it("should report the error of a failing compiler", (done) => {
const compiler = /** @type {import("../../").MultiCompiler} */ (
webpack([
{ name: "a", context: path.join(testDirectory, "fixtures"), entry: "./a.js" },
{
name: "b",
dependencies: ["a"],
context: path.join(testDirectory, "fixtures"),
entry: "./b.js"
}
])
);
const failure = new Error("a failed");
/** @type {string[]} */
const order = [];
compiler.runWithDependencies(
childrenOf(compiler),
(child, callback) => {
order.push(/** @type {string} */ (child.name));
callback(failure);
},
(err) => {
try {
expect(err).toBe(failure);
expect(order).toEqual(["a"]);
done();
} catch (assertionError) {
done(assertionError);
}
}
);
});
});
Loading