Skip to content

Commit

Permalink
feat: add PluginDriver (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed Nov 9, 2023
1 parent 4856b9b commit 526f896
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 14 deletions.
2 changes: 1 addition & 1 deletion crates/rolldown/examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{path::PathBuf, sync::Arc};
use std::path::PathBuf;

use rolldown::{Bundler, InputItem, InputOptions};
use rolldown_fs::FileSystemOs;
Expand Down
17 changes: 13 additions & 4 deletions crates/rolldown/src/bundler/bundler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,37 @@ use super::{
normalized_input_options::NormalizedInputOptions,
normalized_output_options::NormalizedOutputOptions,
},
plugin_driver::{PluginDriver, SharedPluginDriver},
};
use crate::{bundler::bundle::bundle::Bundle, plugin::plugin::BoxPlugin, InputOptions};

type BuildResult<T> = Result<T, Vec<BuildError>>;

pub struct Bundler<T: FileSystemExt> {
input_options: NormalizedInputOptions,
_plugins: Vec<BoxPlugin>,
plugin_driver: SharedPluginDriver,
fs: Arc<T>,
}

impl<T: FileSystemExt + Default + 'static> Bundler<T> {
pub fn new(input_options: InputOptions, fs: T) -> Self {
// rolldown_tracing::enable_tracing_on_demand();
let normalized = NormalizedInputOptions::from_input_options(input_options);
Self { input_options: normalized, _plugins: vec![], fs: Arc::new(fs) }
Self {
input_options: normalized,
plugin_driver: Arc::new(PluginDriver::new(vec![])),
fs: Arc::new(fs),
}
}

pub fn with_plugins(input_options: InputOptions, plugins: Vec<BoxPlugin>, fs: T) -> Self {
// rolldown_tracing::enable_tracing_on_demand();
let normalized = NormalizedInputOptions::from_input_options(input_options);
Self { input_options: normalized, _plugins: plugins, fs: Arc::new(fs) }
Self {
input_options: normalized,
plugin_driver: Arc::new(PluginDriver::new(plugins)),
fs: Arc::new(fs),
}
}

