rolldown 1.0.3

Fast JavaScript bundler in Rust, designed for the future of Vite
Documentation
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
use std::{borrow::Cow, path::Path, sync::Arc};

use arcstr::ArcStr;
use itertools::Either;
use oxc::{transformer::EngineTargets, transformer_plugins::InjectGlobalVariablesConfig};
use rolldown_common::{
  AttachDebugInfo, CodeSplittingMode, GlobalsOutputOption, InjectImport, JsxOptions, JsxPreset,
  LegalComments, MinifyOptions, ModuleType, NormalizedBundlerOptions, OutputFormat, Platform,
  PreserveEntrySignatures, RawTransformOptions, TransformOptions, TreeshakeOptions, TsConfig,
  merge_transform_options_with_tsconfig, normalize_optimization_option,
};
use rolldown_error::{BuildDiagnostic, BuildResult, InvalidOptionType};
use rolldown_fs::{OsFileSystem, OxcResolverFileSystem as _};
use rolldown_resolver::Resolver;
use rolldown_utils::ecmascript::is_validate_identifier_name;
use rustc_hash::{FxHashMap, FxHashSet};
use sugar_path::SugarPath;

use crate::{SharedResolver, utils::determine_minify_internal_exports_default};

pub struct PrepareBuildContext {
  pub fs: OsFileSystem,
  pub resolver: SharedResolver<OsFileSystem>,
  pub options: Arc<NormalizedBundlerOptions>,
  pub warnings: Vec<BuildDiagnostic>,
}

fn verify_raw_options(raw_options: &crate::BundlerOptions) -> BuildResult<Vec<BuildDiagnostic>> {
  let mut warnings: Vec<BuildDiagnostic> = Vec::new();
  let mut errors: Vec<BuildDiagnostic> = Vec::new();

  if raw_options.dir.is_some() && raw_options.file.is_some() {
    warnings.push(
      BuildDiagnostic::invalid_option(InvalidOptionType::InvalidOutputDirOption)
        .with_severity_warning(),
    );
  }

  if let Some(entity) = raw_options.context.as_ref() {
    if !is_validate_identifier_name(entity) {
      warnings.push(
        BuildDiagnostic::invalid_option(InvalidOptionType::InvalidContext(entity.clone()))
          .with_severity_warning(),
      );
    }
  }

  if let Some(format @ (OutputFormat::Umd | OutputFormat::Iife)) = raw_options.format {
    if matches!(raw_options.code_splitting, Some(CodeSplittingMode::Bool(true))) {
      warnings.push(
        BuildDiagnostic::invalid_option(InvalidOptionType::UnsupportedCodeSplittingFormat(
          format.to_string(),
        ))
        .with_severity_warning(),
      );
    }
  }

  if matches!(raw_options.code_splitting, Some(CodeSplittingMode::Bool(false))) {
    if let Some(input) = &raw_options.input
      && input.len() > 1
    {
      errors.push(BuildDiagnostic::invalid_option(
        InvalidOptionType::CodeSplittingDisabledWithMultipleInputs,
      ));
    }
    if matches!(raw_options.preserve_modules, Some(true)) {
      errors.push(BuildDiagnostic::invalid_option(
        InvalidOptionType::CodeSplittingDisabledWithPreserveModules,
      ));
    }
    if raw_options.manual_code_splitting.is_some() {
      errors.push(BuildDiagnostic::invalid_option(
        InvalidOptionType::CodeSplittingDisabledWithManualCodeSplitting,
      ));
    }
  }

  if let Some(manual_code_splitting) = &raw_options.manual_code_splitting {
    let has_groups = manual_code_splitting.groups.as_ref().is_some_and(|groups| !groups.is_empty());

    if !has_groups {
      let mut specified_options = Vec::new();
      if manual_code_splitting.min_share_count.is_some() {
        specified_options.push("minShareCount".to_string());
      }
      if manual_code_splitting.min_size.is_some() {
        specified_options.push("minSize".to_string());
      }
      if manual_code_splitting.max_size.is_some() {
        specified_options.push("maxSize".to_string());
      }
      if manual_code_splitting.min_module_size.is_some() {
        specified_options.push("minModuleSize".to_string());
      }
      if manual_code_splitting.max_module_size.is_some() {
        specified_options.push("maxModuleSize".to_string());
      }
      if manual_code_splitting.include_dependencies_recursively.is_some() {
        specified_options.push("includeDependenciesRecursively".to_string());
      }

      if !specified_options.is_empty() {
        warnings.push(
          BuildDiagnostic::invalid_option(InvalidOptionType::ManualCodeSplittingWithoutGroups(
            specified_options,
          ))
          .with_severity_warning(),
        );
      }
    }

    // Check if `codeSplitting.include_dependencies_recursively` conflict with `preserveEntrySignatures`
    if matches!(manual_code_splitting.include_dependencies_recursively, Some(false)) {
      if let Some(preserve_signatures) = &raw_options.preserve_entry_signatures {
        if matches!(
          preserve_signatures,
          PreserveEntrySignatures::Strict | PreserveEntrySignatures::ExportsOnly
        ) {
          errors.push(BuildDiagnostic::invalid_option(
            InvalidOptionType::IncludeDependenciesRecursivelyWithConflictPreserveEntrySignatures(
              preserve_signatures.to_string(),
            ),
          ));
        }
      }
    }
  }

  if errors.is_empty() { Ok(warnings) } else { Err(errors.into()) }
}

