Skip to main content

rolldown/bundler/
bundler.rs

1use crate::{BundleFactory, BundlerOptions, types::scan_stage_cache::ScanStageCache};
2use rolldown_error::BuildResult;
3use rolldown_plugin::__inner::SharedPluginable;
4use std::ops::Deref;
5
6// TODO: hyf0 This's for avoiding having too many changes for watcher API. Will remove this later.
7impl Deref for Bundler {
8  type Target = BundleFactory;
9
10  fn deref(&self) -> &Self::Target {
11    &self.bundle_factory
12  }
13}
14
15pub struct Bundler {
16  pub(super) session: rolldown_devtools::Session,
17  pub(super) bundle_factory: BundleFactory,
18  #[cfg_attr(not(feature = "experimental"), allow(dead_code))]
19  pub(super) cache: ScanStageCache,
20  pub(super) closed: bool,
21}
22
23impl Bundler {
24  pub fn new(options: BundlerOptions) -> BuildResult<Self> {
25    Self::with_plugins(options, Vec::new())
26  }
27
28  pub fn with_plugins(
29    options: BundlerOptions,
30    plugins: Vec<SharedPluginable>,
31  ) -> BuildResult<Self> {
32    let bundle_factory = BundleFactory::new(crate::BundleFactoryOptions {
33      bundler_options: options,
34      plugins,
35      session: None,
36      disable_tracing_setup: true,
37    })?;
38
39    Ok(Self {
40      bundle_factory,
41      session: rolldown_devtools::Session::dummy(),
42      cache: ScanStageCache::default(),
43      closed: false,
44    })
45  }
46
47  pub(super) fn create_error_if_closed(&self) -> BuildResult<()> {
48    if self.closed {
49      Err(anyhow::anyhow!("Bundler is closed"))?;
50    }
51    Ok(())
52  }
53
54  // Implementation is split across multiple files:
55  // - Normal build operations and lifecycle: `impl_bundler_build.rs`
56  // - Getter/accessor methods: `impl_bundler_getter.rs`
57  // - Incremental build methods: `impl_bundler_incremental_build.rs`
58  // - HMR methods: `impl_bundler_hmr.rs`
59}
60
61fn _test_bundler() {
62  fn assert_send(_foo: impl Send) {}
63  let mut bundler = Bundler::new(BundlerOptions::default()).expect("Failed to create bundler");
64  let write_fut = bundler.write();
65  assert_send(write_fut);
66  let mut bundler = Bundler::new(BundlerOptions::default()).expect("Failed to create bundler");
67  let generate_fut = bundler.generate();
68  assert_send(generate_fut);
69}