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
use crate::{Error, OutputFormat, Platform, Result, Runtime, SharedPluginable};
use rustc_hash::FxHashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
#[cfg(feature = "dts-generation")]
use super::dts::DtsOptions;
use super::entry::EntryPoints;
/// Configuration options for a build operation.
///
/// This struct controls all aspects of the bundling process. Use the builder
/// pattern methods for ergonomic configuration, or construct directly for
/// full control.
#[derive(Debug, Clone)]
pub struct BuildOptions {
/// Entry point(s) for the build.
pub entry: EntryPoints,
/// Whether to bundle dependencies into the output (default: true).
///
/// - `true`: Include node_modules in the bundle (app/component mode)
/// - `false`: Externalize all dependencies (library mode)
pub bundle: bool,
/// Enable code splitting for shared chunks (default: false).
///
/// Only applies when `entry` is `Multiple` or `Named` and `bundle` is `true`.
/// Extracts shared dependencies into separate chunks.
pub splitting: bool,
/// Packages to treat as external (not bundled).
///
/// Only applies when `bundle: true`. When `bundle: false`, all
/// dependencies are automatically externalized.
pub external: Vec<String>,
/// Output directory for bundled files.
///
/// Cannot be used with `outfile`. Required when `splitting: true`.
pub outdir: Option<PathBuf>,
/// Output file path for single-entry builds.
///
/// Cannot be used with `outdir` or when `splitting: true`.
/// Only valid for `EntryPoints::Single`.
pub outfile: Option<PathBuf>,
/// Target runtime platform (default: Browser).
pub platform: Platform,
/// Output module format (default: ESM).
pub format: OutputFormat,
/// Source map generation strategy (default: external file).
pub sourcemap: Option<crate::SourceMapType>,
/// Enable JavaScript minification (default: false).
pub minify: bool,
/// Global variable names for external packages (IIFE/UMD only).
///
/// Maps package names to global variable names.
/// Example: `{"react": "React", "react-dom": "ReactDOM"}`
pub globals: FxHashMap<String, String>,
/// Rolldown/Rollup plugins to apply during bundling.
pub plugins: Vec<SharedPluginable>,
/// Virtual files that don't exist on disk.
///
/// Maps virtual paths to their content. Useful for programmatic entry points.
pub virtual_files: FxHashMap<String, String>,
/// Path aliases for import resolution (e.g., "@" → "src").
///
/// Maps alias prefixes to their target directories. These are resolved
/// relative to the `cwd` if not absolute.
/// Example: `{"@": "./src"}` allows `import "@/components/Button"` to resolve to `./src/components/Button`
pub path_aliases: FxHashMap<String, String>,
/// Working directory for module resolution (default: current directory).
pub cwd: Option<PathBuf>,
/// Runtime for filesystem operations (default: NativeRuntime on native, must be provided for WASM).
///
/// This allows the bundler to work across different platforms:
/// - On native targets, NativeRuntime uses std::fs
/// - On WASM targets, BrowserRuntime bridges to JavaScript
pub runtime: Option<Arc<dyn Runtime>>,
/// Decorator transformation options (modern/Stage 3 decorators).
///
/// Enables transformation of JavaScript/TypeScript decorators.
/// Only supports modern decorators (TC39 Stage 3 proposal), not legacy experimentalDecorators.
pub decorator: Option<crate::DecoratorOptions>,
/// TypeScript declaration file generation options.
#[cfg(feature = "dts-generation")]
pub dts: Option<DtsOptions>,
}
impl BuildOptions {
/// Create a new BuildOptions with a single entry point.
///
/// # Examples
///
/// ```no_run
/// use fob_bundler::BuildOptions;
///
/// let options = BuildOptions::new("./src/index.js");
/// ```
pub fn new(entry: impl AsRef<Path>) -> Self {
Self {
entry: EntryPoints::Single(normalize_entry_path(entry)),
bundle: true,
splitting: false,
external: Vec::new(),
outdir: None,
outfile: None,
platform: Platform::Browser,
format: OutputFormat::Esm,
sourcemap: Some(crate::SourceMapType::File),
minify: false,
globals: FxHashMap::default(),
plugins: Vec::new(),
virtual_files: FxHashMap::default(),
path_aliases: FxHashMap::default(),
cwd: None,
runtime: None,
decorator: None,
#[cfg(feature = "dts-generation")]
dts: None,
}
}
/// Create BuildOptions with multiple entry points.
///
/// # Examples
///
/// ```no_run
/// use fob_bundler::BuildOptions;
///
/// let options = BuildOptions::new_multiple(["./src/a.js", "./src/b.js"]);
/// ```
pub fn new_multiple<P, I>(entries: I) -> Self
where
P: AsRef<Path>,
I: IntoIterator<Item = P>,
{
let normalized = entries.into_iter().map(normalize_entry_path).collect();
let mut opts = Self::new("."); // Dummy entry
opts.entry = EntryPoints::Multiple(normalized);
opts
}
/// Preset: library build configuration.
///
/// Optimized for npm packages:
/// - `bundle: false` (externalize all dependencies)
/// - `platform: Node`
/// - Auto-generates .d.ts for TypeScript entries (if dts-generation feature enabled)
///
/// # Examples
///
/// ```no_run
/// use fob_bundler::BuildOptions;
///
/// # async fn example() -> fob_bundler::Result<()> {
/// let result = BuildOptions::library("./src/index.ts")
/// .external(["react"]) // Optional: explicitly externalize
/// .build()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn library(entry: impl AsRef<Path>) -> Self {
let mut opts = Self::new(entry);
opts.bundle = false;
opts.platform = Platform::Node;
#[cfg(feature = "dts-generation")]
{
opts.dts = Some(DtsOptions::default());
}
opts
}
/// Preset: components build (independent islands).
///
/// Bundles each component separately without shared chunks:
/// - `bundle: true`
/// - `splitting: false`
/// - Creates one bundle per entry
pub fn components<P, I>(entries: I) -> Self
where
P: AsRef<Path>,
I: IntoIterator<Item = P>,
{
let mut opts = Self::new_multiple(entries);
opts.bundle = true;
opts.splitting = false;
opts
}
/// Preset: application build with code splitting.
///
/// Optimized for web apps:
/// - `bundle: true`
/// - `splitting: true`
/// - Shared dependencies extracted to chunks
pub fn app<P, I>(entries: I) -> Self
where
P: AsRef<Path>,
I: IntoIterator<Item = P>,
{
let mut opts = Self::new_multiple(entries);
opts.bundle = true;
opts.splitting = true;
opts
}
/// Set whether to bundle dependencies.
pub fn bundle(mut self, enabled: bool) -> Self {
self.bundle = enabled;
self
}
/// Set whether to enable code splitting.
pub fn splitting(mut self, enabled: bool) -> Self {
self.splitting = enabled;
self
}
/// Add external packages that should not be bundled.
pub fn external<I, S>(mut self, packages: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
for pkg in packages {
let value = pkg.into();
if !self.external.contains(&value) {
self.external.push(value);
}
}
self
}
/// Set the output directory.
pub fn outdir(mut self, dir: impl Into<PathBuf>) -> Self {
self.outdir = Some(dir.into());
self
}
/// Set the output file (single entry only).
pub fn outfile(mut self, file: impl Into<PathBuf>) -> Self {
self.outfile = Some(file.into());
self
}
/// Set the target platform.
pub fn platform(mut self, platform: Platform) -> Self {
self.platform = platform;
self
}
/// Set the output format.
pub fn format(mut self, format: OutputFormat) -> Self {
self.format = format;
self
}
/// Enable or disable source maps.
pub fn sourcemap(mut self, enabled: bool) -> Self {
self.sourcemap = if enabled {
Some(crate::SourceMapType::File)
} else {
None
};
self
}
/// Generate inline source maps.
pub fn sourcemap_inline(mut self) -> Self {
self.sourcemap = Some(crate::SourceMapType::Inline);
self
}
/// Generate hidden source maps.
pub fn sourcemap_hidden(mut self) -> Self {
self.sourcemap = Some(crate::SourceMapType::Hidden);
self
}
/// Enable or disable minification.
pub fn minify(mut self, enabled: bool) -> Self {
self.minify = enabled;
self
}
/// Set global variable mappings for external packages.
pub fn globals_map<I, K, V>(mut self, entries: I) -> Self
where
I: IntoIterator<Item = (K, V)>,
K: Into<String>,
V: Into<String>,
{
for (k, v) in entries {
self.globals.insert(k.into(), v.into());
}
self
}
/// Add a Rolldown plugin.
pub fn plugin<P>(mut self, plugin: P) -> Self
where
P: crate::builders::common::IntoPlugin,
{
self.plugins.push(plugin.into_plugin());
self
}
/// Add a virtual file.
pub fn virtual_file(mut self, path: impl Into<String>, content: impl Into<String>) -> Self {
self.virtual_files.insert(path.into(), content.into());
self
}
/// Add a path alias for import resolution.
///
/// # Examples
///
/// ```no_run
/// use fob_bundler::BuildOptions;
///
/// let options = BuildOptions::new("./src/index.ts")
/// .path_alias("@", "./src") // @/components/Button -> ./src/components/Button
/// .path_alias("~", "./lib"); // ~/utils -> ./lib/utils
/// ```
pub fn path_alias(mut self, alias: impl Into<String>, target: impl Into<String>) -> Self {
self.path_aliases.insert(alias.into(), target.into());
self
}
/// Add multiple path aliases at once.
///
/// # Examples
///
/// ```no_run
/// use fob_bundler::BuildOptions;
/// use rustc_hash::FxHashMap;
///
/// let mut aliases = FxHashMap::default();
/// aliases.insert("@".to_string(), "./src".to_string());
/// aliases.insert("~".to_string(), "./lib".to_string());
///
/// let options = BuildOptions::new("./src/index.ts")
/// .path_aliases(aliases);
/// ```
pub fn path_aliases(mut self, aliases: FxHashMap<String, String>) -> Self {
self.path_aliases = aliases;
self
}
/// Set the working directory for module resolution.
pub fn cwd(mut self, cwd: impl Into<PathBuf>) -> Self {
self.cwd = Some(cwd.into());
self
}
/// Set the runtime for filesystem operations.
///
/// This is required for WASM builds to bridge to JavaScript filesystem APIs.
/// On native platforms, NativeRuntime is automatically created if not specified.
/// You can provide a custom runtime for advanced use cases or testing.
pub fn runtime(mut self, runtime: Arc<dyn Runtime>) -> Self {
self.runtime = Some(runtime);
self
}
/// Enable modern decorator transformation.
///
/// Enables transformation of JavaScript/TypeScript decorators using the TC39 Stage 3 proposal.
/// This is the modern, standardized decorator syntax, not TypeScript's legacy experimentalDecorators.
///
/// # Examples
///
/// ```no_run
/// use fob_bundler::BuildOptions;
///
/// # async fn example() -> fob_bundler::Result<()> {
/// let result = BuildOptions::library("./src/index.ts")
/// .decorators(true)
/// .build()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn decorators(mut self, enabled: bool) -> Self {
if enabled {
self.decorator = Some(crate::DecoratorOptions {
legacy: Some(false), // Modern decorators only
emit_decorator_metadata: None,
});
} else {
self.decorator = None;
}
self
}
/// Enable TypeScript declaration file generation.
#[cfg(feature = "dts-generation")]
pub fn emit_dts(mut self, enabled: bool) -> Self {
let mut dts = self.dts.unwrap_or_default();
dts.emit = Some(enabled);
self.dts = Some(dts);
self
}
/// Set the output directory for declaration files.
#[cfg(feature = "dts-generation")]
pub fn dts_outdir(mut self, dir: impl Into<PathBuf>) -> Self {
let mut dts = self.dts.unwrap_or_default();
dts.outdir = Some(dir.into());
self.dts = Some(dts);
self
}
/// Strip @internal JSDoc tags from declarations.
#[cfg(feature = "dts-generation")]
pub fn strip_internal(mut self, enabled: bool) -> Self {
let mut dts = self.dts.unwrap_or_default();
dts.strip_internal = enabled;
self.dts = Some(dts);
self
}
/// Generate source maps for declaration files.
#[cfg(feature = "dts-generation")]
pub fn declaration_map(mut self, enabled: bool) -> Self {
let mut dts = self.dts.unwrap_or_default();
dts.sourcemap = enabled;
self.dts = Some(dts);
self
}
/// Validate the build options for internal consistency.
///
/// # Errors
///
/// Returns an error if the configuration is invalid, such as:
/// - Using `outfile` with multiple entry points
/// - Using `outfile` with `splitting` enabled
/// - Using both `outdir` and `outfile`
/// - Using `splitting` with a single entry point
/// - Using `splitting` without `bundle`
/// - Too many entry points (DoS protection)
pub fn validate(&self) -> Result<()> {
// outfile requires single entry
if self.outfile.is_some() {
if !matches!(self.entry, EntryPoints::Single(_)) {
return Err(Error::InvalidConfig(
"outfile can only be used with a single entry point".into(),
));
}
if self.splitting {
return Err(Error::InvalidConfig(
"outfile cannot be used with code splitting enabled".into(),
));
}
}
// splitting requires multiple entries and bundle: true
if self.splitting {
if matches!(self.entry, EntryPoints::Single(_)) {
return Err(Error::InvalidConfig(
"code splitting requires multiple entry points".into(),
));
}
if !self.bundle {
return Err(Error::InvalidConfig(
"code splitting requires bundle: true".into(),
));
}
}
// outdir and outfile are mutually exclusive
if self.outdir.is_some() && self.outfile.is_some() {
return Err(Error::InvalidConfig(
"outdir and outfile cannot both be specified".into(),
));
}
// DoS protection: limit entry point count
const MAX_ENTRY_POINTS: usize = 1000;
let entry_count = match &self.entry {
EntryPoints::Single(_) => 1,
EntryPoints::Multiple(v) => v.len(),
EntryPoints::Named(m) => m.len(),
};
if entry_count > MAX_ENTRY_POINTS {
return Err(Error::InvalidConfig(format!(
"Too many entry points: {} (max {})",
entry_count, MAX_ENTRY_POINTS
)));
}
if entry_count == 0 {
return Err(Error::InvalidConfig(
"At least one entry point is required".into(),
));
}
Ok(())
}
/// Execute the build with these options.
///
/// This is a convenience method that calls `build(self)`.
pub async fn build(self) -> Result<super::output::BuildResult> {
super::build(self).await
}
}
impl Default for BuildOptions {
fn default() -> Self {
Self::new(".")
}
}
/// Normalize an entry path by cleaning redundant `.` / `..` segments.
fn normalize_entry_path(entry: impl AsRef<Path>) -> String {
crate::builders::common::normalize_entry_path(entry)
}