diff options
Diffstat (limited to 'src/resolver')
-rw-r--r-- | src/resolver/package_json.zig | 232 | ||||
-rw-r--r-- | src/resolver/resolve_path.zig | 2 | ||||
-rw-r--r-- | src/resolver/resolver.zig | 54 |
3 files changed, 197 insertions, 91 deletions
diff --git a/src/resolver/package_json.zig b/src/resolver/package_json.zig index 0785e0493..f7ff5f3e4 100644 --- a/src/resolver/package_json.zig +++ b/src/resolver/package_json.zig @@ -37,6 +37,7 @@ const FolderResolver = @import("../install/resolvers/folder_resolver.zig"); const Architecture = @import("../install/npm.zig").Architecture; const OperatingSystem = @import("../install/npm.zig").OperatingSystem; +pub const SideEffectsMap = std.HashMapUnmanaged(bun.StringHashMapUnowned.Key, void, bun.StringHashMapUnowned.Adapter, 80); pub const DependencyMap = struct { map: HashMap = .{}, source_buf: []const u8 = "", @@ -72,6 +73,7 @@ pub const PackageJSON = struct { } const node_modules_path = std.fs.path.sep_str ++ "node_modules" ++ std.fs.path.sep_str; + pub fn nameForImport(this: *const PackageJSON, allocator: std.mem.Allocator) !string { if (strings.indexOf(this.source.path.text, node_modules_path)) |_| { return this.name; @@ -110,6 +112,12 @@ pub const PackageJSON = struct { package_manager_package_id: Install.PackageID = Install.invalid_package_id, dependencies: DependencyMap = .{}, + side_effects: union(enum) { + unspecified: void, + false: void, + map: SideEffectsMap, + } = .{ .unspecified = {} }, + // Present if the "browser" field is present. This field is intended to be // used by bundlers and lets you redirect the paths of certain 3rd-party // modules that don't work in the browser to other modules that shim that @@ -641,107 +649,155 @@ pub const PackageJSON = struct { } } - if (json.asProperty("type")) |type_json| { - if (type_json.expr.asString(r.allocator)) |type_str| { - switch (options.ModuleType.List.get(type_str) orelse options.ModuleType.unknown) { - .cjs => { - package_json.module_type = .cjs; - }, - .esm => { - package_json.module_type = .esm; - }, - .unknown => { - r.log.addRangeWarningFmt( - &json_source, - json_source.rangeOfString(type_json.loc), - r.allocator, - "\"{s}\" is not a valid value for \"type\" field (must be either \"commonjs\" or \"module\")", - .{type_str}, - ) catch unreachable; - }, + // If we're coming from `bun run` + // We do not need to parse all this stuff. + if (comptime !include_scripts) { + if (json.asProperty("type")) |type_json| { + if (type_json.expr.asString(r.allocator)) |type_str| { + switch (options.ModuleType.List.get(type_str) orelse options.ModuleType.unknown) { + .cjs => { + package_json.module_type = .cjs; + }, + .esm => { + package_json.module_type = .esm; + }, + .unknown => { + r.log.addRangeWarningFmt( + &json_source, + json_source.rangeOfString(type_json.loc), + r.allocator, + "\"{s}\" is not a valid value for \"type\" field (must be either \"commonjs\" or \"module\")", + .{type_str}, + ) catch unreachable; + }, + } + } else { + r.log.addWarning(&json_source, type_json.loc, "The value for \"type\" must be a string") catch unreachable; } - } else { - r.log.addWarning(&json_source, type_json.loc, "The value for \"type\" must be a string") catch unreachable; } - } - // Read the "main" fields - for (r.opts.main_fields) |main| { - if (json.asProperty(main)) |main_json| { - const expr: js_ast.Expr = main_json.expr; + // Read the "main" fields + for (r.opts.main_fields) |main| { + if (json.asProperty(main)) |main_json| { + const expr: js_ast.Expr = main_json.expr; - if ((expr.asString(r.allocator))) |str| { - if (str.len > 0) { - package_json.main_fields.put(main, r.allocator.dupe(u8, str) catch unreachable) catch unreachable; + if ((expr.asString(r.allocator))) |str| { + if (str.len > 0) { + package_json.main_fields.put(main, r.allocator.dupe(u8, str) catch unreachable) catch unreachable; + } } } } - } - // Read the "browser" property, but only when targeting the browser - if (r.opts.platform.supportsBrowserField()) { - // We both want the ability to have the option of CJS vs. ESM and the - // option of having node vs. browser. The way to do this is to use the - // object literal form of the "browser" field like this: - // - // "main": "dist/index.node.cjs.js", - // "module": "dist/index.node.esm.js", - // "browser": { - // "./dist/index.node.cjs.js": "./dist/index.browser.cjs.js", - // "./dist/index.node.esm.js": "./dist/index.browser.esm.js" - // }, - // - if (json.asProperty("browser")) |browser_prop| { - switch (browser_prop.expr.data) { - .e_object => |obj| { - // The value is an object - - // Remap all files in the browser field - for (obj.properties.slice()) |*prop| { - var _key_str = (prop.key orelse continue).asString(r.allocator) orelse continue; - const value: js_ast.Expr = prop.value orelse continue; - - // Normalize the path so we can compare against it without getting - // confused by "./". There is no distinction between package paths and - // relative paths for these values because some tools (i.e. Browserify) - // don't make such a distinction. - // - // This leads to weird things like a mapping for "./foo" matching an - // import of "foo", but that's actually not a bug. Or arguably it's a - // bug in Browserify but we have to replicate this bug because packages - // do this in the wild. - const key = r.allocator.dupe(u8, r.fs.normalize(_key_str)) catch unreachable; - - switch (value.data) { - .e_string => |str| { - // If this is a string, it's a replacement package - package_json.browser_map.put(key, str.string(r.allocator) catch unreachable) catch unreachable; - }, - .e_boolean => |boolean| { - if (!boolean.value) { - package_json.browser_map.put(key, "") catch unreachable; - } - }, - else => { - r.log.addWarning(&json_source, value.loc, "Each \"browser\" mapping must be a string or boolean") catch unreachable; - }, + // Read the "browser" property, but only when targeting the browser + if (r.opts.platform.supportsBrowserField()) { + // We both want the ability to have the option of CJS vs. ESM and the + // option of having node vs. browser. The way to do this is to use the + // object literal form of the "browser" field like this: + // + // "main": "dist/index.node.cjs.js", + // "module": "dist/index.node.esm.js", + // "browser": { + // "./dist/index.node.cjs.js": "./dist/index.browser.cjs.js", + // "./dist/index.node.esm.js": "./dist/index.browser.esm.js" + // }, + // + if (json.asProperty("browser")) |browser_prop| { + switch (browser_prop.expr.data) { + .e_object => |obj| { + // The value is an object + + // Remap all files in the browser field + for (obj.properties.slice()) |*prop| { + var _key_str = (prop.key orelse continue).asString(r.allocator) orelse continue; + const value: js_ast.Expr = prop.value orelse continue; + + // Normalize the path so we can compare against it without getting + // confused by "./". There is no distinction between package paths and + // relative paths for these values because some tools (i.e. Browserify) + // don't make such a distinction. + // + // This leads to weird things like a mapping for "./foo" matching an + // import of "foo", but that's actually not a bug. Or arguably it's a + // bug in Browserify but we have to replicate this bug because packages + // do this in the wild. + const key = r.allocator.dupe(u8, r.fs.normalize(_key_str)) catch unreachable; + + switch (value.data) { + .e_string => |str| { + // If this is a string, it's a replacement package + package_json.browser_map.put(key, str.string(r.allocator) catch unreachable) catch unreachable; + }, + .e_boolean => |boolean| { + if (!boolean.value) { + package_json.browser_map.put(key, "") catch unreachable; + } + }, + else => { + r.log.addWarning(&json_source, value.loc, "Each \"browser\" mapping must be a string or boolean") catch unreachable; + }, + } } - } - }, - else => {}, + }, + else => {}, + } } } - } - if (json.asProperty("exports")) |exports_prop| { - if (ExportsMap.parse(r.allocator, &json_source, r.log, exports_prop.expr, exports_prop.loc)) |exports_map| { - package_json.exports = exports_map; + if (json.asProperty("exports")) |exports_prop| { + if (ExportsMap.parse(r.allocator, &json_source, r.log, exports_prop.expr, exports_prop.loc)) |exports_map| { + package_json.exports = exports_map; + } } - } - if (json.asProperty("imports")) |imports_prop| { - if (ExportsMap.parse(r.allocator, &json_source, r.log, imports_prop.expr, imports_prop.loc)) |imports_map| { - package_json.imports = imports_map; + if (json.asProperty("imports")) |imports_prop| { + if (ExportsMap.parse(r.allocator, &json_source, r.log, imports_prop.expr, imports_prop.loc)) |imports_map| { + package_json.imports = imports_map; + } + } + + if (json.get("sideEffects")) |side_effects_field| outer: { + if (side_effects_field.asBool()) |boolean| { + if (!boolean) + package_json.side_effects = .{ .false = {} }; + } else if (side_effects_field.asArray()) |array_| { + var array = array_; + // TODO: switch to only storing hashes + var map = SideEffectsMap{}; + map.ensureTotalCapacity(r.allocator, array.array.items.len) catch unreachable; + while (array.next()) |item| { + if (item.asString(r.allocator)) |name| { + // TODO: support RegExp using JavaScriptCore <> C++ bindings + if (strings.containsChar(name, '*')) { + // https://sourcegraph.com/search?q=context:global+file:package.json+sideEffects%22:+%5B&patternType=standard&sm=1&groupBy=repo + // a lot of these seem to be css files which we don't care about for now anyway + // so we can just skip them in here + if (strings.eqlComptime(std.fs.path.extension(name), ".css")) + continue; + + r.log.addWarning( + &json_source, + item.loc, + "wildcard sideEffects are not supported yet, which means this package will be deoptimized", + ) catch unreachable; + map.deinit(r.allocator); + + package_json.side_effects = .{ .unspecified = {} }; + break :outer; + } + + var joined = [_]string{ + json_source.path.name.dirWithTrailingSlash(), + name, + }; + + _ = map.getOrPutAssumeCapacity( + bun.StringHashMapUnowned.Key.init(r.fs.join(&joined)), + ); + } + } + package_json.side_effects = .{ .map = map }; + } } } @@ -938,8 +994,6 @@ pub const PackageJSON = struct { } } - // TODO: side effects - if (generate_hash) { if (package_json.name.len > 0 and package_json.version.len > 0) { package_json.generateHash(); diff --git a/src/resolver/resolve_path.zig b/src/resolver/resolve_path.zig index b1cd6e0bf..50d41eab0 100644 --- a/src/resolver/resolve_path.zig +++ b/src/resolver/resolve_path.zig @@ -130,7 +130,7 @@ pub fn longestCommonPathGeneric(input: []const []const u8, comptime separator: u var string_index: usize = 1; while (index < min_length) : (index += 1) { while (string_index < input.len) : (string_index += 1) { - if (input[0][index] != input[index][string_index]) { + if (input[0][string_index] != input[index][string_index]) { break; } } diff --git a/src/resolver/resolver.zig b/src/resolver/resolver.zig index 493685ca7..da2753772 100644 --- a/src/resolver/resolver.zig +++ b/src/resolver/resolver.zig @@ -102,6 +102,29 @@ pub const PathPair = struct { } }; +// this is ripped from esbuild, comments included +pub const SideEffects = enum { + /// The default value conservatively considers all files to have side effects. + has_side_effects, + + /// This file was listed as not having side effects by a "package.json" + /// file in one of our containing directories with a "sideEffects" field. + no_side_effects__package_json, + + /// This file is considered to have no side effects because the AST was empty + /// after parsing finished. This should be the case for ".d.ts" files. + no_side_effects__empty_ast, + + /// This file was loaded using a data-oriented loader (e.g. "text") that is + /// known to not have side effects. + no_side_effects__pure_data, + + // / Same as above but it came from a plugin. We don't want to warn about + // / unused imports to these files since running the plugin is a side effect. + // / Removing the import would not call the plugin which is observable. + // no_side_effects__pure_data_from_plugin, +}; + pub const Result = struct { path_pair: PathPair, @@ -118,7 +141,7 @@ pub const Result = struct { // If present, any ES6 imports to this file can be considered to have no side // effects. This means they should be removed if unused. - primary_side_effects_data: ?SideEffectsData = null, + primary_side_effects_data: SideEffects = SideEffects.has_side_effects, // If true, unused imports are retained in TypeScript code. This matches the // behavior of the "importsNotUsedAsValues" field in "tsconfig.json" when the @@ -538,6 +561,12 @@ pub const Resolver = struct { } pub fn isExternalPattern(r: *ThisResolver, import_path: string) bool { + if (r.opts.mark_bun_builtins_as_external) { + if (bun.JSC.HardcodedModule.Aliases.has(import_path)) { + return true; + } + } + for (r.opts.external.patterns) |pattern| { if (import_path.len >= pattern.prefix.len + pattern.suffix.len and (strings.startsWith( import_path, @@ -857,12 +886,34 @@ pub const Resolver = struct { var module_type = result.module_type; while (iter.next()) |path| { var dir: *DirInfo = (r.readDirInfo(path.name.dir) catch continue) orelse continue; + var needs_side_effects = true; if (result.package_json) |existing| { + // if we don't have it here, they might put it in a sideEfffects + // map of the parent package.json + // TODO: check if webpack also does this parent lookup + needs_side_effects = existing.side_effects == .unspecified; + + result.primary_side_effects_data = switch (existing.side_effects) { + .unspecified => .has_side_effects, + .false => .no_side_effects__package_json, + .map => |map| if (map.contains(bun.StringHashMapUnowned.Key.init(path.text))) .has_side_effects else .no_side_effects__package_json, + }; + if (existing.name.len == 0 or r.care_about_bin_folder) result.package_json = null; } result.package_json = result.package_json orelse dir.enclosing_package_json; + if (needs_side_effects) { + if (result.package_json) |package_json| { + result.primary_side_effects_data = switch (package_json.side_effects) { + .unspecified => .has_side_effects, + .false => .no_side_effects__package_json, + .map => |map| if (map.contains(bun.StringHashMapUnowned.Key.init(path.text))) .has_side_effects else .no_side_effects__package_json, + }; + } + } + if (dir.enclosing_tsconfig_json) |tsconfig| { result.jsx = tsconfig.mergeJSX(result.jsx); } @@ -1548,6 +1599,7 @@ pub const Resolver = struct { // this is the magic! if (global_cache.canUse(any_node_modules_folder) and r.usePackageManager() and esm_ != null) { + if (comptime bun.fast_debug_build_mode and bun.fast_debug_build_cmd != .RunCommand) unreachable; const esm = esm_.?.withAutoVersion(); load_module_from_cache: { |