Skip to content

Commit 31a69f1

Browse files
refactor: replace neo-async with a built-in async helper (#21959)
* refactor: replace neo-async with a built-in async helper `lib/util/async.js` provides the four helpers webpack used from neo-async — `each`, `eachLimit`, `map` and `parallel` — over arrays and iterables. `eachLimit` drains a synchronous iterator from its dispatch loop instead of recursing through `process.nextTick`, and `each` shares one completion callback across items rather than allocating a wrapper per item. * perf: pre-size the map result array Growing the array, or `Array.from({ length })`, costs measurably more on the small collections `map` and `parallel` are called with. * fix: detect a duplicate item callback in the async helpers `each` and `eachLimit` handed the shared completion callback to every item, so an iterator reporting twice decremented the counter twice and settled the run while a later item was still in flight — silently, where `neo-async` threw. Wrap each item's callback so it can only report once. `map` already could not be corrupted this way; it now throws too, so all four helpers fail the same way. * fix: type the once-guard slot as optional `tsc` rejected clearing it, since the slot took its type from the callback parameter and so could not hold `undefined`.
1 parent c17742f commit 31a69f1

21 files changed

Lines changed: 1065 additions & 353 deletions

‎.changeset/080-drop-neo-async.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Drop the `neo-async` dependency in favor of a faster built-in async helper.

‎examples/build-common.js‎

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
const cp = require("child_process");
88
const fs = require("fs");
99
const path = require("path");
10-
const async = require("neo-async");
1110
const tc = require("./template-common");
1211

1312
const extraArgs = "";
@@ -150,29 +149,26 @@ const doCompileAndReplace = (args, prefix, callback) => {
150149
connectIO(subprocess);
151150
};
152151

153-
async.series(
154-
[
155-
(callback) =>
156-
doCompileAndReplace(
157-
"--mode production --env production",
158-
"production",
159-
callback
160-
),
161-
(callback) =>
162-
doCompileAndReplace(
163-
"--mode development --env development --devtool none",
164-
"development",
165-
callback
166-
),
167-
(callback) =>
168-
doCompileAndReplace(
169-
"--mode none --env none --output-pathinfo verbose",
170-
"",
171-
callback
172-
)
173-
],
174-
() => {
152+
/** @type {[string, string][]} */
153+
const compilations = [
154+
["--mode production --env production", "production"],
155+
["--mode development --env development --devtool none", "development"],
156+
["--mode none --env none --output-pathinfo verbose", ""]
157+
];
158+
159+
/**
160+
* Runs the compilations one after another, as each one rewrites the README.
161+
* @param {number} index compilation to run
162+
* @returns {void}
163+
*/
164+
const runCompilation = (index) => {
165+
if (index === compilations.length) {
175166
readme = tc.replaceBase(readme);
176167
fs.writeFile("README.md", readme, "utf8", () => {});
168+
return;
177169
}
178-
);
170+
const [args, prefix] = compilations[index];
171+
doCompileAndReplace(args, prefix, () => runCompilation(index + 1));
172+
};
173+
174+
runCompilation(0);

‎lib/AutomaticPrefetchPlugin.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
"use strict";
77

8-
const asyncLib = require("neo-async");
98
const NormalModule = require("./NormalModule");
109
const PrefetchDependency = require("./dependencies/PrefetchDependency");
10+
const asyncLib = require("./util/async");
1111

1212
/** @import Compiler from "./Compiler" */
1313

‎lib/CacheFacade.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
"use strict";
77

88
const { forEachBail } = require("enhanced-resolve");
9-
const asyncLib = require("neo-async");
109
const getLazyHashedEtag = require("./cache/getLazyHashedEtag");
1110
const mergeEtags = require("./cache/mergeEtags");
11+
const asyncLib = require("./util/async");
1212

1313
/** @import Cache, { Etag } from "./Cache" */
1414
/** @import { HashableObject } from "./cache/getLazyHashedEtag" */

‎lib/CleanPlugin.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"use strict";
77

88
const path = require("path");
9-
const asyncLib = require("neo-async");
109
const { SyncBailHook } = require("tapable");
10+
const asyncLib = require("./util/async");
1111
const createHooksRegistry = require("./util/createHooksRegistry");
1212
const { join } = require("./util/fs");
1313
const { ABSOLUTE_PATH_REGEXP } = require("./util/identifier");
@@ -98,7 +98,7 @@ const getDiffToFs = (fs, outputPath, currentAssets, callback) => {
9898
const directories = getDirectories(currentAssets);
9999
/** @type {Diff} */
100100
const diff = new Set();
101-
asyncLib.forEachLimit(
101+
asyncLib.eachLimit(
102102
directories,
103103
10,
104104
(directory, callback) => {

0 commit comments

Comments
 (0)