From f575d3ff246ba49720f21856e1f317ffcbbe583e Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Thu, 10 Apr 2025 14:58:46 -0700 Subject: [PATCH] Fixes #18899 (#18920) --- src/js_printer.zig | 5 +++-- test/bundler/bundler_bun.test.ts | 35 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/js_printer.zig b/src/js_printer.zig index db27f3675a..ae18f12816 100644 --- a/src/js_printer.zig +++ b/src/js_printer.zig @@ -1689,10 +1689,11 @@ fn NewPrinter( .bun => { if (record.kind == .dynamic) { p.print("Promise.resolve(globalThis.Bun)"); - } else if (record.kind == .require) { + return; + } else if (record.kind == .require or record.kind == .stmt) { p.print("globalThis.Bun"); + return; } - return; }, .bun_test => { if (record.kind == .dynamic) { diff --git a/test/bundler/bundler_bun.test.ts b/test/bundler/bundler_bun.test.ts index 61fdbf89f8..f8cd17a7f1 100644 --- a/test/bundler/bundler_bun.test.ts +++ b/test/bundler/bundler_bun.test.ts @@ -3,6 +3,41 @@ import { describe, expect } from "bun:test"; import { itBundled } from "./expectBundled"; describe("bundler", () => { + // https://github.com/oven-sh/bun/issues/18899 + itBundled("bun/import-bun-format-cjs", { + target: "bun", + format: "cjs", + bytecode: true, + outdir: "/out", + files: { + "/entry.ts": /* js */ ` + import {RedisClient} from 'bun'; + import * as BunStar from 'bun'; + const bunRequire = require("bun"); + if (RedisClient.name !== "RedisClient") { + throw new Error("RedisClient.name is not RedisClient"); + } + if (BunStar.RedisClient.name !== "RedisClient") { + throw new Error("BunStar.RedisClient.name is not RedisClient"); + } + if (bunRequire.RedisClient.name !== "RedisClient") { + throw new Error("bunRequire.RedisClient.name is not RedisClient"); + } + + console.log(RedisClient.name); + console.log(BunStar.RedisClient.name); + console.log(bunRequire.RedisClient.name); + + export class RedisCache { + constructor(config: any) { + this.connectServer(config); + } + + } + `, + }, + run: { stdout: "RedisClient\nRedisClient\nRedisClient\n" }, + }); itBundled("bun/embedded-sqlite-file", { target: "bun", outfile: "",