Skip to content

Commit 9f6628e

Browse files
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
1 parent 394a05f commit 9f6628e

17 files changed

Lines changed: 274 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;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// required by a CommonJS module, so wrapping propagates from here to the
2+
// external: the default import becomes the external's accessor call
3+
import EventEmitter from "events";
4+
5+
export function make() {
6+
return new EventEmitter();
7+
}
8+
9+
export const raw = EventEmitter;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { readFileSync } from "fs";
2+
import { make, raw } from "./mid.cjs";
3+
4+
it("should apply `new` to a wrapped external's accessor result", () => {
5+
const emitter = make();
6+
7+
expect(emitter).toBeInstanceOf(raw);
8+
expect(typeof emitter.on).toBe("function");
9+
});
10+
11+
it("should really reach the external through a wrapper accessor", () => {
12+
const source = readFileSync(__filename, "utf-8");
13+
14+
expect(source).toMatch(
15+
/external_events_namespaceFn = \/\*#__PURE__\*\/__webpack_require__\.cw\(/
16+
);
17+
expect(source).toMatch(/return new \(external_events_namespaceFn\(\)\)\(\)/);
18+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
3+
const { make, raw } = require("./esm-user.mjs");
4+
5+
exports.make = make;
6+
exports.raw = raw;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
mode: "production",
6+
target: "node",
7+
devtool: false,
8+
optimization: {
9+
concatenateModules: { commonjs: true },
10+
minimize: false,
11+
usedExports: true,
12+
moduleIds: "named",
13+
chunkIds: "named"
14+
}
15+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { readFileSync } from "fs";
2+
import { construct as constructLoose } from "./loose.js";
3+
import { construct as constructStrict, ctor } from "./strict.mjs";
4+
5+
it("should construct through a strict ESM default import", () => {
6+
const thing = constructStrict({ a: 1 });
7+
8+
expect(thing.options).toEqual({ a: 1 });
9+
expect(thing).toBeInstanceOf(ctor);
10+
});
11+
12+
it("should construct through the javascript/auto interop", () => {
13+
const thing = constructLoose({ b: 2 });
14+
15+
expect(thing.options).toEqual({ b: 2 });
16+
expect(thing).toBeInstanceOf(ctor);
17+
});
18+
19+
it("should emit both constructions with the callee parenthesized", () => {
20+
const source = readFileSync(__filename, "utf-8");
21+
22+
expect(source).toMatch(/new \(thing_namespaceFn\(\)\)\(options\)/);
23+
expect(source).toMatch(/new \(thing_default\(\)\(\)\)\(options\)/);
24+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// javascript/auto: the default import goes through the interop helper instead
2+
import Thing from "./thing.cjs";
3+
4+
export function construct(options) {
5+
return new Thing(options);
6+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// strict ESM: the default import binds to the wrapper accessor directly
2+
import Thing from "./thing.cjs";
3+
4+
export function construct(options) {
5+
return new Thing(options);
6+
}
7+
8+
export const ctor = Thing;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
3+
class Thing {
4+
constructor(options) {
5+
this.options = options;
6+
}
7+
}
8+
9+
module.exports = Thing;

0 commit comments

Comments
 (0)