diff options
author | 2023-01-27 21:39:28 -0800 | |
---|---|---|
committer | 2023-01-27 21:39:40 -0800 | |
commit | 8d622ad541e7c6ee72bf00d813c6764f3b16ba03 (patch) | |
tree | 200aaaa24f31042aead4faff243b4f39a82d6a2f | |
parent | ed5bcf17fc0c14a81b7428e9d8b1c09a2f7d1e09 (diff) | |
download | bun-8d622ad541e7c6ee72bf00d813c6764f3b16ba03.tar.gz bun-8d622ad541e7c6ee72bf00d813c6764f3b16ba03.tar.zst bun-8d622ad541e7c6ee72bf00d813c6764f3b16ba03.zip |
Fixes #1904
-rw-r--r-- | src/bundler/generate_node_modules_bundle.zig | 94 |
1 files changed, 57 insertions, 37 deletions
diff --git a/src/bundler/generate_node_modules_bundle.zig b/src/bundler/generate_node_modules_bundle.zig index 23b3efa57..c57e50399 100644 --- a/src/bundler/generate_node_modules_bundle.zig +++ b/src/bundler/generate_node_modules_bundle.zig @@ -1132,7 +1132,7 @@ var json_ast_symbols = [_]js_ast.Symbol{ js_ast.Symbol{ .original_name = "$$bun_runtime_json_parse" }, }; const json_parse_string = "parse"; -var json_ast_symbols_list = std.mem.span(&json_ast_symbols); + threadlocal var override_file_path_buf: [bun.MAX_PATH_BYTES]u8 = undefined; pub fn appendToModuleList( @@ -1479,29 +1479,45 @@ pub fn processFile(this: *GenerateNodeModuleBundle, worker: *ThreadPool.Worker, const expr = brk: { // If it's an ascii string, we just print it out with a big old JSON.parse() if (json_parse_result.tag == .ascii) { - json_e_string = js_ast.E.String{ .data = source.contents, .prefer_template = true }; - var json_string_expr = js_ast.Expr{ .data = .{ .e_string = &json_e_string }, .loc = logger.Loc{ .start = 0 } }; - json_call_args[0] = json_string_expr; - json_e_identifier = js_ast.E.Identifier{ .ref = Ref.atIndex(json_ast_symbols_list.len - 1) }; - - json_e_call = js_ast.E.Call{ - .target = js_ast.Expr{ .data = .{ .e_identifier = json_e_identifier }, .loc = logger.Loc{ .start = 0 } }, - .args = js_ast.ExprNodeList.init(std.mem.span(&json_call_args)), - }; - break :brk js_ast.Expr{ .data = .{ .e_call = &json_e_call }, .loc = logger.Loc{ .start = 0 } }; - // If we're going to have to convert it to a UTF16, just make it an object actually + json_call_args[0] = js_ast.Expr.init( + js_ast.E.String, + js_ast.E.String{ + .data = source.contents, + }, + .{ .start = 0 }, + ); + + break :brk js_ast.Expr.init( + js_ast.E.Call, + js_ast.E.Call{ + .target = js_ast.Expr{ + .data = .{ + .e_identifier = js_ast.E.Identifier{ + .ref = Ref.atIndex(json_ast_symbols.len - 1), + }, + }, + .loc = .{ .start = 0 }, + }, + .args = js_ast.ExprNodeList.init(std.mem.span(&json_call_args)), + }, + .{ .start = 0 }, + ); } else { break :brk json_parse_result.expr; } }; - var stmt = js_ast.Stmt.alloc(js_ast.S.ExportDefault, js_ast.S.ExportDefault{ - .value = js_ast.StmtOrExpr{ .expr = expr }, - .default_name = js_ast.LocRef{ - .loc = logger.Loc{}, - .ref = Ref.None, + var stmt = js_ast.Stmt.alloc( + js_ast.S.ExportDefault, + js_ast.S.ExportDefault{ + .value = js_ast.StmtOrExpr{ .expr = expr }, + .default_name = js_ast.LocRef{ + .loc = .{}, + .ref = null, + }, }, - }, logger.Loc{ .start = 0 }); + .{ .start = 0 }, + ); var stmts = worker.allocator.alloc(js_ast.Stmt, 1) catch unreachable; stmts[0] = stmt; var parts = worker.allocator.alloc(js_ast.Part, 1) catch unreachable; @@ -1511,7 +1527,7 @@ pub fn processFile(this: *GenerateNodeModuleBundle, worker: *ThreadPool.Worker, ast.runtime_imports = runtime.Runtime.Imports{}; ast.runtime_imports.@"$$m" = .{ .ref = Ref.atIndex(0), .primary = Ref.None, .backup = Ref.None }; ast.runtime_imports.__export = .{ .ref = Ref.atIndex(1), .primary = Ref.None, .backup = Ref.None }; - ast.symbols = json_ast_symbols_list; + ast.symbols = &json_ast_symbols; ast.module_ref = Ref.atIndex(2); ast.exports_ref = ast.runtime_imports.__export.?.ref; ast.bundle_export_ref = Ref.atIndex(3); @@ -1553,7 +1569,6 @@ pub fn processFile(this: *GenerateNodeModuleBundle, worker: *ThreadPool.Worker, var package_path = js_ast.E.String{ .data = module_data.package_path }; var target_identifier = E.Identifier{ .ref = register_ref }; - var cjs_args: [2]js_ast.G.Arg = undefined; var module_binding = js_ast.B.Identifier{ .ref = ast.module_ref.? }; var exports_binding = js_ast.B.Identifier{ .ref = ast.exports_ref.? }; @@ -1561,6 +1576,24 @@ pub fn processFile(this: *GenerateNodeModuleBundle, worker: *ThreadPool.Worker, var new_stmts: [1]Stmt = undefined; var register_args: [1]Expr = undefined; + + var cjs_args: [2]js_ast.G.Arg = [_]js_ast.G.Arg{ + .{ + .binding = .{ + .loc = logger.Loc.Empty, + .data = .{ .b_identifier = &module_binding }, + }, + }, + .{ + .binding = js_ast.Binding{ + .loc = logger.Loc.Empty, + .data = .{ .b_identifier = &exports_binding }, + }, + }, + }; + + const module_path_str = js_ast.Expr{ .data = .{ .e_string = &package_path }, .loc = logger.Loc.Empty }; + var closure = E.Arrow{ .args = &cjs_args, .body = .{ @@ -1569,29 +1602,16 @@ pub fn processFile(this: *GenerateNodeModuleBundle, worker: *ThreadPool.Worker, }, }; - cjs_args[0] = js_ast.G.Arg{ - .binding = js_ast.Binding{ - .loc = logger.Loc.Empty, - .data = .{ .b_identifier = &module_binding }, - }, - }; - cjs_args[1] = js_ast.G.Arg{ - .binding = js_ast.Binding{ - .loc = logger.Loc.Empty, - .data = .{ .b_identifier = &exports_binding }, + var properties: [1]js_ast.G.Property = [_]js_ast.G.Property{ + .{ + .key = module_path_str, + .value = Expr{ .loc = logger.Loc.Empty, .data = .{ .e_arrow = &closure } }, }, }; - var properties: [1]js_ast.G.Property = undefined; var e_object = E.Object{ .properties = js_ast.G.Property.List.init(&properties), }; - const module_path_str = js_ast.Expr{ .data = .{ .e_string = &package_path }, .loc = logger.Loc.Empty }; - properties[0] = js_ast.G.Property{ - .key = module_path_str, - .value = Expr{ .loc = logger.Loc.Empty, .data = .{ .e_arrow = &closure } }, - }; - // if (!ast.uses_module_ref) { // var symbol = &ast.symbols[ast.module_ref.?.innerIndex()]; // symbol.original_name = "_$$"; |