aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/module_loader.zig
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2023-09-10 22:15:35 -0800
committerGravatar GitHub <noreply@github.com> 2023-09-10 23:15:35 -0700
commit51d3d4382281f789f8175079ed426a63529eb3e7 (patch)
tree14f6fe77a1e3b300488e9343d8e9d54f64bde376 /src/bun.js/module_loader.zig
parentedea4f095a3bebf54f986c0fa038482316f4cde8 (diff)
downloadbun-51d3d4382281f789f8175079ed426a63529eb3e7.tar.gz
bun-51d3d4382281f789f8175079ed426a63529eb3e7.tar.zst
bun-51d3d4382281f789f8175079ed426a63529eb3e7.zip
Support named imports for json & toml files at runtime (#4783)
* Support named exports in json imports * Support named imports for `*.json` files * Remove stale comments * Don't export arrays as non-default * Add test for default exports * Don't break webpack --------- 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.zig75
1 files changed, 39 insertions, 36 deletions
diff --git a/src/bun.js/module_loader.zig b/src/bun.js/module_loader.zig
index f5e5cde72..cf86cb460 100644
--- a/src/bun.js/module_loader.zig
+++ b/src/bun.js/module_loader.zig
@@ -482,10 +482,6 @@ pub const RuntimeTranspilerStore = struct {
.source_code = bun.String.createLatin1(parse_result.source.contents),
.specifier = String.create(specifier),
.source_url = ZigString.init(path.text),
- // // TODO: change hash to a bitfield
- // .hash = 1,
-
- // having JSC own the memory causes crashes
.hash = 0,
};
return;
@@ -1290,10 +1286,7 @@ pub const ModuleLoader = struct {
std.math.maxInt(u32)
else
0,
- // // TODO: change hash to a bitfield
- // .hash = 1,
- // having JSC own the memory causes crashes
.hash = 0,
};
}
@@ -1466,31 +1459,36 @@ pub const ModuleLoader = struct {
}
}
- var parse_result = jsc_vm.bundler.parseMaybeReturnFileOnly(
- parse_options,
- null,
- disable_transpilying,
- ) orelse {
- if (comptime !disable_transpilying) {
- if (jsc_vm.isWatcherEnabled()) {
- if (input_file_fd != 0) {
- if (jsc_vm.bun_watcher != null and !is_node_override and std.fs.path.isAbsolute(path.text) and !strings.contains(path.text, "node_modules")) {
- should_close_input_file_fd = false;
- jsc_vm.bun_watcher.?.addFile(
- input_file_fd,
- path.text,
- hash,
- loader,
- 0,
- package_json,
- true,
- ) catch {};
+ var parse_result = switch (disable_transpilying or
+ (loader == .json and !path.isJSONCFile())) {
+ inline else => |return_file_only| brk: {
+ break :brk jsc_vm.bundler.parseMaybeReturnFileOnly(
+ parse_options,
+ null,
+ return_file_only,
+ ) orelse {
+ if (comptime !disable_transpilying) {
+ if (jsc_vm.isWatcherEnabled()) {
+ if (input_file_fd != 0) {
+ if (jsc_vm.bun_watcher != null and !is_node_override and std.fs.path.isAbsolute(path.text) and !strings.contains(path.text, "node_modules")) {
+ should_close_input_file_fd = false;
+ jsc_vm.bun_watcher.?.addFile(
+ input_file_fd,
+ path.text,
+ hash,
+ loader,
+ 0,
+ package_json,
+ true,
+ ) catch {};
+ }
+ }
}
}
- }
- }
- return error.ParseError;
+ return error.ParseError;
+ };
+ },
};
if (parse_result.loader == .wasm) {
@@ -1535,6 +1533,18 @@ pub const ModuleLoader = struct {
return error.ParseError;
}
+ if (loader == .json and !path.isJSONCFile()) {
+ return ResolvedSource{
+ .allocator = null,
+ .source_code = bun.String.create(parse_result.source.contents),
+ .specifier = input_specifier,
+ .source_url = ZigString.init(path.text),
+
+ .hash = 0,
+ .tag = ResolvedSource.Tag.json_for_object_loader,
+ };
+ }
+
if (comptime disable_transpilying) {
return ResolvedSource{
.allocator = null,
@@ -1555,10 +1565,7 @@ pub const ModuleLoader = struct {
.source_code = bun.String.createLatin1(parse_result.source.contents),
.specifier = input_specifier,
.source_url = ZigString.init(path.text),
- // // TODO: change hash to a bitfield
- // .hash = 1,
- // having JSC own the memory causes crashes
.hash = 0,
};
}
@@ -1692,10 +1699,6 @@ pub const ModuleLoader = struct {
std.math.maxInt(u32)
else
0,
- // // TODO: change hash to a bitfield
- // .hash = 1,
-
- // having JSC own the memory causes crashes
.hash = 0,
.tag = tag,