diff options
author | 2023-08-20 19:22:55 -0700 | |
---|---|---|
committer | 2023-08-20 19:22:55 -0700 | |
commit | 65280853acf2385eae124ef4870af2751ad662df (patch) | |
tree | 358bf3d99d5391f2e89269424af309c5a156425a | |
parent | 3a9a6c63ac58564d8bae126a3edfc368ca4be671 (diff) | |
download | bun-65280853acf2385eae124ef4870af2751ad662df.tar.gz bun-65280853acf2385eae124ef4870af2751ad662df.tar.zst bun-65280853acf2385eae124ef4870af2751ad662df.zip |
Fix test failures from 3a9a6c63a (#4231)
cc @Hanaasagi
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
-rw-r--r-- | src/bun.js/module_loader.zig | 13 | ||||
-rw-r--r-- | src/options.zig | 4 | ||||
-rw-r--r-- | src/resolver/resolver.zig | 30 | ||||
-rw-r--r-- | test/js/node/stubs.test.js | 19 |
4 files changed, 34 insertions, 32 deletions
diff --git a/src/bun.js/module_loader.zig b/src/bun.js/module_loader.zig index b7c5ea53e..be10b9722 100644 --- a/src/bun.js/module_loader.zig +++ b/src/bun.js/module_loader.zig @@ -2456,8 +2456,6 @@ pub const HardcodedModule = enum { // It implements the same interface .{ "sys", .{ .path = "node:util" } }, .{ "node:sys", .{ .path = "node:util" } }, - // .{ "inspector/promises", .{ .path = "node:inspector" } }, - .{ "node:inspector/promises", .{ .path = "node:inspector" } }, // These are returned in builtinModules, but probably not many packages use them // so we will just alias them. @@ -2501,9 +2499,18 @@ pub const HardcodedModule = enum { .{ "utf-8-validate", .{ .path = "utf-8-validate" } }, .{ "ws", .{ .path = "ws" } }, .{ "ws/lib/websocket", .{ .path = "ws" } }, + + .{ "inspector/promises", .{ .path = "node:inspector" } }, + .{ "node:inspector/promises", .{ .path = "node:inspector" } }, + }; + + const node_alias_kvs = .{ + .{ "inspector/promises", .{ .path = "node:inspector/promises" } }, + .{ "node:inspector/promises", .{ .path = "node:inspector/promises" } }, + .{ "node:test", .{ .path = "node:test" } }, }; - const NodeAliases = bun.ComptimeStringMap(Alias, common_alias_kvs); + const NodeAliases = bun.ComptimeStringMap(Alias, common_alias_kvs ++ node_alias_kvs); const BunAliases = bun.ComptimeStringMap(Alias, common_alias_kvs ++ bun_extra_alias_kvs); pub fn has(name: []const u8, target: options.Target) bool { diff --git a/src/options.zig b/src/options.zig index 51ee27dab..fec09405c 100644 --- a/src/options.zig +++ b/src/options.zig @@ -1417,7 +1417,7 @@ pub const BundleOptions = struct { defines_loaded: bool = false, env: Env = Env{}, transform_options: Api.TransformOptions, - polyfill_node_globals: bool = true, + polyfill_node_globals: bool = false, transform_only: bool = false, rewrite_jest_for_tests: bool = false, @@ -1844,7 +1844,7 @@ pub const BundleOptions = struct { opts.output_dir = try fs.getFdPath(opts.output_dir_handle.?.fd); } - opts.polyfill_node_globals = opts.target != .node; + opts.polyfill_node_globals = opts.target == .browser; Analytics.Features.framework = Analytics.Features.framework or opts.framework != null; Analytics.Features.filesystem_router = Analytics.Features.filesystem_router or opts.routes.routes_enabled; diff --git a/src/resolver/resolver.zig b/src/resolver/resolver.zig index 41b329734..2730daf42 100644 --- a/src/resolver/resolver.zig +++ b/src/resolver/resolver.zig @@ -1269,16 +1269,28 @@ pub const Resolver = struct { result.package_json = @as(*PackageJSON, @ptrFromInt(@intFromPtr(fallback_module.package_json))); result.is_from_node_modules = true; return .{ .success = result }; - // "node:* - // "fs" - // "fs/*" - // These are disabled! - } else if (had_node_prefix and !JSC.HardcodedModule.Aliases.has(import_path_without_node_prefix, r.opts.target)) { - return .{ .not_found = {} }; - } else if (had_node_prefix or - (strings.hasPrefixComptime(import_path_without_node_prefix, "fs") and + } + + if (had_node_prefix) { + // Module resolution fails automatically for unknown node builtins + if (!bun.JSC.HardcodedModule.Aliases.has(import_path_without_node_prefix, .node)) { + return .{ .not_found = {} }; + } + + // Valid node:* modules becomes {} in the output + result.path_pair.primary.namespace = "node"; + result.path_pair.primary.text = import_path_without_node_prefix; + result.path_pair.primary.name = Fs.PathName.init(import_path_without_node_prefix); + result.module_type = .cjs; + result.path_pair.primary.is_disabled = true; + result.is_from_node_modules = true; + return .{ .success = result }; + } + + // Always mark "fs" as disabled, matching Webpack v4 behavior + if (strings.hasPrefixComptime(import_path_without_node_prefix, "fs") and (import_path_without_node_prefix.len == 2 or - import_path_without_node_prefix[2] == '/'))) + import_path_without_node_prefix[2] == '/')) { result.path_pair.primary.namespace = "node"; result.path_pair.primary.text = import_path_without_node_prefix; diff --git a/test/js/node/stubs.test.js b/test/js/node/stubs.test.js index 11d8edbd9..057215c4f 100644 --- a/test/js/node/stubs.test.js +++ b/test/js/node/stubs.test.js @@ -89,24 +89,7 @@ for (let specifier of specifiers) { if ("default" in mod) { expect(mod).toHaveProperty("default"); } else { - // TODO: uncomment this after node:module can be default imported - // throw new Error(`Module ${specifier} has no default export`); + throw new Error(`Module ${specifier} has no default export`); } }); } - -// TODO: when node:vm is implemented, delete this test. -test("node:vm", () => { - const { Script } = import.meta.require("node:vm"); - try { - // **This line should appear in the stack trace** - // That way it shows the "real" line causing the issue - // Instead of several layers of wrapping - new Script("1 + 1"); - throw new Error("unreacahble"); - } catch (e) { - const msg = Bun.inspect(e); - expect(msg).not.toContain("node:vm:"); - expect(msg).toContain("**This line should appear in the stack trace**"); - } -}); |