diff options
-rw-r--r-- | src/bundler.zig | 4 | ||||
-rw-r--r-- | src/http.zig | 2 | ||||
-rw-r--r-- | src/js_parser.zig | 26 | ||||
-rw-r--r-- | src/options.zig | 69 | ||||
-rw-r--r-- | src/resolver/tsconfig_json.zig | 10 |
5 files changed, 50 insertions, 61 deletions
diff --git a/src/bundler.zig b/src/bundler.zig index aba5f9e5a..00a9d6954 100644 --- a/src/bundler.zig +++ b/src/bundler.zig @@ -565,9 +565,7 @@ pub const Bundler = struct { try this.runEnvLoader(); - if (this.env.isProduction()) { - this.options.jsx.setProduction(this.allocator, true); - } + this.options.jsx.setProduction(this.env.isProduction()); js_ast.Expr.Data.Store.create(this.allocator); js_ast.Stmt.Data.Store.create(this.allocator); diff --git a/src/http.zig b/src/http.zig index 6b34adac4..8541ee007 100644 --- a/src/http.zig +++ b/src/http.zig @@ -3911,7 +3911,7 @@ pub const Server = struct { if (node_modules_bundle.getPackageIDByName(package_name) != null) return; } - _ = this.bundler.resolver.resolve(this.bundler.fs.top_level_dir, this.bundler.options.jsx.import_source, .internal) catch { + _ = this.bundler.resolver.resolve(this.bundler.fs.top_level_dir, this.bundler.options.jsx.importSource(), .internal) catch { // if they don't have React, they can't use fast refresh this.bundler.options.jsx.supports_fast_refresh = false; return; diff --git a/src/js_parser.zig b/src/js_parser.zig index 6ae92a47a..261eabf3e 100644 --- a/src/js_parser.zig +++ b/src/js_parser.zig @@ -2737,7 +2737,7 @@ pub const Parser = struct { _ = p.addImportRecord( .require, logger.Loc{ .start = 0 }, - p.options.jsx.import_source, + p.options.jsx.importSource(), ); // Ensure we have both classic and automatic // This is to handle cases where they use fragments in the automatic runtime @@ -3957,7 +3957,7 @@ pub const Parser = struct { if (runtime_import_names.len > 0) { p.generateImportStmt( - p.options.jsx.import_source, + p.options.jsx.importSource(), runtime_import_names, &before, &p.jsx_imports, @@ -4796,10 +4796,13 @@ fn NewParser_( const import_record_index = p.addImportRecord(.require, arg.loc, arg.data.e_string.string(p.allocator) catch unreachable); p.import_records.items[import_record_index].handles_import_errors = p.fn_or_arrow_data_visit.try_body_count != 0; p.import_records_for_current_part.append(p.allocator, import_record_index) catch unreachable; - return p.newExpr(E.RequireOrRequireResolve{ - .import_record_index = Ref.toInt(import_record_index), - // .leading_interior_comments = arg.getString(). - }, arg.loc); + return p.newExpr( + E.RequireResolveString{ + .import_record_index = Ref.toInt(import_record_index), + // .leading_interior_comments = arg.getString(). + }, + arg.loc, + ); } if (p.options.warn_about_unbundled_modules) { @@ -4829,7 +4832,7 @@ fn NewParser_( p.import_records_for_current_part.append(p.allocator, import_record_index) catch unreachable; if (!p.options.transform_require_to_import) { - return p.newExpr(E.Require{ .import_record_index = import_record_index }, arg.loc); + return p.newExpr(E.RequireString{ .import_record_index = import_record_index }, arg.loc); } p.import_records.items[import_record_index].was_originally_require = true; @@ -6051,8 +6054,9 @@ fn NewParser_( } if (p.lexer.jsx_pragma.jsxImportSource()) |import_source| { - p.options.jsx.import_source = try p.allocator.dupe(u8, import_source.text); - p.options.jsx.classic_import_source = options.JSX.Pragma.parsePackageName(p.options.jsx.import_source); + p.options.jsx.classic_import_source = options.JSX.Pragma.parsePackageName(import_source.text); + p.options.jsx.package_name = p.options.jsx.classic_import_source; + p.options.jsx.setImportSource(p.allocator); } if (p.lexer.jsx_pragma.jsxRuntime()) |runtime| { @@ -6165,9 +6169,7 @@ fn NewParser_( p.jsx_classic = p.declareGeneratedSymbol(.other, "ClassicImportSource") catch unreachable; } - if (p.options.jsx.import_source.len > 0) { - p.jsx_automatic = p.declareGeneratedSymbol(.other, "ImportSource") catch unreachable; - } + p.jsx_automatic = p.declareGeneratedSymbol(.other, "ImportSource") catch unreachable; } }, diff --git a/src/options.zig b/src/options.zig index f59784d0a..a189c4d7a 100644 --- a/src/options.zig +++ b/src/options.zig @@ -901,11 +901,11 @@ pub const JSX = struct { factory: []const string = Defaults.Factory, fragment: []const string = Defaults.Fragment, runtime: JSX.Runtime = JSX.Runtime.automatic, + import_source: ImportSource = .{}, /// Facilitates automatic JSX importing /// Set on a per file basis like this: /// /** @jsxImportSource @emotion/core */ - import_source: string = "react/jsx-dev-runtime", classic_import_source: string = "react", package_name: []const u8 = "react", // https://github.com/facebook/react/commit/2f26eb85d657a08c21edbac1e00f9626d68f84ae @@ -913,12 +913,21 @@ pub const JSX = struct { supports_fast_refresh: bool = true, use_embedded_refresh_runtime: bool = false, - jsx: string = Defaults.JSXFunctionDev, - // jsx_static: string = Defaults.JSXStaticFunction, - development: bool = true, parse: bool = true, + pub const ImportSource = struct { + development: string = "react/jsx-dev-runtime", + production: string = "react/jsx-runtime", + }; + + pub fn importSource(this: *const Pragma) string { + return switch (this.development) { + true => this.import_source.development, + false => this.import_source.production, + }; + } + pub fn parsePackageName(str: string) string { if (str[0] == '@') { if (strings.indexOfChar(str[1..], '/')) |first_slash| { @@ -941,34 +950,34 @@ pub const JSX = struct { return strings.eqlComptime(pragma.package_name, "react") or strings.eqlComptime(pragma.package_name, "@emotion/jsx") or strings.eqlComptime(pragma.package_name, "@emotion/react"); } - pub fn setImportSource(pragma: *Pragma, allocator: std.mem.Allocator, suffix: []const u8) void { + pub fn setImportSource(pragma: *Pragma, allocator: std.mem.Allocator) void { strings.concatIfNeeded( allocator, - &pragma.import_source, + &pragma.import_source.development, &[_]string{ pragma.package_name, - suffix, + "jsx-dev-runtime", }, &.{ - Defaults.ImportSource, Defaults.ImportSourceDev, }, ) catch unreachable; + + strings.concatIfNeeded( + allocator, + &pragma.import_source.production, + &[_]string{ + pragma.package_name, + "jsx-runtime", + }, + &.{ + Defaults.ImportSource, + }, + ) catch unreachable; } - pub fn setProduction(pragma: *Pragma, allocator: std.mem.Allocator, is_production: bool) void { + pub fn setProduction(pragma: *Pragma, is_production: bool) void { pragma.development = !is_production; - const package_name = parsePackageName(pragma.import_source); - pragma.package_name = package_name; - pragma.classic_import_source = package_name; - - if (is_production) { - pragma.setImportSource(allocator, "/jsx-runtime"); - pragma.jsx = "jsx"; - } else { - pragma.setImportSource(allocator, "/jsx-dev-runtime"); - pragma.jsx = "jsxDEV"; - } } pub const Defaults = struct { @@ -1034,23 +1043,9 @@ pub const JSX = struct { pragma.runtime = jsx.runtime; if (jsx.import_source.len > 0) { - pragma.import_source = jsx.import_source; - if (jsx.import_source.len > "solid-js".len and strings.eqlComptime(jsx.import_source[0.."solid-js".len], "solid-js")) { - pragma.runtime = .solid; - pragma.supports_fast_refresh = false; - } - pragma.package_name = parsePackageName(pragma.import_source); - } else if (jsx.development) { - pragma.import_source = Defaults.ImportSourceDev; - pragma.package_name = "react"; - } else { - pragma.import_source = Defaults.ImportSource; - } - - if (jsx.development) { - pragma.jsx = Defaults.JSXFunctionDev; - } else { - pragma.jsx = Defaults.JSXFunction; + pragma.package_name = parsePackageName(pragma.importSource()); + pragma.setImportSource(allocator); + pragma.classic_import_source = pragma.package_name; } pragma.supports_fast_refresh = if (pragma.runtime == .solid) false else pragma.supports_fast_refresh; diff --git a/src/resolver/tsconfig_json.zig b/src/resolver/tsconfig_json.zig index 68d3f28c9..7ce1fa358 100644 --- a/src/resolver/tsconfig_json.zig +++ b/src/resolver/tsconfig_json.zig @@ -161,7 +161,7 @@ pub const TSConfigJSON = struct { if (options.JSX.RuntimeMap.get(str)) |runtime| { result.jsx.runtime = runtime; if (runtime == .automatic) { - result.jsx.setProduction(allocator, strings.eqlComptime(str, "react-jsxDEV")); + result.jsx.setProduction(!strings.contains(str, "jsxDEV")); is_jsx_development = result.jsx.development; result.jsx_flags.insert(.development); } @@ -175,18 +175,12 @@ pub const TSConfigJSON = struct { if (compiler_opts.expr.asProperty("jsxImportSource")) |jsx_prop| { if (jsx_prop.expr.asString(allocator)) |str| { if (str.len >= "solid-js".len and strings.eqlComptime(str[0.."solid-js".len], "solid-js")) { - result.jsx.import_source = str; result.jsx.runtime = .solid; result.jsx_flags.insert(.runtime); - } else { - if (is_jsx_development) { - result.jsx.setImportSource(allocator, "{s}/jsx-dev-runtime"); - } else { - result.jsx.setImportSource(allocator, "{s}/jsx-runtime"); - } } result.jsx.package_name = options.JSX.Pragma.parsePackageName(str); + result.jsx.setImportSource(allocator); result.jsx_flags.insert(.import_source); } } |