aboutsummaryrefslogtreecommitdiff
path: root/src/javascript/jsc/config.zig
blob: edc6f342d0d2477a7fd437b269227e5a46150766 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
usingnamespace @import("../../global.zig");
const std = @import("std");

const Fs = @import("../../fs.zig");
const resolver = @import("../../resolver/resolver.zig");
const ast = @import("../../import_record.zig");
const NodeModuleBundle = @import("../../node_module_bundle.zig").NodeModuleBundle;
const logger = @import("../../logger.zig");
const Api = @import("../../api/schema.zig").Api;
const options = @import("../../options.zig");
const Bundler = @import("../../bundler.zig").ServeBundler;
const js_printer = @import("../../js_printer.zig");
const hash_map = @import("../../hash_map.zig");
const http = @import("../../http.zig");

usingnamespace @import("./node_env_buf_map.zig");

pub const DefaultSpeedyDefines = struct {
    pub const Keys = struct {
        const window = "window";
    };
    pub const Values = struct {
        const window = "undefined";
    };
};

pub fn configureTransformOptionsForSpeedyVM(allocator: *std.mem.Allocator, _args: Api.TransformOptions) !Api.TransformOptions {
    var args = _args;

    // args.serve = false;
    args.write = false;
    args.resolve = Api.ResolveMode.lazy;
    args.generate_node_module_bundle = false;
    return try configureTransformOptionsForSpeedy(allocator, args);
}

pub fn configureTransformOptionsForSpeedy(allocator: *std.mem.Allocator, _args: Api.TransformOptions) !Api.TransformOptions {
    var args = _args;
    args.platform = Api.Platform.speedy;
    // 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;
}