diff options
Diffstat (limited to '')
-rw-r--r-- | src/resolver/resolver.zig | 14 | ||||
-rw-r--r-- | src/resolver/tsconfig_json.zig | 38 |
2 files changed, 46 insertions, 6 deletions
diff --git a/src/resolver/resolver.zig b/src/resolver/resolver.zig index a1066dfb7..4b766b81e 100644 --- a/src/resolver/resolver.zig +++ b/src/resolver/resolver.zig @@ -689,6 +689,10 @@ pub const Resolver = struct { var dir: *DirInfo = (r.readDirInfo(path.name.dir) catch continue) orelse continue; result.package_json = result.package_json orelse dir.enclosing_package_json; + if (dir.enclosing_tsconfig_json) |tsconfig| { + result.jsx = tsconfig.mergeJSX(result.jsx); + } + if (dir.getEntries()) |entries| { if (entries.get(path.name.filename)) |query| { const symlink_path = query.entry.symlink(&r.fs.fs); @@ -758,7 +762,12 @@ pub const Resolver = struct { // This implements the module resolution algorithm from node.js, which is // described here: https://nodejs.org/api/modules.html#modules_all_together - var result: Result = Result{ .path_pair = PathPair{ .primary = Path.empty } }; + var result: Result = Result{ + .path_pair = PathPair{ + .primary = Path.empty, + }, + .jsx = r.opts.jsx, + }; // Return early if this is already an absolute path. In addition to asking // the file system whether this is an absolute path, we also explicitly check @@ -788,6 +797,7 @@ pub const Resolver = struct { .package_json = res.package_json, .dirname_fd = res.dirname_fd, .file_fd = res.file_fd, + .jsx = tsconfig.mergeJSX(result.jsx), }; } } @@ -1325,7 +1335,7 @@ pub const Resolver = struct { const source = logger.Source.initPathString(key_path.text, entry.contents); const file_dir = source.path.sourceDir(); - var result = (try TSConfigJSON.parse(r.allocator, r.log, source, @TypeOf(r.caches.json), &r.caches.json)) orelse return null; + var result = (try TSConfigJSON.parse(r.allocator, r.log, source, &r.caches.json, r.opts.jsx.development)) orelse return null; if (result.hasBaseURL()) { // this might leak diff --git a/src/resolver/tsconfig_json.zig b/src/resolver/tsconfig_json.zig index f61b48eb5..bbd58f900 100644 --- a/src/resolver/tsconfig_json.zig +++ b/src/resolver/tsconfig_json.zig @@ -34,6 +34,9 @@ pub const TSConfigJSON = struct { paths: PathsMap, jsx: options.JSX.Pragma = options.JSX.Pragma{}, + has_jsxFactory: bool = false, + has_jsxFragmentFactory: bool = false, + has_jsxImportSource: bool = false, use_define_for_class_fields: ?bool = null, @@ -56,12 +59,30 @@ pub const TSConfigJSON = struct { }); }; + pub fn mergeJSX(this: *const TSConfigJSON, current: options.JSX.Pragma) options.JSX.Pragma { + var out = current; + + if (this.has_jsxFactory) { + out.factory = this.jsx.factory; + } + + if (this.has_jsxFragmentFactory) { + out.fragment = this.jsx.fragment; + } + + if (this.has_jsxImportSource) { + out.import_source = this.jsx.import_source; + } + + return out; + } + pub fn parse( allocator: *std.mem.Allocator, log: *logger.Log, source: logger.Source, - comptime JSONCache: type, - json_cache: *JSONCache, + json_cache: *cache.Json, + is_jsx_development: bool, ) anyerror!?*TSConfigJSON { // Unfortunately "tsconfig.json" isn't actually JSON. It's some other // format that appears to be defined by the implementation details of the @@ -107,20 +128,29 @@ pub const TSConfigJSON = struct { if (compiler_opts.expr.asProperty("jsxFactory")) |jsx_prop| { if (jsx_prop.expr.asString(allocator)) |str| { result.jsx.factory = try parseMemberExpressionForJSX(log, &source, jsx_prop.loc, str, allocator); + result.has_jsxFactory = true; } } // Parse "jsxFragmentFactory" - if (compiler_opts.expr.asProperty("jsxFactory")) |jsx_prop| { + if (compiler_opts.expr.asProperty("jsxFragmentFactory")) |jsx_prop| { if (jsx_prop.expr.asString(allocator)) |str| { result.jsx.fragment = try parseMemberExpressionForJSX(log, &source, jsx_prop.loc, str, allocator); + result.has_jsxFragmentFactory = true; } } // Parse "jsxImportSource" if (compiler_opts.expr.asProperty("jsxImportSource")) |jsx_prop| { if (jsx_prop.expr.asString(allocator)) |str| { - result.jsx.import_source = str; + if (is_jsx_development) { + result.jsx.import_source = std.fmt.allocPrint(allocator, "{s}/jsx-dev-runtime", .{str}) catch unreachable; + } else { + result.jsx.import_source = std.fmt.allocPrint(allocator, "{s}/jsx-runtime", .{str}) catch unreachable; + } + + result.jsx.package_name = options.JSX.Pragma.parsePackageName(str); + result.has_jsxImportSource = true; } } |