aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/bun.zig33
-rw-r--r--src/bunfig.zig2
-rw-r--r--src/cli.zig15
-rw-r--r--src/cli/colon_list_type.zig24
-rw-r--r--src/options.zig44
-rw-r--r--test/js/bun/plugin/plugins.test.ts9
6 files changed, 68 insertions, 59 deletions
diff --git a/src/bun.zig b/src/bun.zig
index 9fd6102e8..256b96688 100644
--- a/src/bun.zig
+++ b/src/bun.zig
@@ -40,6 +40,39 @@ pub const fmt = struct {
};
}
+ pub fn EnumTagListFormatter(comptime Enum: type, comptime Separator: @Type(.EnumLiteral)) type {
+ return struct {
+ pretty: bool = true,
+ const output = brk: {
+ var text: []const u8 = "";
+ const names = std.meta.fieldNames(Enum);
+ inline for (names, 0..) |name, i| {
+ if (Separator == .list) {
+ if (i > 0) {
+ if (i + 1 == names.len) {
+ text = text ++ ", or ";
+ } else {
+ text = text ++ ", ";
+ }
+ }
+
+ text = text ++ "\"" ++ name ++ "\"";
+ } else if (Separator == .dash) {
+ text = text ++ "\n- " ++ name;
+ }
+ }
+ break :brk text;
+ };
+ pub fn format(_: @This(), comptime _: []const u8, _: fmt.FormatOptions, writer: anytype) !void {
+ try writer.writeAll(output);
+ }
+ };
+ }
+
+ pub fn enumTagList(comptime Enum: type, comptime separator: @Type(.EnumLiteral)) EnumTagListFormatter(Enum, separator) {
+ return EnumTagListFormatter(Enum, separator){};
+ }
+
pub fn formatIp(address: std.net.Address, into: []u8) ![]u8 {
// std.net.Address.format includes `:<port>` and square brackets (IPv6)
// while Node does neither. This uses format then strips these to bring
diff --git a/src/bunfig.zig b/src/bunfig.zig
index fc6a5e773..ef7cfba4d 100644
--- a/src/bunfig.zig
+++ b/src/bunfig.zig
@@ -620,7 +620,7 @@ pub const Bunfig = struct {
var key = item.key.?.asString(allocator).?;
if (key.len == 0) continue;
if (key[0] != '.') {
- try this.addError(item.key.?.loc, "file extension must start with a dot");
+ try this.addError(item.key.?.loc, "file extension for loader must start with a '.'");
}
var value = item.value.?;
try this.expect(value, .e_string);
diff --git a/src/cli.zig b/src/cli.zig
index 53b421282..3f796e798 100644
--- a/src/cli.zig
+++ b/src/cli.zig
@@ -91,19 +91,8 @@ fn invalidTarget(diag: *clap.Diagnostic, _target: []const u8) noreturn {
}
pub const Arguments = struct {
pub fn loader_resolver(in: string) !Api.Loader {
- const Matcher = strings.ExactSizeMatcher(4);
- switch (Matcher.match(in)) {
- Matcher.case("jsx") => return Api.Loader.jsx,
- Matcher.case("js") => return Api.Loader.js,
- Matcher.case("ts") => return Api.Loader.ts,
- Matcher.case("tsx") => return Api.Loader.tsx,
- Matcher.case("css") => return Api.Loader.css,
- Matcher.case("file") => return Api.Loader.file,
- Matcher.case("json") => return Api.Loader.json,
- else => {
- return error.InvalidLoader;
- },
- }
+ const option_loader = options.Loader.fromString(in) orelse return error.InvalidLoader;
+ return option_loader.toAPI();
}
pub fn noop_resolver(in: string) !string {
diff --git a/src/cli/colon_list_type.zig b/src/cli/colon_list_type.zig
index 9f94a1b4c..dfb47377a 100644
--- a/src/cli/colon_list_type.zig
+++ b/src/cli/colon_list_type.zig
@@ -31,14 +31,34 @@ pub fn ColonListType(comptime t: type, comptime value_resolver: anytype) type {
return error.InvalidSeparator;
}
+ if (comptime t == bun.Schema.Api.Loader) {
+ if (str[0..midpoint].len > 0 and str[0] != '.') {
+ Output.prettyErrorln("<r><red>error<r><d>:<r> <b>file extension must start with a '.'<r> <d>(while mapping loader {s})<r>", .{bun.fmt.quote(str)});
+ Global.exit(1);
+ }
+ }
+
self.keys[i] = str[0..midpoint];
- self.values[i] = try value_resolver(str[midpoint + 1 .. str.len]);
+ self.values[i] = value_resolver(str[midpoint + 1 .. str.len]) catch |err| {
+ if (err == error.InvalidLoader) {
+ Output.prettyErrorln("<r><red>error<r><d>:<r> <b>invalid loader {}<r>, expected one of:{}", .{ bun.fmt.quote(str[midpoint + 1 .. str.len]), bun.fmt.enumTagList(bun.options.Loader, .dash) });
+ Global.exit(1);
+ }
+ return err;
+ };
}
}
pub fn resolve(allocator: std.mem.Allocator, input: []const string) !@This() {
var list = try init(allocator, input.len);
- try list.load(input);
+ list.load(input) catch |err| {
+ if (err == error.InvalidSeparator) {
+ Output.prettyErrorln("<r><red>error<r><d>:<r> expected \":\" separator", .{});
+ Global.exit(1);
+ }
+
+ return err;
+ };
return list;
}
};
diff --git a/src/options.zig b/src/options.zig
index 3cb5c333c..a701cd035 100644
--- a/src/options.zig
+++ b/src/options.zig
@@ -770,12 +770,14 @@ pub const Loader = enum(u8) {
.ts => .ts,
.tsx => .tsx,
.css => .css,
+ .file => .file,
.json => .json,
.toml => .toml,
.wasm => .wasm,
.napi => .napi,
+ .base64 => .base64,
+ .dataurl => .dataurl,
.text => .text,
- else => .file,
};
}
@@ -786,10 +788,13 @@ pub const Loader = enum(u8) {
.ts => .ts,
.tsx => .tsx,
.css => .css,
+ .file => .file,
.json => .json,
.toml => .toml,
.wasm => .wasm,
.napi => .napi,
+ .base64 => .base64,
+ .dataurl => .dataurl,
.text => .text,
else => .file,
};
@@ -1217,41 +1222,8 @@ pub fn loadersFromTransformOptions(allocator: std.mem.Allocator, _loaders: ?Api.
var input_loaders = _loaders orelse std.mem.zeroes(Api.LoaderMap);
var loader_values = try allocator.alloc(Loader, input_loaders.loaders.len);
- if (target.isBun()) {
- for (loader_values, 0..) |_, i| {
- const loader = switch (input_loaders.loaders[i]) {
- .jsx => Loader.jsx,
- .js => Loader.js,
- .ts => Loader.ts,
- .css => Loader.css,
- .tsx => Loader.tsx,
- .json => Loader.json,
- .toml => Loader.toml,
- .wasm => Loader.wasm,
- .napi => Loader.napi,
- .text => Loader.text,
- else => unreachable,
- };
-
- loader_values[i] = loader;
- }
- } else {
- for (loader_values, 0..) |_, i| {
- const loader = switch (input_loaders.loaders[i]) {
- .jsx => Loader.jsx,
- .js => Loader.js,
- .ts => Loader.ts,
- .css => Loader.css,
- .tsx => Loader.tsx,
- .json => Loader.json,
- .toml => Loader.toml,
- .wasm => Loader.wasm,
- .text => Loader.text,
- else => unreachable,
- };
-
- loader_values[i] = loader;
- }
+ for (loader_values, input_loaders.loaders) |*loader, input| {
+ loader.* = Loader.fromAPI(input);
}
var loaders = try stringHashMapFromArrays(
diff --git a/test/js/bun/plugin/plugins.test.ts b/test/js/bun/plugin/plugins.test.ts
index 79213239b..54ebb15e7 100644
--- a/test/js/bun/plugin/plugins.test.ts
+++ b/test/js/bun/plugin/plugins.test.ts
@@ -307,15 +307,10 @@ describe("errors", () => {
});
it("async transpiler errors work", async () => {
- try {
+ expect(async () => {
globalThis.asyncOnLoad = `const x: string = -NaNAn../!!;`;
await import("async:fail");
throw -1;
- } catch (e: any) {
- if (e === -1) {
- throw new Error("Expected error");
- }
- expect(e.message.length > 0).toBe(true);
- }
+ }).toThrow('Cannot find package "');
});
});