diff options
author | 2021-08-08 14:16:01 -0700 | |
---|---|---|
committer | 2021-08-08 14:16:01 -0700 | |
commit | 063ac7dce5705a148ec5b268fe9c62e2e0f6371d (patch) | |
tree | daedc4c025af6fea4fa2a3dce9f15ff058e8701c /src/js_parser/js_parser.zig | |
parent | e490577a7d49dd35c54b0bab486766a34ad22c0c (diff) | |
download | bun-063ac7dce5705a148ec5b268fe9c62e2e0f6371d.tar.gz bun-063ac7dce5705a148ec5b268fe9c62e2e0f6371d.tar.zst bun-063ac7dce5705a148ec5b268fe9c62e2e0f6371d.zip |
Fix export default in HMR
Former-commit-id: 4eb1220c53ec179ed40081a3d50a5248ae013af3
Diffstat (limited to 'src/js_parser/js_parser.zig')
-rw-r--r-- | src/js_parser/js_parser.zig | 105 |
1 files changed, 78 insertions, 27 deletions
diff --git a/src/js_parser/js_parser.zig b/src/js_parser/js_parser.zig index bee836910..1d41945c1 100644 --- a/src/js_parser/js_parser.zig +++ b/src/js_parser/js_parser.zig @@ -456,39 +456,90 @@ pub const ImportScanner = struct { try p.recordExport(st.default_name.loc, "default", st.default_name.ref.?); // Rewrite this export to be: // exports.default = + // But only if it's anonymous if (p.options.features.hot_module_reloading) { - var exports_default_ident = p.e(E.Dot{ .target = p.e(E.Identifier{ .ref = p.hmr_module_ref }, stmt.loc), .name = "default", .name_loc = st.default_name.loc }, stmt.loc); // export default can be: // - an expression // - a function // - a class - switch (st.value) { - .expr => |ex| { - stmt = Expr.assignStmt(exports_default_ident, ex, p.allocator); - }, - .stmt => |class_or_func| { - switch (class_or_func.data) { - .s_function => |func| { - // convert this to an E.Function - stmt = Expr.assignStmt(exports_default_ident, p.e(E.Function{ .func = func.func }, stmt.loc), p.allocator); - }, - .s_class => |class| { - stmt = Expr.assignStmt(exports_default_ident, p.e( - E.Class{ - .class_keyword = class.class.class_keyword, - .ts_decorators = class.class.ts_decorators, - .class_name = class.class.class_name, - .extends = class.class.extends, - .body_loc = class.class.body_loc, - .properties = class.class.properties, - }, - stmt.loc, - ), p.allocator); - }, - else => unreachable, - } - }, + // it cannot be a declaration! + // we want to avoid adding a new name + // but we must remove the export default clause. + transform_export_default_when_its_anonymous: { + switch (st.value) { + .expr => |ex| { + switch (ex.data) { + .e_identifier => { + continue; + }, + .e_function => |func| { + if (func.func.name) |name_ref| { + if (name_ref.ref != null) { + stmt = p.s(S.Function{ .func = func.func }, ex.loc); + break :transform_export_default_when_its_anonymous; + } + } + }, + .e_class => |class| { + if (class.class_name) |name_ref| { + if (name_ref.ref != null) { + stmt = p.s( + S.Class{ + .class = class.*, + }, + ex.loc, + ); + break :transform_export_default_when_its_anonymous; + } + } + }, + else => {}, + } + const exports_default_ident = p.e(E.Dot{ .target = p.e(E.Identifier{ .ref = p.hmr_module_ref }, stmt.loc), .name = "default", .name_loc = st.default_name.loc }, stmt.loc); + + stmt = Expr.assignStmt(exports_default_ident, ex, p.allocator); + }, + .stmt => |class_or_func| { + switch (class_or_func.data) { + .s_function => |func| { + if (func.func.name) |name_ref| { + if (name_ref.ref != null) { + stmt = class_or_func; + break :transform_export_default_when_its_anonymous; + } + } + + // convert this to an E.Function + const exports_default_ident = p.e(E.Dot{ .target = p.e(E.Identifier{ .ref = p.hmr_module_ref }, stmt.loc), .name = "default", .name_loc = st.default_name.loc }, stmt.loc); + + stmt = Expr.assignStmt(exports_default_ident, p.e(E.Function{ .func = func.func }, stmt.loc), p.allocator); + }, + .s_class => |class| { + if (class.class.class_name) |name_ref| { + if (name_ref.ref != null) { + stmt = class_or_func; + break :transform_export_default_when_its_anonymous; + } + } + + const exports_default_ident = p.e(E.Dot{ .target = p.e(E.Identifier{ .ref = p.hmr_module_ref }, stmt.loc), .name = "default", .name_loc = st.default_name.loc }, stmt.loc); + stmt = Expr.assignStmt(exports_default_ident, p.e( + E.Class{ + .class_keyword = class.class.class_keyword, + .ts_decorators = class.class.ts_decorators, + .class_name = class.class.class_name, + .extends = class.class.extends, + .body_loc = class.class.body_loc, + .properties = class.class.properties, + }, + stmt.loc, + ), p.allocator); + }, + else => unreachable, + } + }, + } } } }, |