Skip to content

Commit

Permalink
chore: reduce duplicated code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanaasagi committed Aug 20, 2023
1 parent 831d480 commit 00d64f8
Showing 1 changed file with 25 additions and 147 deletions.
172 changes: 25 additions & 147 deletions src/bun.js/module_loader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2345,16 +2345,10 @@ pub const HardcodedModule = enum {
};

pub const Aliases = struct {
const BunAliases = bun.ComptimeStringMap(
// Used by both Bun and Node.
const CommonAliases = bun.ComptimeStringMap(
Alias,
.{
.{ "bun", .{ .path = "bun", .tag = .bun } },
.{ "bun:ffi", .{ .path = "bun:ffi" } },
.{ "bun:jsc", .{ .path = "bun:jsc" } },
.{ "bun:sqlite", .{ .path = "bun:sqlite" } },
.{ "bun:wrap", .{ .path = "bun:wrap" } },
.{ "ffi", .{ .path = "bun:ffi" } },

.{ "node:assert", .{ .path = "node:assert" } },
.{ "node:assert/strict", .{ .path = "node:assert/strict" } },
.{ "node:async_hooks", .{ .path = "node:async_hooks" } },
Expand Down Expand Up @@ -2464,7 +2458,7 @@ pub const HardcodedModule = enum {
// It implements the same interface
.{ "sys", .{ .path = "node:util" } },
.{ "node:sys", .{ .path = "node:util" } },
.{ "inspector/promises", .{ .path = "node:inspector" } },
// .{ "inspector/promises", .{ .path = "node:inspector" } },
.{ "node:inspector/promises", .{ .path = "node:inspector" } },

// These are returned in builtinModules, but probably not many packages use them
Expand All @@ -2489,6 +2483,18 @@ pub const HardcodedModule = enum {
// .{ "readable-stream", .{ .path = "node:stream" } },
// .{ "readable-stream/consumer", .{ .path = "node:stream/consumers" } },
// .{ "readable-stream/web", .{ .path = "node:stream/web" } },
},
);

const BunAliases = bun.ComptimeStringMap(
Alias,
.{
.{ "bun", .{ .path = "bun", .tag = .bun } },
.{ "bun:ffi", .{ .path = "bun:ffi" } },
.{ "bun:jsc", .{ .path = "bun:jsc" } },
.{ "bun:sqlite", .{ .path = "bun:sqlite" } },
.{ "bun:wrap", .{ .path = "bun:wrap" } },
.{ "ffi", .{ .path = "bun:ffi" } },

// Thirdparty packages we override
.{ "@vercel/fetch", .{ .path = "@vercel/fetch" } },
Expand All @@ -2503,163 +2509,35 @@ pub const HardcodedModule = enum {
},
);

const NodeAliases = bun.ComptimeStringMap(
Alias,
.{
.{ "node:assert", .{ .path = "node:assert" } },
.{ "node:assert/strict", .{ .path = "node:assert/strict" } },
.{ "node:async_hooks", .{ .path = "node:async_hooks" } },
.{ "node:buffer", .{ .path = "node:buffer" } },
.{ "node:child_process", .{ .path = "node:child_process" } },
.{ "node:cluster", .{ .path = "node:cluster" } },
.{ "node:console", .{ .path = "node:console" } },
.{ "node:constants", .{ .path = "node:constants" } },
.{ "node:crypto", .{ .path = "node:crypto" } },
.{ "node:dgram", .{ .path = "node:dgram" } },
.{ "node:diagnostics_channel", .{ .path = "node:diagnostics_channel" } },
.{ "node:dns", .{ .path = "node:dns" } },
.{ "node:dns/promises", .{ .path = "node:dns/promises" } },
.{ "node:domain", .{ .path = "node:domain" } },
.{ "node:events", .{ .path = "node:events" } },
.{ "node:fs", .{ .path = "node:fs" } },
.{ "node:fs/promises", .{ .path = "node:fs/promises" } },
.{ "node:http", .{ .path = "node:http" } },
.{ "node:http2", .{ .path = "node:http2" } },
.{ "node:https", .{ .path = "node:https" } },
.{ "node:inspector", .{ .path = "node:inspector" } },
.{ "node:module", .{ .path = "node:module" } },
.{ "node:net", .{ .path = "node:net" } },
.{ "node:os", .{ .path = "node:os" } },
.{ "node:path", .{ .path = "node:path" } },
.{ "node:path/posix", .{ .path = "node:path/posix" } },
.{ "node:path/win32", .{ .path = "node:path/win32" } },
.{ "node:perf_hooks", .{ .path = "node:perf_hooks" } },
.{ "node:process", .{ .path = "node:process" } },
.{ "node:punycode", .{ .path = "node:punycode" } },
.{ "node:querystring", .{ .path = "node:querystring" } },
.{ "node:readline", .{ .path = "node:readline" } },
.{ "node:readline/promises", .{ .path = "node:readline/promises" } },
.{ "node:repl", .{ .path = "node:repl" } },
.{ "node:stream", .{ .path = "node:stream" } },
.{ "node:stream/consumers", .{ .path = "node:stream/consumers" } },
.{ "node:stream/promises", .{ .path = "node:stream/promises" } },
.{ "node:stream/web", .{ .path = "node:stream/web" } },
.{ "node:string_decoder", .{ .path = "node:string_decoder" } },
.{ "node:timers", .{ .path = "node:timers" } },
.{ "node:timers/promises", .{ .path = "node:timers/promises" } },
.{ "node:tls", .{ .path = "node:tls" } },
.{ "node:trace_events", .{ .path = "node:trace_events" } },
.{ "node:tty", .{ .path = "node:tty" } },
.{ "node:url", .{ .path = "node:url" } },
.{ "node:util", .{ .path = "node:util" } },
.{ "node:util/types", .{ .path = "node:util/types" } },
.{ "node:v8", .{ .path = "node:v8" } },
.{ "node:vm", .{ .path = "node:vm" } },
.{ "node:wasi", .{ .path = "node:wasi" } },
.{ "node:worker_threads", .{ .path = "node:worker_threads" } },
.{ "node:zlib", .{ .path = "node:zlib" } },

.{ "assert", .{ .path = "node:assert" } },
.{ "assert/strict", .{ .path = "node:assert/strict" } },
.{ "async_hooks", .{ .path = "node:async_hooks" } },
.{ "buffer", .{ .path = "node:buffer" } },
.{ "child_process", .{ .path = "node:child_process" } },
.{ "cluster", .{ .path = "node:cluster" } },
.{ "console", .{ .path = "node:console" } },
.{ "constants", .{ .path = "node:constants" } },
.{ "crypto", .{ .path = "node:crypto" } },
.{ "dgram", .{ .path = "node:dgram" } },
.{ "diagnostics_channel", .{ .path = "node:diagnostics_channel" } },
.{ "dns", .{ .path = "node:dns" } },
.{ "dns/promises", .{ .path = "node:dns/promises" } },
.{ "domain", .{ .path = "node:domain" } },
.{ "events", .{ .path = "node:events" } },
.{ "fs", .{ .path = "node:fs" } },
.{ "fs/promises", .{ .path = "node:fs/promises" } },
.{ "http", .{ .path = "node:http" } },
.{ "http2", .{ .path = "node:http2" } },
.{ "https", .{ .path = "node:https" } },
.{ "inspector", .{ .path = "node:inspector" } },
.{ "module", .{ .path = "node:module" } },
.{ "net", .{ .path = "node:net" } },
.{ "os", .{ .path = "node:os" } },
.{ "path", .{ .path = "node:path" } },
.{ "path/posix", .{ .path = "node:path/posix" } },
.{ "path/win32", .{ .path = "node:path/win32" } },
.{ "perf_hooks", .{ .path = "node:perf_hooks" } },
.{ "process", .{ .path = "node:process" } },
.{ "punycode", .{ .path = "node:punycode" } },
.{ "querystring", .{ .path = "node:querystring" } },
.{ "readline", .{ .path = "node:readline" } },
.{ "readline/promises", .{ .path = "node:readline/promises" } },
.{ "repl", .{ .path = "node:repl" } },
.{ "stream", .{ .path = "node:stream" } },
.{ "stream/consumers", .{ .path = "node:stream/consumers" } },
.{ "stream/promises", .{ .path = "node:stream/promises" } },
.{ "stream/web", .{ .path = "node:stream/web" } },
.{ "string_decoder", .{ .path = "node:string_decoder" } },
.{ "timers", .{ .path = "node:timers" } },
.{ "timers/promises", .{ .path = "node:timers/promises" } },
.{ "tls", .{ .path = "node:tls" } },
.{ "trace_events", .{ .path = "node:trace_events" } },
.{ "tty", .{ .path = "node:tty" } },
.{ "url", .{ .path = "node:url" } },
.{ "util", .{ .path = "node:util" } },
.{ "util/types", .{ .path = "node:util/types" } },
.{ "v8", .{ .path = "node:v8" } },
.{ "vm", .{ .path = "node:vm" } },
.{ "wasi", .{ .path = "node:wasi" } },
.{ "worker_threads", .{ .path = "node:worker_threads" } },
.{ "zlib", .{ .path = "node:zlib" } },

// It implements the same interface
.{ "sys", .{ .path = "node:util" } },
.{ "node:sys", .{ .path = "node:util" } },
// .{ "inspector/promises", .{ .path = "node:inspector" } },
.{ "node:inspector/promises", .{ .path = "node:inspector" } },

// These are returned in builtinModules, but probably not many packages use them
// so we will just alias them.
.{ "_http_agent", .{ .path = "node:http" } },
.{ "_http_client", .{ .path = "node:http" } },
.{ "_http_common", .{ .path = "node:http" } },
.{ "_http_incoming", .{ .path = "node:http" } },
.{ "_http_outgoing", .{ .path = "node:http" } },
.{ "_http_server", .{ .path = "node:http" } },
.{ "_stream_duplex", .{ .path = "node:stream" } },
.{ "_stream_passthrough", .{ .path = "node:stream" } },
.{ "_stream_readable", .{ .path = "node:stream" } },
.{ "_stream_transform", .{ .path = "node:stream" } },
.{ "_stream_writable", .{ .path = "node:stream" } },
.{ "_stream_wrap", .{ .path = "node:stream" } },
.{ "_tls_wrap", .{ .path = "node:tls" } },
.{ "_tls_common", .{ .path = "node:tls" } },
},
);

pub fn has(name: []const u8, target: options.Target) bool {
if (target.isBun()) {
return BunAliases.has(name);
return CommonAliases.has(name) or BunAliases.has(name);
} else if (target.isNode()) {
return NodeAliases.has(name);
return CommonAliases.has(name);
}
return false;
}

pub fn get(name: []const u8, target: options.Target) ?Alias {
if (target.isBun()) {
if (CommonAliases.get(name)) |alias| {
return alias;
}
return BunAliases.get(name);
} else if (target.isNode()) {
return NodeAliases.get(name);
return CommonAliases.get(name);
}
return null;
}

pub fn getWithEql(name: anytype, comptime eql: anytype, target: options.Target) ?Alias {
if (target.isBun()) {
if (CommonAliases.getWithEql(name, eql)) |alias| {
return alias;
}
return BunAliases.getWithEql(name, eql);
} else if (target.isNode()) {
return NodeAliases.getWithEql(name, eql);
return CommonAliases.getWithEql(name, eql);
}
return null;
}
Expand Down

0 comments on commit 00d64f8

Please sign in to comment.