diff --git a/internal/bundler/linker.go b/internal/bundler/linker.go index 78a0ccfd634..13fd8b1b6c6 100644 --- a/internal/bundler/linker.go +++ b/internal/bundler/linker.go @@ -1726,10 +1726,11 @@ func (c *linkerContext) createExportsForFile(sourceIndex uint32) { } } - // Prefix this part with "var exports = {}" if this isn't a CommonJS module + // Prefix this part with "var exports = { __esModule: true }" if this isn't a CommonJS module declaredSymbols := []js_ast.DeclaredSymbol{} var nsExportStmts []js_ast.Stmt - if repr.AST.ExportsKind != js_ast.ExportsCommonJS && (!file.IsEntryPoint() || c.options.OutputFormat != config.FormatCommonJS) { + isNotCommonJSAndEntry := repr.AST.ExportsKind != js_ast.ExportsCommonJS && (!file.IsEntryPoint() || c.options.OutputFormat != config.FormatCommonJS) + if isNotCommonJSAndEntry { nsExportStmts = append(nsExportStmts, js_ast.Stmt{Data: &js_ast.SLocal{Decls: []js_ast.Decl{{ Binding: js_ast.Binding{Data: &js_ast.BIdentifier{Ref: repr.AST.ExportsRef}}, ValueOrNil: js_ast.Expr{Data: &js_ast.EObject{}}, @@ -1744,8 +1745,10 @@ func (c *linkerContext) createExportsForFile(sourceIndex uint32) { // "__markAsModule" which sets the "__esModule" property to true. This must // be done before any to "require()" or circular imports of multiple modules // that have been each converted from ESM to CommonJS may not work correctly. - if repr.AST.ExportKeyword.Len > 0 && (repr.AST.ExportsKind == js_ast.ExportsCommonJS || - (file.IsEntryPoint() && c.options.OutputFormat == config.FormatCommonJS)) { + if (repr.AST.ExportKeyword.Len > 0 && + (repr.AST.ExportsKind == js_ast.ExportsCommonJS || + (file.IsEntryPoint() && c.options.OutputFormat == config.FormatCommonJS))) || + isNotCommonJSAndEntry { runtimeRepr := c.graph.Files[runtime.SourceIndex].InputFile.Repr.(*graph.JSRepr) markAsModuleRef := runtimeRepr.AST.ModuleScope.Members["__markAsModule"].Ref nsExportStmts = append(nsExportStmts, js_ast.Stmt{Data: &js_ast.SExpr{Value: js_ast.Expr{Data: &js_ast.ECall{ diff --git a/internal/bundler/snapshots/snapshots_dce.txt b/internal/bundler/snapshots/snapshots_dce.txt index 8c844e2522a..277b1b152cf 100644 --- a/internal/bundler/snapshots/snapshots_dce.txt +++ b/internal/bundler/snapshots/snapshots_dce.txt @@ -145,6 +145,7 @@ TestPackageJsonSideEffectsArrayKeepMainImplicitMain ---------- /out.js ---------- // Users/user/project/node_modules/demo-pkg/index-main.js var index_main_exports = {}; +__markAsModule(index_main_exports); __export(index_main_exports, { foo: () => foo }); @@ -191,6 +192,7 @@ TestPackageJsonSideEffectsArrayKeepModuleImplicitMain ---------- /out.js ---------- // Users/user/project/node_modules/demo-pkg/index-main.js var index_main_exports = {}; +__markAsModule(index_main_exports); __export(index_main_exports, { foo: () => foo }); @@ -258,6 +260,7 @@ var init_b = __esm({ // Users/user/project/node_modules/a/index.js var a_exports = {}; +__markAsModule(a_exports); __export(a_exports, { foo: () => foo }); @@ -345,6 +348,7 @@ TestPackageJsonSideEffectsFalseKeepBareImportAndRequireES6 ---------- /out.js ---------- // Users/user/project/node_modules/demo-pkg/index.js var demo_pkg_exports = {}; +__markAsModule(demo_pkg_exports); __export(demo_pkg_exports, { foo: () => foo }); @@ -405,6 +409,7 @@ TestPackageJsonSideEffectsFalseKeepStarImportES6 ---------- /out.js ---------- // Users/user/project/node_modules/demo-pkg/index.js var demo_pkg_exports = {}; +__markAsModule(demo_pkg_exports); __export(demo_pkg_exports, { foo: () => foo }); @@ -450,6 +455,7 @@ var init_b = __esm({ // Users/user/project/node_modules/a/index.js var a_exports = {}; +__markAsModule(a_exports); __export(a_exports, { foo: () => foo }); @@ -678,6 +684,7 @@ var init_lib = __esm({ // cjs.js var cjs_exports = {}; +__markAsModule(cjs_exports); __export(cjs_exports, { default: () => cjs_default }); diff --git a/internal/bundler/snapshots/snapshots_default.txt b/internal/bundler/snapshots/snapshots_default.txt index 6fa43a56ee6..7e782f5d341 100644 --- a/internal/bundler/snapshots/snapshots_default.txt +++ b/internal/bundler/snapshots/snapshots_default.txt @@ -244,6 +244,7 @@ TestCommonJSFromES6 ---------- /out.js ---------- // foo.js var foo_exports = {}; +__markAsModule(foo_exports); __export(foo_exports, { foo: () => foo }); @@ -257,6 +258,7 @@ var init_foo = __esm({ // bar.js var bar_exports = {}; +__markAsModule(bar_exports); __export(bar_exports, { bar: () => bar }); @@ -492,6 +494,7 @@ TestEmptyExportClauseBundleAsCommonJSIssue910 ---------- /out.js ---------- // types.mjs var types_exports = {}; +__markAsModule(types_exports); var init_types = __esm({ "types.mjs"() { } @@ -548,6 +551,7 @@ var init_a = __esm({ // b.js var b_exports = {}; +__markAsModule(b_exports); __export(b_exports, { xyz: () => xyz }); @@ -560,6 +564,7 @@ var init_b = __esm({ // commonjs.js var commonjs_exports = {}; +__markAsModule(commonjs_exports); __export(commonjs_exports, { C: () => Class, Class: () => Class, @@ -589,6 +594,7 @@ var init_commonjs = __esm({ // c.js var c_exports = {}; +__markAsModule(c_exports); __export(c_exports, { default: () => c_default2 }); @@ -603,6 +609,7 @@ var init_c = __esm({ // d.js var d_exports = {}; +__markAsModule(d_exports); __export(d_exports, { default: () => d_default }); @@ -618,6 +625,7 @@ var init_d = __esm({ // e.js var e_exports = {}; +__markAsModule(e_exports); __export(e_exports, { default: () => e_default }); @@ -630,6 +638,7 @@ var init_e = __esm({ // f.js var f_exports = {}; +__markAsModule(f_exports); __export(f_exports, { default: () => foo }); @@ -643,6 +652,7 @@ var init_f = __esm({ // g.js var g_exports = {}; +__markAsModule(g_exports); __export(g_exports, { default: () => g_default }); @@ -655,6 +665,7 @@ var init_g = __esm({ // h.js var h_exports = {}; +__markAsModule(h_exports); __export(h_exports, { default: () => foo2 }); @@ -683,6 +694,7 @@ var abc = void 0; // b.js var b_exports = {}; +__markAsModule(b_exports); __export(b_exports, { xyz: () => xyz }); @@ -715,6 +727,7 @@ TestExportFormsIIFE var globalName = (() => { // entry.js var entry_exports = {}; + __markAsModule(entry_exports); __export(entry_exports, { C: () => Class, Class: () => Class, @@ -732,6 +745,7 @@ var globalName = (() => { // b.js var b_exports = {}; + __markAsModule(b_exports); __export(b_exports, { xyz: () => xyz }); @@ -802,6 +816,7 @@ TestExportsAndModuleFormatCommonJS ---------- /out.js ---------- // foo/test.js var test_exports = {}; +__markAsModule(test_exports); __export(test_exports, { foo: () => foo }); @@ -809,6 +824,7 @@ var foo = 123; // bar/test.js var test_exports2 = {}; +__markAsModule(test_exports2); __export(test_exports2, { bar: () => bar }); @@ -822,6 +838,7 @@ TestExternalES6ConvertedToCommonJS ---------- /out.js ---------- // a.js var a_exports = {}; +__markAsModule(a_exports); __export(a_exports, { ns: () => ns }); @@ -833,6 +850,7 @@ var init_a = __esm({ // b.js var b_exports = {}; +__markAsModule(b_exports); __export(b_exports, { ns: () => ns2 }); @@ -844,6 +862,7 @@ var init_b = __esm({ // c.js var c_exports = {}; +__markAsModule(c_exports); __export(c_exports, { ns: () => ns3 }); @@ -855,6 +874,7 @@ var init_c = __esm({ // d.js var d_exports = {}; +__markAsModule(d_exports); __export(d_exports, { ns: () => ns4 }); @@ -866,6 +886,7 @@ var init_d = __esm({ // e.js var e_exports = {}; +__markAsModule(e_exports); import * as x_star from "x"; var init_e = __esm({ "e.js"() { @@ -1815,21 +1836,23 @@ import("foo");import(foo()); TestMinifiedExportsAndModuleFormatCommonJS ---------- /out.js ---------- // foo/test.js -var o = {}; -s(o, { - foo: () => p +var t = {}; +f(t); +p(t, { + foo: () => l }); -var p = 123; +var l = 123; // bar/test.js -var t = {}; -s(t, { - bar: () => l +var r = {}; +f(r); +p(r, { + bar: () => m }); -var l = 123; +var m = 123; // entry.js -console.log(exports, module.exports, o, t); +console.log(exports, module.exports, t, r); ================================================================================ TestMinifyArguments @@ -2866,6 +2889,7 @@ var require_cjs = __commonJS({ // dummy.js var dummy_exports = {}; +__markAsModule(dummy_exports); __export(dummy_exports, { dummy: () => dummy }); @@ -3132,6 +3156,7 @@ TestTopLevelAwaitAllowedImportWithoutSplitting ---------- /out.js ---------- // c.js var c_exports = {}; +__markAsModule(c_exports); var init_c = __esm({ async "c.js"() { await 0; @@ -3140,6 +3165,7 @@ var init_c = __esm({ // b.js var b_exports = {}; +__markAsModule(b_exports); var init_b = __esm({ async "b.js"() { await init_c(); @@ -3148,6 +3174,7 @@ var init_b = __esm({ // a.js var a_exports = {}; +__markAsModule(a_exports); var init_a = __esm({ async "a.js"() { await init_b(); @@ -3156,6 +3183,7 @@ var init_a = __esm({ // entry.js var entry_exports = {}; +__markAsModule(entry_exports); var init_entry = __esm({ async "entry.js"() { init_a(); diff --git a/internal/bundler/snapshots/snapshots_importstar.txt b/internal/bundler/snapshots/snapshots_importstar.txt index adeda10db8d..5f5081dfd3f 100644 --- a/internal/bundler/snapshots/snapshots_importstar.txt +++ b/internal/bundler/snapshots/snapshots_importstar.txt @@ -94,6 +94,7 @@ TestExportSelfAsNamespaceES6 ---------- /out.js ---------- // entry.js var entry_exports = {}; +__markAsModule(entry_exports); __export(entry_exports, { foo: () => foo, ns: () => entry_exports @@ -147,6 +148,7 @@ TestExportSelfIIFEWithName var someName = (() => { // entry.js var entry_exports = {}; + __markAsModule(entry_exports); __export(entry_exports, { foo: () => foo }); @@ -212,6 +214,7 @@ console.log(internal_default, internal_default); ---------- /out/internal-ns.js ---------- // internal.js var internal_exports = {}; +__markAsModule(internal_exports); __export(internal_exports, { default: () => internal_default }); @@ -223,6 +226,7 @@ console.log(internal_default, internal_exports); ---------- /out/internal-ns-default.js ---------- // internal.js var internal_exports = {}; +__markAsModule(internal_exports); __export(internal_exports, { default: () => internal_default }); @@ -234,6 +238,7 @@ console.log(internal_default, internal_exports, internal_default); ---------- /out/internal-ns-def.js ---------- // internal.js var internal_exports = {}; +__markAsModule(internal_exports); __export(internal_exports, { default: () => internal_default }); @@ -278,6 +283,7 @@ TestImportExportSelfAsNamespaceES6 ---------- /out.js ---------- // entry.js var entry_exports = {}; +__markAsModule(entry_exports); __export(entry_exports, { foo: () => foo, ns: () => entry_exports @@ -346,6 +352,7 @@ TestImportStarAndCommonJS ---------- /out.js ---------- // foo.js var foo_exports = {}; +__markAsModule(foo_exports); __export(foo_exports, { foo: () => foo }); @@ -366,6 +373,7 @@ TestImportStarCapture ---------- /out.js ---------- // foo.js var foo_exports = {}; +__markAsModule(foo_exports); __export(foo_exports, { foo: () => foo }); @@ -425,6 +433,7 @@ TestImportStarExportImportStarCapture ---------- /out.js ---------- // foo.js var foo_exports = {}; +__markAsModule(foo_exports); __export(foo_exports, { foo: () => foo }); @@ -439,6 +448,7 @@ TestImportStarExportImportStarNoCapture ---------- /out.js ---------- // foo.js var foo_exports = {}; +__markAsModule(foo_exports); __export(foo_exports, { foo: () => foo }); @@ -460,6 +470,7 @@ TestImportStarExportStarAsCapture ---------- /out.js ---------- // foo.js var foo_exports = {}; +__markAsModule(foo_exports); __export(foo_exports, { foo: () => foo }); @@ -474,6 +485,7 @@ TestImportStarExportStarAsNoCapture ---------- /out.js ---------- // foo.js var foo_exports = {}; +__markAsModule(foo_exports); __export(foo_exports, { foo: () => foo }); @@ -495,6 +507,7 @@ TestImportStarExportStarCapture ---------- /out.js ---------- // bar.js var bar_exports = {}; +__markAsModule(bar_exports); __export(bar_exports, { foo: () => foo }); @@ -521,6 +534,7 @@ TestImportStarExportStarOmitAmbiguous ---------- /out.js ---------- // common.js var common_exports = {}; +__markAsModule(common_exports); __export(common_exports, { x: () => x, z: () => z @@ -599,12 +613,14 @@ TestImportStarOfExportStarAs ---------- /out.js ---------- // foo.js var foo_exports = {}; +__markAsModule(foo_exports); __export(foo_exports, { bar_ns: () => bar_exports }); // bar.js var bar_exports = {}; +__markAsModule(bar_exports); __export(bar_exports, { bar: () => bar }); @@ -625,6 +641,7 @@ TestIssue176 ---------- /out.js ---------- // folders/index.js var folders_exports = {}; +__markAsModule(folders_exports); __export(folders_exports, { foo: () => foo }); @@ -654,6 +671,7 @@ TestNamespaceImportMissingES6 ---------- /out.js ---------- // foo.js var foo_exports = {}; +__markAsModule(foo_exports); __export(foo_exports, { x: () => x }); @@ -667,6 +685,7 @@ TestNamespaceImportReExportStarMissingES6 ---------- /out.js ---------- // foo.js var foo_exports = {}; +__markAsModule(foo_exports); __export(foo_exports, { x: () => x }); @@ -726,6 +745,7 @@ TestReExportNamespaceImportMissingES6 ---------- /out.js ---------- // bar.js var bar_exports = {}; +__markAsModule(bar_exports); __export(bar_exports, { x: () => x }); @@ -739,6 +759,7 @@ TestReExportNamespaceImportUnusedMissingES6 ---------- /out.js ---------- // bar.js var bar_exports = {}; +__markAsModule(bar_exports); __export(bar_exports, { x: () => x }); @@ -752,6 +773,7 @@ TestReExportOtherFileExportSelfAsNamespaceES6 ---------- /out.js ---------- // foo.js var foo_exports = {}; +__markAsModule(foo_exports); __export(foo_exports, { foo: () => foo, ns: () => foo_exports @@ -767,6 +789,7 @@ TestReExportOtherFileImportExportSelfAsNamespaceES6 ---------- /out.js ---------- // foo.js var foo_exports = {}; +__markAsModule(foo_exports); __export(foo_exports, { foo: () => foo, ns: () => foo_exports @@ -819,6 +842,7 @@ TestReExportStarAsExternalIIFE var mod = (() => { // entry.js var entry_exports = {}; + __markAsModule(entry_exports); __export(entry_exports, { out: () => out }); @@ -831,6 +855,7 @@ TestReExportStarAsIIFENoBundle ---------- /out.js ---------- var mod = (() => { var entry_exports = {}; + __markAsModule(entry_exports); __export(entry_exports, { out: () => out }); @@ -868,6 +893,7 @@ TestReExportStarExternalIIFE var mod = (() => { // entry.js var entry_exports = {}; + __markAsModule(entry_exports); __reExport(entry_exports, __toModule(__require("foo"))); return entry_exports; })(); @@ -877,6 +903,7 @@ TestReExportStarIIFENoBundle ---------- /out.js ---------- var mod = (() => { var entry_exports = {}; + __markAsModule(entry_exports); __reExport(entry_exports, __toModule(require("foo"))); return entry_exports; })(); diff --git a/internal/bundler/snapshots/snapshots_importstar_ts.txt b/internal/bundler/snapshots/snapshots_importstar_ts.txt index 0ff0a4304e0..6d94c857b8e 100644 --- a/internal/bundler/snapshots/snapshots_importstar_ts.txt +++ b/internal/bundler/snapshots/snapshots_importstar_ts.txt @@ -2,6 +2,7 @@ TestTSImportStarAndCommonJS ---------- /out.js ---------- // foo.ts var foo_exports = {}; +__markAsModule(foo_exports); __export(foo_exports, { foo: () => foo }); @@ -22,6 +23,7 @@ TestTSImportStarCapture ---------- /out.js ---------- // foo.ts var foo_exports = {}; +__markAsModule(foo_exports); __export(foo_exports, { foo: () => foo }); @@ -73,6 +75,7 @@ TestTSImportStarExportImportStarCapture ---------- /out.js ---------- // foo.ts var foo_exports = {}; +__markAsModule(foo_exports); __export(foo_exports, { foo: () => foo }); @@ -87,6 +90,7 @@ TestTSImportStarExportImportStarNoCapture ---------- /out.js ---------- // foo.ts var foo_exports = {}; +__markAsModule(foo_exports); __export(foo_exports, { foo: () => foo }); @@ -108,6 +112,7 @@ TestTSImportStarExportStarAsCapture ---------- /out.js ---------- // foo.ts var foo_exports = {}; +__markAsModule(foo_exports); __export(foo_exports, { foo: () => foo }); @@ -122,6 +127,7 @@ TestTSImportStarExportStarAsNoCapture ---------- /out.js ---------- // foo.ts var foo_exports = {}; +__markAsModule(foo_exports); __export(foo_exports, { foo: () => foo }); @@ -143,6 +149,7 @@ TestTSImportStarExportStarCapture ---------- /out.js ---------- // bar.ts var bar_exports = {}; +__markAsModule(bar_exports); __export(bar_exports, { foo: () => foo }); diff --git a/internal/bundler/snapshots/snapshots_loader.txt b/internal/bundler/snapshots/snapshots_loader.txt index 33e270f4c4c..32bdf7e9d7a 100644 --- a/internal/bundler/snapshots/snapshots_loader.txt +++ b/internal/bundler/snapshots/snapshots_loader.txt @@ -169,6 +169,7 @@ var invalid_identifier = true; // test2.json var test2_exports = {}; +__markAsModule(test2_exports); __export(test2_exports, { default: () => test2_default, "invalid-identifier": () => invalid_identifier2 diff --git a/internal/bundler/snapshots/snapshots_packagejson.txt b/internal/bundler/snapshots/snapshots_packagejson.txt index 97a9c190b8c..c9ad8786467 100644 --- a/internal/bundler/snapshots/snapshots_packagejson.txt +++ b/internal/bundler/snapshots/snapshots_packagejson.txt @@ -371,6 +371,7 @@ TestPackageJsonDualPackageHazardImportAndRequireForceModuleBeforeMain ---------- /Users/user/project/out.js ---------- // Users/user/project/node_modules/demo-pkg/module.js var module_exports = {}; +__markAsModule(module_exports); __export(module_exports, { default: () => module_default }); @@ -410,6 +411,7 @@ TestPackageJsonDualPackageHazardImportAndRequireImplicitMainForceModuleBeforeMai ---------- /Users/user/project/out.js ---------- // Users/user/project/node_modules/demo-pkg/module.js var module_exports = {}; +__markAsModule(module_exports); __export(module_exports, { default: () => module_default }); diff --git a/internal/bundler/snapshots/snapshots_splitting.txt b/internal/bundler/snapshots/snapshots_splitting.txt index d6c7fed8bfe..0c3f4b270c8 100644 --- a/internal/bundler/snapshots/snapshots_splitting.txt +++ b/internal/bundler/snapshots/snapshots_splitting.txt @@ -259,10 +259,16 @@ export { ================================================================================ TestSplittingDynamicCommonJSIntoES6 ---------- /out/entry.js ---------- +import "./chunk-VD7MAPBT.js"; + // entry.js -import("./foo-KZX24I45.js").then(({ default: { bar } }) => console.log(bar)); +import("./foo-YGPNXO73.js").then(({ default: { bar } }) => console.log(bar)); + +---------- /out/foo-YGPNXO73.js ---------- +import { + __commonJS +} from "./chunk-VD7MAPBT.js"; ----------- /out/foo-KZX24I45.js ---------- // foo.js var require_foo = __commonJS({ "foo.js"(exports) { @@ -271,6 +277,11 @@ var require_foo = __commonJS({ }); export default require_foo(); +---------- /out/chunk-VD7MAPBT.js ---------- +export { + __commonJS +}; + ================================================================================ TestSplittingDynamicES6IntoES6 ---------- /out/entry.js ---------- @@ -317,7 +328,7 @@ TestSplittingHybridESMAndCJSIssue617 import { foo, init_a -} from "./chunk-V7Q4P7WA.js"; +} from "./chunk-T3MG3LRV.js"; init_a(); export { foo @@ -327,7 +338,7 @@ export { import { a_exports, init_a -} from "./chunk-V7Q4P7WA.js"; +} from "./chunk-T3MG3LRV.js"; // b.js var bar = (init_a(), a_exports); @@ -335,9 +346,10 @@ export { bar }; ----------- /out/chunk-V7Q4P7WA.js ---------- +---------- /out/chunk-T3MG3LRV.js ---------- // a.js var a_exports = {}; +__markAsModule(a_exports); __export(a_exports, { foo: () => foo }); @@ -388,7 +400,7 @@ TestSplittingMissingLazyExport ---------- /out/a.js ---------- import { foo -} from "./chunk-QVTGQSXT.js"; +} from "./chunk-KFPHQBDL.js"; // a.js console.log(foo()); @@ -396,14 +408,15 @@ console.log(foo()); ---------- /out/b.js ---------- import { bar -} from "./chunk-QVTGQSXT.js"; +} from "./chunk-KFPHQBDL.js"; // b.js console.log(bar()); ----------- /out/chunk-QVTGQSXT.js ---------- +---------- /out/chunk-KFPHQBDL.js ---------- // empty.js var empty_exports = {}; +__markAsModule(empty_exports); // common.js function foo() { diff --git a/internal/bundler/snapshots/snapshots_ts.txt b/internal/bundler/snapshots/snapshots_ts.txt index 5c4ebf82bf5..fabfa15c8f5 100644 --- a/internal/bundler/snapshots/snapshots_ts.txt +++ b/internal/bundler/snapshots/snapshots_ts.txt @@ -2,6 +2,7 @@ TestExportTypeIssue379 ---------- /out.js ---------- // a.ts var a_exports = {}; +__markAsModule(a_exports); __export(a_exports, { foo: () => foo }); @@ -9,6 +10,7 @@ var foo = 123; // b.ts var b_exports = {}; +__markAsModule(b_exports); __export(b_exports, { foo: () => foo2 }); @@ -16,6 +18,7 @@ var foo2 = 123; // c.ts var c_exports = {}; +__markAsModule(c_exports); __export(c_exports, { foo: () => foo3 }); @@ -23,6 +26,7 @@ var foo3 = 123; // d.ts var d_exports = {}; +__markAsModule(d_exports); __export(d_exports, { foo: () => foo4 }); @@ -212,6 +216,7 @@ TestTSExportMissingES6 ---------- /out.js ---------- // foo.ts var foo_exports = {}; +__markAsModule(foo_exports); // entry.js console.log(foo_exports);