diff options
Diffstat (limited to '')
-rw-r--r-- | src/base64/base64.zig | 4 | ||||
-rw-r--r-- | src/import_record.zig | 1 | ||||
-rw-r--r-- | src/js_printer.zig | 23 | ||||
-rw-r--r-- | src/linker.zig | 5 |
4 files changed, 30 insertions, 3 deletions
diff --git a/src/base64/base64.zig b/src/base64/base64.zig index a5dcff5e0..3210eff29 100644 --- a/src/base64/base64.zig +++ b/src/base64/base64.zig @@ -38,11 +38,11 @@ pub fn encode(destination: []u8, source: []const u8) usize { /// ceiling[len / 3] * 4 /// /// -pub fn decodeLen(source: []const u8) usize { +pub fn decodeLen(source: anytype) usize { return (source.len / 4 * 3 + 2); } -pub fn encodeLen(source: []const u8) usize { +pub fn encodeLen(source: anytype) usize { return (source.len + 2) / 3 * 4; } diff --git a/src/import_record.zig b/src/import_record.zig index 9894048d7..e287db36e 100644 --- a/src/import_record.zig +++ b/src/import_record.zig @@ -157,5 +157,6 @@ pub const ImportRecord = struct { normal, import_path, css, + napi_module, }; }; diff --git a/src/js_printer.zig b/src/js_printer.zig index 319bb001b..7b4a469ae 100644 --- a/src/js_printer.zig +++ b/src/js_printer.zig @@ -3579,6 +3579,29 @@ pub fn NewPrinter( } return; }, + .napi_module => { + if (comptime is_bun_platform) { + const import_record = &p.import_records[s.import_record_index]; + + if (import_record.print_mode == .napi_module) { + p.printIndent(); + const quotes = p.bestQuoteCharForString(import_record.path.text, true); + p.print("var "); + p.printSymbol(s.namespace_ref); + p.print(" = "); + + p.print( + \\((path, cache, process) => {var mod = cache.get(path); if (mod) return mod.exports; mod = {exports: {}};process.dlopen(mod, path); cache.set(path, mod); return mod.exports;})( + ); + p.print(quotes); + p.printUTF8StringEscapedQuotes(import_record.path.text, quotes); + p.print(quotes); + p.print(", (globalThis[globalThis.Symbol.for('_dlcache')] ||= new globalThis.Map()), globalThis.process);"); + p.printSemicolonAfterStatement(); + } + } + return; + }, else => {}, } diff --git a/src/linker.zig b/src/linker.zig index 55e367dcb..099cf5e72 100644 --- a/src/linker.zig +++ b/src/linker.zig @@ -515,7 +515,7 @@ pub const Linker = struct { // If it's a namespace import, assume it's safe. // We can do this in the printer instead of creating a bunch of AST nodes here. // But we need to at least tell the printer that this needs to happen. - if (resolved_import.shouldAssumeCommonJS(import_record.kind)) { + if (loader != .napi and resolved_import.shouldAssumeCommonJS(import_record.kind)) { import_record.wrap_with_to_module = true; import_record.module_id = @truncate(u32, std.hash.Wyhash.hash(0, path.pretty)); @@ -790,6 +790,9 @@ pub const Linker = struct { // This saves us a less reliable string check import_record.print_mode = .css; }, + .napi => { + import_record.print_mode = .napi_module; + }, .wasm, .file => { import_record.print_mode = .import_path; }, |