diff options
| author | 2021-08-14 21:28:13 -0700 | |
|---|---|---|
| committer | 2021-08-14 21:28:13 -0700 | |
| commit | d95ffe63023c09ea792d4f0379374f7c6c7975e6 (patch) | |
| tree | 6dfb29fd239d81fbca04182b718376efb08721dc /src/resolver | |
| parent | 76bb26de8302f4dd1c833496cb7b5f2f4e808ce8 (diff) | |
| download | bun-d95ffe63023c09ea792d4f0379374f7c6c7975e6.tar.gz bun-d95ffe63023c09ea792d4f0379374f7c6c7975e6.tar.zst bun-d95ffe63023c09ea792d4f0379374f7c6c7975e6.zip | |
.env works
Former-commit-id: 45ee7874f2c8566c3034743161031b0859423f41
Diffstat (limited to '')
| -rw-r--r-- | src/resolver/package_json.zig | 142 | ||||
| -rw-r--r-- | src/resolver/resolver.zig | 3 |
2 files changed, 96 insertions, 49 deletions
diff --git a/src/resolver/package_json.zig b/src/resolver/package_json.zig index 160f748fd..3191f0a59 100644 --- a/src/resolver/package_json.zig +++ b/src/resolver/package_json.zig @@ -61,7 +61,71 @@ pub const PackageJSON = struct { // browser_map: BrowserMap, - fn loadFrameworkExpression(framework: *options.Framework, json: js_ast.Expr, allocator: *std.mem.Allocator) bool { + fn loadDefineDefaults( + env: *options.Env, + json: *const js_ast.E.Object, + allocator: *std.mem.Allocator, + ) !void { + var valid_count: usize = 0; + for (json.properties) |prop| { + if (prop.value.?.data != .e_string) continue; + valid_count += 1; + } + + env.defaults.shrinkRetainingCapacity(0); + env.defaults.ensureTotalCapacity(allocator, valid_count) catch {}; + + for (json.properties) |prop| { + if (prop.value.?.data != .e_string) continue; + env.defaults.appendAssumeCapacity(.{ + .key = prop.key.?.data.e_string.string(allocator) catch unreachable, + .value = prop.value.?.data.e_string.string(allocator) catch unreachable, + }); + } + } + + fn loadDefineExpression( + env: *options.Env, + json: *const js_ast.E.Object, + allocator: *std.mem.Allocator, + ) anyerror!void { + for (json.properties) |prop| { + switch (prop.key.?.data) { + .e_string => |e_str| { + const str = e_str.string(allocator) catch ""; + + if (strings.eqlComptime(str, "defaults")) { + switch (prop.value.?.data) { + .e_object => |obj| { + try loadDefineDefaults(env, obj, allocator); + }, + else => { + env.defaults.shrinkRetainingCapacity(0); + }, + } + } else if (strings.eqlComptime(str, ".env")) { + switch (prop.value.?.data) { + .e_string => |value_str| { + env.setBehaviorFromPrefix(value_str.string(allocator) catch ""); + }, + else => { + env.behavior = .disable; + env.prefix = ""; + }, + } + } + }, + else => continue, + } + } + } + + fn loadFrameworkExpression( + framework: *options.Framework, + json: js_ast.Expr, + allocator: *std.mem.Allocator, + comptime read_define: bool, + ) bool { if (json.asProperty("client")) |client| { if (client.expr.asString(allocator)) |str| { if (str.len > 0) { @@ -70,55 +134,30 @@ pub const PackageJSON = struct { } } - // "env": { - // "client": { - // "NEXT_TRAILING_SLASH": false, - // }, - // "clientPrefix": "NEXT_PUBLIC_", - // "server": { - // "NEXT_TRAILING_SLASH": false, - // }, - // "serverPrefix": "", - // } - - if (json.asProperty("env")) |defines| { - if (defines.expr.asProperty("client")) |client| { - if (client.expr.data == .e_object) { - const object = client.expr.data.e_object; - var i: usize = 0; - for (object.properties) |prop| { - // must be strings - const key = prop.key orelse continue; - const value = prop.value orelse continue; - i += @intCast(usize, @boolToInt(key.data == .e_string and value.data == .e_string)); - } - - if (i > 0) { - if (framework.client_env == null) { - framework.client_env = options.Framework.Env.init(allocator, ""); - } - var env = &framework.client_env.?; - try env.defaults.ensureUnusedCapacity(i); + if (comptime read_define) { + if (json.asProperty("define")) |defines| { + if (defines.expr.asProperty("client")) |client| { + if (client.expr.data == .e_object) { + const object = client.expr.data.e_object; + framework.client_env = options.Env.init( + allocator, + ); - for (object.properties) |prop| { - // must be strings - // not for any good reason. - // we should fix this later + loadDefineExpression(&framework.client_env, object, allocator) catch {}; + } + } - const key = prop.key orelse continue; - const value = prop.value orelse continue; - if (key.data != .e_string or value.data != .e_string) continue; + if (defines.expr.asProperty("server")) |server| { + if (server.expr.data == .e_object) { + const object = server.expr.data.e_object; + framework.server_env = options.Env.init( + allocator, + ); - var res = try define.getOrPut(try key.asString(allocator)); - if (!res.found_existing) { - res.value_ptr.* = try value.asString(allocator); - } - } + loadDefineExpression(&framework.server_env, object, allocator) catch {}; } } } - - if (defines.expr.asProperty("server")) |server| {} } if (json.asProperty("server")) |server| { @@ -132,7 +171,14 @@ pub const PackageJSON = struct { return framework.client.len > 0; } - pub fn loadFrameworkWithPreference(package_json: *const PackageJSON, pair: *FrameworkRouterPair, json: js_ast.Expr, allocator: *std.mem.Allocator, comptime load_framework: LoadFramework) void { + pub fn loadFrameworkWithPreference( + package_json: *const PackageJSON, + pair: *FrameworkRouterPair, + json: js_ast.Expr, + allocator: *std.mem.Allocator, + comptime read_defines: bool, + comptime load_framework: LoadFramework, + ) void { const framework_object = json.asProperty("framework") orelse return; if (framework_object.expr.asProperty("static")) |static_prop| { @@ -197,7 +243,7 @@ pub const PackageJSON = struct { switch (comptime load_framework) { .development => { if (framework_object.expr.asProperty("development")) |env| { - if (loadFrameworkExpression(pair.framework, env.expr, allocator)) { + if (loadFrameworkExpression(pair.framework, env.expr, allocator, read_defines)) { pair.framework.package = package_json.name; pair.framework.development = true; if (env.expr.asProperty("static")) |static_prop| { @@ -215,7 +261,7 @@ pub const PackageJSON = struct { }, .production => { if (framework_object.expr.asProperty("production")) |env| { - if (loadFrameworkExpression(pair.framework, env.expr, allocator)) { + if (loadFrameworkExpression(pair.framework, env.expr, allocator, read_defines)) { pair.framework.package = package_json.name; pair.framework.development = false; @@ -235,7 +281,7 @@ pub const PackageJSON = struct { else => unreachable, } - if (loadFrameworkExpression(pair.framework, framework_object.expr, allocator)) { + if (loadFrameworkExpression(pair.framework, framework_object.expr, allocator, read_defines)) { pair.framework.package = package_json.name; pair.framework.development = false; } diff --git a/src/resolver/resolver.zig b/src/resolver/resolver.zig index 0c97ea6c8..9ff5a4eab 100644 --- a/src/resolver/resolver.zig +++ b/src/resolver/resolver.zig @@ -376,6 +376,7 @@ pub fn NewResolver(cache_files: bool) type { package: string, pair: *PackageJSON.FrameworkRouterPair, comptime preference: PackageJSON.LoadFramework, + comptime load_defines: bool, ) !void { // TODO: make this only parse package.json once @@ -384,7 +385,7 @@ pub fn NewResolver(cache_files: bool) type { const pkg: *const PackageJSON = result.package_json orelse r.packageJSONForResolvedNodeModuleWithIgnoreMissingName(&result, true) orelse return error.MissingPackageJSON; const json: Expr = (try r.caches.json.parseJSON(r.log, pkg.source, r.allocator)) orelse return error.JSONParseError; - pkg.loadFrameworkWithPreference(pair, json, r.allocator, preference); + pkg.loadFrameworkWithPreference(pair, json, r.allocator, load_defines, preference); const dir = pkg.source.path.name.dirWithTrailingSlash(); var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; if (pair.framework.client.len > 0) { |
