Skip to content

Commit edaf32a

Browse files
committed
fix #2097: no babel mode special case of "default"
1 parent f4ff26d commit edaf32a

File tree

3 files changed

+50
-43
lines changed

3 files changed

+50
-43
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
* Avoid generating an enumerable `default` import for CommonJS files in Babel mode ([#2097](https://github.com/evanw/esbuild/issues/2097))
6+
7+
Importing a CommonJS module into an ES module can be done in two different ways. In node mode the `default` import is always set to `module.exports`, while in Babel mode the `default` import passes through to `module.exports.default` instead. Node mode is triggered when the importing file ends in `.mjs`, has `type: "module"` in its `package.json` file, or the imported module does not have a `__esModule` marker.
8+
9+
Previously esbuild always created the forwarding `default` import in Babel mode, even if `module.exports` had no property called `default`. This was problematic because the getter named `default` still showed up as a property on the imported namespace object, and could potentially interfere with code that iterated over the properties of the imported namespace object. With this release the getter named `default` will now only be added in Babel mode if the `default` property exists at the time of the import.
10+
311
## 0.14.26
412

513
* Fix a tree shaking regression regarding `var` declarations ([#2080](https://github.com/evanw/esbuild/issues/2080), [#2085](https://github.com/evanw/esbuild/pull/2085), [#2098](https://github.com/evanw/esbuild/issues/2098), [#2099](https://github.com/evanw/esbuild/issues/2099))

internal/bundler/snapshots/snapshots_splitting.txt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -205,19 +205,19 @@ TestSplittingDynamicAndNotDynamicCommonJSIntoES6
205205
import {
206206
__toESM,
207207
require_foo
208-
} from "./chunk-KUHUE66N.js";
208+
} from "./chunk-6FVSQLGP.js";
209209

210210
// entry.js
211211
var import_foo = __toESM(require_foo());
212-
import("./foo-U6JCMRHQ.js").then(({ default: { bar: b } }) => console.log(import_foo.bar, b));
212+
import("./foo-NCFYBVPB.js").then(({ default: { bar: b } }) => console.log(import_foo.bar, b));
213213

214-
---------- /out/foo-U6JCMRHQ.js ----------
214+
---------- /out/foo-NCFYBVPB.js ----------
215215
import {
216216
require_foo
217-
} from "./chunk-KUHUE66N.js";
217+
} from "./chunk-6FVSQLGP.js";
218218
export default require_foo();
219219

220-
---------- /out/chunk-KUHUE66N.js ----------
220+
---------- /out/chunk-6FVSQLGP.js ----------
221221
// foo.js
222222
var require_foo = __commonJS({
223223
"foo.js"(exports) {
@@ -260,9 +260,9 @@ export {
260260
TestSplittingDynamicCommonJSIntoES6
261261
---------- /out/entry.js ----------
262262
// entry.js
263-
import("./foo-PPQD77K4.js").then(({ default: { bar } }) => console.log(bar));
263+
import("./foo-MGFNEEYE.js").then(({ default: { bar } }) => console.log(bar));
264264

265-
---------- /out/foo-PPQD77K4.js ----------
265+
---------- /out/foo-MGFNEEYE.js ----------
266266
// foo.js
267267
var require_foo = __commonJS({
268268
"foo.js"(exports) {
@@ -317,7 +317,7 @@ TestSplittingHybridESMAndCJSIssue617
317317
import {
318318
foo,
319319
init_a
320-
} from "./chunk-QRBKE4XE.js";
320+
} from "./chunk-WNGDXRWX.js";
321321
init_a();
322322
export {
323323
foo
@@ -328,15 +328,15 @@ import {
328328
__toCommonJS,
329329
a_exports,
330330
init_a
331-
} from "./chunk-QRBKE4XE.js";
331+
} from "./chunk-WNGDXRWX.js";
332332

333333
// b.js
334334
var bar = (init_a(), __toCommonJS(a_exports));
335335
export {
336336
bar
337337
};
338338

339-
---------- /out/chunk-QRBKE4XE.js ----------
339+
---------- /out/chunk-WNGDXRWX.js ----------
340340
// a.js
341341
var a_exports = {};
342342
__export(a_exports, {
@@ -487,7 +487,7 @@ TestSplittingSharedCommonJSIntoES6
487487
---------- /out/a.js ----------
488488
import {
489489
require_shared
490-
} from "./chunk-KTJ3L72M.js";
490+
} from "./chunk-GI5SL2RW.js";
491491

492492
// a.js
493493
var { foo } = require_shared();
@@ -496,13 +496,13 @@ console.log(foo);
496496
---------- /out/b.js ----------
497497
import {
498498
require_shared
499-
} from "./chunk-KTJ3L72M.js";
499+
} from "./chunk-GI5SL2RW.js";
500500

501501
// b.js
502502
var { foo } = require_shared();
503503
console.log(foo);
504504

505-
---------- /out/chunk-KTJ3L72M.js ----------
505+
---------- /out/chunk-GI5SL2RW.js ----------
506506
// shared.js
507507
var require_shared = __commonJS({
508508
"shared.js"(exports) {

internal/runtime/runtime.go

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,6 @@ func code(isES6 bool) string {
112112
}
113113
export var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b))
114114
115-
// Tells importing modules that this can be considered an ES module
116-
var __markAsModule = target => __defProp(target, '__esModule', { value: true })
117-
118115
// Update the "name" property on the function or class for "--keep-names"
119116
export var __name = (target, value) => __defProp(target, 'name', { value, configurable: true })
120117
@@ -185,53 +182,55 @@ func code(isES6 bool) string {
185182
for (var name in all)
186183
__defProp(target, name, { get: all[name], enumerable: true })
187184
}
188-
export var __reExport = (target, module, copyDefault, desc) => {
189-
if (module && typeof module === 'object' || typeof module === 'function')
185+
186+
var __copyProps = (to, from, except, desc) => {
187+
if (from && typeof from === 'object' || typeof from === 'function')
190188
`
191189

192190
// Avoid "let" when not using ES6
193191
if isES6 {
194192
text += `
195-
for (let key of __getOwnPropNames(module))
196-
if (!__hasOwnProp.call(target, key) && (copyDefault || key !== 'default'))
197-
__defProp(target, key, { get: () => module[key], enumerable: !(desc = __getOwnPropDesc(module, key)) || desc.enumerable })
193+
for (let key of __getOwnPropNames(from))
194+
if (!__hasOwnProp.call(to, key) && key !== except)
195+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable })
198196
`
199197
} else {
200198
text += `
201-
for (var keys = __getOwnPropNames(module), i = 0, n = keys.length, key; i < n; i++) {
199+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
202200
key = keys[i]
203-
if (!__hasOwnProp.call(target, key) && (copyDefault || key !== 'default'))
204-
__defProp(target, key, { get: (k => module[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(module, key)) || desc.enumerable })
201+
if (!__hasOwnProp.call(to, key) && key !== except)
202+
__defProp(to, key, { get: (k => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable })
205203
}
206204
`
207205
}
208206

209207
text += `
210-
return target
208+
return to
211209
}
212210
213-
// Converts the module from CommonJS to ESM
214-
export var __toESM = (module, isNodeMode) => {
215-
return __reExport(__markAsModule(
216-
__defProp(
217-
module != null ? __create(__getProtoOf(module)) : {},
218-
'default',
219-
220-
// If the importer is not in node compatibility mode and this is an ESM
221-
// file that has been converted to a CommonJS file using a Babel-
222-
// compatible transform (i.e. "__esModule" has been set), then forward
223-
// "default" to the export named "default". Otherwise set "default" to
224-
// "module.exports" for node compatibility.
225-
!isNodeMode && module && module.__esModule
226-
? { get: () => module.default, enumerable: true }
227-
: { value: module, enumerable: true })
228-
), module)
229-
}
211+
export var __reExport = (target, mod) => __copyProps(target, mod, 'default')
212+
213+
// Converts the module from CommonJS to ESM. When in node mode (i.e. in an
214+
// ".mjs" file, package.json has "type: module", or the "__esModule" export
215+
// in the CommonJS file is falsy or missing), the "default" property is
216+
// overridden to point to the original CommonJS exports object instead.
217+
export var __toESM = (mod, isNodeMode, target) => (
218+
target = mod != null ? __create(__getProtoOf(mod)) : {},
219+
__copyProps(
220+
// If the importer is in node compatibility mode or this is not an ESM
221+
// file that has been converted to a CommonJS file using a Babel-
222+
// compatible transform (i.e. "__esModule" has not been set), then set
223+
// "default" to the CommonJS "module.exports" for node compatibility.
224+
isNodeMode || !mod || !mod.__esModule
225+
? __defProp(target, 'default', { value: mod, enumerable: true })
226+
: target,
227+
mod)
228+
)
230229
231230
// Converts the module from ESM to CommonJS. This clones the input module
232231
// object with the addition of a non-enumerable "__esModule" property set
233232
// to "true", which overwrites any existing export named "__esModule".
234-
export var __toCommonJS = module => __reExport(__markAsModule({}), module, /* copyDefault */ 1)
233+
export var __toCommonJS = mod => __copyProps(__defProp({}, '__esModule', { value: true }), mod)
235234
236235
// For TypeScript decorators
237236
// - kind === undefined: class

0 commit comments

Comments
 (0)