diff options
Diffstat (limited to 'src/options.zig')
-rw-r--r-- | src/options.zig | 227 |
1 files changed, 109 insertions, 118 deletions
diff --git a/src/options.zig b/src/options.zig index 0ad92a5e1..3cb5c333c 100644 --- a/src/options.zig +++ b/src/options.zig @@ -112,7 +112,7 @@ pub const ExternalModules = struct { cwd: string, externals: []const string, log: *logger.Log, - platform: Platform, + target: Target, ) ExternalModules { var result = ExternalModules{ .node_modules = std.BufSet.init(allocator), @@ -120,7 +120,7 @@ pub const ExternalModules = struct { .patterns = default_wildcard_patterns[0..], }; - switch (platform) { + switch (target) { .node => { // TODO: fix this stupid copy result.node_modules.hash_map.ensureTotalCapacity(NodeBuiltinPatterns.len) catch unreachable; @@ -377,42 +377,37 @@ pub const ModuleType = enum { }); }; -pub const Platform = enum { - neutral, +pub const Target = enum { browser, bun, bun_macro, node, pub const Map = ComptimeStringMap( - Platform, + Target, .{ .{ - "neutral", - Platform.neutral, - }, - .{ "browser", - Platform.browser, + Target.browser, }, .{ "bun", - Platform.bun, + Target.bun, }, .{ "bun_macro", - Platform.bun_macro, + Target.bun_macro, }, .{ "node", - Platform.node, + Target.node, }, }, ); - pub fn fromJS(global: *JSC.JSGlobalObject, value: JSC.JSValue, exception: JSC.C.ExceptionRef) ?Platform { + pub fn fromJS(global: *JSC.JSGlobalObject, value: JSC.JSValue, exception: JSC.C.ExceptionRef) ?Target { if (!value.jsType().isStringLike()) { - JSC.throwInvalidArguments("platform must be a string", .{}, global, exception); + JSC.throwInvalidArguments("target must be a string", .{}, global, exception); return null; } @@ -424,60 +419,58 @@ pub const Platform = enum { const Eight = strings.ExactSizeMatcher(8); return switch (Eight.match(slice)) { - Eight.case("deno"), Eight.case("browser") => Platform.browser, - Eight.case("bun") => Platform.bun, - Eight.case("macro") => Platform.bun_macro, - Eight.case("node") => Platform.node, - Eight.case("neutral") => Platform.neutral, + Eight.case("deno"), Eight.case("browser") => Target.browser, + Eight.case("bun") => Target.bun, + Eight.case("macro") => Target.bun_macro, + Eight.case("node") => Target.node, else => { - JSC.throwInvalidArguments("platform must be one of: deno, browser, bun, macro, node, neutral", .{}, global, exception); + JSC.throwInvalidArguments("target must be one of: deno, browser, bun, macro, node", .{}, global, exception); return null; }, }; } - pub fn toAPI(this: Platform) Api.Platform { + pub fn toAPI(this: Target) Api.Target { return switch (this) { .node => .node, .browser => .browser, .bun => .bun, .bun_macro => .bun_macro, - else => ._none, }; } - pub inline fn isServerSide(this: Platform) bool { + pub inline fn isServerSide(this: Target) bool { return switch (this) { .bun_macro, .node, .bun => true, else => false, }; } - pub inline fn isBun(this: Platform) bool { + pub inline fn isBun(this: Target) bool { return switch (this) { .bun_macro, .bun => true, else => false, }; } - pub inline fn isNotBun(this: Platform) bool { + pub inline fn isNotBun(this: Target) bool { return switch (this) { .bun_macro, .bun => false, else => true, }; } - pub inline fn isClient(this: Platform) bool { + pub inline fn isClient(this: Target) bool { return switch (this) { .bun_macro, .bun => false, else => true, }; } - pub inline fn supportsBrowserField(this: Platform) bool { + pub inline fn supportsBrowserField(this: Target) bool { return switch (this) { - .neutral, .browser => true, + .browser => true, else => false, }; } @@ -485,17 +478,16 @@ pub const Platform = enum { const browser_define_value_true = "true"; const browser_define_value_false = "false"; - pub inline fn processBrowserDefineValue(this: Platform) ?string { + pub inline fn processBrowserDefineValue(this: Target) ?string { return switch (this) { .browser => browser_define_value_true, .bun_macro, .bun, .node => browser_define_value_false, - else => null, }; } - pub inline fn isWebLike(platform: Platform) bool { - return switch (platform) { - .neutral, .browser => true, + pub inline fn isWebLike(target: Target) bool { + return switch (target) { + .browser => true, else => false, }; } @@ -512,13 +504,13 @@ pub const Platform = enum { }; }; - pub fn outExtensions(platform: Platform, allocator: std.mem.Allocator) bun.StringHashMap(string) { + pub fn outExtensions(target: Target, allocator: std.mem.Allocator) bun.StringHashMap(string) { var exts = bun.StringHashMap(string).init(allocator); const js = Extensions.Out.JavaScript[0]; const mjs = Extensions.Out.JavaScript[1]; - if (platform == .node) { + if (target == .node) { exts.ensureTotalCapacity(Extensions.In.JavaScript.len * 2) catch unreachable; for (Extensions.In.JavaScript) |ext| { exts.put(ext, mjs) catch unreachable; @@ -535,8 +527,8 @@ pub const Platform = enum { return exts; } - pub fn from(plat: ?api.Api.Platform) Platform { - return switch (plat orelse api.Api.Platform._none) { + pub fn from(plat: ?api.Api.Target) Target { + return switch (plat orelse api.Api.Target._none) { .node => .node, .browser => .browser, .bun => .bun, @@ -555,8 +547,8 @@ pub const Platform = enum { // Older packages might use jsnext:main in place of module "jsnext:main", }; - pub const DefaultMainFields: std.EnumArray(Platform, []const string) = brk: { - var array = std.EnumArray(Platform, []const string).initUndefined(); + pub const DefaultMainFields: std.EnumArray(Target, []const string) = brk: { + var array = std.EnumArray(Target, []const string).initUndefined(); // Note that this means if a package specifies "module" and "main", the ES6 // module will not be selected. This means tree shaking will not work when @@ -572,7 +564,7 @@ pub const Platform = enum { // This is unfortunate but it's a problem on the side of those packages. // They won't work correctly with other popular bundlers (with node as a target) anyway. var list = [_]string{ MAIN_FIELD_NAMES[2], MAIN_FIELD_NAMES[1] }; - array.set(Platform.node, &list); + array.set(Target.node, &list); // Note that this means if a package specifies "main", "module", and // "browser" then "browser" will win out over "module". This is the @@ -584,24 +576,23 @@ pub const Platform = enum { var listc = [_]string{ MAIN_FIELD_NAMES[0], MAIN_FIELD_NAMES[1], MAIN_FIELD_NAMES[3], MAIN_FIELD_NAMES[2] }; var listd = [_]string{ MAIN_FIELD_NAMES[1], MAIN_FIELD_NAMES[2], MAIN_FIELD_NAMES[3] }; - array.set(Platform.browser, &listc); - array.set(Platform.bun, &listd); - array.set(Platform.bun_macro, &listd); + array.set(Target.browser, &listc); + array.set(Target.bun, &listd); + array.set(Target.bun_macro, &listd); // Original comment: - // The neutral platform is for people that don't want esbuild to try to + // The neutral target is for people that don't want esbuild to try to // pick good defaults for their platform. In that case, the list of main // fields is empty by default. You must explicitly configure it yourself. - - array.set(Platform.neutral, &listc); + // array.set(Target.neutral, &listc); break :brk array; }; - pub const DefaultConditions: std.EnumArray(Platform, []const string) = brk: { - var array = std.EnumArray(Platform, []const string).initUndefined(); + pub const DefaultConditions: std.EnumArray(Target, []const string) = brk: { + var array = std.EnumArray(Target, []const string).initUndefined(); - array.set(Platform.node, &[_]string{ + array.set(Target.node, &[_]string{ "node", "module", }); @@ -610,9 +601,9 @@ pub const Platform = enum { "browser", "module", }; - array.set(Platform.browser, &listc); + array.set(Target.browser, &listc); array.set( - Platform.bun, + Target.bun, &[_]string{ "bun", "worker", @@ -623,7 +614,7 @@ pub const Platform = enum { }, ); array.set( - Platform.bun_macro, + Target.bun_macro, &[_]string{ "bun", "worker", @@ -633,14 +624,13 @@ pub const Platform = enum { "browser", }, ); - // array.set(Platform.bun_macro, [_]string{ "bun_macro", "browser", "default", },); + // array.set(Target.bun_macro, [_]string{ "bun_macro", "browser", "default", },); // Original comment: - // The neutral platform is for people that don't want esbuild to try to + // The neutral target is for people that don't want esbuild to try to // pick good defaults for their platform. In that case, the list of main // fields is empty by default. You must explicitly configure it yourself. - - array.set(Platform.neutral, &listc); + // array.set(Target.neutral, &listc); break :brk array; }; @@ -965,7 +955,7 @@ pub const JSX = struct { &pragma.import_source.development, &[_]string{ pragma.package_name, - "jsx-dev-runtime", + "/jsx-dev-runtime", }, &.{ Defaults.ImportSourceDev, @@ -977,7 +967,7 @@ pub const JSX = struct { &pragma.import_source.production, &[_]string{ pragma.package_name, - "jsx-runtime", + "/jsx-runtime", }, &.{ Defaults.ImportSource, @@ -1093,8 +1083,7 @@ pub const DefaultUserDefines = struct { pub const Key = "process.env.NODE_ENV"; pub const Value = "\"development\""; }; - - pub const PlatformDefine = struct { + pub const ProcessBrowserDefine = struct { pub const Key = "process.browser"; pub const Value = []string{ "false", "true" }; }; @@ -1105,7 +1094,7 @@ pub fn definesFromTransformOptions( log: *logger.Log, _input_define: ?Api.StringMap, hmr: bool, - platform: Platform, + target: Target, loader: ?*DotEnv.Loader, framework_env: ?*const Env, NODE_ENV: ?string, @@ -1148,36 +1137,38 @@ pub fn definesFromTransformOptions( } } - if (NODE_ENV) |node_env| { - if (node_env.len > 0) { - var quoted_node_env: string = ""; - if ((strings.startsWithChar(node_env, '"') and strings.endsWithChar(node_env, '"')) or - (strings.startsWithChar(node_env, '\'') and strings.endsWithChar(node_env, '\''))) - { - quoted_node_env = node_env; - } else { + var quoted_node_env: string = brk: { + if (NODE_ENV) |node_env| { + if (node_env.len > 0) { + if ((strings.startsWithChar(node_env, '"') and strings.endsWithChar(node_env, '"')) or + (strings.startsWithChar(node_env, '\'') and strings.endsWithChar(node_env, '\''))) + { + break :brk node_env; + } + // avoid allocating if we can if (strings.eqlComptime(node_env, "production")) { - quoted_node_env = "\"production\""; + break :brk "\"production\""; } else if (strings.eqlComptime(node_env, "development")) { - quoted_node_env = "\"development\""; + break :brk "\"development\""; } else if (strings.eqlComptime(node_env, "test")) { - quoted_node_env = "\"test\""; + break :brk "\"test\""; } else { - quoted_node_env = try std.fmt.allocPrint(allocator, "\"{s}\"", .{node_env}); + break :brk try std.fmt.allocPrint(allocator, "\"{s}\"", .{node_env}); } } - - _ = try user_defines.getOrPutValue( - "process.env.NODE_ENV", - quoted_node_env, - ); - _ = try user_defines.getOrPutValue( - "process.env.BUN_ENV", - quoted_node_env, - ); } - } + break :brk "\"development\""; + }; + + _ = try user_defines.getOrPutValue( + "process.env.NODE_ENV", + quoted_node_env, + ); + _ = try user_defines.getOrPutValue( + "process.env.BUN_ENV", + quoted_node_env, + ); if (hmr) { try user_defines.put(DefaultUserDefines.HotModuleReloading.Key, DefaultUserDefines.HotModuleReloading.Value); @@ -1185,11 +1176,11 @@ pub fn definesFromTransformOptions( // Automatically set `process.browser` to `true` for browsers and false for node+js // This enables some extra dead code elimination - if (platform.processBrowserDefineValue()) |value| { - _ = try user_defines.getOrPutValue(DefaultUserDefines.PlatformDefine.Key, value); + if (target.processBrowserDefineValue()) |value| { + _ = try user_defines.getOrPutValue(DefaultUserDefines.ProcessBrowserDefine.Key, value); } - if (platform.isBun()) { + if (target.isBun()) { if (!user_defines.contains("window")) { _ = try environment_defines.getOrPutValue("window", .{ .valueless = true, @@ -1222,11 +1213,11 @@ const default_loader_ext = [_]string{ ".txt", ".text", }; -pub fn loadersFromTransformOptions(allocator: std.mem.Allocator, _loaders: ?Api.LoaderMap, platform: Platform) !bun.StringArrayHashMap(Loader) { +pub fn loadersFromTransformOptions(allocator: std.mem.Allocator, _loaders: ?Api.LoaderMap, target: Target) !bun.StringArrayHashMap(Loader) { var input_loaders = _loaders orelse std.mem.zeroes(Api.LoaderMap); var loader_values = try allocator.alloc(Loader, input_loaders.loaders.len); - if (platform.isBun()) { + if (target.isBun()) { for (loader_values, 0..) |_, i| { const loader = switch (input_loaders.loaders[i]) { .jsx => Loader.jsx, @@ -1274,7 +1265,7 @@ pub fn loadersFromTransformOptions(allocator: std.mem.Allocator, _loaders: ?Api. _ = try loaders.getOrPutValue(ext, defaultLoaders.get(ext).?); } - if (platform.isBun()) { + if (target.isBun()) { inline for (default_loader_ext_bun) |ext| { _ = try loaders.getOrPutValue(ext, defaultLoaders.get(ext).?); } @@ -1379,14 +1370,14 @@ pub const BundleOptions = struct { resolve_mode: api.Api.ResolveMode, tsconfig_override: ?string = null, - platform: Platform = Platform.browser, - main_fields: []const string = Platform.DefaultMainFields.get(Platform.browser), + target: Target = Target.browser, + main_fields: []const string = Target.DefaultMainFields.get(Target.browser), log: *logger.Log, external: ExternalModules = ExternalModules{}, entry_points: []const string, - entry_names: []const u8 = "", - asset_names: []const u8 = "", - chunk_names: []const u8 = "", + entry_naming: []const u8 = "", + asset_naming: []const u8 = "", + chunk_naming: []const u8 = "", public_path: []const u8 = "", extension_order: []const string = &Defaults.ExtensionOrder, esm_extension_order: []const string = &Defaults.ModuleExtensionOrder, @@ -1446,8 +1437,8 @@ pub const BundleOptions = struct { }; pub inline fn cssImportBehavior(this: *const BundleOptions) Api.CssInJsBehavior { - switch (this.platform) { - .neutral, .browser => { + switch (this.target) { + .browser => { if (this.framework) |framework| { return framework.client_css_in_js; } @@ -1471,7 +1462,7 @@ pub const BundleOptions = struct { this.log, this.transform_options.define, this.transform_options.serve orelse false, - this.platform, + this.target, loader_, env, if (loader_) |e| @@ -1540,9 +1531,9 @@ pub const BundleOptions = struct { .log = log, .resolve_mode = transform.resolve orelse .dev, .define = undefined, - .loaders = try loadersFromTransformOptions(allocator, transform.loaders, Platform.from(transform.platform)), + .loaders = try loadersFromTransformOptions(allocator, transform.loaders, Target.from(transform.target)), .output_dir = transform.output_dir orelse "out", - .platform = Platform.from(transform.platform), + .target = Target.from(transform.target), .write = transform.write orelse false, .external = undefined, .entry_points = transform.entry_points, @@ -1566,12 +1557,12 @@ pub const BundleOptions = struct { opts.extension_order = transform.extension_order; } - if (transform.platform) |plat| { - opts.platform = Platform.from(plat); - opts.main_fields = Platform.DefaultMainFields.get(opts.platform); + if (transform.target) |t| { + opts.target = Target.from(t); + opts.main_fields = Target.DefaultMainFields.get(opts.target); } - opts.conditions = try ESMConditions.init(allocator, Platform.DefaultConditions.get(opts.platform)); + opts.conditions = try ESMConditions.init(allocator, Target.DefaultConditions.get(opts.target)); if (transform.serve orelse false) { // When we're serving, we need some kind of URL. @@ -1596,7 +1587,7 @@ pub const BundleOptions = struct { } } - switch (opts.platform) { + switch (opts.target) { .node => { opts.import_path_format = .relative; opts.allow_runtime = false; @@ -1695,8 +1686,8 @@ pub const BundleOptions = struct { if (opts.framework == null and is_generating_bundle) opts.env.behavior = .load_all; - opts.external = ExternalModules.init(allocator, &fs.fs, fs.top_level_dir, transform.external, log, opts.platform); - opts.out_extensions = opts.platform.outExtensions(allocator); + opts.external = ExternalModules.init(allocator, &fs.fs, fs.top_level_dir, transform.external, log, opts.target); + opts.out_extensions = opts.target.outExtensions(allocator); if (transform.serve orelse false) { opts.preserve_extensions = true; @@ -1839,7 +1830,7 @@ pub const BundleOptions = struct { if (Environment.isWindows and opts.routes.static_dir_handle != null) { opts.routes.static_dir_handle.?.close(); } - opts.hot_module_reloading = opts.platform.isWebLike(); + opts.hot_module_reloading = opts.target.isWebLike(); if (transform.disable_hmr orelse false) opts.hot_module_reloading = false; @@ -1849,7 +1840,7 @@ pub const BundleOptions = struct { opts.sourcemap = SourceMapOption.fromApi(transform.source_map orelse Api.SourceMapMode._none); } - opts.tree_shaking = opts.serve or opts.platform.isBun() or opts.production or is_generating_bundle; + opts.tree_shaking = opts.serve or opts.target.isBun() or opts.production or is_generating_bundle; opts.inlining = opts.tree_shaking; if (opts.inlining) opts.minify_syntax = true; @@ -1863,7 +1854,7 @@ pub const BundleOptions = struct { opts.output_dir = try fs.getFdPath(opts.output_dir_handle.?.fd); } - opts.polyfill_node_globals = opts.platform != .node; + opts.polyfill_node_globals = opts.target != .node; Analytics.Features.framework = Analytics.Features.framework or opts.framework != null; Analytics.Features.filesystem_router = Analytics.Features.filesystem_router or opts.routes.routes_enabled; @@ -1871,7 +1862,7 @@ pub const BundleOptions = struct { Analytics.Features.public_folder = Analytics.Features.public_folder or opts.routes.static_dir_enabled; Analytics.Features.bun_bun = Analytics.Features.bun_bun or transform.node_modules_bundle_path != null; Analytics.Features.bunjs = Analytics.Features.bunjs or transform.node_modules_bundle_path_server != null; - Analytics.Features.macros = Analytics.Features.macros or opts.platform == .bun_macro; + Analytics.Features.macros = Analytics.Features.macros or opts.target == .bun_macro; Analytics.Features.external = Analytics.Features.external or transform.external.len > 0; Analytics.Features.single_page_app_routing = Analytics.Features.single_page_app_routing or opts.routes.single_page_app_routing; return opts; @@ -1908,8 +1899,8 @@ pub const TransformOptions = struct { resolve_paths: bool = false, tsconfig_override: ?string = null, - platform: Platform = Platform.browser, - main_fields: []string = Platform.DefaultMainFields.get(Platform.browser), + target: Target = Target.browser, + main_fields: []string = Target.DefaultMainFields.get(Target.browser), pub fn initUncached(allocator: std.mem.Allocator, entryPointName: string, code: string) !TransformOptions { assert(entryPointName.len > 0); @@ -1940,7 +1931,7 @@ pub const TransformOptions = struct { .define = define, .loader = loader, .resolve_dir = entryPoint.path.name.dir, - .main_fields = Platform.DefaultMainFields.get(Platform.browser), + .main_fields = Target.DefaultMainFields.get(Target.browser), .jsx = if (Loader.isJSX(loader)) JSX.Pragma{} else null, }; } @@ -1958,7 +1949,7 @@ pub const OutputFile = struct { mtime: ?i128 = null, // Depending on: - // - The platform + // - The target // - The number of open file handles // - Whether or not a file of the same name exists // We may use a different system call @@ -2389,9 +2380,9 @@ pub const Framework = struct { pub const fallback_html: string = @embedFile("./fallback.html"); - pub fn platformEntryPoint(this: *const Framework, platform: Platform) ?*const EntryPoint { - const entry: *const EntryPoint = switch (platform) { - .neutral, .browser => &this.client, + pub fn platformEntryPoint(this: *const Framework, target: Target) ?*const EntryPoint { + const entry: *const EntryPoint = switch (target) { + .browser => &this.client, .bun => &this.server, .node => return null, }; |