Skip to content

Commit 76bd619

Browse files
fix(concatenate-modules): parenthesize a wrapped module's accessor call (#21876)
* fix(concatenate-modules): parenthesize a wrapped module's accessor call A wrapped CommonJS module is reached through `X_namespaceFn()`, so a reference standing in for an identifier is a call expression. Without parentheses `new X_namespaceFn()` applies `new` to the accessor instead of to the exports it returns, which throws at runtime. Closes #21873 * test(concatenate-modules): snapshot the emitted bundles instead of pinning substrings The generated code is what these cases check, so review it as a whole rather than one matched fragment at a time. Runtime assertions stay. * test(concatenate-modules): shorten the snapshot plugin comments
1 parent fa214ac commit 76bd619

23 files changed

Lines changed: 2093 additions & 6 deletions

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+
Fix `new` on a default import of a wrapped CommonJS module.

‎lib/optimize/ConcatenatedModule.js‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ const findIdentifiers = (source) => {
181181
* @property {string=} comment
182182
* @property {ExportName} ids
183183
* @property {ExportName} exportName
184+
* @property {boolean=} accessorCall the raw name is the wrapper accessor call itself, so it binds looser than the identifier it replaces
184185
*/
185186

186187
/**
@@ -494,7 +495,8 @@ const getFinalBinding = (
494495
/** @type {NonNullable<ConcatenatedModuleInfo["namespaceObjectName"]>} */
495496
(info.namespaceObjectName),
496497
ids: /** @type {ExportName} */ (usedName),
497-
exportName
498+
exportName,
499+
accessorCall: info.wrapped
498500
};
499501
}
500502

@@ -509,7 +511,8 @@ const getFinalBinding = (
509511
? /** @type {string} */ (info.deferredNamespaceObjectName)
510512
: getInteropExpr(info, info.interopNamespaceObject2Name),
511513
ids: exportName,
512-
exportName
514+
exportName,
515+
accessorCall: !deferred && info.wrapped
513516
};
514517
case "default-with-named":
515518
if (deferred) info.deferredNamespaceObjectUsed = true;
@@ -520,7 +523,8 @@ const getFinalBinding = (
520523
? /** @type {string} */ (info.deferredNamespaceObjectName)
521524
: getInteropExpr(info, info.interopNamespaceObjectName),
522525
ids: exportName,
523-
exportName
526+
exportName,
527+
accessorCall: !deferred && info.wrapped
524528
};
525529
case "namespace":
526530
case "dynamic":
@@ -611,7 +615,8 @@ const getFinalBinding = (
611615
info,
612616
rawName: getExternalModuleExpr(info),
613617
ids: exportName,
614-
exportName
618+
exportName,
619+
accessorCall: info.wrapped
615620
};
616621
}
617622
info.interopDefaultAccessUsed = true;
@@ -657,7 +662,8 @@ const getFinalBinding = (
657662
/** @type {NonNullable<ConcatenatedModuleInfo["namespaceObjectName"]>} */
658663
(info.namespaceObjectName),
659664
ids: exportName,
660-
exportName
665+
exportName,
666+
accessorCall: info.wrapped
661667
};
662668
case "external":
663669
if (deferred) {
@@ -673,7 +679,8 @@ const getFinalBinding = (
673679
info,
674680
rawName: getExternalModuleExpr(info),
675681
ids: exportName,
676-
exportName
682+
exportName,
683+
accessorCall: info.wrapped
677684
};
678685
}
679686
}
@@ -938,9 +945,13 @@ const getFinalName = (
938945
let reference;
939946
/** @type {boolean} */
940947
let isPropertyAccess;
948+
// the raw name is the wrapper accessor call itself, so the reference binds
949+
// looser than the identifier it stands in for
950+
let accessorCall = false;
941951
if ("rawName" in binding) {
942952
reference = `${binding.rawName}${comment || ""}${propertyAccess(ids)}`;
943953
isPropertyAccess = ids.length > 0;
954+
accessorCall = binding.accessorCall === true;
944955
} else {
945956
const { info, name: exportId } = binding;
946957
const name = info.internalNames.get(exportId);
@@ -970,6 +981,10 @@ const getFinalName = (
970981
} else if (binding.info.wrapped) {
971982
return asiSafe === false ? `;(${reference})` : `(${reference})`;
972983
}
984+
} else if (accessorCall && !asCall) {
985+
// without parentheses `new X()` would construct the accessor rather
986+
// than what it returns
987+
return asiSafe === false ? `;(${reference})` : `(${reference})`;
973988
}
974989

975990
return reference;

0 commit comments

Comments
 (0)