diff options
author | 2021-09-23 21:45:15 -0700 | |
---|---|---|
committer | 2021-09-23 21:45:15 -0700 | |
commit | f78f4854a4e474a08023841f4714f0cd3774126b (patch) | |
tree | 6252b32e23a9aeb3ab279f2f1061a264e755c797 | |
parent | ff01dfa03da3b9cb17fe2797a7e5e096844e5ee6 (diff) | |
download | bun-f78f4854a4e474a08023841f4714f0cd3774126b.tar.gz bun-f78f4854a4e474a08023841f4714f0cd3774126b.tar.zst bun-f78f4854a4e474a08023841f4714f0cd3774126b.zip |
Handle more edgecases in our CJS2ESM conversion code
-rw-r--r-- | src/js_parser/js_parser.zig | 29 | ||||
-rw-r--r-- | src/js_printer.zig | 10 | ||||
-rw-r--r-- | src/runtime.js | 25 | ||||
-rw-r--r-- | src/runtime.version | 2 |
4 files changed, 57 insertions, 9 deletions
diff --git a/src/js_parser/js_parser.zig b/src/js_parser/js_parser.zig index b548416cc..1de8c4656 100644 --- a/src/js_parser/js_parser.zig +++ b/src/js_parser/js_parser.zig @@ -12453,6 +12453,7 @@ pub fn NewParser( switch (s2.data) { .s_function => |func| { var name: string = ""; + const had_name = func.func.name != null; if (func.func.name) |func_loc| { name = p.loadNameFromRef(func_loc.ref.?); } else { @@ -12465,7 +12466,15 @@ pub fn NewParser( if (p.options.enable_bundling) { var export_default_args = p.allocator.alloc(Expr, 2) catch unreachable; export_default_args[0] = p.e(E.Identifier{ .ref = p.exports_ref }, s2.loc); - export_default_args[1] = p.e(E.Function{ .func = func.func }, s2.loc); + + if (had_name) { + export_default_args[1] = p.e(E.Identifier{ .ref = func.func.name.?.ref.? }, s2.loc); + stmts.ensureUnusedCapacity(2) catch unreachable; + + stmts.appendAssumeCapacity(s2); + } else { + export_default_args[1] = p.e(E.Function{ .func = func.func }, s2.loc); + } stmts.append(p.s(S.SExpr{ .value = p.callRuntime(s2.loc, "__exportDefault", export_default_args) }, s2.loc)) catch unreachable; return; @@ -12481,10 +12490,26 @@ pub fn NewParser( }, .s_class => |class| { var shadow_ref = p.visitClass(s2.loc, &class.class); + if (p.options.enable_bundling) { var export_default_args = p.allocator.alloc(Expr, 2) catch unreachable; export_default_args[0] = p.e(E.Identifier{ .ref = p.exports_ref }, s2.loc); - export_default_args[1] = p.e(class.class, s2.loc); + + const class_name_ref = brk: { + if (class.class.class_name) |class_name_ref| { + if (class_name_ref.ref) |ref| { + break :brk ref; + } + } + break :brk null; + }; + if (class_name_ref) |ref| { + stmts.ensureUnusedCapacity(2) catch unreachable; + stmts.appendAssumeCapacity(s2); + export_default_args[1] = p.e(E.Identifier{ .ref = ref }, s2.loc); + } else { + export_default_args[1] = p.e(class.class, s2.loc); + } stmts.append(p.s(S.SExpr{ .value = p.callRuntime(s2.loc, "__exportDefault", export_default_args) }, s2.loc)) catch unreachable; return; diff --git a/src/js_printer.zig b/src/js_printer.zig index c73e03464..3bdeb09ba 100644 --- a/src/js_printer.zig +++ b/src/js_printer.zig @@ -2485,7 +2485,7 @@ pub fn NewPrinter( // Object.assign(__export, {prop1, prop2, prop3}); else => { if (comptime is_inside_bundle) { - p.printSymbol(p.options.runtime_imports.__exportValue.?.ref); + p.printSymbol(p.options.runtime_imports.__export.?.ref); } else { p.print("Object.assign"); } @@ -2497,8 +2497,12 @@ pub fn NewPrinter( for (s.items) |item, i| { const name = p.renamer.nameForSymbol(item.name.ref.?); p.printClauseAlias(item.alias); - - if (!strings.eql(name, item.alias)) { + if (comptime is_inside_bundle) { + p.print(":"); + p.printSpace(); + p.print("() => "); + p.printIdentifier(name); + } else if (!strings.eql(name, item.alias)) { p.print(":"); p.printSpace(); p.printIdentifier(name); diff --git a/src/runtime.js b/src/runtime.js index 6e8822767..83bdff938 100644 --- a/src/runtime.js +++ b/src/runtime.js @@ -54,9 +54,24 @@ export var __commonJS = (cb, name) => { cb(mod, mod.exports); const kind = typeof mod.exports; - - // If it's a default-only export, don't crash if they call .default on the module if ( + (kind === "function" || kind === "object") && + "__esModule" in mod.exports && + !mod.exports[tagSymbol] + ) { + if (!("default" in mod.exports)) { + Object.defineProperty(mod.exports, "default", { + get() { + return mod.exports; + }, + set(v) { + mod.exports = v; + }, + enumerable: true, + configurable: true, + }); + } + } else if ( kind === "object" && "default" in mod.exports && !mod.exports[tagSymbol] && @@ -73,6 +88,9 @@ export var __commonJS = (cb, name) => { get() { return mod.exports; }, + set(v) { + mod.exports = v; + }, enumerable: true, configurable: true, }); @@ -168,13 +186,14 @@ export var __export = (target, all) => { }; export var __exportValue = (target, all) => { - for (var name in all) + for (var name in all) { __defProp(target, name, { get: () => all[name], set: (newValue) => (all[name] = newValue), enumerable: true, configurable: true, }); + } }; export var __exportDefault = (target, value) => { diff --git a/src/runtime.version b/src/runtime.version index f5f0047b9..1d3a39c60 100644 --- a/src/runtime.version +++ b/src/runtime.version @@ -1 +1 @@ -c92310420c760f74
\ No newline at end of file +80c43f840c4f57eb
\ No newline at end of file |