aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js
diff options
context:
space:
mode:
authorGravatar Ai Hoshino <ambiguous404@gmail.com> 2023-08-20 16:52:17 +0800
committerGravatar GitHub <noreply@github.com> 2023-08-20 01:52:17 -0700
commit3a9a6c63ac58564d8bae126a3edfc368ca4be671 (patch)
tree4f39dec7658d733bf6dd12cc8df5749957a56c0a /src/bun.js
parent360acf5a80d7e0191d13b87226c8122a633f701f (diff)
downloadbun-3a9a6c63ac58564d8bae126a3edfc368ca4be671.tar.gz
bun-3a9a6c63ac58564d8bae126a3edfc368ca4be671.tar.zst
bun-3a9a6c63ac58564d8bae126a3edfc368ca4be671.zip
Fix(bundler): use different alias mappings based on the target. (#4163)
* Fix(bundler): use different alias mappings based on the target. Close: #3844 * chore: reduce duplicated code. * chore: split to two separate ComptimeStringMap.
Diffstat (limited to 'src/bun.js')
-rw-r--r--src/bun.js/javascript.zig2
-rw-r--r--src/bun.js/module_loader.zig60
2 files changed, 48 insertions, 14 deletions
diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig
index a74da82c8..98e61f3b0 100644
--- a/src/bun.js/javascript.zig
+++ b/src/bun.js/javascript.zig
@@ -1592,7 +1592,7 @@ pub const VirtualMachine = struct {
}
}
- if (JSC.HardcodedModule.Aliases.getWithEql(specifier, bun.String.eqlComptime)) |hardcoded| {
+ if (JSC.HardcodedModule.Aliases.getWithEql(specifier, bun.String.eqlComptime, jsc_vm.bundler.options.target)) |hardcoded| {
if (hardcoded.tag == .none) {
resolveMaybeNeedsTrailingSlash(
res,
diff --git a/src/bun.js/module_loader.zig b/src/bun.js/module_loader.zig
index 3d18b61e2..b7c5ea53e 100644
--- a/src/bun.js/module_loader.zig
+++ b/src/bun.js/module_loader.zig
@@ -484,7 +484,7 @@ pub const RuntimeTranspilerStore = struct {
for (parse_result.ast.import_records.slice()) |*import_record_| {
var import_record: *bun.ImportRecord = import_record_;
- if (JSC.HardcodedModule.Aliases.get(import_record.path.text)) |replacement| {
+ if (JSC.HardcodedModule.Aliases.get(import_record.path.text, bundler.options.target)) |replacement| {
import_record.path.text = replacement.path;
import_record.tag = replacement.tag;
continue;
@@ -2338,20 +2338,15 @@ pub const HardcodedModule = enum {
.{ "utf-8-validate", HardcodedModule.@"utf-8-validate" },
},
);
+
pub const Alias = struct {
path: string,
tag: ImportRecord.Tag = ImportRecord.Tag.hardcoded,
};
- pub const Aliases = 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" } },
+ pub const Aliases = struct {
+ // Used by both Bun and Node.
+ const common_alias_kvs = .{
.{ "node:assert", .{ .path = "node:assert" } },
.{ "node:assert/strict", .{ .path = "node:assert/strict" } },
.{ "node:async_hooks", .{ .path = "node:async_hooks" } },
@@ -2461,7 +2456,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
@@ -2486,6 +2481,15 @@ pub const HardcodedModule = enum {
// .{ "readable-stream", .{ .path = "node:stream" } },
// .{ "readable-stream/consumer", .{ .path = "node:stream/consumers" } },
// .{ "readable-stream/web", .{ .path = "node:stream/web" } },
+ };
+
+ const bun_extra_alias_kvs = .{
+ .{ "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" } },
@@ -2497,6 +2501,36 @@ pub const HardcodedModule = enum {
.{ "utf-8-validate", .{ .path = "utf-8-validate" } },
.{ "ws", .{ .path = "ws" } },
.{ "ws/lib/websocket", .{ .path = "ws" } },
- },
- );
+ };
+
+ const NodeAliases = bun.ComptimeStringMap(Alias, common_alias_kvs);
+ const BunAliases = bun.ComptimeStringMap(Alias, common_alias_kvs ++ bun_extra_alias_kvs);
+
+ pub fn has(name: []const u8, target: options.Target) bool {
+ if (target.isBun()) {
+ return BunAliases.has(name);
+ } else if (target.isNode()) {
+ return NodeAliases.has(name);
+ }
+ return false;
+ }
+
+ pub fn get(name: []const u8, target: options.Target) ?Alias {
+ if (target.isBun()) {
+ return BunAliases.get(name);
+ } else if (target.isNode()) {
+ return NodeAliases.get(name);
+ }
+ return null;
+ }
+
+ pub fn getWithEql(name: anytype, comptime eql: anytype, target: options.Target) ?Alias {
+ if (target.isBun()) {
+ return BunAliases.getWithEql(name, eql);
+ } else if (target.isNode()) {
+ return NodeAliases.getWithEql(name, eql);
+ }
+ return null;
+ }
+ };
};