diff options
author | 2023-06-19 22:34:22 -0700 | |
---|---|---|
committer | 2023-06-19 22:34:22 -0700 | |
commit | 7d94a49ef403750886c2e9ebfc5a7752b8ccb882 (patch) | |
tree | 33cf444dfbf380e04c45ccfb753a8e46ee6dbcd5 /src/js_parser.zig | |
parent | cbd6d24d34b78de2b28fad75df2be1b728561d93 (diff) | |
download | bun-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>
Diffstat (limited to 'src/js_parser.zig')
-rw-r--r-- | src/js_parser.zig | 37 |
1 files changed, 35 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) { |