aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/bundler.zig2
-rw-r--r--src/options.zig49
2 files changed, 44 insertions, 7 deletions
diff --git a/src/bundler.zig b/src/bundler.zig
index 3050ac970..04a3725f9 100644
--- a/src/bundler.zig
+++ b/src/bundler.zig
@@ -125,7 +125,7 @@ pub const Transformer = struct {
);
var use_default_loaders = loader_map.count() == 0;
- var jsx = if (opts.jsx) |_jsx| options.JSX.Pragma.fromApi(_jsx) else options.JSX.Pragma{};
+ var jsx = if (opts.jsx) |_jsx| try options.JSX.Pragma.fromApi(_jsx, allocator) else options.JSX.Pragma{};
var output_i: usize = 0;
var chosen_alloc: *std.mem.Allocator = allocator;
diff --git a/src/options.zig b/src/options.zig
index b994dbe4b..265c11148 100644
--- a/src/options.zig
+++ b/src/options.zig
@@ -180,8 +180,8 @@ pub const defaultLoaders = std.ComptimeStringMap(Loader, .{
pub const JSX = struct {
pub const Pragma = struct {
// these need to be arrays
- factory: []string = &(Defaults.Factory),
- fragment: []string = &(Defaults.Fragment),
+ factory: []const string = &(Defaults.Factory),
+ fragment: []const string = &(Defaults.Fragment),
runtime: JSX.Runtime = JSX.Runtime.automatic,
/// Facilitates automatic JSX importing
@@ -197,15 +197,52 @@ pub const JSX = struct {
pub var Fragment = [_]string{ "React", "Fragment" };
};
- pub fn fromApi(jsx: api.Api.Jsx) Pragma {
+ // "React.createElement" => ["React", "createElement"]
+ // ...unless new is "React.createElement" and original is ["React", "createElement"]
+ // saves an allocation for the majority case
+ pub fn memberListToComponentsIfDifferent(allocator: *std.mem.Allocator, original: []const string, new: string) ![]const string {
+ var splitter = std.mem.split(new, ".");
+
+ var needs_alloc = false;
+ var count: usize = 0;
+ while (splitter.next()) |str| {
+ const i = (splitter.index orelse break);
+ count = i;
+ if (i > original.len) {
+ needs_alloc = true;
+ break;
+ }
+
+ if (!strings.eql(original[i], str)) {
+ needs_alloc = true;
+ break;
+ }
+ }
+
+ if (!needs_alloc) {
+ return original;
+ }
+
+ var out = try allocator.alloc(string, count + 1);
+
+ splitter = std.mem.split(new, ".");
+ var i: usize = 0;
+ while (splitter.next()) |str| {
+ out[i] = str;
+ i += 1;
+ }
+ return out;
+ }
+
+ pub fn fromApi(jsx: api.Api.Jsx, allocator: *std.mem.Allocator) !Pragma {
var pragma = JSX.Pragma{};
if (jsx.fragment.len > 0) {
- pragma.jsx = jsx.fragment;
+ pragma.fragment = try memberListToComponentsIfDifferent(allocator, pragma.fragment, jsx.fragment);
}
if (jsx.factory.len > 0) {
- pragma.jsx = jsx.factory;
+ pragma.factory = try memberListToComponentsIfDifferent(allocator, pragma.factory, jsx.factory);
}
if (jsx.import_source.len > 0) {
@@ -298,7 +335,7 @@ pub const BundleOptions = struct {
};
if (transform.jsx) |jsx| {
- opts.jsx = JSX.Pragma.fromApi(jsx);
+ opts.jsx = try JSX.Pragma.fromApi(jsx, allocator);
}
if (transform.platform) |plat| {