aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/module_loader.zig
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2023-05-29 11:23:10 -0700
committerGravatar GitHub <noreply@github.com> 2023-05-29 11:23:10 -0700
commite2de1f5c133ed3aac6fcea7e8e7c5fcd771d65f9 (patch)
tree219ad30067db8641999811b67744ce665f45dd46 /src/bun.js/module_loader.zig
parent2b04ef4fae088b6f39628da312643cf4c54921ad (diff)
downloadbun-e2de1f5c133ed3aac6fcea7e8e7c5fcd771d65f9.tar.gz
bun-e2de1f5c133ed3aac6fcea7e8e7c5fcd771d65f9.tar.zst
bun-e2de1f5c133ed3aac6fcea7e8e7c5fcd771d65f9.zip
Natively support CommonJS at runtime (#3104)
* Natively support CommonJS at runtime * cleanup how getters are handled, add fast path * more consistent with node * use * As * Remove thrown modules on exception * Handle exception better --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'src/bun.js/module_loader.zig')
-rw-r--r--src/bun.js/module_loader.zig31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/bun.js/module_loader.zig b/src/bun.js/module_loader.zig
index 6524c8084..635e0f4a9 100644
--- a/src/bun.js/module_loader.zig
+++ b/src/bun.js/module_loader.zig
@@ -820,11 +820,26 @@ pub const ModuleLoader = struct {
return resolved_source;
}
+ var commonjs_exports = try bun.default_allocator.alloc(ZigString, parse_result.ast.commonjs_export_names.len);
+ for (parse_result.ast.commonjs_export_names, commonjs_exports) |name, *out| {
+ out.* = ZigString.fromUTF8(name);
+ }
+
return ResolvedSource{
.allocator = null,
.source_code = ZigString.init(try default_allocator.dupe(u8, printer.ctx.getWritten())),
.specifier = ZigString.init(specifier),
.source_url = ZigString.init(path.text),
+ .commonjs_exports = if (commonjs_exports.len > 0)
+ commonjs_exports.ptr
+ else
+ null,
+ .commonjs_exports_len = if (commonjs_exports.len > 0)
+ @truncate(u32, commonjs_exports.len)
+ else if (parse_result.ast.exports_kind == .cjs)
+ std.math.maxInt(u32)
+ else
+ 0,
// // TODO: change hash to a bitfield
// .hash = 1,
@@ -943,6 +958,7 @@ pub const ModuleLoader = struct {
.virtual_source = virtual_source,
.hoist_bun_plugin = true,
.dont_bundle_twice = true,
+ .allow_commonjs = true,
.inject_jest_globals = jsc_vm.bundler.options.rewrite_jest_for_tests and
jsc_vm.main.len == path.text.len and
jsc_vm.main_hash == hash and
@@ -1146,11 +1162,26 @@ pub const ModuleLoader = struct {
return resolved_source;
}
+ var commonjs_exports = try bun.default_allocator.alloc(ZigString, parse_result.ast.commonjs_export_names.len);
+ for (parse_result.ast.commonjs_export_names, commonjs_exports) |name, *out| {
+ out.* = ZigString.fromUTF8(name);
+ }
+
return .{
.allocator = null,
.source_code = ZigString.init(try default_allocator.dupe(u8, printer.ctx.getWritten())),
.specifier = ZigString.init(display_specifier),
.source_url = ZigString.init(path.text),
+ .commonjs_exports = if (commonjs_exports.len > 0)
+ commonjs_exports.ptr
+ else
+ null,
+ .commonjs_exports_len = if (commonjs_exports.len > 0)
+ @truncate(u32, commonjs_exports.len)
+ else if (parse_result.ast.exports_kind == .cjs)
+ std.math.maxInt(u32)
+ else
+ 0,
// // TODO: change hash to a bitfield
// .hash = 1,