aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2023-06-19 22:34:22 -0700
committerGravatar GitHub <noreply@github.com> 2023-06-19 22:34:22 -0700
commit7d94a49ef403750886c2e9ebfc5a7752b8ccb882 (patch)
tree33cf444dfbf380e04c45ccfb753a8e46ee6dbcd5
parentcbd6d24d34b78de2b28fad75df2be1b728561d93 (diff)
downloadbun-7d94a49ef403750886c2e9ebfc5a7752b8ccb882.tar.gz
bun-7d94a49ef403750886c2e9ebfc5a7752b8ccb882.tar.zst
bun-7d94a49ef403750886c2e9ebfc5a7752b8ccb882.zip
Fix bug that breaks `bunx prisma init` when node is not installed (#3362)
* tweak cjs * Handle more cases, add a test --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
-rw-r--r--src/js_parser.zig37
-rw-r--r--test/cli/run/commonjs-no-export.test.ts14
-rw-r--r--test/cli/run/commonjs-no-exports-fixture.js7
3 files changed, 56 insertions, 2 deletions
diff --git a/src/js_parser.zig b/src/js_parser.zig
index 5a9bca91a..51627a134 100644
--- a/src/js_parser.zig
+++ b/src/js_parser.zig
@@ -3425,10 +3425,43 @@ pub const Parser = struct {
.esm => {
exports_kind = .esm;
},
- else => {
- exports_kind = .esm;
+ .unknown => {
+ // Divergence from esbuild and Node.js: we default to ESM
+ // when there are no exports.
+ //
+ // However, this breaks certain packages.
+ // For example, the checkpoint-client used by
+ // Prisma does an eval("__dirname") but does not export
+ // anything.
+ //
+ // If they use an import statement, we say it's ESM because that's not allowed in CommonJS files.
+ const uses_any_import_statements = brk: {
+ for (p.import_records.items) |*import_record| {
+ if (import_record.is_internal or import_record.is_unused) continue;
+ if (import_record.kind == .stmt) break :brk true;
+ }
+
+ break :brk false;
+ };
+
+ if (uses_any_import_statements) {
+ exports_kind = .esm;
+
+ // Otherwise, if they use CommonJS features its CommonJS
+ } else if (p.symbols.items[p.require_ref.innerIndex()].use_count_estimate > 0 or uses_dirname or uses_filename) {
+ exports_kind = .cjs;
+ } else {
+ // If unknown, we default to ESM
+ exports_kind = .esm;
+ }
},
}
+
+ if (exports_kind == .cjs and p.options.features.commonjs_at_runtime) {
+ wrapper_expr = .{
+ .bun_js = {},
+ };
+ }
}
if (exports_kind == .esm and p.commonjs_named_exports.count() > 0 and !p.unwrap_all_requires and !force_esm) {
diff --git a/test/cli/run/commonjs-no-export.test.ts b/test/cli/run/commonjs-no-export.test.ts
new file mode 100644
index 000000000..b0bedd9d6
--- /dev/null
+++ b/test/cli/run/commonjs-no-export.test.ts
@@ -0,0 +1,14 @@
+import { test, expect } from "bun:test";
+import { bunEnv, bunExe } from "harness";
+import { join } from "path";
+
+test("CommonJS entry point with no exports", () => {
+ const { stdout, exitCode } = Bun.spawnSync({
+ cmd: [bunExe(), "run", "--bun", join(import.meta.dir, "commonjs-no-exports-fixture.js")],
+ env: bunEnv,
+ stderr: "inherit",
+ });
+
+ expect(stdout.toString().trim().endsWith("--pass--")).toBe(true);
+ expect(exitCode).toBe(0);
+});
diff --git a/test/cli/run/commonjs-no-exports-fixture.js b/test/cli/run/commonjs-no-exports-fixture.js
new file mode 100644
index 000000000..12458c3de
--- /dev/null
+++ b/test/cli/run/commonjs-no-exports-fixture.js
@@ -0,0 +1,7 @@
+// see https://www.npmjs.com/package/checkpoint-client
+require("node:fs");
+
+eval("__dirname");
+eval("__filename");
+
+console.log("--pass--");