diff options
-rw-r--r-- | src/bun.js/javascript.zig | 2 | ||||
-rw-r--r-- | src/bun.js/module_loader.zig | 60 | ||||
-rw-r--r-- | src/bundler/bundle_v2.zig | 2 | ||||
-rw-r--r-- | src/linker.zig | 2 | ||||
-rw-r--r-- | src/options.zig | 7 | ||||
-rw-r--r-- | src/resolver/resolver.zig | 4 | ||||
-rw-r--r-- | test/bundler/bun-build-api.test.ts | 28 | ||||
-rw-r--r-- | test/bundler/fixtures/trivial/bundle-ws.ts | 3 |
8 files changed, 90 insertions, 18 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; + } + }; }; diff --git a/src/bundler/bundle_v2.zig b/src/bundler/bundle_v2.zig index b682b622f..092c4d8e0 100644 --- a/src/bundler/bundle_v2.zig +++ b/src/bundler/bundle_v2.zig @@ -1854,7 +1854,7 @@ pub const BundleV2 = struct { } if (ast.target.isBun()) { - if (JSC.HardcodedModule.Aliases.get(import_record.path.text)) |replacement| { + if (JSC.HardcodedModule.Aliases.get(import_record.path.text, options.Target.bun)) |replacement| { import_record.path.text = replacement.path; import_record.tag = replacement.tag; import_record.source_index = Index.invalid; diff --git a/src/linker.zig b/src/linker.zig index d3d620f4b..7cf9b9ea1 100644 --- a/src/linker.zig +++ b/src/linker.zig @@ -247,7 +247,7 @@ pub const Linker = struct { } if (comptime is_bun) { - if (JSC.HardcodedModule.Aliases.get(import_record.path.text)) |replacement| { + if (JSC.HardcodedModule.Aliases.get(import_record.path.text, linker.options.target)) |replacement| { import_record.path.text = replacement.path; import_record.tag = replacement.tag; if (replacement.tag != .none) { diff --git a/src/options.zig b/src/options.zig index 1b8e167fc..51ee27dab 100644 --- a/src/options.zig +++ b/src/options.zig @@ -467,6 +467,13 @@ pub const Target = enum { }; } + pub inline fn isNode(this: Target) bool { + return switch (this) { + .node => true, + else => false, + }; + } + pub inline fn supportsBrowserField(this: Target) bool { return switch (this) { .browser => true, diff --git a/src/resolver/resolver.zig b/src/resolver/resolver.zig index fa7fd7d16..41b329734 100644 --- a/src/resolver/resolver.zig +++ b/src/resolver/resolver.zig @@ -588,7 +588,7 @@ pub const Resolver = struct { return true; } - if (bun.JSC.HardcodedModule.Aliases.has(import_path)) { + if (bun.JSC.HardcodedModule.Aliases.has(import_path, r.opts.target)) { return true; } } @@ -1273,7 +1273,7 @@ pub const Resolver = struct { // "fs" // "fs/*" // These are disabled! - } else if (had_node_prefix and !JSC.HardcodedModule.Aliases.has(import_path_without_node_prefix)) { + } else if (had_node_prefix and !JSC.HardcodedModule.Aliases.has(import_path_without_node_prefix, r.opts.target)) { return .{ .not_found = {} }; } else if (had_node_prefix or (strings.hasPrefixComptime(import_path_without_node_prefix, "fs") and diff --git a/test/bundler/bun-build-api.test.ts b/test/bundler/bun-build-api.test.ts index 3b528b7be..3c2308d94 100644 --- a/test/bundler/bun-build-api.test.ts +++ b/test/bundler/bun-build-api.test.ts @@ -262,4 +262,32 @@ describe("Bun.build", () => { expect(x.logs[0].name).toBe("BuildMessage"); expect(x.logs[0].position).toBeTruthy(); }); + + test("test bun target", async () => { + const x = await Bun.build({ + entrypoints: [join(import.meta.dir, "./fixtures/trivial/bundle-ws.ts")], + target: "bun", + }); + expect(x.success).toBe(true); + const [blob] = x.outputs; + const content = await blob.text(); + + // use bun's ws + expect(content).toContain('import {WebSocket} from "ws"'); + expect(content).not.toContain("var websocket = __toESM(require_websocket(), 1);"); + }); + + test("test node target, issue #3844", async () => { + const x = await Bun.build({ + entrypoints: [join(import.meta.dir, "./fixtures/trivial/bundle-ws.ts")], + target: "node", + }); + expect(x.success).toBe(true); + const [blob] = x.outputs; + const content = await blob.text(); + + expect(content).not.toContain('import {WebSocket} from "ws"'); + // depends on the ws package in the test/node_modules. + expect(content).toContain("var websocket = __toESM(require_websocket(), 1);"); + }); }); diff --git a/test/bundler/fixtures/trivial/bundle-ws.ts b/test/bundler/fixtures/trivial/bundle-ws.ts new file mode 100644 index 000000000..0064bfdd9 --- /dev/null +++ b/test/bundler/fixtures/trivial/bundle-ws.ts @@ -0,0 +1,3 @@ +import { WebSocket } from "ws"; + +console.log(WebSocket); |