diff options
author | 2022-01-19 23:07:03 -0800 | |
---|---|---|
committer | 2022-01-19 23:07:03 -0800 | |
commit | a09b99565138bb4bc73a5328397428fb5025817b (patch) | |
tree | 3412ae330b4a6078173d0048665b9d547b3d718b /src/resolver/package_json.zig | |
parent | 4098484ff5d1c66a5f146e773a9be25cbcd2a1f4 (diff) | |
download | bun-a09b99565138bb4bc73a5328397428fb5025817b.tar.gz bun-a09b99565138bb4bc73a5328397428fb5025817b.tar.zst bun-a09b99565138bb4bc73a5328397428fb5025817b.zip |
Bun.Transpiler – API for scanning imports/exports of JSX/TSX/TS/JS files
Diffstat (limited to 'src/resolver/package_json.zig')
-rw-r--r-- | src/resolver/package_json.zig | 127 |
1 files changed, 69 insertions, 58 deletions
diff --git a/src/resolver/package_json.zig b/src/resolver/package_json.zig index f2918cffa..eefb5474b 100644 --- a/src/resolver/package_json.zig +++ b/src/resolver/package_json.zig @@ -448,6 +448,74 @@ pub const PackageJSON = struct { } } + pub fn parseMacrosJSON( + allocator: std.mem.Allocator, + macros: js_ast.Expr, + log: *logger.Log, + json_source: *const logger.Source, + ) MacroMap { + var macro_map = MacroMap{}; + if (macros.data != .e_object) return macro_map; + + const properties = macros.data.e_object.properties; + + for (properties) |property| { + const key = property.key.?.asString(allocator) orelse continue; + if (!resolver.isPackagePath(key)) { + log.addRangeWarningFmt( + json_source, + json_source.rangeOfString(property.key.?.loc), + allocator, + "\"{s}\" is not a package path. \"macros\" remaps package paths to macros. Skipping.", + .{key}, + ) catch unreachable; + continue; + } + + const value = property.value.?; + if (value.data != .e_object) { + log.addWarningFmt( + json_source, + value.loc, + allocator, + "Invalid macro remapping in \"{s}\": expected object where the keys are import names and the value is a string path to replace", + .{key}, + ) catch unreachable; + continue; + } + + const remap_properties = value.data.e_object.properties; + if (remap_properties.len == 0) continue; + + var map = MacroImportReplacementMap.init(allocator); + map.ensureUnusedCapacity(remap_properties.len) catch unreachable; + for (remap_properties) |remap| { + const import_name = remap.key.?.asString(allocator) orelse continue; + const remap_value = remap.value.?; + if (remap_value.data != .e_string or remap_value.data.e_string.utf8.len == 0) { + log.addWarningFmt( + json_source, + remap_value.loc, + allocator, + "Invalid macro remapping for import \"{s}\": expected string to remap to. e.g. \"graphql\": \"bun-macro-relay\" ", + .{import_name}, + ) catch unreachable; + continue; + } + + const remap_value_str = remap_value.data.e_string.utf8; + + map.putAssumeCapacityNoClobber(import_name, remap_value_str); + } + + if (map.count() > 0) { + macro_map.put(allocator, key, map) catch unreachable; + } + } + + return macro_map; + } + pub fn parse( comptime ResolverType: type, r: *ResolverType, @@ -569,64 +637,7 @@ pub const PackageJSON = struct { } if (bun_json.expr.asProperty("macros")) |macros| { - if (macros.expr.data == .e_object) { - const properties = macros.expr.data.e_object.properties; - - for (properties) |property| { - const key = property.key.?.asString(r.allocator) orelse continue; - if (!resolver.isPackagePath(key)) { - r.log.addRangeWarningFmt( - &json_source, - json_source.rangeOfString(property.key.?.loc), - r.allocator, - "\"{s}\" is not a package path. \"macros\" remaps package paths to macros. Skipping.", - .{key}, - ) catch unreachable; - continue; - } - - const value = property.value.?; - if (value.data != .e_object) { - r.log.addWarningFmt( - &json_source, - value.loc, - r.allocator, - "Invalid macro remapping in \"{s}\": expected object where the keys are import names and the value is a string path to replace", - .{key}, - ) catch unreachable; - continue; - } - - const remap_properties = value.data.e_object.properties; - if (remap_properties.len == 0) continue; - - var map = MacroImportReplacementMap.init(r.allocator); - map.ensureUnusedCapacity(remap_properties.len) catch unreachable; - for (remap_properties) |remap| { - const import_name = remap.key.?.asString(r.allocator) orelse continue; - const remap_value = remap.value.?; - if (remap_value.data != .e_string or remap_value.data.e_string.utf8.len == 0) { - r.log.addWarningFmt( - &json_source, - remap_value.loc, - r.allocator, - "Invalid macro remapping for import \"{s}\": expected string to remap to. e.g. \"graphql\": \"bun-macro-relay\" ", - .{import_name}, - ) catch unreachable; - continue; - } - - const remap_value_str = remap_value.data.e_string.utf8; - - map.putAssumeCapacityNoClobber(import_name, remap_value_str); - } - - if (map.count() > 0) { - package_json.macros.put(r.allocator, key, map) catch unreachable; - } - } - // for (var i = 0; i < bundle_.expr.data.e_array.len; i++) { - } + package_json.macros = parseMacrosJSON(r.allocator, macros.expr, r.log, &json_source); } } |