mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 18:38:55 +00:00
WIP
Former-commit-id: 60cc85dc0652b34e9c7ec409f32ba635cc4b2e51
This commit is contained in:
@@ -15,111 +15,7 @@ const http = @import("../../http.zig");
|
||||
usingnamespace @import("./node_env_buf_map.zig");
|
||||
usingnamespace @import("./base.zig");
|
||||
usingnamespace @import("./webcore/response.zig");
|
||||
|
||||
const DefaultSpeedyDefines = struct {
|
||||
pub const Keys = struct {
|
||||
const window = "window";
|
||||
};
|
||||
pub const Values = struct {
|
||||
const window = "undefined";
|
||||
};
|
||||
};
|
||||
|
||||
pub fn configureTransformOptionsForSpeedy(allocator: *std.mem.Allocator, _args: Api.TransformOptions) !Api.TransformOptions {
|
||||
var args = _args;
|
||||
|
||||
args.platform = Api.Platform.speedy;
|
||||
args.serve = false;
|
||||
args.write = false;
|
||||
args.resolve = Api.ResolveMode.lazy;
|
||||
args.generate_node_module_bundle = false;
|
||||
|
||||
// We inline process.env.* at bundle time but process.env is a proxy object which will otherwise return undefined.
|
||||
|
||||
var env_map = try getNodeEnvMap(allocator);
|
||||
var env_count = env_map.count();
|
||||
|
||||
if (args.define) |def| {
|
||||
for (def.keys) |key| {
|
||||
env_count += @boolToInt((env_map.get(key) == null));
|
||||
}
|
||||
}
|
||||
var needs_node_env = env_map.get("NODE_ENV") == null;
|
||||
var needs_window_undefined = true;
|
||||
|
||||
var needs_regenerate = args.define == null and env_count > 0;
|
||||
if (args.define) |def| {
|
||||
if (def.keys.len != env_count) {
|
||||
needs_regenerate = true;
|
||||
}
|
||||
for (def.keys) |key| {
|
||||
if (strings.eql(key, "process.env.NODE_ENV")) {
|
||||
needs_node_env = false;
|
||||
} else if (strings.eql(key, "window")) {
|
||||
needs_window_undefined = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var extras_count = @intCast(usize, @boolToInt(needs_node_env)) + @intCast(usize, @boolToInt(needs_window_undefined));
|
||||
|
||||
if (needs_regenerate) {
|
||||
var new_list = try allocator.alloc([]const u8, env_count * 2 + extras_count * 2);
|
||||
var keys = new_list[0 .. new_list.len / 2];
|
||||
var values = new_list[keys.len..];
|
||||
var new_map = Api.StringMap{
|
||||
.keys = keys,
|
||||
.values = values,
|
||||
};
|
||||
var iter = env_map.iterator();
|
||||
|
||||
var last: usize = 0;
|
||||
while (iter.next()) |entry| {
|
||||
keys[last] = entry.key_ptr.*;
|
||||
var value = entry.value_ptr.*;
|
||||
|
||||
if (value.len == 0 or value[0] != '"' or value[value.len - 1] != '"') {
|
||||
value = try std.fmt.allocPrint(allocator, "\"{s}\"", .{value});
|
||||
}
|
||||
values[last] = value;
|
||||
last += 1;
|
||||
}
|
||||
|
||||
if (args.define) |def| {
|
||||
var from_env = keys[0..last];
|
||||
|
||||
for (def.keys) |pre, i| {
|
||||
if (env_map.get(pre) != null) {
|
||||
for (from_env) |key, j| {
|
||||
if (strings.eql(key, pre)) {
|
||||
values[j] = def.values[i];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
keys[last] = pre;
|
||||
values[last] = def.values[i];
|
||||
last += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (needs_node_env) {
|
||||
keys[last] = options.DefaultUserDefines.NodeEnv.Key;
|
||||
values[last] = options.DefaultUserDefines.NodeEnv.Value;
|
||||
last += 1;
|
||||
}
|
||||
|
||||
if (needs_window_undefined) {
|
||||
keys[last] = DefaultSpeedyDefines.Keys.window;
|
||||
values[last] = DefaultSpeedyDefines.Values.window;
|
||||
last += 1;
|
||||
}
|
||||
|
||||
args.define = new_map;
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
usingnamespace @import("./config.zig");
|
||||
|
||||
// If you read JavascriptCore/API/JSVirtualMachine.mm - https://github.com/WebKit/WebKit/blob/acff93fb303baa670c055cb24c2bad08691a01a0/Source/JavaScriptCore/API/JSVirtualMachine.mm#L101
|
||||
// We can see that it's sort of like std.mem.Allocator but for JSGlobalContextRef, to support Automatic Reference Counting
|
||||
|
||||
Reference in New Issue
Block a user