esbuild 0.14.27
-
Avoid generating an enumerable
defaultimport for CommonJS files in Babel mode (#2097)Importing a CommonJS module into an ES module can be done in two different ways. In node mode the
defaultimport is always set tomodule.exports, while in Babel mode thedefaultimport passes through tomodule.exports.defaultinstead. Node mode is triggered when the importing file ends in.mjs, hastype: "module"in itspackage.jsonfile, or the imported module does not have a__esModulemarker.Previously esbuild always created the forwarding
defaultimport in Babel mode, even ifmodule.exportshad no property calleddefault. This was problematic because the getter nameddefaultstill 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 nameddefaultwill now only be added in Babel mode if thedefaultproperty exists at the time of the import. -
Fix a circular import edge case regarding ESM-to-CommonJS conversion (#1894, #2059)
This fixes a regression that was introduced in version 0.14.5 of esbuild. Ever since that version, esbuild now creates two separate export objects when you convert an ES module file into a CommonJS module: one for ES modules and one for CommonJS modules. The one for CommonJS modules is written to
module.exportsand exported from the file, and the one for ES modules is internal and can be accessed by bundling code that imports the entry point (for example, the entry point might import itself to be able to inspect its own exports).The reason for these two separate export objects is that CommonJS modules are supposed to see a special export called
__esModulewhich indicates that the module used to be an ES module, while ES modules are not supposed to see any automatically-added export named__esModule. This matters for real-world code both because people sometimes iterate over the properties of ES module export namespace objects and because some people write ES module code containing their own exports named__esModulethat they expect other ES module code to be able to read.However, this change to split exports into two separate objects broke ES module re-exports in the edge case where the imported module is involved in an import cycle. This happened because the CommonJS
module.exportsobject was no longer mutated as exports were added. Instead it was being initialized at the end of the generated file after the import statements to other modules (which are converted intorequire()calls). This release changesmodule.exportsinitialization to happen earlier in the file and then double-writes further exports to both the ES module and CommonJS module export objects.This fix was contributed by @indutny.