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
//! `bundler/options.zig` `Target` — bundle target platform.
//!
//! Data-only enum + pure predicates. `to_api()` / `from(api::Target)` live in
//! `bun_options_types::TargetExt` (would back-edge into the schema crate).
use enum_map::Enum;
use phf;
/// Zig field default is `.browser` (`Target = .browser` in BundleOptions);
/// keep `Default` so resolver can field-default it.
#[repr(u8)]
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash, Enum, strum::IntoStaticStr, Default)]
pub enum Target {
#[default]
Browser,
Bun,
BunMacro,
Node,
/// This is used by bake.Framework.ServerComponents.separate_ssr_graph
BakeServerComponentsSsr,
}
impl Target {
pub const MAP: phf::Map<&'static [u8], Target> = phf::phf_map! {
b"browser" => Target::Browser,
b"bun" => Target::Bun,
b"bun_macro" => Target::BunMacro,
b"macro" => Target::BunMacro,
b"node" => Target::Node,
};
// `from_js` lives in bundler_jsc as an extension trait — see PORTING.md.
// `to_api`/`from(api)` live in `bun_options_types::TargetExt`.
#[inline]
pub fn is_server_side(self) -> bool {
matches!(
self,
Target::BunMacro | Target::Node | Target::Bun | Target::BakeServerComponentsSsr
)
}
#[inline]
pub fn is_bun(self) -> bool {
matches!(
self,
Target::BunMacro | Target::Bun | Target::BakeServerComponentsSsr
)
}
#[inline]
pub fn is_node(self) -> bool {
matches!(self, Target::Node)
}
#[inline]
pub fn process_browser_define_value(self) -> Option<&'static str> {
match self {
Target::Browser => Some("true"),
_ => Some("false"),
}
}
// `bake_graph()` stays in bun_bake (would back-edge into tier-6).
// `out_extensions()` stays in bun_bundler (allocator-heavy, only used there).
const MAIN_FIELD_NAMES: [&'static str; 4] = [
"browser",
"module",
"main",
// https://github.com/jsforum/jsforum/issues/5
// Older packages might use jsnext:main in place of module
"jsnext:main",
];
pub fn default_main_fields(self) -> &'static [&'static str] {
// Zig: `std.EnumArray(Target, []const string)` initialized at comptime.
// See bundler/options.zig for the rationale comments on each ordering.
const NODE: &[&str] = &[Target::MAIN_FIELD_NAMES[2], Target::MAIN_FIELD_NAMES[1]];
const BROWSER: &[&str] = &[
Target::MAIN_FIELD_NAMES[0],
Target::MAIN_FIELD_NAMES[1],
Target::MAIN_FIELD_NAMES[3],
Target::MAIN_FIELD_NAMES[2],
];
const BUN: &[&str] = &[
Target::MAIN_FIELD_NAMES[1],
Target::MAIN_FIELD_NAMES[2],
Target::MAIN_FIELD_NAMES[3],
];
match self {
Target::Node => NODE,
Target::Browser => BROWSER,
Target::Bun | Target::BunMacro | Target::BakeServerComponentsSsr => BUN,
}
}
pub fn default_conditions(self) -> &'static [&'static [u8]] {
// PORT NOTE: Zig `default_conditions` is `std.EnumArray(Target, []const string)`
// — `string` is `[]const u8`. Callers (`ESMConditions::init`) take byte
// slices, so surface bytes directly rather than `&str`.
match self {
Target::Node => &[b"node"],
Target::Browser => &[b"browser", b"module"],
Target::Bun => &[b"bun", b"node"],
Target::BakeServerComponentsSsr => &[b"bun", b"node"],
Target::BunMacro => &[b"macro", b"bun", b"node"],
}
}
}
// ported from: src/options_types/BundleEnums.zig (Target)