diff options
author | 2022-09-03 21:48:06 -0700 | |
---|---|---|
committer | 2022-09-03 21:48:06 -0700 | |
commit | 04cc1968dbe7947b63b93d800ecc1747dc11eb5f (patch) | |
tree | 136f3d7f45c5df03befdf0caa4b311fc0bcf6ea1 /src/bun.js/javascript.zig | |
parent | 4891be8d0d551fb4ee12510f63fed592f8bd8fc2 (diff) | |
download | bun-04cc1968dbe7947b63b93d800ecc1747dc11eb5f.tar.gz bun-04cc1968dbe7947b63b93d800ecc1747dc11eb5f.tar.zst bun-04cc1968dbe7947b63b93d800ecc1747dc11eb5f.zip |
Fix `createRequire()` in `node:module`
Fixes https://github.com/oven-sh/bun/issues/831
Fixes https://github.com/oven-sh/bun/issues/453
Diffstat (limited to 'src/bun.js/javascript.zig')
-rw-r--r-- | src/bun.js/javascript.zig | 58 |
1 files changed, 17 insertions, 41 deletions
diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig index 6da062b23..54486e38a 100644 --- a/src/bun.js/javascript.zig +++ b/src/bun.js/javascript.zig @@ -685,6 +685,7 @@ pub const VirtualMachine = struct { return this != .transpile; } }; + fn _fetch( jsc_vm: *VirtualMachine, globalObject: *JSGlobalObject, @@ -818,16 +819,10 @@ pub const VirtualMachine = struct { .hash = 0, }; }, - .@"node:buffer" => { - return ResolvedSource{ - .allocator = null, - .source_code = ZigString.init(""), - .specifier = ZigString.init("node:buffer"), - .source_url = ZigString.init("node:buffer"), - .hash = 0, - .tag = ResolvedSource.Tag.@"node:buffer", - }; - }, + .@"node:buffer" => return jsSyntheticModule(.@"node:buffer"), + .@"node:string_decoder" => return jsSyntheticModule(.@"node:string_decoder"), + .@"node:module" => return jsSyntheticModule(.@"node:module"), + .@"node:events" => return jsSyntheticModule(.@"node:events"), .@"node:stream" => { return ResolvedSource{ .allocator = null, @@ -837,16 +832,7 @@ pub const VirtualMachine = struct { .hash = 0, }; }, - .@"node:events" => { - return ResolvedSource{ - .allocator = null, - .source_code = ZigString.init(""), - .specifier = ZigString.init("node:events"), - .source_url = ZigString.init("node:events"), - .hash = 0, - .tag = ResolvedSource.Tag.@"node:events", - }; - }, + .@"node:fs/promises" => { return ResolvedSource{ .allocator = null, @@ -884,16 +870,6 @@ pub const VirtualMachine = struct { .hash = 0, }; }, - .@"node:string_decoder" => { - return ResolvedSource{ - .allocator = null, - .source_code = ZigString.init(""), - .specifier = ZigString.init("node:string_decoder"), - .source_url = ZigString.init("node:string_decoder"), - .hash = 0, - .tag = ResolvedSource.Tag.@"node:string_decoder", - }; - }, .@"bun:ffi" => { return ResolvedSource{ .allocator = null, @@ -943,17 +919,6 @@ pub const VirtualMachine = struct { .hash = 0, }; }, - .@"node:module" => { - return ResolvedSource{ - .allocator = null, - .source_code = ZigString.init( - @as(string, jsModuleFromFile("./module.exports.js")), - ), - .specifier = ZigString.init("node:module"), - .source_url = ZigString.init("node:module"), - .hash = 0, - }; - }, .@"node:perf_hooks" => { return ResolvedSource{ .allocator = null, @@ -3123,3 +3088,14 @@ fn jsModuleFromFile(comptime input: string) string { var contents = file.readToEndAlloc(bun.default_allocator, std.math.maxInt(usize)) catch @panic("Cannot read file: " ++ absolute_path); return contents; } + +inline fn jsSyntheticModule(comptime name: ResolvedSource.Tag) ResolvedSource { + return ResolvedSource{ + .allocator = null, + .source_code = ZigString.init(""), + .specifier = ZigString.init(@tagName(name)), + .source_url = ZigString.init(@tagName(name)), + .hash = 0, + .tag = name, + }; +} |