diff --git a/src/bun.js/api/JSBundler.zig b/src/bun.js/api/JSBundler.zig index 3d652f3b45..fa9120d6f6 100644 --- a/src/bun.js/api/JSBundler.zig +++ b/src/bun.js/api/JSBundler.zig @@ -276,7 +276,7 @@ pub const JSBundler = struct { autoload_dotenv: bool = true, autoload_bunfig: bool = true, autoload_tsconfig: bool = false, - autoload_package_json: bool = false, + autoload_package_json: bool = true, pub fn fromJS(globalThis: *jsc.JSGlobalObject, config: jsc.JSValue, allocator: std.mem.Allocator, compile_target: ?CompileTarget) JSError!?CompileOptions { var this = CompileOptions{ diff --git a/src/cli.zig b/src/cli.zig index ca3ea8c083..1dbacd8da9 100644 --- a/src/cli.zig +++ b/src/cli.zig @@ -477,7 +477,7 @@ pub const Command = struct { compile_autoload_dotenv: bool = true, compile_autoload_bunfig: bool = true, compile_autoload_tsconfig: bool = false, - compile_autoload_package_json: bool = false, + compile_autoload_package_json: bool = true, compile_executable_path: ?[]const u8 = null, windows: options.WindowsOptions = .{}, }; diff --git a/test/bundler/bundler_compile.test.ts b/test/bundler/bundler_compile.test.ts index c7758f2343..bdb82cf26e 100644 --- a/test/bundler/bundler_compile.test.ts +++ b/test/bundler/bundler_compile.test.ts @@ -564,6 +564,54 @@ describe("bundler", () => { }, compile: true, }); + // https://github.com/oven-sh/bun/issues/27058 + itBundled("compile/DynamicRequireWithPackageJsonMain", { + files: { + "/entry.tsx": /* tsx */ ` + const req = (x) => require(x); + const result = req('pkg-with-main'); + console.log(JSON.stringify(result)); + `, + "/node_modules/pkg-with-main/package.json": JSON.stringify({ name: "pkg-with-main", main: "lib/index.js" }), + "/node_modules/pkg-with-main/lib/index.js": "throw new Error('Must be runtime import.')", + }, + runtimeFiles: { + "/node_modules/pkg-with-main/package.json": JSON.stringify({ name: "pkg-with-main", main: "lib/index.js" }), + "/node_modules/pkg-with-main/lib/index.js": `module.exports = { hello: "world" };`, + }, + run: { + stdout: '{"hello":"world"}', + setCwd: true, + }, + compile: true, + }); + // https://github.com/oven-sh/bun/issues/27058 + itBundled("compile/DynamicRequireWithPackageJsonExports", { + files: { + "/entry.tsx": /* tsx */ ` + const req = (x) => require(x); + const result = req('pkg-with-exports'); + console.log(JSON.stringify(result)); + `, + "/node_modules/pkg-with-exports/package.json": JSON.stringify({ + name: "pkg-with-exports", + exports: { ".": "./src/entry.js" }, + }), + "/node_modules/pkg-with-exports/src/entry.js": "throw new Error('Must be runtime import.')", + }, + runtimeFiles: { + "/node_modules/pkg-with-exports/package.json": JSON.stringify({ + name: "pkg-with-exports", + exports: { ".": "./src/entry.js" }, + }), + "/node_modules/pkg-with-exports/src/entry.js": `module.exports = { status: "ok" };`, + }, + run: { + stdout: '{"status":"ok"}', + setCwd: true, + }, + compile: true, + }); // see comment in `usePackageManager` for why this is a test itBundled("compile/NoAutoInstall", { files: {