#[expect(clippy::too_many_lines)] // This function is long, but it's mostly just mapping values
pub fn prepare_build_context(
  mut raw_options: crate::BundlerOptions,
) -> BuildResult<PrepareBuildContext> {
  let mut warnings = verify_raw_options(&raw_options)?;

  let format = raw_options.format.unwrap_or(crate::OutputFormat::Esm);

  let preserve_entry_signatures = if let Some(manual_code_splitting) =
    &raw_options.manual_code_splitting
    && matches!(manual_code_splitting.include_dependencies_recursively, Some(false))
    && raw_options.preserve_entry_signatures.is_none()
  {
    warnings.push(
      BuildDiagnostic::invalid_option(
        InvalidOptionType::IncludeDependenciesRecursivelyWithImplicitPreserveEntrySignatures,
      )
      .with_severity_warning(),
    );
    PreserveEntrySignatures::AllowExtension
  } else {
    raw_options.preserve_entry_signatures.unwrap_or_default()
  };

  let platform = raw_options.platform.unwrap_or(match format {
    OutputFormat::Cjs => Platform::Node,
    OutputFormat::Esm | OutputFormat::Iife | OutputFormat::Umd => Platform::Browser,
  });

  let raw_minify = raw_options.minify.unwrap_or_default();

  let mut raw_define = raw_options.define.unwrap_or_default();
  if matches!(platform, Platform::Browser) && !raw_define.contains_key("process.env.NODE_ENV") {
    if raw_minify.is_production() {
      raw_define.insert("process.env.NODE_ENV".to_string(), "'production'".to_string());
    } else {
      raw_define.insert("process.env.NODE_ENV".to_string(), "'development'".to_string());
    }
  }

  let define = raw_define.into_iter().collect();

  // Take out resolve options
  let mut raw_resolve = std::mem::take(&mut raw_options.resolve).unwrap_or_default();

  // https://github.com/evanw/esbuild/blob/ea453bf687c8e5cf3c5f11aae372c5ca33be0c98/pkg/api/api_impl.go#L1403-L1405
  // https://github.com/evanw/esbuild/commit/5abe0715f9be662b182989d2f38a44c7c8b28a2d
  if raw_resolve.condition_names.is_none() && matches!(platform, Platform::Browser | Platform::Node)
  {
    raw_resolve.condition_names = Some(vec!["module".to_string()]);
  }

  let mut module_types: FxHashMap<Cow<'static, str>, ModuleType> = FxHashMap::from(
    [
      ("js".into(), ModuleType::Js),
      ("mjs".into(), ModuleType::Js),
      ("cjs".into(), ModuleType::Js),
      ("jsx".into(), ModuleType::Jsx),
      ("ts".into(), ModuleType::Ts),
      ("mts".into(), ModuleType::Ts),
      ("cts".into(), ModuleType::Ts),
      ("tsx".into(), ModuleType::Tsx),
      ("json".into(), ModuleType::Json),
      ("txt".into(), ModuleType::Text),
      ("css".into(), ModuleType::Css),
    ]
    .into_iter()
    .collect(),
  );

  if let Some(user_defined_loaders) = raw_options.module_types {
    user_defined_loaders.into_iter().for_each(|(ext, value)| {
      let stripped = ext.strip_prefix('.').map(ToString::to_string).unwrap_or(ext);
      module_types.insert(Cow::Owned(stripped), value);
    });
  }

  let globals = raw_options.globals.unwrap_or(GlobalsOutputOption::FxHashMap(FxHashMap::default()));
  let generated_code = raw_options.generated_code.unwrap_or_default();

  let oxc_inject_global_variables_config = InjectGlobalVariablesConfig::new(
    raw_options
      .inject
      .as_ref()
      .map(|raw_injects| {
        raw_injects
          .iter()
          .map(|raw| match raw {
            InjectImport::Named { imported, alias, from } => {
              oxc::transformer_plugins::InjectImport::named_specifier(
                from,
                Some(imported),
                alias.as_deref().unwrap_or(imported),
              )
            }
            InjectImport::Namespace { alias, from } => {
              oxc::transformer_plugins::InjectImport::namespace_specifier(from, alias)
            }
          })
          .collect()
      })
      .unwrap_or_default(),
  );

  let mut experimental = raw_options.experimental.unwrap_or_default();
  if experimental.dev_mode.is_some() {
    experimental.incremental_build = Some(true);
  }

  if experimental.attach_debug_info.is_none() {
    experimental.attach_debug_info = Some(AttachDebugInfo::Simple);
  }

  let code_splitting = match format {
    OutputFormat::Umd | OutputFormat::Iife => CodeSplittingMode::Bool(false),
    _ => raw_options.code_splitting.unwrap_or_default(),
  };

  // If the `file` is provided, use the parent directory of the file as the `out_dir`.
  // Otherwise, use the `dir` if provided, or default to `dist`.
  let out_dir = raw_options.file.as_ref().map_or_else(
    || raw_options.dir.clone().unwrap_or_else(|| "dist".to_string()),
    |file| {
      Path::new(file.as_str())
        .parent()
        .map(|parent| parent.to_string_lossy().to_string())
        .unwrap_or_default()
    },
  );
  let cwd =
    raw_options.cwd.unwrap_or_else(|| std::env::current_dir().expect("Failed to get current dir"));

  let mut raw_treeshake = raw_options.treeshake;
  if experimental.dev_mode.is_some() {
    // Dev mode requires treeshaking to be disabled
    raw_treeshake = TreeshakeOptions::Boolean(false);
  }

  let tsconfig = raw_options.tsconfig.map(|tsconfig| tsconfig.with_base(&cwd)).unwrap_or_default();
  let yarn_pnp = raw_resolve.yarn_pnp.unwrap_or(false);
  let fs = OsFileSystem::new(yarn_pnp);
  let resolver = Arc::new(Resolver::new(fs.clone(), cwd.clone(), platform, &tsconfig, raw_resolve));

  let transform_options = {
    let mut raw_transform_options = raw_options.transform.unwrap_or_default();

    let target = match &raw_transform_options.target {
      Some(Either::Left(target)) => EngineTargets::from_target(target),
      Some(Either::Right(targets)) => EngineTargets::from_target_list(targets),
      None => Ok(EngineTargets::default()),
    }
    .map_err(|message| {
      let hint = message
        .contains("Invalid target")
        .then(|| "Rolldown only supports ES2015 (ES6) and later.".to_owned());
      BuildDiagnostic::bundler_initialize_error(message, hint)
    })?;

    let mut jsx_preset = JsxPreset::Enable;
    if let Some(Either::Left(jsx_str)) = &mut raw_transform_options.jsx {
      match jsx_str.as_str() {
        "react" => {
          raw_transform_options.jsx = Some(Either::Right(JsxOptions {
            runtime: Some(String::from("classic")),
            ..Default::default()
          }));
        }
        "react-jsx" => {
          raw_transform_options.jsx = Some(Either::Right(JsxOptions::default()));
        }
        // Keep JSX syntax as-is in the output (parser enabled, transformer disabled)
        "preserve" => jsx_preset = JsxPreset::Preserve,
        // Disable JSX parser and transformer entirely - will error if JSX syntax is encountered
        "disable" => {
          jsx_preset = JsxPreset::Disable;
          "preserve".clone_into(jsx_str);
        }
        _ => {
          Err(BuildDiagnostic::bundler_initialize_error(
            format!("Invalid jsx option: `{jsx_str}`."),
            Some(
              "Valid options are `false | 'react' | 'react-jsx' | 'preserve'`, or jsx options."
                .to_owned(),
            ),
          ))?;
        }
      }
    }

    // Create TransformOptions based on tsconfig mode:
    // - Auto: Create Raw mode (will resolve tsconfig per file)
    // - None/Manual: Create Normal mode (resolve tsconfig once now)
    match tsconfig {
      ref v @ TsConfig::Manual(ref path) => {
        // Manual mode: Resolve tsconfig now and create Normal mode
        let resolved_tsconfig = resolver.resolve_tsconfig(&path).map_err(|err| {
          anyhow::anyhow!("Failed to resolve `tsconfig` option: {}", path.display()).context(err)
        })?;
        Box::new(if resolved_tsconfig.references_resolved.is_empty() {
          TransformOptions::new(
            merge_transform_options_with_tsconfig(
              raw_transform_options,
              Some(&resolved_tsconfig),
              &mut warnings,
            )?,
            target,
            jsx_preset,
          )
        } else {
          TransformOptions::new_raw(
            RawTransformOptions::new(raw_transform_options, v.clone(), yarn_pnp),
            target,
            jsx_preset,
          )
        })
      }
      v @ TsConfig::Auto(is_auto) => {
        Box::new(if is_auto {
          // Auto mode: Create Raw mode TransformOptions
          // Each file will find its nearest tsconfig during compilation
          TransformOptions::new_raw(
            RawTransformOptions::new(raw_transform_options, v, yarn_pnp),
            target,
            jsx_preset,
          )
        } else {
          TransformOptions::new(
            merge_transform_options_with_tsconfig(raw_transform_options, None, &mut warnings)?,
            target,
            jsx_preset,
          )
        })
      }
    }
  };

  let mut normalized = NormalizedBundlerOptions {
    input: raw_options.input.unwrap_or_default(),
    external: raw_options.external.unwrap_or_default(),
    treeshake: raw_treeshake.into_normalized_options(),
    platform,
    name: raw_options.name,
    entry_filenames: raw_options.entry_filenames.unwrap_or_else(|| "[name].js".to_string().into()),
    chunk_filenames: raw_options
      .chunk_filenames
      .unwrap_or_else(|| "[name]-[hash].js".to_string().into()),
    asset_filenames: raw_options
      .asset_filenames
      .unwrap_or_else(|| "assets/[name]-[hash][extname]".to_string().into()),
    sanitize_filename: raw_options.sanitize_filename.unwrap_or_default(),
    banner: raw_options.banner,
    footer: raw_options.footer,
    post_banner: raw_options.post_banner,
    post_footer: raw_options.post_footer,
    intro: raw_options.intro,
    outro: raw_options.outro,
    es_module: raw_options.es_module.unwrap_or_default(),
    dir: raw_options.dir,
    out_dir,
    file: raw_options.file,
    format,
    exports: raw_options.exports.unwrap_or(crate::OutputExports::Auto),
    hash_characters: raw_options.hash_characters.unwrap_or(crate::HashCharacters::Base64),
    globals,
    paths: raw_options.paths,
    generated_code,
    sourcemap: raw_options.sourcemap,
    sourcemap_base_url: raw_options.sourcemap_base_url,
    sourcemap_ignore_list: raw_options.sourcemap_ignore_list,
    sourcemap_path_transform: raw_options.sourcemap_path_transform,
    sourcemap_debug_ids: raw_options.sourcemap_debug_ids.unwrap_or(false),
    sourcemap_exclude_sources: raw_options.sourcemap_exclude_sources.unwrap_or(false),
    shim_missing_exports: raw_options.shim_missing_exports.unwrap_or(false),
    module_types,
    experimental,
    profiler_names: raw_options.profiler_names.unwrap_or(false),
    // Use placeholder for minify options at first
    minify: MinifyOptions::Disabled,
    define,
    inject: raw_options.inject.unwrap_or_default(),
    oxc_inject_global_variables_config,
    extend: raw_options.extend.unwrap_or(false),
    external_live_bindings: raw_options.external_live_bindings.unwrap_or(true),
    code_splitting,
    dynamic_import_in_cjs: raw_options.dynamic_import_in_cjs.unwrap_or(true),
    manual_code_splitting: raw_options.manual_code_splitting,
    checks: raw_options.checks.unwrap_or_default().into(),
    watch: raw_options.watch.unwrap_or_default(),
    legal_comments: raw_options.legal_comments.unwrap_or(LegalComments::Inline),
    comments: {
      let mut comments = raw_options.comments.unwrap_or_default();
      // When `comments` option is not explicitly set, `legalComments` can override `comments.legal`
      if raw_options.comments.is_none() {
        if let Some(legal) = raw_options.legal_comments {
          comments.legal = matches!(legal, LegalComments::Inline);
        }
      }
      comments
    },
    drop_labels: FxHashSet::from_iter(raw_options.drop_labels.unwrap_or_default()),
    keep_names: raw_options.keep_names.unwrap_or_default(),
    polyfill_require: raw_options.polyfill_require.unwrap_or(true),
    defer_sync_scan_data: raw_options.defer_sync_scan_data,
    transform_options,
    make_absolute_externals_relative: raw_options
      .make_absolute_externals_relative
      .unwrap_or_default(),
    invalidate_js_side_cache: raw_options.invalidate_js_side_cache,
    log_level: raw_options.log_level,
    on_log: raw_options.on_log,
    preserve_modules: raw_options.preserve_modules.unwrap_or_default(),
    virtual_dirname: raw_options
      .virtual_dirname
      .map(ArcStr::from)
      .unwrap_or_else(|| arcstr::literal!("_virtual")),
    preserve_modules_root: raw_options.preserve_modules_root.map(|preserve_modules_root| {
      let p = Path::new(&preserve_modules_root);
      cwd.join(p).normalize().to_string_lossy().to_string()
    }),
    cwd,
    preserve_entry_signatures,
    devtools: raw_options.devtools.is_some(),
    optimization: normalize_optimization_option(raw_options.optimization, platform),
    top_level_var: raw_options.top_level_var.unwrap_or(false),
    minify_internal_exports: raw_options
      .minify_internal_exports
      .unwrap_or_else(|| determine_minify_internal_exports_default(Some(format), &raw_minify)),
    clean_dir: raw_options.clean_dir.unwrap_or(false),
    context: raw_options.context.unwrap_or_default(),
    strict_execution_order: raw_options.strict_execution_order.unwrap_or(false),
    strict: raw_options.strict.unwrap_or_default(),
  };

  normalized.minify = raw_minify.normalize(&normalized);

  Ok(PrepareBuildContext { fs, resolver, options: Arc::new(normalized), warnings })
}