aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/js_ast.zig15
-rw-r--r--src/js_parser.zig20
2 files changed, 35 insertions, 0 deletions
diff --git a/src/js_ast.zig b/src/js_ast.zig
index 9173ce6eb..5c95b8f5e 100644
--- a/src/js_ast.zig
+++ b/src/js_ast.zig
@@ -4949,6 +4949,21 @@ pub const S = struct {
pub const ExportDefault = struct {
default_name: LocRef, // value may be a SFunction or SClass
value: StmtOrExpr,
+
+ pub fn canBeMovedAround(self: ExportDefault) bool {
+ return switch (self.value) {
+ .expr => |e| switch (e.data) {
+ .e_class => |class| class.extends == null,
+ .e_arrow, .e_function => true,
+ else => e.canBeConstValue(),
+ },
+ .stmt => |s| switch (s.data) {
+ .s_class => |class| class.class.extends == null,
+ .s_function => true,
+ else => false,
+ },
+ };
+ }
};
pub const Enum = struct {
diff --git a/src/js_parser.zig b/src/js_parser.zig
index 225ae1af8..bf78573ed 100644
--- a/src/js_parser.zig
+++ b/src/js_parser.zig
@@ -2975,6 +2975,26 @@ pub const Parser = struct {
// we might also need to do this for classes but i'm not sure yet.
try p.appendPart(&before, sliced.items);
},
+ .s_class => |class| {
+ // Move class export statements to the top of the file if we can
+ // This automatically resolves some cyclical import issues
+ // https://github.com/kysely-org/kysely/issues/412
+ var list = if (!p.options.bundle and class.is_export and class.class.extends == null) &before else &parts;
+ var sliced = try ListManaged(Stmt).initCapacity(p.allocator, 1);
+ sliced.items.len = 1;
+ sliced.items[0] = stmt;
+ try p.appendPart(list, sliced.items);
+ },
+ .s_export_default => |value| {
+ // We move export default statements when we can
+ // This automatically resolves some cyclical import issues in packages like luxon
+ // https://github.com/oven-sh/bun/issues/1961
+ var list = if (!p.options.bundle and value.canBeMovedAround()) &before else &parts;
+ var sliced = try ListManaged(Stmt).initCapacity(p.allocator, 1);
+ sliced.items.len = 1;
+ sliced.items[0] = stmt;
+ try p.appendPart(list, sliced.items);
+ },
else => {
var sliced = try ListManaged(Stmt).initCapacity(p.allocator, 1);