aboutsummaryrefslogtreecommitdiff
path: root/src/javascript/jsc/esbuild/esbuild_plugins.zig
blob: 022634a802f2d8cc505f20e8762253990e083bea (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
126
127
128
129
130
131
132
133
134
const std = @import("std");
const Api = @import("../../../api/schema.zig").Api;
const RequestContext = @import("../../../http.zig").RequestContext;
const MimeType = @import("../../../http.zig").MimeType;
const ZigURL = @import("../../../query_string_map.zig").URL;
const HTTPClient = @import("http");
const NetworkThread = HTTPClient.NetworkThread;
const strings = @import("../../../global.zig").strings;
const string = @import("../../../global.zig").string;
const default_allocator = @import("../../../global.zig").default_allocator;
const FeatureFlags = @import("../../../global.zig").FeatureFlags;
const Path = @import("../../../fs.zig").Path;
const logger = @import("../../../logger.zig");

const JSC = @import("../../../jsc.zig");
const Bundler = @import("../../../bundler.zig").Bundler;
const Resolver = @import("../../../bundler.zig").Resolver;
const js = JSC.C;

pub const Plugin = struct {
    name: string,

    pub const Registry = struct {
        bundler: *Bundler,
        resolver: *Resolver,
        allocator: std.mem.Allocator,

        load: LoadCallback.Map,
        resolve: ResolveCallback.Map,

        pub const Class = JSC.NewClass(
            Registry,
            .{ .name = "Registry" },
            .{
                .onLoad = .{
                    .rfn = handleOnLoad,
                },
                .onResolve = .{
                    .rfn = handleOnResolve,
                },
                .onStart = .{
                    .rfn = handleOnStart,
                },
                .onEnd = .{
                    .rfn = handleOnEnd,
                },
                .resolve = .{
                    .rfn = handleResolve,
                },
            },
            .{},
        );

        pub fn addLoadCallback(this: *Registry, namespace_: string, callback: LoadCallback) void {
            var entry = this.load.getOrPut(this.allocator, namespace_) catch unreachable;
        }

        pub fn handleOnLoad(
            this: *Registry,
            ctx: js.JSContextRef,
            _: js.JSObjectRef,
            _: js.JSObjectRef,
            args_: []const js.JSValueRef,
            exception: js.ExceptionRef,
        ) js.JSValueRef {
            const args: []const JSC.JSValue = @ptrCast([*]const JSC.JSValue, args_.ptr)[0..args_.len];
            if (args.len < 2) {
                JSC.throwInvalidArguments("onLoad expects a filter object and a callback", .{}, ctx, exception);
                return null;
            }

            const object = args[0];
            if (!object.jsType().isObject()) {
                JSC.throwInvalidArguments("onLoad expects an object with \"filter\"", .{}, ctx, exception);
                return null;
            }

var namespace_slice = JSC.ZigString.init("file").toSlice(this.allocator);

            if (object.get(ctx.ptr(), "namespace")) |namespace_prop| {
namespace_slice = namespace_prop.toSlice(ctx.ptr(), this.allocator);                
            }

            if (object.get(ctx.ptr(), "filter")) |filter_prop| {
                switch (filter_prop.jsType()) {
                    JSC.JSValue.JSType.RegExpObject => {

                    },
                }
            }
        }

        pub fn noop(
            _: *Registry,
            _: js.JSContextRef,
            _: js.JSObjectRef,
            _: js.JSObjectRef,
            _: []const js.JSValueRef,
            _: js.ExceptionRef,
        ) js.JSValueRef {
            return JSC.JSValue.jsUndefined();
        }
        const handleOnResolve = noop;
        const handleOnStart = noop;
        const handleOnEnd = noop;
        const handleResolve = noop;
    };
};

pub const LoadCallback = struct {
    plugin: *Plugin,
    javascript_function: JSC.JSValue,
    filter: FilterList,

    pub const List = std.MultiArrayList(LoadCallback);
    pub const Map = std.AutoHashMapUnmanaged(u64, List);

    pub const Context = struct {
        registry: 
        log: *logger.Log,
        namespace: string,
        import_path: string,
    };
};
pub const ResolveCallback = struct {
    plugin: *Plugin,
    javascript_function: JSC.JSValue,
    filter: FilterList,

    pub const List = std.MultiArrayList(ResolveCallback);
    pub const Map =  std.AutoHashMapUnmanaged(u64, List);
};

pub const FilterList = []const JSC.JSValue;