diff options
author | 2022-08-28 21:21:20 -0700 | |
---|---|---|
committer | 2022-08-28 21:21:20 -0700 | |
commit | b2141a204fbc351a40467037138168aea23a6930 (patch) | |
tree | 819a03a5d537eb1470ed34f6d8c2137fd86a8e01 | |
parent | ce90e0c372f3e92b2a163d24018132aa2ce2ee6b (diff) | |
download | bun-b2141a204fbc351a40467037138168aea23a6930.tar.gz bun-b2141a204fbc351a40467037138168aea23a6930.tar.zst bun-b2141a204fbc351a40467037138168aea23a6930.zip |
feat: hack in support for tsconfig `extends` (#1147)
* hack in support for extends
* Update src/resolver/resolver.zig
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
* wip review fixes
* fix style
Co-authored-by: Victor Lin <victor.lin@airbnb.com>
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
Co-authored-by: Victor Lin <vlin@twitter.com>
-rw-r--r-- | src/resolver/resolver.zig | 44 | ||||
-rw-r--r-- | src/resolver/tsconfig_json.zig | 17 |
2 files changed, 47 insertions, 14 deletions
diff --git a/src/resolver/resolver.zig b/src/resolver/resolver.zig index 8dbbba50e..97e4194b0 100644 --- a/src/resolver/resolver.zig +++ b/src/resolver/resolver.zig @@ -234,6 +234,7 @@ threadlocal var check_browser_map_buf: [bun.MAX_PATH_BYTES]u8 = undefined; threadlocal var remap_path_buf: [bun.MAX_PATH_BYTES]u8 = undefined; threadlocal var load_as_file_buf: [bun.MAX_PATH_BYTES]u8 = undefined; threadlocal var remap_path_trailing_slash: [bun.MAX_PATH_BYTES]u8 = undefined; +threadlocal var tsconfig_paths_buf: [bun.MAX_PATH_BYTES]u8 = undefined; pub const DebugLogs = struct { what: string = "", @@ -1771,8 +1772,8 @@ pub const Resolver = struct { const original_paths = entry.value_ptr.*; if (strings.indexOfChar(key, '*')) |star| { - const prefix = key[0 .. star - 1]; - const suffix = key[star + 1 ..]; + const prefix = if (star == 0) "" else key[0..star]; + const suffix = if (star == key.len - 1) "" else key[star + 1 ..]; // Find the match with the longest prefix. If two matches have the same // prefix length, pick the one with the longest suffix. This second edge @@ -2697,6 +2698,45 @@ pub const Resolver = struct { } break :brk null; }; + if (info.tsconfig_json) |tsconfig_json| { + var parent_configs = try std.BoundedArray(*TSConfigJSON, 64).init(0); + try parent_configs.append(tsconfig_json); + var current = tsconfig_json; + while (current.extends.len > 0) { + var ts_dir_name = Dirname.dirname(current.abs_path); + // not sure why this needs cwd but we'll just pass in the dir of the tsconfig... + var abs_path = ResolvePath.joinAbsStringBuf(ts_dir_name, &tsconfig_path_abs_buf, &[_]string{ ts_dir_name, current.extends }, .auto); + var parent_config_maybe = try r.parseTSConfig(abs_path, 0); + if (parent_config_maybe) |parent_config| { + try parent_configs.append(parent_config); + current = parent_config; + } else { + break; + } + } + + var merged_config = parent_configs.pop(); + // starting from the base config (end of the list) + // successively apply the inheritable attributes to the next config + while (parent_configs.popOrNull()) |parent_config| { + if (parent_config.base_url.len > 0) { + merged_config.base_url = parent_config.base_url; + merged_config.base_url_for_paths = parent_config.base_url_for_paths; + } + merged_config.jsx = parent_config.mergeJSX(merged_config.jsx); + + if (parent_config.preserve_imports_not_used_as_values) |value| { + merged_config.preserve_imports_not_used_as_values = value; + } + + var iter = parent_config.paths.iterator(); + while (iter.next()) |c| { + merged_config.paths.put(c.key_ptr.*, c.value_ptr.*) catch unreachable; + } + // todo deinit these parent configs somehow? + } + info.tsconfig_json = merged_config; + } info.enclosing_tsconfig_json = info.tsconfig_json; } } diff --git a/src/resolver/tsconfig_json.zig b/src/resolver/tsconfig_json.zig index 34732beb8..d3a6a2062 100644 --- a/src/resolver/tsconfig_json.zig +++ b/src/resolver/tsconfig_json.zig @@ -33,6 +33,7 @@ pub const TSConfigJSON = struct { // More info: https://github.com/microsoft/TypeScript/issues/31869 base_url_for_paths: string = "", + extends: string = "", // The verbatim values of "compilerOptions.paths". The keys are patterns to // match and the values are arrays of fallback paths to search. Each key and // each fallback path can optionally have a single "*" wildcard character. @@ -49,7 +50,7 @@ pub const TSConfigJSON = struct { use_define_for_class_fields: ?bool = null, - preserve_imports_not_used_as_values: bool = false, + preserve_imports_not_used_as_values: ?bool = false, pub fn hasBaseURL(tsconfig: *const TSConfigJSON) bool { return tsconfig.base_url.len > 0; @@ -108,18 +109,10 @@ pub const TSConfigJSON = struct { errdefer allocator.free(result.paths); if (json.asProperty("extends")) |extends_value| { if (!source.path.isNodeModule()) { - log.addWarning(&source, extends_value.loc, "\"extends\" is not implemented yet") catch unreachable; + if (extends_value.expr.asString(allocator) orelse null) |str| { + result.extends = str; + } } - // if ((extends_value.expr.asString(allocator) catch null)) |str| { - // if (extends(str, source.rangeOfString(extends_value.loc))) |base| { - // result.jsx = base.jsx; - // result.base_url_for_paths = base.base_url_for_paths; - // result.use_define_for_class_fields = base.use_define_for_class_fields; - // result.preserve_imports_not_used_as_values = base.preserve_imports_not_used_as_values; - // // https://github.com/microsoft/TypeScript/issues/14527#issuecomment-284948808 - // result.paths = base.paths; - // } - // } } var has_base_url = false; |