aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/bun.js')
-rw-r--r--src/bun.js/api/JSTranspiler.zig1
-rw-r--r--src/bun.js/api/bun.zig2
-rw-r--r--src/bun.js/api/ffi.zig2
-rw-r--r--src/bun.js/api/server.zig2
-rw-r--r--src/bun.js/config.zig2
-rw-r--r--src/bun.js/javascript.zig145
-rw-r--r--src/bun.js/module_loader.zig19
-rw-r--r--src/bun.js/web_worker.zig13
8 files changed, 46 insertions, 140 deletions
diff --git a/src/bun.js/api/JSTranspiler.zig b/src/bun.js/api/JSTranspiler.zig
index 5c5bee171..9376eb964 100644
--- a/src/bun.js/api/JSTranspiler.zig
+++ b/src/bun.js/api/JSTranspiler.zig
@@ -767,7 +767,6 @@ pub fn constructor(
allocator,
log,
transpiler_options.transform,
- null,
JavaScript.VirtualMachine.get().bundler.env,
) catch |err| {
if ((log.warnings + log.errors) > 0) {
diff --git a/src/bun.js/api/bun.zig b/src/bun.js/api/bun.zig
index d2349ce2b..6daff5fcc 100644
--- a/src/bun.js/api/bun.zig
+++ b/src/bun.js/api/bun.zig
@@ -14,7 +14,7 @@ const IdentityContext = @import("../../identity_context.zig").IdentityContext;
const Fs = @import("../../fs.zig");
const Resolver = @import("../../resolver/resolver.zig");
const ast = @import("../../import_record.zig");
-const NodeModuleBundle = @import("../../node_module_bundle.zig").NodeModuleBundle;
+
const MacroEntryPoint = bun.bundler.MacroEntryPoint;
const logger = @import("root").bun.logger;
const Api = @import("../../api/schema.zig").Api;
diff --git a/src/bun.js/api/ffi.zig b/src/bun.js/api/ffi.zig
index 1ca44619d..8d02f6672 100644
--- a/src/bun.js/api/ffi.zig
+++ b/src/bun.js/api/ffi.zig
@@ -15,7 +15,7 @@ const IdentityContext = @import("../../identity_context.zig").IdentityContext;
const Fs = @import("../../fs.zig");
const Resolver = @import("../../resolver/resolver.zig");
const ast = @import("../../import_record.zig");
-const NodeModuleBundle = @import("../../node_module_bundle.zig").NodeModuleBundle;
+
const MacroEntryPoint = bun.bundler.MacroEntryPoint;
const logger = @import("root").bun.logger;
const Api = @import("../../api/schema.zig").Api;
diff --git a/src/bun.js/api/server.zig b/src/bun.js/api/server.zig
index 195d2a3f3..4eca89cc6 100644
--- a/src/bun.js/api/server.zig
+++ b/src/bun.js/api/server.zig
@@ -14,7 +14,7 @@ const IdentityContext = @import("../../identity_context.zig").IdentityContext;
const Fs = @import("../../fs.zig");
const Resolver = @import("../../resolver/resolver.zig");
const ast = @import("../../import_record.zig");
-const NodeModuleBundle = @import("../../node_module_bundle.zig").NodeModuleBundle;
+
const MacroEntryPoint = bun.bundler.MacroEntryPoint;
const logger = @import("root").bun.logger;
const Api = @import("../../api/schema.zig").Api;
diff --git a/src/bun.js/config.zig b/src/bun.js/config.zig
index 6e304d526..f95146c6d 100644
--- a/src/bun.js/config.zig
+++ b/src/bun.js/config.zig
@@ -13,7 +13,6 @@ const std = @import("std");
const Fs = @import("../fs.zig");
const resolver = @import("../resolver/resolver.zig");
const ast = @import("../import_record.zig");
-const NodeModuleBundle = @import("../node_module_bundle.zig").NodeModuleBundle;
const logger = @import("root").bun.logger;
const Api = @import("../api/schema.zig").Api;
const options = @import("../options.zig");
@@ -36,7 +35,6 @@ pub fn configureTransformOptionsForBunVM(allocator: std.mem.Allocator, _args: Ap
// args.serve = false;
args.write = false;
args.resolve = Api.ResolveMode.lazy;
- args.generate_node_module_bundle = false;
return try configureTransformOptionsForBun(allocator, args);
}
diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig
index 741affa6a..e7ed3e0ab 100644
--- a/src/bun.js/javascript.zig
+++ b/src/bun.js/javascript.zig
@@ -21,7 +21,6 @@ const IdentityContext = @import("../identity_context.zig").IdentityContext;
const Fs = @import("../fs.zig");
const Resolver = @import("../resolver/resolver.zig");
const ast = @import("../import_record.zig");
-const NodeModuleBundle = @import("../node_module_bundle.zig").NodeModuleBundle;
const MacroEntryPoint = bun.bundler.MacroEntryPoint;
const ParseResult = bun.bundler.ParseResult;
const logger = @import("root").bun.logger;
@@ -383,7 +382,6 @@ pub const VirtualMachine = struct {
global: *JSGlobalObject,
allocator: std.mem.Allocator,
has_loaded_constructors: bool = false,
- node_modules: ?*NodeModuleBundle = null,
bundler: Bundler,
bun_dev_watcher: ?*http.Watcher = null,
bun_watcher: ?*JSC.Watcher = null,
@@ -398,7 +396,6 @@ pub const VirtualMachine = struct {
entry_point: ServerEntryPoint = undefined,
origin: URL = URL{},
node_fs: ?*Node.NodeFS = null,
- has_loaded_node_modules: bool = false,
timer: Bun.Timer = Bun.Timer{},
uws_event_loop: ?*uws.Loop = null,
pending_unref_counter: i32 = 0,
@@ -833,18 +830,17 @@ pub const VirtualMachine = struct {
}
const RuntimeTranspilerStore = JSC.RuntimeTranspilerStore;
pub fn initWithModuleGraph(
- allocator: std.mem.Allocator,
- log: *logger.Log,
- graph: *bun.StandaloneModuleGraph,
+ opts: Options,
) !*VirtualMachine {
+ const allocator = opts.allocator;
VMHolder.vm = try allocator.create(VirtualMachine);
var console = try allocator.create(ZigConsoleClient);
console.* = ZigConsoleClient.init(Output.errorWriter(), Output.writer());
+ var log = opts.log.?;
const bundler = try Bundler.init(
allocator,
log,
- std.mem.zeroes(Api.TransformOptions),
- null,
+ opts.args,
null,
);
var vm = VMHolder.vm.?;
@@ -857,7 +853,6 @@ pub const VirtualMachine = struct {
.event_listeners = EventListenerMixin.Map.init(allocator),
.bundler = bundler,
.console = console,
- .node_modules = bundler.options.node_modules_bundle,
.log = log,
.flush_list = std.ArrayList(string).init(allocator),
.blobs = null,
@@ -871,7 +866,7 @@ pub const VirtualMachine = struct {
.ref_strings = JSC.RefString.Map.init(allocator),
.ref_strings_mutex = Lock.init(),
.file_blobs = JSC.WebCore.Blob.Store.Map.init(allocator),
- .standalone_module_graph = graph,
+ .standalone_module_graph = opts.graph.?,
.parser_arena = @import("root").bun.ArenaAllocator.init(allocator),
};
vm.source_mappings = .{ .map = &vm.saved_source_map_table };
@@ -892,7 +887,7 @@ pub const VirtualMachine = struct {
.onDependencyError = JSC.ModuleLoader.AsyncModule.Queue.onDependencyError,
};
- vm.bundler.resolver.standalone_module_graph = graph;
+ vm.bundler.resolver.standalone_module_graph = opts.graph.?;
// Avoid reading from tsconfig.json & package.json when we're in standalone mode
vm.bundler.configureLinkerWithAutoJSX(false);
@@ -925,17 +920,20 @@ pub const VirtualMachine = struct {
return vm;
}
- pub fn init(
+ pub const Options = struct {
allocator: std.mem.Allocator,
- _args: Api.TransformOptions,
- existing_bundle: ?*NodeModuleBundle,
- _log: ?*logger.Log,
- env_loader: ?*DotEnv.Loader,
- store_fd: bool,
- smol: bool,
- ) !*VirtualMachine {
+ args: Api.TransformOptions = std.mem.zeroes(Api.TransformOptions),
+ log: ?*logger.Log = null,
+ env_loader: ?*DotEnv.Loader = null,
+ store_fd: bool = false,
+ smol: bool = false,
+ graph: ?*bun.StandaloneModuleGraph = null,
+ };
+
+ pub fn init(opts: Options) !*VirtualMachine {
+ const allocator = opts.allocator;
var log: *logger.Log = undefined;
- if (_log) |__log| {
+ if (opts.log) |__log| {
log = __log;
} else {
log = try allocator.create(logger.Log);
@@ -948,9 +946,8 @@ pub const VirtualMachine = struct {
const bundler = try Bundler.init(
allocator,
log,
- try Config.configureTransformOptionsForBunVM(allocator, _args),
- existing_bundle,
- env_loader,
+ try Config.configureTransformOptionsForBunVM(allocator, opts.args),
+ opts.env_loader,
);
var vm = VMHolder.vm.?;
@@ -962,10 +959,9 @@ pub const VirtualMachine = struct {
.event_listeners = EventListenerMixin.Map.init(allocator),
.bundler = bundler,
.console = console,
- .node_modules = bundler.options.node_modules_bundle,
.log = log,
.flush_list = std.ArrayList(string).init(allocator),
- .blobs = if (_args.serve orelse false) try Blob.Group.init(allocator) else null,
+ .blobs = if (opts.args.serve orelse false) try Blob.Group.init(allocator) else null,
.origin = bundler.options.origin,
.saved_source_map_table = SavedSourceMap.HashTable.init(allocator),
.source_mappings = undefined,
@@ -987,7 +983,7 @@ pub const VirtualMachine = struct {
vm.event_loop = &vm.regular_event_loop;
vm.bundler.macro_context = null;
- vm.bundler.resolver.store_fd = store_fd;
+ vm.bundler.resolver.store_fd = opts.store_fd;
vm.bundler.resolver.prefer_module_field = false;
vm.bundler.resolver.onWakePackageManager = .{
@@ -1001,7 +997,7 @@ pub const VirtualMachine = struct {
vm.bundler.macro_context = js_ast.Macro.MacroContext.init(&vm.bundler);
- if (_args.serve orelse false) {
+ if (opts.args.serve orelse false) {
vm.bundler.linker.onImportCSS = Bun.onImportCSS;
}
@@ -1014,7 +1010,7 @@ pub const VirtualMachine = struct {
@as(i32, @intCast(global_classes.len)),
vm.console,
-1,
- smol,
+ opts.smol,
null,
);
vm.regular_event_loop.global = vm.global;
@@ -1031,15 +1027,12 @@ pub const VirtualMachine = struct {
}
pub fn initWorker(
- allocator: std.mem.Allocator,
- _args: Api.TransformOptions,
- _log: ?*logger.Log,
- env_loader: ?*DotEnv.Loader,
- store_fd: bool,
worker: *WebWorker,
+ opts: Options,
) anyerror!*VirtualMachine {
var log: *logger.Log = undefined;
- if (_log) |__log| {
+ const allocator = opts.allocator;
+ if (opts.log) |__log| {
log = __log;
} else {
log = try allocator.create(logger.Log);
@@ -1052,9 +1045,8 @@ pub const VirtualMachine = struct {
const bundler = try Bundler.init(
allocator,
log,
- try Config.configureTransformOptionsForBunVM(allocator, _args),
- null,
- env_loader,
+ try Config.configureTransformOptionsForBunVM(allocator, opts.args),
+ opts.env_loader,
);
var vm = VMHolder.vm.?;
@@ -1066,10 +1058,9 @@ pub const VirtualMachine = struct {
.event_listeners = EventListenerMixin.Map.init(allocator),
.bundler = bundler,
.console = console,
- .node_modules = bundler.options.node_modules_bundle,
.log = log,
.flush_list = std.ArrayList(string).init(allocator),
- .blobs = if (_args.serve orelse false) try Blob.Group.init(allocator) else null,
+ .blobs = if (opts.args.serve orelse false) try Blob.Group.init(allocator) else null,
.origin = bundler.options.origin,
.saved_source_map_table = SavedSourceMap.HashTable.init(allocator),
.source_mappings = undefined,
@@ -1093,7 +1084,7 @@ pub const VirtualMachine = struct {
vm.event_loop = &vm.regular_event_loop;
vm.hot_reload = worker.parent.hot_reload;
vm.bundler.macro_context = null;
- vm.bundler.resolver.store_fd = store_fd;
+ vm.bundler.resolver.store_fd = opts.store_fd;
vm.bundler.resolver.prefer_module_field = false;
vm.bundler.resolver.onWakePackageManager = .{
.context = &vm.modules,
@@ -1106,7 +1097,7 @@ pub const VirtualMachine = struct {
vm.bundler.macro_context = js_ast.Macro.MacroContext.init(&vm.bundler);
- if (_args.serve orelse false) {
+ if (opts.args.serve orelse false) {
vm.bundler.linker.onImportCSS = Bun.onImportCSS;
}
@@ -1286,7 +1277,6 @@ pub const VirtualMachine = struct {
source: string,
is_esm: bool,
comptime is_a_file_path: bool,
- comptime realpath: bool,
) !void {
std.debug.assert(VirtualMachine.isLoaded());
// macOS threadlocal vars are very slow
@@ -1294,12 +1284,9 @@ pub const VirtualMachine = struct {
// so we can copy it here
var jsc_vm = VirtualMachine.get();
- if (jsc_vm.node_modules == null and strings.eqlComptime(std.fs.path.basename(specifier), Runtime.Runtime.Imports.alt_name)) {
+ if (strings.eqlComptime(std.fs.path.basename(specifier), Runtime.Runtime.Imports.alt_name)) {
ret.path = Runtime.Runtime.Imports.Name;
return;
- } else if (jsc_vm.node_modules != null and strings.eqlComptime(specifier, bun_file_import_path)) {
- ret.path = bun_file_import_path;
- return;
} else if (strings.eqlComptime(specifier, main_file_name)) {
ret.result = null;
ret.path = jsc_vm.entry_point.source.path.text;
@@ -1383,49 +1370,6 @@ pub const VirtualMachine = struct {
ret.query_string = query_string;
const result_path = result.pathConst() orelse return error.ModuleNotFound;
jsc_vm.resolved_count += 1;
- if (comptime !realpath) {
- if (jsc_vm.node_modules != null and !strings.eqlComptime(result_path.namespace, "node") and result.isLikelyNodeModule()) {
- const node_modules_bundle = jsc_vm.node_modules.?;
-
- node_module_checker: {
- const package_json = result.package_json orelse brk: {
- if (jsc_vm.bundler.resolver.packageJSONForResolvedNodeModule(&result)) |pkg| {
- break :brk pkg;
- } else {
- break :node_module_checker;
- }
- };
-
- if (node_modules_bundle.getPackageIDByName(package_json.name)) |possible_pkg_ids| {
- const pkg_id: u32 = brk: {
- for (possible_pkg_ids) |pkg_id| {
- const pkg = node_modules_bundle.bundle.packages[pkg_id];
- if (pkg.hash == package_json.hash) {
- break :brk pkg_id;
- }
- }
- break :node_module_checker;
- };
-
- const package = &node_modules_bundle.bundle.packages[pkg_id];
-
- if (Environment.isDebug) {
- std.debug.assert(strings.eql(node_modules_bundle.str(package.name), package_json.name));
- }
-
- const package_relative_path = jsc_vm.bundler.fs.relative(
- package_json.source.path.name.dirWithTrailingSlash(),
- result_path.text,
- );
-
- if (node_modules_bundle.findModuleIDInPackage(package, package_relative_path) == null) break :node_module_checker;
-
- ret.path = bun_file_import_path;
- return;
- }
- }
- }
- }
ret.path = result_path.text;
}
@@ -1452,7 +1396,7 @@ pub const VirtualMachine = struct {
query_string: *ZigString,
is_esm: bool,
) void {
- resolveMaybeNeedsTrailingSlash(res, global, specifier, source, query_string, is_esm, false, true);
+ resolveMaybeNeedsTrailingSlash(res, global, specifier, source, query_string, is_esm, false);
}
pub fn resolveFilePathForAPI(
@@ -1463,7 +1407,7 @@ pub const VirtualMachine = struct {
query_string: *ZigString,
is_esm: bool,
) void {
- resolveMaybeNeedsTrailingSlash(res, global, specifier, source, query_string, is_esm, true, true);
+ resolveMaybeNeedsTrailingSlash(res, global, specifier, source, query_string, is_esm, true);
}
pub fn resolve(
@@ -1474,7 +1418,7 @@ pub const VirtualMachine = struct {
query_string: *ZigString,
is_esm: bool,
) void {
- resolveMaybeNeedsTrailingSlash(res, global, specifier, source, query_string, is_esm, true, false);
+ resolveMaybeNeedsTrailingSlash(res, global, specifier, source, query_string, is_esm, true);
}
fn normalizeSource(source: []const u8) []const u8 {
@@ -1493,7 +1437,6 @@ pub const VirtualMachine = struct {
query_string: ?*ZigString,
is_esm: bool,
comptime is_a_file_path: bool,
- comptime realpath: bool,
) void {
var result = ResolveFunctionResult{ .path = "", .result = null };
var jsc_vm = VirtualMachine.get();
@@ -1527,7 +1470,6 @@ pub const VirtualMachine = struct {
query_string,
is_esm,
is_a_file_path,
- realpath,
);
return;
}
@@ -1547,7 +1489,7 @@ pub const VirtualMachine = struct {
jsc_vm.bundler.linker.log = old_log;
jsc_vm.bundler.resolver.log = old_log;
}
- _resolve(&result, global, specifier_utf8.slice(), normalizeSource(source_utf8.slice()), is_esm, is_a_file_path, realpath) catch |err_| {
+ _resolve(&result, global, specifier_utf8.slice(), normalizeSource(source_utf8.slice()), is_esm, is_a_file_path) catch |err_| {
var err = err_;
const msg: logger.Msg = brk: {
var msgs: []logger.Msg = log.msgs.items;
@@ -1808,19 +1750,6 @@ pub const VirtualMachine = struct {
var promise: *JSInternalPromise = undefined;
if (!this.bundler.options.disable_transpilation) {
-
- // We first import the node_modules bundle. This prevents any potential TDZ issues.
- // The contents of the node_modules bundle are lazy, so hopefully this should be pretty quick.
- if (this.node_modules != null and !this.has_loaded_node_modules) {
- this.has_loaded_node_modules = true;
- promise = JSModuleLoader.loadAndEvaluateModule(this.global, &String.static(bun_file_import_path));
- this.waitForPromise(JSC.AnyPromise{
- .Internal = promise,
- });
- if (promise.status(this.global.vm()) == .Rejected)
- return promise;
- }
-
{
this.is_in_preload = true;
defer this.is_in_preload = false;
diff --git a/src/bun.js/module_loader.zig b/src/bun.js/module_loader.zig
index ccee5208d..5d9158b58 100644
--- a/src/bun.js/module_loader.zig
+++ b/src/bun.js/module_loader.zig
@@ -20,7 +20,6 @@ const IdentityContext = @import("../identity_context.zig").IdentityContext;
const Fs = @import("../fs.zig");
const Resolver = @import("../resolver/resolver.zig");
const ast = @import("../import_record.zig");
-const NodeModuleBundle = @import("../node_module_bundle.zig").NodeModuleBundle;
const MacroEntryPoint = bun.bundler.MacroEntryPoint;
const ParseResult = bun.bundler.ParseResult;
const logger = @import("root").bun.logger;
@@ -1994,23 +1993,7 @@ pub const ModuleLoader = struct {
}
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:
- // - hash
- // - file descriptor for source input
- // - file path + file descriptor for bytecode caching
- // - separate bundles for server build vs browser build OR at least separate sections
- const code = try jsc_vm.node_modules.?.readCodeAsStringSlow(jsc_vm.allocator);
-
- return ResolvedSource{
- .allocator = null,
- .source_code = bun.String.init(code),
- .specifier = bun.String.init(JSC.bun_file_import_path),
- .source_url = ZigString.init(JSC.bun_file_import_path[1..]),
- .hash = 0, // TODO
- };
- } else if (jsc_vm.node_modules == null and specifier.eqlComptime(Runtime.Runtime.Imports.Name)) {
+ if (specifier.eqlComptime(Runtime.Runtime.Imports.Name)) {
return ResolvedSource{
.allocator = null,
.source_code = bun.String.init(Runtime.Runtime.sourceContentBun()),
diff --git a/src/bun.js/web_worker.zig b/src/bun.js/web_worker.zig
index bbe708d18..7fa9c6690 100644
--- a/src/bun.js/web_worker.zig
+++ b/src/bun.js/web_worker.zig
@@ -170,14 +170,11 @@ pub const WebWorker = struct {
std.debug.assert(this.status == .start);
std.debug.assert(this.vm == null);
this.arena = try bun.MimallocArena.init();
- var vm = try JSC.VirtualMachine.initWorker(
- this.arena.allocator(),
- this.parent.bundler.options.transform_options,
- null,
- null,
- this.store_fd,
- this,
- );
+ var vm = try JSC.VirtualMachine.initWorker(this, .{
+ .allocator = this.arena.allocator(),
+ .args = this.parent.bundler.options.transform_options,
+ .store_fd = this.store_fd,
+ });
vm.allocator = this.arena.allocator();
vm.arena = &this.arena;