diff options
Diffstat (limited to 'src/bun.js/module_loader.zig')
-rw-r--r-- | src/bun.js/module_loader.zig | 123 |
1 files changed, 10 insertions, 113 deletions
diff --git a/src/bun.js/module_loader.zig b/src/bun.js/module_loader.zig index ca6071d0d..c66649d3c 100644 --- a/src/bun.js/module_loader.zig +++ b/src/bun.js/module_loader.zig @@ -533,9 +533,9 @@ pub const RuntimeTranspilerStore = struct { var printer = source_code_printer.?.*; printer.ctx.reset(); - const written = brk: { + { defer source_code_printer.?.* = printer; - break :brk bundler.printWithSourceMap( + _ = bundler.printWithSourceMap( parse_result, @TypeOf(&printer), &printer, @@ -545,11 +545,6 @@ pub const RuntimeTranspilerStore = struct { this.parse_error = err; return; }; - }; - - if (written == 0) { - this.parse_error = error.PrintingErrorWriteFailed; - return; } if (comptime Environment.dump_source) { @@ -1235,10 +1230,7 @@ pub const ModuleLoader = struct { SavedSourceMap.SourceMapHandler.init(&jsc_vm.source_mappings), ); }; - - if (written == 0) { - return error.PrintingErrorWriteFailed; - } + _ = written; if (comptime Environment.dump_source) { try dumpSource(specifier, &printer); @@ -1653,7 +1645,6 @@ pub const ModuleLoader = struct { .hash = 0, }; } - return error.PrintingErrorWriteFailed; } if (comptime Environment.dump_source) { @@ -2025,7 +2016,11 @@ pub const ModuleLoader = struct { JSC.markBinding(@src()); var log = logger.Log.init(jsc_vm.bundler.allocator); defer log.deinit(); - if (ModuleLoader.fetchBuiltinModule(jsc_vm, specifier.*, &log, false) catch |err| { + + if (ModuleLoader.fetchBuiltinModule( + jsc_vm, + specifier.*, + ) catch |err| { if (err == error.AsyncModule) { unreachable; } @@ -2156,7 +2151,7 @@ pub const ModuleLoader = struct { return globalObject.runOnLoadPlugins(bun.String.init(namespace), bun.String.init(after_namespace), .bun) orelse return JSValue.zero; } - pub fn fetchBuiltinModule(jsc_vm: *VirtualMachine, specifier: bun.String, log: *logger.Log, comptime disable_transpilying: bool) !?ResolvedSource { + pub fn fetchBuiltinModule(jsc_vm: *VirtualMachine, specifier: bun.String) !?ResolvedSource { if (jsc_vm.node_modules != null and specifier.eqlComptime(JSC.bun_file_import_path)) { // We kind of need an abstraction around this. // Basically we should subclass JSC::SourceCode with: @@ -2183,9 +2178,6 @@ pub const ModuleLoader = struct { }; } else if (HardcodedModule.Map.getWithEql(specifier, bun.String.eqlComptime)) |hardcoded| { switch (hardcoded) { - // This is all complicated because the imports have to be linked and we want to run the printer on it - // so it consistently handles bundled imports - // we can't take the shortcut of just directly importing the file, sadly. .@"bun:main" => { defer { if (jsc_vm.worker) |worker| { @@ -2193,104 +2185,9 @@ pub const ModuleLoader = struct { } } - if (comptime disable_transpilying) { - return ResolvedSource{ - .allocator = null, - .source_code = bun.String.init(jsc_vm.entry_point.source.contents), - .specifier = bun.String.init(bun.asByteSlice(JSC.VirtualMachine.main_file_name)), - .source_url = ZigString.init(bun.asByteSlice(JSC.VirtualMachine.main_file_name)), - .hash = 0, - }; - } - defer jsc_vm.transpiled_count += 1; - var arena: bun.ArenaAllocator = undefined; - - // Attempt to reuse the Arena from the parser when we can - // This code is potentially re-entrant, so only one Arena can be reused at a time - // That's why we have to check if the Arena is null - // - // Using an Arena here is a significant memory optimization when loading many files - if (jsc_vm.parser_arena) |shared| { - arena = shared; - jsc_vm.parser_arena = null; - _ = arena.reset(.retain_capacity); - } else { - arena = bun.ArenaAllocator.init(jsc_vm.allocator); - } - - defer { - if (jsc_vm.parser_arena == null) { - jsc_vm.parser_arena = arena; - } else { - arena.deinit(); - } - } - - var bundler = &jsc_vm.bundler; - var old = jsc_vm.bundler.log; - jsc_vm.bundler.log = log; - jsc_vm.bundler.linker.log = log; - jsc_vm.bundler.resolver.log = log; - defer { - jsc_vm.bundler.log = old; - jsc_vm.bundler.linker.log = old; - jsc_vm.bundler.resolver.log = old; - } - - var jsx = bundler.options.jsx; - jsx.parse = false; - var opts = js_parser.Parser.Options.init(jsx, .js); - opts.enable_legacy_bundling = false; - opts.legacy_transform_require_to_import = false; - opts.features.dynamic_require = true; - opts.can_import_from_bundle = bundler.options.node_modules_bundle != null; - opts.features.hot_module_reloading = false; - opts.features.top_level_await = true; - opts.features.react_fast_refresh = false; - opts.features.minify_identifiers = bundler.options.minify_identifiers; - opts.features.minify_syntax = bundler.options.minify_syntax; - opts.filepath_hash_for_hmr = 0; - opts.warn_about_unbundled_modules = false; - opts.macro_context = &jsc_vm.bundler.macro_context.?; - const main_ast = ((bundler.resolver.caches.js.parse(arena.allocator(), opts, bundler.options.define, bundler.log, &jsc_vm.entry_point.source) catch null) orelse { - return error.ParseError; - }).ast; - var parse_result = ParseResult{ .source = jsc_vm.entry_point.source, .ast = main_ast, .loader = .js, .input_fd = null }; - var file_path = Fs.Path.init(bundler.fs.top_level_dir); - file_path.name.dir = bundler.fs.top_level_dir; - file_path.name.base = "bun:main"; - try bundler.linker.link( - file_path, - &parse_result, - jsc_vm.origin, - .absolute_path, - false, - true, - ); - var printer = JSC.VirtualMachine.source_code_printer.?.*; - var written: usize = undefined; - printer.ctx.reset(); - { - defer JSC.VirtualMachine.source_code_printer.?.* = printer; - written = try jsc_vm.bundler.printWithSourceMap( - parse_result, - @TypeOf(&printer), - &printer, - .esm_ascii, - SavedSourceMap.SourceMapHandler.init(&jsc_vm.source_mappings), - ); - } - - if (comptime Environment.dump_source) - try dumpSource(JSC.VirtualMachine.main_file_name, &printer); - - if (written == 0) { - return error.PrintingErrorWriteFailed; - } - return ResolvedSource{ .allocator = null, - .source_code = bun.String.createLatin1(printer.ctx.written), + .source_code = bun.String.create(jsc_vm.entry_point.source.contents), .specifier = specifier, .source_url = ZigString.init(bun.asByteSlice(JSC.VirtualMachine.main_file_name)), .hash = 0, |