Skip to content

Commit c959c39

Browse files
fix: collect a require reached through a spread (#22230)
The scan skips a `require` preceded by a dot, which rules out `foo.require(…)` but also swallowed the last dot of `...require("…")`, so `module.exports = { ...require("./base") }` in a config named no build dependency and editing that base left the pack valid. Only Bun reaches the scan — Node and Deno recover the same dependencies from `module.children` — so the stale cache was Bun's alone.
1 parent 0ae3361 commit c959c39

3 files changed

Lines changed: 19 additions & 1 deletion

File tree

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+
Record a build dependency required through a spread on Bun.

‎lib/javascript/syntax-parser.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17729,7 +17729,9 @@ const collectCjsRequireSpecifiers = (source) => {
1772917729
if (isIdentifierChar(source.charCodeAt(i + 7))) continue;
1773017730
if (i > 0) {
1773117731
const before = source.charCodeAt(i - 1);
17732-
if (isIdentifierChar(before) || before === 46) continue;
17732+
if (isIdentifierChar(before)) continue;
17733+
// a dot is a member access, except as the last of a `...` spread
17734+
if (before === 46 && !source.startsWith("...", i - 3)) continue;
1773317735
}
1773417736
let j = skipTrivia(source, i + 7);
1773517737
if (source.charCodeAt(j) !== 40) continue;

‎test/unitCases/collectCjsRequireSpecifiers.unittest.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ describe("collectCjsRequireSpecifiers", () => {
7070
`const s = \`x${OPEN}require('./a')}y\`; require('./b');`
7171
],
7272
["member access is not a call", 'foo.require("./nope"); require("./a");'],
73+
[
74+
"optional member access is not a call",
75+
'foo?.require("./nope"); require("./a");'
76+
],
77+
["spread into an object", 'module.exports = { ...require("./a") };'],
78+
["spread into an array", 'module.exports = [...require("./a")];'],
79+
["spread into a call", 'f(...require("./a"));'],
80+
[
81+
"spread beside a member access",
82+
'({ ...require("./a"), x: foo.require("./nope") });'
83+
],
7384
["longer identifier", 'requireX("./nope"); require("./a");'],
7485
["identifier ending in require", 'preRequire("./nope"); require("./a");'],
7586
["nested call", 'f(require("./a"), require("./b"));'],

0 commit comments

Comments
 (0)