aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2023-07-06 00:06:43 -0700
committerGravatar GitHub <noreply@github.com> 2023-07-06 00:06:43 -0700
commitee57935260214cbd6f580a756903df7eebe495ef (patch)
tree42346736ab0dd23306d779b85ba7a067b8d27b3c
parent6bf8f6f9f252dc92dc7da1fcfc3449057edd8be1 (diff)
downloadbun-ee57935260214cbd6f580a756903df7eebe495ef.tar.gz
bun-ee57935260214cbd6f580a756903df7eebe495ef.tar.zst
bun-ee57935260214cbd6f580a756903df7eebe495ef.zip
Fixes #3537 (#3539)
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
-rw-r--r--src/js_parser.zig51
1 files changed, 41 insertions, 10 deletions
diff --git a/src/js_parser.zig b/src/js_parser.zig
index 6a2cc1228..230a09fd0 100644
--- a/src/js_parser.zig
+++ b/src/js_parser.zig
@@ -3159,22 +3159,53 @@ pub const Parser = struct {
const left = bin.left;
const right = bin.right;
if (bin.op == .bin_assign and
- right.data == .e_require_string and
left.data == .e_dot and
strings.eqlComptime(left.data.e_dot.name, "exports") and
left.data.e_dot.target.data == .e_identifier and
left.data.e_dot.target.data.e_identifier.ref.eql(p.module_ref))
{
- part.symbol_uses = .{};
- return js_ast.Result{
- .ast = js_ast.Ast{
- .allocator = p.allocator,
- .import_records = ImportRecord.List.init(p.import_records.items),
- .redirect_import_record_index = right.data.e_require_string.import_record_index,
- .named_imports = p.named_imports,
- .named_exports = p.named_exports,
- },
+ const redirect_import_record_index: ?u32 = brk: {
+ // general case:
+ //
+ // module.exports = require("foo");
+ //
+ if (right.data == .e_require_string) {
+ break :brk right.data.e_require_string.import_record_index;
+ }
+
+ // special case: a module for us to unwrap
+ //
+ // module.exports = require("react/jsx-runtime")
+ // ^ was converted into:
+ //
+ // import * as Foo from 'bar';
+ // module.exports = Foo;
+ //
+ // This is what fixes #3537
+ if (right.data == .e_identifier and
+ p.import_records.items.len == 1 and
+ p.imports_to_convert_from_require.items.len == 1 and
+ p.imports_to_convert_from_require.items[0].namespace.ref.?.eql(right.data.e_identifier.ref))
+ {
+ // We know it's 0 because there is only one import in the whole file
+ // so that one import must be the one we're looking for
+ break :brk 0;
+ }
+
+ break :brk null;
};
+ if (redirect_import_record_index) |id| {
+ part.symbol_uses = .{};
+ return js_ast.Result{
+ .ast = js_ast.Ast{
+ .allocator = p.allocator,
+ .import_records = ImportRecord.List.init(p.import_records.items),
+ .redirect_import_record_index = id,
+ .named_imports = p.named_imports,
+ .named_exports = p.named_exports,
+ },
+ };
+ }
}
}
}