Skip to content

Commit

Permalink
feat: add basic renderChunk hook (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin authored Jan 23, 2024
1 parent 4832c55 commit b7ea28b
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 16 deletions.
14 changes: 13 additions & 1 deletion crates/rolldown/src/bundler/plugin_driver/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::sync::Arc;

use rolldown_error::BuildError;

use crate::{
plugin::{
args::HookBuildEndArgs,
args::{HookBuildEndArgs, RenderChunkArgs},
plugin::{BoxPlugin, HookNoopReturn},
},
HookLoadArgs, HookLoadReturn, HookResolveIdArgs, HookResolveIdReturn, HookTransformArgs,
Expand Down Expand Up @@ -60,4 +62,14 @@ impl PluginDriver {
}
Ok(())
}

#[allow(dead_code)]
pub async fn render_chunk(&self, mut args: RenderChunkArgs) -> Result<String, BuildError> {
for plugin in &self.plugins {
if let Some(r) = plugin.render_chunk(&PluginContext::new(), &args).await? {
args.code = r.code;
}
}
Ok(args.code)
}
}
13 changes: 2 additions & 11 deletions crates/rolldown/src/bundler/stages/scan_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
symbols::Symbols,
},
},
error::{BatchedErrors, BatchedResult},
error::{collect_errors, BatchedResult},
HookResolveIdArgsOptions, SharedResolver,
};

Expand Down Expand Up @@ -101,15 +101,6 @@ impl<Fs: FileSystem + Default + 'static> ScanStage<Fs> {
}
}));

let mut errors = BatchedErrors::default();

let collected =
resolved_ids.into_iter().filter_map(|item| errors.take_err_from(item)).collect();

if errors.is_empty() {
Ok(collected)
} else {
Err(errors)
}
collect_errors(resolved_ids)
}
}
1 change: 1 addition & 0 deletions crates/rolldown/src/bundler/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod bitset;
pub mod finalizer;
pub mod load_source;
pub mod renamer;
pub mod render_chunks;
pub mod resolve_id;
pub mod symbols;
pub mod transform_source;
22 changes: 22 additions & 0 deletions crates/rolldown/src/bundler/utils/render_chunks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use rolldown_utils::block_on_spawn_all;

use crate::{
bundler::{chunk::ChunkId, plugin_driver::PluginDriver},
error::{collect_errors, BatchedErrors},
plugin::args::RenderChunkArgs,
};

#[allow(clippy::unused_async)]
pub async fn _render_chunks<'a>(
plugin_driver: &PluginDriver,
chunks: Vec<(ChunkId, String)>,
) -> Result<Vec<(ChunkId, String)>, BatchedErrors> {
let result = block_on_spawn_all(chunks.iter().map(|(chunk, content)| async move {
match plugin_driver.render_chunk(RenderChunkArgs { code: content.to_string() }).await {
Ok(value) => Ok((*chunk, value)),
Err(e) => Err(e),
}
}));

collect_errors(result)
}
12 changes: 12 additions & 0 deletions crates/rolldown/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ impl BatchedErrors {
}
}

pub fn collect_errors<T>(value: Vec<Result<T, BuildError>>) -> BatchedResult<Vec<T>> {
let mut errors = BatchedErrors::default();

let collected = value.into_iter().filter_map(|item| errors.take_err_from(item)).collect();

if errors.is_empty() {
Ok(collected)
} else {
Err(errors)
}
}

pub type BatchedResult<T> = Result<T, BatchedErrors>;

impl From<BuildError> for BatchedErrors {
Expand Down
5 changes: 3 additions & 2 deletions crates/rolldown/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ pub use crate::{
HookTransformArgs,
},
context::PluginContext,
output::{HookLoadOutput, HookResolveIdOutput},
output::{HookLoadOutput, HookRenderChunkOutput, HookResolveIdOutput},
plugin::{
BoxPlugin, HookLoadReturn, HookNoopReturn, HookResolveIdReturn, HookTransformReturn, Plugin,
BoxPlugin, HookLoadReturn, HookNoopReturn, HookRenderChunkReturn, HookResolveIdReturn,
HookTransformReturn, Plugin,
},
},
};
5 changes: 5 additions & 0 deletions crates/rolldown/src/plugin/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ pub struct HookLoadArgs<'a> {
pub struct HookBuildEndArgs {
pub error: String,
}

#[derive(Debug)]
pub struct RenderChunkArgs {
pub code: String,
}
5 changes: 5 additions & 0 deletions crates/rolldown/src/plugin/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ pub struct HookResolveIdOutput {
pub struct HookLoadOutput {
pub code: String,
}

#[derive(Debug)]
pub struct HookRenderChunkOutput {
pub code: String,
}
13 changes: 11 additions & 2 deletions crates/rolldown/src/plugin/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ use std::{borrow::Cow, fmt::Debug};
use rolldown_error::BuildError;

use super::{
args::{HookBuildEndArgs, HookLoadArgs, HookResolveIdArgs, HookTransformArgs},
args::{HookBuildEndArgs, HookLoadArgs, HookResolveIdArgs, HookTransformArgs, RenderChunkArgs},
context::PluginContext,
output::{HookLoadOutput, HookResolveIdOutput},
output::{HookLoadOutput, HookRenderChunkOutput, HookResolveIdOutput},
};

pub type HookResolveIdReturn = Result<Option<HookResolveIdOutput>, BuildError>;
pub type HookTransformReturn = Result<Option<HookLoadOutput>, BuildError>;
pub type HookLoadReturn = Result<Option<HookLoadOutput>, BuildError>;
pub type HookNoopReturn = Result<(), BuildError>;
pub type HookRenderChunkReturn = Result<Option<HookRenderChunkOutput>, BuildError>;

#[async_trait::async_trait]
pub trait Plugin: Debug + Send + Sync {
Expand Down Expand Up @@ -50,6 +51,14 @@ pub trait Plugin: Debug + Send + Sync {
) -> HookNoopReturn {
Ok(())
}

async fn render_chunk(
&self,
_ctx: &PluginContext,
_args: &RenderChunkArgs,
) -> HookRenderChunkReturn {
Ok(None)
}
}

pub type BoxPlugin = Box<dyn Plugin>;

0 comments on commit b7ea28b

Please sign in to comment.