pub async fn write(&mut self, output_options: crate::OutputOptions) -> BuildResult<Vec<Asset>> {
Expand Down Expand Up @@ -82,7 +91,7 @@ impl<T: FileSystemExt + Default + 'static> Bundler<T> {
tracing::trace!("NormalizedOutputOptions: {output_options:#?}",);

let mut graph = Graph::default();
graph.generate_module_graph(&self.input_options, fs).await?;
graph.generate_module_graph(&self.input_options, Arc::clone(&self.plugin_driver), fs).await?;

let mut bundle = Bundle::new(&mut graph, &output_options);
let assets = bundle.generate(&self.input_options);
Expand Down
6 changes: 4 additions & 2 deletions crates/rolldown/src/bundler/graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use super::{linker::Linker, linker_info::LinkingInfoVec, symbols::Symbols};
use crate::{
bundler::{
module::ModuleVec, module_loader::ModuleLoader,
options::normalized_input_options::NormalizedInputOptions, runtime::Runtime,
options::normalized_input_options::NormalizedInputOptions, plugin_driver::SharedPluginDriver,
runtime::Runtime,
},
error::BatchedResult,
};
Expand All @@ -26,9 +27,10 @@ impl Graph {
pub async fn generate_module_graph<T: FileSystemExt + Default + 'static>(
&mut self,
input_options: &NormalizedInputOptions,
plugin_driver: SharedPluginDriver,
fs: Arc<T>,
) -> BatchedResult<()> {
ModuleLoader::new(input_options, self, fs).fetch_all_modules().await?;
ModuleLoader::new(input_options, plugin_driver, self, fs).fetch_all_modules().await?;

tracing::trace!("{:#?}", self);

Expand Down
10 changes: 5 additions & 5 deletions crates/rolldown/src/bundler/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
pub mod bundle;
#[allow(clippy::module_inception)]
pub mod bundler;
mod chunk;
mod chunk_graph;
mod graph;
mod module;
mod module_loader;
pub mod options;
pub mod plugin_driver;
mod renderer;
mod runtime;
pub mod utils;
mod visitors;

#[allow(clippy::module_inception)]
pub mod bundler;
mod chunk;
mod module_loader;
11 changes: 10 additions & 1 deletion crates/rolldown/src/bundler/module_loader/module_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::bundler::module::external_module::ExternalModule;
use crate::bundler::module::Module;
use crate::bundler::module_loader::module_task_context::ModuleTaskContext;
use crate::bundler::options::normalized_input_options::NormalizedInputOptions;
use crate::bundler::plugin_driver::SharedPluginDriver;
use crate::bundler::runtime::RUNTIME_PATH;
use crate::bundler::utils::ast_symbol::AstSymbol;
use crate::bundler::utils::resolve_id::{resolve_id, ResolvedRequestInfo};
Expand All @@ -30,6 +31,7 @@ pub struct ModuleLoader<'a, T: FileSystemExt + Default> {
tx: tokio::sync::mpsc::UnboundedSender<Msg>,
rx: tokio::sync::mpsc::UnboundedReceiver<Msg>,
fs: Arc<T>,
plugin_driver: SharedPluginDriver,
}

#[derive(Debug, Default)]
Expand Down Expand Up @@ -103,7 +105,12 @@ impl ModuleLoaderContext {
}

impl<'a, T: FileSystemExt + 'static + Default> ModuleLoader<'a, T> {
pub fn new(input_options: &'a NormalizedInputOptions, graph: &'a mut Graph, fs: Arc<T>) -> Self {
pub fn new(
input_options: &'a NormalizedInputOptions,
plugin_driver: SharedPluginDriver,
graph: &'a mut Graph,
fs: Arc<T>,
) -> Self {
let (tx, rx) = tokio::sync::mpsc::unbounded_channel::<Msg>();
Self {
tx,
Expand All @@ -113,6 +120,7 @@ impl<'a, T: FileSystemExt + 'static + Default> ModuleLoader<'a, T> {
graph,
fs,
ctx: ModuleLoaderContext::default(),
plugin_driver,
}
}

Expand All @@ -128,6 +136,7 @@ impl<'a, T: FileSystemExt + 'static + Default> ModuleLoader<'a, T> {
tx: &self.tx,
resolver: &self.resolver,
fs: &*self.fs,
plugin_driver: &self.plugin_driver,
};

self.graph.runtime.id = self.ctx.try_spawn_runtime_normal_module_task(&shared_task_context);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use rolldown_fs::FileSystemExt;

use crate::{bundler::options::normalized_input_options::NormalizedInputOptions, SharedResolver};
use crate::{
bundler::{
options::normalized_input_options::NormalizedInputOptions, plugin_driver::SharedPluginDriver,
},
SharedResolver,
};

use super::Msg;

Expand All @@ -10,6 +15,7 @@ pub struct ModuleTaskContext<'task, T: FileSystemExt + Default> {
pub tx: &'task tokio::sync::mpsc::UnboundedSender<Msg>,
pub resolver: &'task SharedResolver<T>,
pub fs: &'task dyn FileSystemExt,
pub plugin_driver: &'task SharedPluginDriver,
}

impl<'task, T: FileSystemExt + Default> ModuleTaskContext<'task, T> {
Expand Down
60 changes: 60 additions & 0 deletions crates/rolldown/src/bundler/plugin_driver/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use std::sync::Arc;

use crate::{
plugin::plugin::BoxPlugin, HookLoadArgs, HookLoadReturn, HookResolveIdArgs, HookResolveIdReturn,
HookTransformArgs, HookTransformReturn, PluginContext,
};

pub type SharedPluginDriver = Arc<PluginDriver>;

pub struct PluginDriver {
_plugins: Vec<BoxPlugin>,
}

impl PluginDriver {
pub fn new(plugins: Vec<BoxPlugin>) -> Self {
Self { _plugins: plugins }
}

pub async fn _resolve_id(&self, args: &HookResolveIdArgs<'_>) -> HookResolveIdReturn {
for plugin in &self._plugins {
match plugin.resolve_id(&mut PluginContext::new(), args).await {
Err(e) => return Err(e),
Ok(r) => {
if let Some(r) = r {
return Ok(Some(r));
}
}
}
}
Ok(None)
}

pub async fn _load(&self, args: &HookLoadArgs<'_>) -> HookLoadReturn {
for plugin in &self._plugins {
match plugin.load(&mut PluginContext::new(), args).await {
Err(e) => return Err(e),
Ok(r) => {
if let Some(r) = r {
return Ok(Some(r));
}
}
}
}
Ok(None)
}

pub async fn _transform(&self, args: &HookTransformArgs<'_>) -> HookTransformReturn {
for plugin in &self._plugins {
match plugin.transform(&mut PluginContext::new(), args).await {
Err(e) => return Err(e),
Ok(r) => {
if let Some(r) = r {
return Ok(Some(r));
}
}
}
}
Ok(None)
}
}

0 comments on commit 526f896

Please sign in to comment.