Skip to content

Commit

Permalink
feat: remove build api
Browse files Browse the repository at this point in the history
  • Loading branch information
hyf0 committed Mar 3, 2024
1 parent 4a34345 commit 65b9bd6
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 82 deletions.
33 changes: 16 additions & 17 deletions crates/bench/benches/threejs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn criterion_benchmark(c: &mut Criterion) {
group.sample_size(20);
items.into_iter().for_each(|item| {
let scan_id = format!("{}-scan", item.name);
let build_id = format!("{}-build", item.name);
// let build_id = format!("{}-build", item.name);
let bundle_id = format!("{}-bundle", item.name);
group.bench_function(scan_id, |b| {
b.iter(|| {
Expand All @@ -37,21 +37,21 @@ fn criterion_benchmark(c: &mut Criterion) {
})
});
});
group.bench_function(build_id, |b| {
b.iter(|| {
tokio::runtime::Runtime::new().unwrap().block_on(async {
let mut rolldown_bundler = rolldown::Bundler::new(InputOptions {
input: vec![rolldown::InputItem {
name: Some(item.name.to_string()),
import: item.entry_path.to_string_lossy().to_string(),
}],
cwd: join_by_repo_root("crates/benches"),
..Default::default()
});
rolldown_bundler.build().await.unwrap();
})
});
});
// group.bench_function(build_id, |b| {
// b.iter(|| {
// tokio::runtime::Runtime::new().unwrap().block_on(async {
// let mut rolldown_bundler = rolldown::Bundler::new(InputOptions {
// input: vec![rolldown::InputItem {
// name: Some(item.name.to_string()),
// import: item.entry_path.to_string_lossy().to_string(),
// }],
// cwd: join_by_repo_root("crates/benches"),
// ..Default::default()
// });
// rolldown_bundler.build().await.unwrap();
// })
// });
// });
group.bench_function(bundle_id, |b| {
b.iter(|| {
tokio::runtime::Runtime::new().unwrap().block_on(async {
Expand All @@ -63,7 +63,6 @@ fn criterion_benchmark(c: &mut Criterion) {
cwd: join_by_repo_root("crates/bench"),
..Default::default()
});
rolldown_bundler.build().await.unwrap();
rolldown_bundler.write(Default::default()).await.unwrap();
})
});
Expand Down
1 change: 0 additions & 1 deletion crates/rolldown/examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ async fn main() {
cwd,
..Default::default()
});
bundler.build().await.unwrap();

let _outputs = bundler.write(OutputOptions::default()).await.unwrap();
// println!("{outputs:#?}");
Expand Down
42 changes: 10 additions & 32 deletions crates/rolldown/src/bundler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ use crate::{
InputOptions, OutputOptions, SharedResolver,
};

// Rolldown use this alias for outside users.
type BuildResult<T> = Result<T, Vec<BuildError>>;

pub struct RolldownOutput {
pub warnings: Vec<BuildError>,
pub assets: Vec<Output>,
Expand All @@ -34,8 +31,6 @@ pub struct Bundler<T: FileSystem + Default> {
plugin_driver: SharedPluginDriver,
fs: T,
resolver: SharedResolver<T>,
// Store the build result, using for generate/write.
build_result: Option<LinkStageOutput>,
}

impl Bundler<OsFileSystem> {
Expand Down Expand Up @@ -65,7 +60,6 @@ impl<T: FileSystem + Default + 'static> Bundler<T> {
plugin_driver: Arc::new(PluginDriver::new(plugins)),
input_options: Arc::new(input_options),
fs,
build_result: None,
}
}

Expand Down Expand Up @@ -103,11 +97,6 @@ impl<T: FileSystem + Default + 'static> Bundler<T> {
self.bundle_up(output_options, false).await
}

pub async fn build(&mut self) -> BuildResult<()> {
self.build_inner().await?;
Ok(())
}

pub async fn scan(&mut self) -> BatchedResult<()> {
self.plugin_driver.build_start().await?;

Expand All @@ -118,23 +107,6 @@ impl<T: FileSystem + Default + 'static> Bundler<T> {
Ok(())
}

async fn build_inner(&mut self) -> BatchedResult<()> {
self.plugin_driver.build_start().await?;

let ret = self.try_build().await;

let (err, value) = match ret {
Err(e) => (Some(e), None),
Ok(value) => (None, Some(value)),
};

self.call_build_end_hook(err).await?;

self.build_result = value;

Ok(())
}

async fn call_build_end_hook(&mut self, ret: Option<BatchedErrors>) -> BatchedResult<()> {
if let Some(e) = ret {
let error = e.get().expect("should have a error");
Expand Down Expand Up @@ -181,13 +153,19 @@ impl<T: FileSystem + Default + 'static> Bundler<T> {
) -> BatchedResult<RolldownOutput> {
tracing::trace!("InputOptions {:#?}", self.input_options);
tracing::trace!("OutputOptions: {output_options:#?}",);
let graph = self.build_result.as_mut().expect("Build should success");
let mut bundle_stage =
BundleStage::new(graph, &self.input_options, &output_options, &self.plugin_driver);
let mut link_stage_output = self.try_build().await?;

let mut bundle_stage = BundleStage::new(
&mut link_stage_output,
&self.input_options,
&output_options,
&self.plugin_driver,
);

let assets = bundle_stage.bundle().await?;

self.plugin_driver.generate_bundle(&assets, is_write).await?;

Ok(RolldownOutput { warnings: std::mem::take(&mut graph.warnings), assets })
Ok(RolldownOutput { warnings: std::mem::take(&mut link_stage_output.warnings), assets })
}
}
1 change: 0 additions & 1 deletion crates/rolldown/tests/common/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ impl Fixture {
std::fs::remove_dir_all(fixture_path.join("dist")).unwrap();
}

bundler.build().await?;
let value = bundler
.write(OutputOptions {
entry_file_names: FileNameTemplate::from("[name].mjs".to_string()),
Expand Down
1 change: 0 additions & 1 deletion crates/rolldown_binding/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,5 @@ export class Bundler {
constructor(inputOpts: InputOptions)
write(opts: OutputOptions): Promise<Outputs>
generate(opts: OutputOptions): Promise<Outputs>
build(): Promise<void>
scan(): Promise<void>
}
25 changes: 0 additions & 25 deletions crates/rolldown_binding/src/bundler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ impl Bundler {
self.generate_impl(opts).await
}

#[napi]
pub async fn build(&self) -> napi::Result<()> {
self.build_impl().await
}

#[napi]
pub async fn scan(&self) -> napi::Result<()> {
self.scan_impl().await
Expand Down Expand Up @@ -70,26 +65,6 @@ impl Bundler {
Ok(())
}

#[instrument(skip_all)]
#[allow(clippy::significant_drop_tightening)]
pub async fn build_impl(&self) -> napi::Result<()> {
let mut bundler_core = self.inner.try_lock().map_err(|_| {
napi::Error::from_reason("Failed to lock the bundler. Is another operation in progress?")
})?;

let result = bundler_core.build().await;

if let Err(err) = result {
// TODO: better handing errors
for err in err {
eprintln!("{err:?}");
}
return Err(napi::Error::from_reason("Build failed"));
}

Ok(())
}

#[instrument(skip_all)]
#[allow(clippy::significant_drop_tightening)]
pub async fn write_impl(&self, output_opts: OutputOptions) -> napi::Result<Outputs> {
Expand Down
4 changes: 0 additions & 4 deletions crates/rolldown_binding_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ pub fn bundle(file_list: Vec<FileItem>) -> Vec<AssetItem> {
memory_fs,
);

if let Err(e) = bundler.build().await {
panic!("{e:?}",);
}

match bundler.write(OutputOptions::default()).await {
Ok(assets) => assets
.assets
Expand Down
1 change: 0 additions & 1 deletion packages/node/src/rolldown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { createBundler } from './utils'
// Compat to `rollup.rollup`, it is include scan module graph and linker.
export const rolldown = async (input: InputOptions): Promise<RolldownBuild> => {
const bundler = await createBundler(input)
await bundler.build()
return new RolldownBuild(bundler)
}

Expand Down

0 comments on commit 65b9bd6

Please sign in to comment.