Skip to content

Commit 15f98b6

Browse files
authored
optimize __createBinding (#168)
Reflect microsoft/TypeScript#46997: When the binding is itself one that was created by `__createBinding`, re-use its descriptor, which avoids piling multiple levels of getters in the case of multiple levels of exports. In addition, reuse a descriptor if the bindings is marked as non-writable and non-configurable, which makes a getter not necessary. (This can be done manually if needed, even though tsc doesn't do it now.) Could be considered as a fix for #165 -- first, this PR prevents piling up multiple layers of getters. Second, it allows a hack of adding if (typeof exports === "object") exports = Object.freeze(exports); to avoid getters altogether. (And in the future, tsc could mark `const` exports as non-writable and non-configurable which would make it possible to avoid this hack.)
1 parent 06853a8 commit 15f98b6

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

tslib.es6.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ export function __generator(thisArg, body) {
107107

108108
export var __createBinding = Object.create ? (function(o, m, k, k2) {
109109
if (k2 === undefined) k2 = k;
110-
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
110+
var desc = Object.getOwnPropertyDescriptor(m, k);
111+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
112+
desc = { enumerable: true, get: function() { return m[k]; } };
113+
}
114+
Object.defineProperty(o, k2, desc);
111115
}) : (function(o, m, k, k2) {
112116
if (k2 === undefined) k2 = k;
113117
o[k2] = m[k];

tslib.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,11 @@ var __createBinding;
153153

154154
__createBinding = Object.create ? (function(o, m, k, k2) {
155155
if (k2 === undefined) k2 = k;
156-
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
156+
var desc = Object.getOwnPropertyDescriptor(m, k);
157+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
158+
desc = { enumerable: true, get: function() { return m[k]; } };
159+
}
160+
Object.defineProperty(o, k2, desc);
157161
}) : (function(o, m, k, k2) {
158162
if (k2 === undefined) k2 = k;
159163
o[k2] = m[k];

0 commit comments

Comments
 (0)