-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathModuleLoader.zig
More file actions
1391 lines (1248 loc) · 55.8 KB
/
ModuleLoader.zig
File metadata and controls
1391 lines (1248 loc) · 55.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const ModuleLoader = @This();
pub const node_fallbacks = @import("../node_fallbacks.zig");
pub const AsyncModule = @import("./AsyncModule.zig").AsyncModule;
pub const RuntimeTranspilerStore = @import("./RuntimeTranspilerStore.zig").RuntimeTranspilerStore;
pub const HardcodedModule = @import("./HardcodedModule.zig").HardcodedModule;
transpile_source_code_arena: ?*bun.ArenaAllocator = null,
eval_source: ?*logger.Source = null,
comptime {
_ = Bun__transpileVirtualModule;
_ = Bun__runVirtualModule;
_ = Bun__transpileFile;
_ = Bun__fetchBuiltinModule;
_ = Bun__getDefaultLoader;
}
pub var is_allowed_to_use_internal_testing_apis = false;
/// This must be called after calling transpileSourceCode
pub fn resetArena(this: *ModuleLoader, jsc_vm: *VirtualMachine) void {
bun.assert(&jsc_vm.module_loader == this);
if (this.transpile_source_code_arena) |arena| {
if (jsc_vm.smol) {
_ = arena.reset(.free_all);
} else {
_ = arena.reset(.{ .retain_with_limit = 8 * 1024 * 1024 });
}
}
}
pub fn resolveEmbeddedFile(vm: *VirtualMachine, path_buf: *bun.PathBuffer, input_path: []const u8, extname: []const u8) ?[]const u8 {
if (input_path.len == 0) return null;
var graph = vm.standalone_module_graph orelse return null;
const file = graph.find(input_path) orelse return null;
if (comptime Environment.isLinux) {
// TODO: use /proc/fd/12346 instead! Avoid the copy!
}
// atomically write to a tmpfile and then move it to the final destination
const tmpname_buf = bun.path_buffer_pool.get();
defer bun.path_buffer_pool.put(tmpname_buf);
const tmpfilename = bun.fs.FileSystem.tmpname(extname, tmpname_buf, bun.hash(file.name)) catch return null;
const tmpdir: bun.FD = .fromStdDir(bun.fs.FileSystem.instance.tmpdir() catch return null);
// First we open the tmpfile, to avoid any other work in the event of failure.
const tmpfile = bun.Tmpfile.create(tmpdir, tmpfilename).unwrap() catch return null;
defer tmpfile.fd.close();
switch (bun.api.node.fs.NodeFS.writeFileWithPathBuffer(
tmpname_buf, // not used
.{
.data = .{
.encoded_slice = ZigString.Slice.fromUTF8NeverFree(file.contents),
},
.dirfd = tmpdir,
.file = .{ .fd = tmpfile.fd },
.encoding = .buffer,
},
)) {
.err => {
return null;
},
else => {},
}
return bun.path.joinAbsStringBuf(bun.fs.FileSystem.RealFS.tmpdirPath(), path_buf, &[_]string{tmpfilename}, .auto);
}
pub export fn Bun__getDefaultLoader(global: *JSGlobalObject, str: *const bun.String) api.Loader {
var jsc_vm = global.bunVM();
const filename = str.toUTF8(jsc_vm.allocator);
defer filename.deinit();
const loader = jsc_vm.transpiler.options.loader(Fs.PathName.init(filename.slice()).ext).toAPI();
if (loader == .file) {
return api.Loader.js;
}
return loader;
}
pub fn transpileSourceCode(
jsc_vm: *VirtualMachine,
specifier: string,
referrer: string,
input_specifier: String,
path: Fs.Path,
loader: options.Loader,
module_type: options.ModuleType,
log: *logger.Log,
virtual_source: ?*const logger.Source,
promise_ptr: ?*?*jsc.JSInternalPromise,
source_code_printer: *js_printer.BufferPrinter,
globalObject: ?*JSGlobalObject,
comptime flags: FetchFlags,
) !ResolvedSource {
const disable_transpilying = comptime flags.disableTranspiling();
if (comptime disable_transpilying) {
if (!(loader.isJavaScriptLike() or loader == .toml or loader == .yaml or loader == .json5 or loader == .text or loader == .json or loader == .jsonc or loader == .mdx)) {
// Don't print "export default <file path>"
return ResolvedSource{
.allocator = null,
.source_code = bun.String.empty,
.specifier = input_specifier,
.source_url = input_specifier.createIfDifferent(path.text),
};
}
}
switch (loader) {
.js, .jsx, .ts, .tsx, .json, .jsonc, .toml, .yaml, .json5, .text, .md, .mdx => {
// Ensure that if there was an ASTMemoryAllocator in use, it's not used anymore.
var ast_scope = js_ast.ASTMemoryAllocator.Scope{};
ast_scope.enter();
defer ast_scope.exit();
jsc_vm.transpiled_count += 1;
jsc_vm.transpiler.resetStore();
const hash = bun.Watcher.getHash(path.text);
const is_main = jsc_vm.main.len == path.text.len and
jsc_vm.main_hash == hash and
strings.eqlLong(jsc_vm.main, path.text, false);
var arena_: ?*bun.ArenaAllocator = brk: {
// 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.module_loader.transpile_source_code_arena) |shared| {
jsc_vm.module_loader.transpile_source_code_arena = null;
break :brk shared;
}
// we must allocate the arena so that the pointer it points to is always valid.
const arena = try jsc_vm.allocator.create(bun.ArenaAllocator);
arena.* = bun.ArenaAllocator.init(bun.default_allocator);
break :brk arena;
};
var give_back_arena = true;
defer {
if (give_back_arena) {
if (jsc_vm.module_loader.transpile_source_code_arena == null) {
// when .print_source is used
// caller is responsible for freeing the arena
if (flags != .print_source) {
if (jsc_vm.smol) {
_ = arena_.?.reset(.free_all);
} else {
_ = arena_.?.reset(.{ .retain_with_limit = 8 * 1024 * 1024 });
}
}
jsc_vm.module_loader.transpile_source_code_arena = arena_;
} else {
arena_.?.deinit();
jsc_vm.allocator.destroy(arena_.?);
}
}
}
var arena = arena_.?;
const allocator = arena.allocator();
var fd: ?StoredFileDescriptorType = null;
var package_json: ?*PackageJSON = null;
if (jsc_vm.bun_watcher.indexOf(hash)) |index| {
fd = jsc_vm.bun_watcher.watchlist().items(.fd)[index].unwrapValid();
package_json = jsc_vm.bun_watcher.watchlist().items(.package_json)[index];
}
var cache = jsc.RuntimeTranspilerCache{
.output_code_allocator = allocator,
.sourcemap_allocator = bun.default_allocator,
.esm_record_allocator = bun.default_allocator,
};
const old = jsc_vm.transpiler.log;
jsc_vm.transpiler.log = log;
jsc_vm.transpiler.linker.log = log;
jsc_vm.transpiler.resolver.log = log;
if (jsc_vm.transpiler.resolver.package_manager) |pm| {
pm.log = log;
}
defer {
jsc_vm.transpiler.log = old;
jsc_vm.transpiler.linker.log = old;
jsc_vm.transpiler.resolver.log = old;
if (jsc_vm.transpiler.resolver.package_manager) |pm| {
pm.log = old;
}
}
// this should be a cheap lookup because 24 bytes == 8 * 3 so it's read 3 machine words
const is_node_override = strings.hasPrefixComptime(specifier, node_fallbacks.import_path);
const macro_remappings = if (jsc_vm.macro_mode or !jsc_vm.has_any_macro_remappings or is_node_override)
MacroRemap{}
else
jsc_vm.transpiler.options.macro_remap;
var fallback_source: logger.Source = undefined;
// Usually, we want to close the input file automatically.
//
// If we're re-using the file descriptor from the fs watcher
// Do not close it because that will break the kqueue-based watcher
//
var should_close_input_file_fd = fd == null;
// We don't want cjs wrappers around non-js files
const module_type_only_for_wrappables = switch (loader) {
.js, .jsx, .ts, .tsx => module_type,
else => .unknown,
};
var input_file_fd: StoredFileDescriptorType = bun.invalid_fd;
var parse_options = Transpiler.ParseOptions{
.allocator = allocator,
.path = path,
.loader = loader,
.dirname_fd = bun.invalid_fd,
.file_descriptor = fd,
.file_fd_ptr = &input_file_fd,
.file_hash = hash,
.macro_remappings = macro_remappings,
.jsx = jsc_vm.transpiler.options.jsx,
.emit_decorator_metadata = jsc_vm.transpiler.options.emit_decorator_metadata,
.experimental_decorators = jsc_vm.transpiler.options.experimental_decorators,
.virtual_source = virtual_source,
.dont_bundle_twice = true,
.allow_commonjs = true,
.module_type = module_type_only_for_wrappables,
.inject_jest_globals = jsc_vm.transpiler.options.rewrite_jest_for_tests,
.keep_json_and_toml_as_one_statement = true,
.allow_bytecode_cache = true,
.set_breakpoint_on_first_line = is_main and
jsc_vm.debugger != null and
jsc_vm.debugger.?.set_breakpoint_on_first_line and
setBreakPointOnFirstLine(),
.runtime_transpiler_cache = if (!disable_transpilying and !jsc.RuntimeTranspilerCache.is_disabled) &cache else null,
.remove_cjs_module_wrapper = is_main and jsc_vm.module_loader.eval_source != null,
};
defer {
if (should_close_input_file_fd and input_file_fd != bun.invalid_fd) {
input_file_fd.close();
input_file_fd = bun.invalid_fd;
}
}
if (is_node_override) {
if (node_fallbacks.contentsFromPath(specifier)) |code| {
const fallback_path = Fs.Path.initWithNamespace(specifier, "node");
fallback_source = logger.Source{ .path = fallback_path, .contents = code };
parse_options.virtual_source = &fallback_source;
}
}
var parse_result: ParseResult = switch (disable_transpilying or
(loader == .json)) {
inline else => |return_file_only| brk: {
break :brk jsc_vm.transpiler.parseMaybeReturnFileOnly(
parse_options,
null,
return_file_only,
) orelse {
if (comptime !disable_transpilying) {
if (jsc_vm.isWatcherEnabled()) {
if (input_file_fd.isValid()) {
if (!is_node_override and std.fs.path.isAbsolute(path.text) and !strings.contains(path.text, "node_modules")) {
should_close_input_file_fd = false;
_ = jsc_vm.bun_watcher.addFile(
input_file_fd,
path.text,
hash,
loader,
.invalid,
package_json,
true,
);
}
}
}
}
give_back_arena = false;
return error.ParseError;
};
},
};
const source = &parse_result.source;
if (parse_result.loader == .wasm) {
return transpileSourceCode(
jsc_vm,
specifier,
referrer,
input_specifier,
path,
.wasm,
.unknown, // cjs/esm don't make sense for wasm
log,
&parse_result.source,
promise_ptr,
source_code_printer,
globalObject,
flags,
);
}
if (comptime !disable_transpilying) {
if (jsc_vm.isWatcherEnabled()) {
if (input_file_fd.isValid()) {
if (!is_node_override and std.fs.path.isAbsolute(path.text) and !strings.contains(path.text, "node_modules")) {
should_close_input_file_fd = false;
_ = jsc_vm.bun_watcher.addFile(
input_file_fd,
path.text,
hash,
loader,
.invalid,
package_json,
true,
);
}
}
}
}
if (jsc_vm.transpiler.log.errors > 0) {
give_back_arena = false;
return error.ParseError;
}
if (loader == .json) {
return ResolvedSource{
.allocator = null,
.source_code = bun.String.cloneUTF8(source.contents),
.specifier = input_specifier,
.source_url = input_specifier.createIfDifferent(path.text),
.tag = ResolvedSource.Tag.json_for_object_loader,
};
}
if (comptime disable_transpilying) {
return ResolvedSource{
.allocator = null,
.source_code = switch (comptime flags) {
.print_source_and_clone => bun.String.init(jsc_vm.allocator.dupe(u8, source.contents) catch unreachable),
.print_source => bun.String.init(source.contents),
else => @compileError("unreachable"),
},
.specifier = input_specifier,
.source_url = input_specifier.createIfDifferent(path.text),
};
}
if (loader == .json or loader == .jsonc or loader == .toml or loader == .yaml or loader == .json5) {
if (parse_result.empty) {
return ResolvedSource{
.allocator = null,
.specifier = input_specifier,
.source_url = input_specifier.createIfDifferent(path.text),
.jsvalue_for_export = JSValue.createEmptyObject(jsc_vm.global, 0),
.tag = .exports_object,
};
}
return ResolvedSource{
.allocator = null,
.specifier = input_specifier,
.source_url = input_specifier.createIfDifferent(path.text),
.jsvalue_for_export = parse_result.ast.parts.at(0).stmts[0].data.s_expr.value.toJS(allocator, globalObject orelse jsc_vm.global) catch |e| panic("Unexpected JS error: {s}", .{@errorName(e)}),
.tag = .exports_object,
};
}
if (parse_result.already_bundled != .none) {
const bytecode_slice = parse_result.already_bundled.bytecodeSlice();
return ResolvedSource{
.allocator = null,
.source_code = bun.String.cloneLatin1(source.contents),
.specifier = input_specifier,
.source_url = input_specifier.createIfDifferent(path.text),
.already_bundled = true,
.bytecode_cache = if (bytecode_slice.len > 0) bytecode_slice.ptr else null,
.bytecode_cache_size = bytecode_slice.len,
.is_commonjs_module = parse_result.already_bundled.isCommonJS(),
};
}
if (parse_result.empty) {
const was_cjs = (loader == .js or loader == .ts) and brk: {
const ext = std.fs.path.extension(source.path.text);
break :brk strings.eqlComptime(ext, ".cjs") or strings.eqlComptime(ext, ".cts");
};
if (was_cjs) {
return .{
.allocator = null,
.source_code = bun.String.static("(function(){})"),
.specifier = input_specifier,
.source_url = input_specifier.createIfDifferent(path.text),
.is_commonjs_module = true,
.tag = .javascript,
};
}
}
if (cache.entry) |*entry| {
jsc_vm.source_mappings.putMappings(source, .{
.list = .{ .items = @constCast(entry.sourcemap), .capacity = entry.sourcemap.len },
.allocator = bun.default_allocator,
}) catch {};
if (comptime Environment.allow_assert) {
dumpSourceString(jsc_vm, specifier, entry.output_code.byteSlice());
}
// TODO: module_info is only needed for standalone ESM bytecode.
// For now, skip it entirely in the runtime transpiler.
const module_info: ?*analyze_transpiled_module.ModuleInfoDeserialized = null;
return ResolvedSource{
.allocator = null,
.source_code = switch (entry.output_code) {
.string => entry.output_code.string,
.utf8 => brk: {
const result = bun.String.cloneUTF8(entry.output_code.utf8);
cache.output_code_allocator.free(entry.output_code.utf8);
entry.output_code.utf8 = "";
break :brk result;
},
},
.specifier = input_specifier,
.source_url = input_specifier.createIfDifferent(path.text),
.is_commonjs_module = entry.metadata.module_type == .cjs,
.module_info = module_info,
.tag = brk: {
if (entry.metadata.module_type == .cjs and source.path.isFile()) {
const actual_package_json: *PackageJSON = package_json orelse brk2: {
// this should already be cached virtually always so it's fine to do this
const dir_info = (jsc_vm.transpiler.resolver.readDirInfo(source.path.name.dir) catch null) orelse
break :brk .javascript;
break :brk2 dir_info.package_json orelse dir_info.enclosing_package_json;
} orelse break :brk .javascript;
if (actual_package_json.module_type == .esm) {
break :brk ResolvedSource.Tag.package_json_type_module;
}
}
break :brk ResolvedSource.Tag.javascript;
},
};
}
const start_count = jsc_vm.transpiler.linker.import_counter;
// We _must_ link because:
// - node_modules bundle won't be properly
try jsc_vm.transpiler.linker.link(
path,
&parse_result,
jsc_vm.origin,
.absolute_path,
false,
true,
);
if (parse_result.pending_imports.len > 0) {
if (promise_ptr == null) {
return error.UnexpectedPendingResolution;
}
if (source.contents_is_recycled) {
// this shared buffer is about to become owned by the AsyncModule struct
jsc_vm.transpiler.resolver.caches.fs.resetSharedBuffer(
jsc_vm.transpiler.resolver.caches.fs.sharedBuffer(),
);
}
jsc_vm.modules.enqueue(
globalObject.?,
.{
.parse_result = parse_result,
.path = path,
.loader = loader,
.fd = fd,
.package_json = package_json,
.hash = hash,
.promise_ptr = promise_ptr,
.specifier = specifier,
.referrer = referrer,
.arena = arena,
},
);
give_back_arena = false;
return error.AsyncModule;
}
if (!jsc_vm.macro_mode)
jsc_vm.resolved_count += jsc_vm.transpiler.linker.import_counter - start_count;
jsc_vm.transpiler.linker.import_counter = 0;
const is_commonjs_module = parse_result.ast.has_commonjs_export_names or parse_result.ast.exports_kind == .cjs;
// TODO: module_info is only needed for standalone ESM bytecode.
// For now, skip it entirely in the runtime transpiler.
const module_info: ?*analyze_transpiled_module.ModuleInfo = null;
var printer = source_code_printer.*;
printer.ctx.reset();
defer source_code_printer.* = printer;
_ = brk: {
var mapper = jsc_vm.sourceMapHandler(&printer);
break :brk try jsc_vm.transpiler.printWithSourceMap(
parse_result,
@TypeOf(&printer),
&printer,
.esm_ascii,
mapper.get(),
module_info,
);
};
if (comptime Environment.dump_source) {
dumpSource(jsc_vm, specifier, &printer);
}
defer {
if (is_main) {
jsc_vm.has_loaded = true;
}
}
const module_info_deserialized: ?*anyopaque = if (module_info) |mi| @ptrCast(mi.asDeserialized()) else null;
if (jsc_vm.isWatcherEnabled()) {
var resolved_source = jsc_vm.refCountedResolvedSource(printer.ctx.written, input_specifier, path.text, null, false);
resolved_source.is_commonjs_module = is_commonjs_module;
resolved_source.module_info = module_info_deserialized;
return resolved_source;
}
// Pass along package.json type "module" if set.
const tag: ResolvedSource.Tag = switch (loader) {
.json, .jsonc => .json_for_object_loader,
.js, .jsx, .ts, .tsx => brk: {
const module_type_ = if (package_json) |pkg| pkg.module_type else module_type;
break :brk switch (module_type_) {
.esm => .package_json_type_module,
.cjs => .package_json_type_commonjs,
else => .javascript,
};
},
else => .javascript,
};
return .{
.allocator = null,
.source_code = brk: {
const written = printer.ctx.getWritten();
const result = cache.output_code orelse bun.String.cloneLatin1(written);
if (written.len > 1024 * 1024 * 2 or jsc_vm.smol) {
printer.ctx.buffer.deinit();
}
break :brk result;
},
.specifier = input_specifier,
.source_url = input_specifier.createIfDifferent(path.text),
.is_commonjs_module = is_commonjs_module,
.module_info = module_info_deserialized,
.tag = tag,
};
},
// provideFetch() should be called
.napi => unreachable,
// .wasm => {
// jsc_vm.transpiled_count += 1;
// var fd: ?StoredFileDescriptorType = null;
// var allocator = if (jsc_vm.has_loaded) jsc_vm.arena.allocator() else jsc_vm.allocator;
// const hash = http.Watcher.getHash(path.text);
// if (jsc_vm.watcher) |watcher| {
// if (watcher.indexOf(hash)) |index| {
// const _fd = watcher.watchlist().items(.fd)[index];
// fd = if (_fd > 0) _fd else null;
// }
// }
// var parse_options = Transpiler.ParseOptions{
// .allocator = allocator,
// .path = path,
// .loader = loader,
// .dirname_fd = 0,
// .file_descriptor = fd,
// .file_hash = hash,
// .macro_remappings = MacroRemap{},
// .jsx = jsc_vm.transpiler.options.jsx,
// };
// var parse_result = jsc_vm.transpiler.parse(
// parse_options,
// null,
// ) orelse {
// return error.ParseError;
// };
// return ResolvedSource{
// .allocator = if (jsc_vm.has_loaded) &jsc_vm.allocator else null,
// .source_code = ZigString.init(jsc_vm.allocator.dupe(u8, source.contents) catch unreachable),
// .specifier = ZigString.init(specifier),
// .source_url = input_specifier.createIfDifferent(path.text),
// .tag = ResolvedSource.Tag.wasm,
// };
// },
.wasm => {
if (strings.eqlComptime(referrer, "undefined") and strings.eqlLong(jsc_vm.main, path.text, true)) {
if (virtual_source) |source| {
if (globalObject) |globalThis| {
// attempt to avoid reading the WASM file twice.
const decoded: jsc.DecodedJSValue = .{
.u = .{ .ptr = @ptrCast(globalThis) },
};
const globalValue = decoded.encode();
globalValue.put(
globalThis,
ZigString.static("wasmSourceBytes"),
try jsc.ArrayBuffer.create(globalThis, source.contents, .Uint8Array),
);
}
}
return ResolvedSource{
.allocator = null,
.source_code = bun.String.static(@embedFile("../js/wasi-runner.js")),
.specifier = input_specifier,
.source_url = input_specifier.createIfDifferent(path.text),
.tag = .esm,
};
}
return transpileSourceCode(
jsc_vm,
specifier,
referrer,
input_specifier,
path,
.file,
.unknown, // cjs/esm don't make sense for wasm
log,
virtual_source,
promise_ptr,
source_code_printer,
globalObject,
flags,
);
},
.sqlite_embedded, .sqlite => {
const sqlite_module_source_code_string = brk: {
if (jsc_vm.hot_reload == .hot) {
break :brk
\\// Generated code
\\import {Database} from 'bun:sqlite';
\\const {path} = import.meta;
\\
\\// Don't reload the database if it's already loaded
\\const registry = (globalThis[Symbol.for("bun:sqlite:hot")] ??= new Map());
\\
\\export let db = registry.get(path);
\\export const __esModule = true;
\\if (!db) {
\\ // Load the database
\\ db = new Database(path);
\\ registry.set(path, db);
\\}
\\
\\export default db;
;
}
break :brk
\\// Generated code
\\import {Database} from 'bun:sqlite';
\\export const db = new Database(import.meta.path);
\\
\\export const __esModule = true;
\\export default db;
;
};
return ResolvedSource{
.allocator = null,
.source_code = bun.String.cloneUTF8(sqlite_module_source_code_string),
.specifier = input_specifier,
.source_url = input_specifier.createIfDifferent(path.text),
.tag = .esm,
};
},
.html => {
if (flags.disableTranspiling()) {
return ResolvedSource{
.allocator = null,
.source_code = bun.String.empty,
.specifier = input_specifier,
.source_url = input_specifier.createIfDifferent(path.text),
.tag = .esm,
};
}
if (globalObject == null) {
return error.NotSupported;
}
const html_bundle = try jsc.API.HTMLBundle.init(globalObject.?, path.text);
return ResolvedSource{
.allocator = &jsc_vm.allocator,
.jsvalue_for_export = html_bundle.toJS(globalObject.?),
.specifier = input_specifier,
.source_url = input_specifier.createIfDifferent(path.text),
.tag = .export_default_object,
};
},
else => {
if (flags.disableTranspiling()) {
return ResolvedSource{
.allocator = null,
.source_code = bun.String.empty,
.specifier = input_specifier,
.source_url = input_specifier.createIfDifferent(path.text),
.tag = .esm,
};
}
if (virtual_source == null) {
if (jsc_vm.isWatcherEnabled()) auto_watch: {
if (std.fs.path.isAbsolute(path.text) and !strings.contains(path.text, "node_modules")) {
const input_fd: bun.StoredFileDescriptorType = brk: {
// on macOS, we need a file descriptor to receive event notifications on it.
// so we use O_EVTONLY to open the file descriptor without asking any additional permissions.
if (bun.Watcher.requires_file_descriptors) {
switch (bun.sys.open(
&(std.posix.toPosixPath(path.text) catch break :auto_watch),
bun.c.O_EVTONLY,
0,
)) {
.err => break :auto_watch,
.result => |fd| break :brk fd,
}
} else {
// Otherwise, don't even bother opening it.
break :brk .invalid;
}
};
const hash = bun.Watcher.getHash(path.text);
switch (jsc_vm.bun_watcher.addFile(
input_fd,
path.text,
hash,
loader,
.invalid,
null,
true,
)) {
.err => {
if (comptime Environment.isMac) {
// If any error occurs and we just
// opened the file descriptor to
// receive event notifications on
// it, we should close it.
if (input_fd.isValid()) {
input_fd.close();
}
}
// we don't consider it a failure if we cannot watch the file
// they didn't open the file
},
.result => {},
}
}
}
}
const value = brk: {
if (!jsc_vm.origin.isEmpty()) {
var buf = bun.handleOom(MutableString.init2048(jsc_vm.allocator));
defer buf.deinit();
var writer = buf.writer();
jsc.API.Bun.getPublicPath(specifier, jsc_vm.origin, @TypeOf(&writer), &writer);
break :brk try bun.String.createUTF8ForJS(globalObject.?, buf.slice());
}
break :brk try bun.String.createUTF8ForJS(globalObject.?, path.text);
};
return ResolvedSource{
.allocator = null,
.jsvalue_for_export = value,
.specifier = input_specifier,
.source_url = input_specifier.createIfDifferent(path.text),
.tag = .export_default_object,
};
},
}
}
pub export fn Bun__resolveAndFetchBuiltinModule(
jsc_vm: *VirtualMachine,
specifier: *bun.String,
ret: *jsc.ErrorableResolvedSource,
) bool {
jsc.markBinding(@src());
var log = logger.Log.init(jsc_vm.transpiler.allocator);
defer log.deinit();
const alias = HardcodedModule.Alias.bun_aliases.getWithEql(specifier.*, bun.String.eqlComptime) orelse
return false;
const hardcoded = HardcodedModule.map.get(alias.path) orelse {
bun.debugAssert(false);
return false;
};
ret.* = .ok(
getHardcodedModule(jsc_vm, specifier.*, hardcoded) orelse
return false,
);
return true;
}
pub export fn Bun__fetchBuiltinModule(
jsc_vm: *VirtualMachine,
globalObject: *JSGlobalObject,
specifier: *bun.String,
referrer: *bun.String,
ret: *jsc.ErrorableResolvedSource,
) bool {
jsc.markBinding(@src());
var log = logger.Log.init(jsc_vm.transpiler.allocator);
defer log.deinit();
if (ModuleLoader.fetchBuiltinModule(
jsc_vm,
specifier.*,
) catch |err| {
if (err == error.AsyncModule) {
unreachable;
}
VirtualMachine.processFetchLog(globalObject, specifier.*, referrer.*, &log, ret, err);
return true;
}) |builtin| {
ret.* = jsc.ErrorableResolvedSource.ok(builtin);
return true;
} else {
return false;
}
}
const always_sync_modules = .{"reflect-metadata"};
pub export fn Bun__transpileFile(
jsc_vm: *VirtualMachine,
globalObject: *JSGlobalObject,
specifier_ptr: *bun.String,
referrer: *bun.String,
type_attribute: ?*const bun.String,
ret: *jsc.ErrorableResolvedSource,
allow_promise: bool,
is_commonjs_require: bool,
_force_loader_type: bun.schema.api.Loader,
) ?*anyopaque {
jsc.markBinding(@src());
const force_loader_type: bun.options.Loader.Optional = .fromAPI(_force_loader_type);
var log = logger.Log.init(jsc_vm.transpiler.allocator);
defer log.deinit();
var _specifier = specifier_ptr.toUTF8(jsc_vm.allocator);
var referrer_slice = referrer.toUTF8(jsc_vm.allocator);
defer _specifier.deinit();
defer referrer_slice.deinit();
var type_attribute_str: ?string = null;
if (type_attribute) |attribute| if (attribute.asUTF8()) |attr_utf8| {
type_attribute_str = attr_utf8;
};
var virtual_source_to_use: ?logger.Source = null;
var blob_to_deinit: ?jsc.WebCore.Blob = null;
var lr = options.getLoaderAndVirtualSource(_specifier.slice(), jsc_vm, &virtual_source_to_use, &blob_to_deinit, type_attribute_str) catch {
ret.* = jsc.ErrorableResolvedSource.err(error.JSErrorObject, globalObject.ERR(.MODULE_NOT_FOUND, "Blob not found", .{}).toJS());
return null;
};
defer if (blob_to_deinit) |*blob| blob.deinit();
if (force_loader_type.unwrap()) |loader_type| {
@branchHint(.unlikely);
bun.assert(!is_commonjs_require);
lr.loader = loader_type;
} else if (is_commonjs_require and jsc_vm.has_mutated_built_in_extensions > 0) {
@branchHint(.unlikely);
if (node_module_module.findLongestRegisteredExtension(jsc_vm, _specifier.slice())) |entry| {
switch (entry) {
.loader => |loader| {
lr.loader = loader;
},
.custom => |strong| {
ret.* = jsc.ErrorableResolvedSource.ok(ResolvedSource{
.allocator = null,
.source_code = bun.String.empty,
.specifier = .empty,
.source_url = .empty,
.cjs_custom_extension_index = strong.get(),
.tag = .common_js_custom_extension,
});
return null;
},
}
}
}
const module_type: options.ModuleType = brk: {
const ext = lr.path.name.ext;
// regular expression /.[cm][jt]s$/
if (ext.len == ".cjs".len) {
if (strings.eqlComptimeIgnoreLen(ext, ".cjs"))
break :brk .cjs;
if (strings.eqlComptimeIgnoreLen(ext, ".mjs"))
break :brk .esm;
if (strings.eqlComptimeIgnoreLen(ext, ".cts"))
break :brk .cjs;
if (strings.eqlComptimeIgnoreLen(ext, ".mts"))
break :brk .esm;
}
// regular expression /.[jt]s$/
if (ext.len == ".ts".len) {
if (strings.eqlComptimeIgnoreLen(ext, ".js") or
strings.eqlComptimeIgnoreLen(ext, ".ts"))
{
// Use the package.json module type if it exists
break :brk if (lr.package_json) |pkg|
pkg.module_type
else
.unknown;
}
}
// For JSX TSX and other extensions, let the file contents.
break :brk .unknown;
};
const pkg_name: ?[]const u8 = if (lr.package_json) |pkg|
if (pkg.name.len > 0) pkg.name else null
else
null;
// We only run the transpiler concurrently when we can.
// Today, that's:
//
// Import Statements (import 'foo')
// Import Expressions (import('foo'))
//
transpile_async: {
if (comptime bun.FeatureFlags.concurrent_transpiler) {
const concurrent_loader = lr.loader orelse .file;
if (blob_to_deinit == null and
allow_promise and
(jsc_vm.has_loaded or jsc_vm.is_in_preload) and
concurrent_loader.isJavaScriptLike() and
!lr.is_main and
// Plugins make this complicated,
// TODO: allow running concurrently when no onLoad handlers match a plugin.
jsc_vm.plugin_runner == null and jsc_vm.transpiler_store.enabled)
{
// This absolutely disgusting hack is a workaround in cases
// where an async import is made to a CJS file with side
// effects that other modules depend on, without incurring
// the cost of transpiling/loading CJS modules synchronously.
//
// The cause of this comes from the fact that we immediately
// and synchronously evaluate CJS modules after they've been
// transpiled, but transpiling (which, for async imports,
// happens in a thread pool), can resolve in whatever order.
// This messes up module execution order.
//
// This is only _really_ important for
// import("some-polyfill") cases, the most impactful of
// which is `reflect-metadata`. People could also use