Skip to content

Commit

Permalink
feat: support running Js plugin in parallel via Worker (#825)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Apr 14, 2024
1 parent b922e07 commit cff55de
Show file tree
Hide file tree
Showing 44 changed files with 2,492 additions and 479 deletions.
53 changes: 53 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ single_match = "allow"
[workspace.dependencies]
anyhow = "1.0.82"
ariadne = "0.4.0"
async-channel = "2.2.0"
async-trait = "0.1.80"
codspeed-criterion-compat = "2.4"
dashmap = "5.5.3"
Expand Down
4 changes: 4 additions & 0 deletions crates/rolldown_binding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,21 @@ workspace = true
crate-type = ["cdylib"]

[dependencies]
async-channel = { workspace = true }
async-trait = { workspace = true }
dashmap = { workspace = true }
derivative = { workspace = true }
futures = { workspace = true }
napi = { workspace = true }
napi-derive = { workspace = true }
once_cell = { workspace = true }
rolldown = { workspace = true }
rolldown_common = { workspace = true }
rolldown_error = { workspace = true, features = ["napi"] }
rolldown_plugin = { workspace = true }
rolldown_sourcemap = { workspace = true }
rolldown_tracing = { workspace = true }
rustc-hash = { workspace = true }
serde = { workspace = true }
tracing = { workspace = true }

Expand Down
19 changes: 18 additions & 1 deletion crates/rolldown_binding/src/bundler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ use tracing::instrument;

use crate::{
options::{BindingInputOptions, BindingOutputOptions},
parallel_js_plugin_registry::ParallelJsPluginRegistry,
types::binding_outputs::BindingOutputs,
utils::{normalize_binding_options::normalize_binding_options, try_init_custom_trace_subscriber},
worker_manager::WorkerManager,
};

#[napi]
Expand All @@ -23,9 +25,24 @@ impl Bundler {
env: Env,
input_options: BindingInputOptions,
output_options: BindingOutputOptions,
parallel_plugins_registry: Option<ParallelJsPluginRegistry>,
) -> napi::Result<Self> {
try_init_custom_trace_subscriber(env);
let ret = normalize_binding_options(input_options, output_options)?;

let worker_count =
parallel_plugins_registry.as_ref().map(|registry| registry.worker_count).unwrap_or_default();
let parallel_plugins_map =
parallel_plugins_registry.map(|registry| registry.take_plugin_values());

let worker_manager =
if worker_count > 0 { Some(WorkerManager::new(worker_count)) } else { None };

let ret = normalize_binding_options(
input_options,
output_options,
parallel_plugins_map,
worker_manager,
)?;

Ok(Self { inner: Mutex::new(NativeBundler::with_plugins(ret.bundler_options, ret.plugins)) })
}
Expand Down
2 changes: 2 additions & 0 deletions crates/rolldown_binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;

pub mod bundler;
pub mod options;
pub mod parallel_js_plugin_registry;
pub mod types;
pub mod utils;
mod worker_manager;
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use serde::Deserialize;

use self::{binding_input_item::BindingInputItem, binding_resolve_options::BindingResolveOptions};

use super::plugin::BindingPluginOptions;
use super::plugin::BindingPluginOrParallelJsPluginPlaceholder;

mod binding_input_item;
mod binding_resolve_options;
Expand Down Expand Up @@ -43,7 +43,7 @@ pub struct BindingInputOptions {
// moduleContext?: ((id: string) => string | null | void) | { [id: string]: string };
// onwarn?: WarningHandlerWithDefault;
// perf?: boolean;
pub plugins: Vec<BindingPluginOptions>,
pub plugins: Vec<BindingPluginOrParallelJsPluginPlaceholder>,
pub resolve: Option<BindingResolveOptions>,
// preserveEntrySignatures?: PreserveEntrySignaturesOption;
// /** @deprecated Use the "preserveModules" output option instead. */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::super::types::binding_rendered_chunk::RenderedChunk;
use super::plugin::BindingPluginOptions;
use super::plugin::BindingPluginOrParallelJsPluginPlaceholder;
use crate::types::js_callback::MaybeAsyncJsCallback;
use derivative::Derivative;
use napi_derive::napi;
Expand Down Expand Up @@ -59,7 +59,7 @@ pub struct BindingOutputOptions {
// noConflict: boolean;
// outro: () => string | Promise<string>;
// paths: OptionsPaths;
pub plugins: Vec<BindingPluginOptions>,
pub plugins: Vec<BindingPluginOrParallelJsPluginPlaceholder>,
// preferConst: boolean;
// preserveModules: boolean;
// preserveModulesRoot: string | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ use super::{
},
};

/// none is parallel js plugin
pub type BindingPluginOrParallelJsPluginPlaceholder = Option<BindingPluginOptions>;

#[napi_derive::napi(object, object_to_js = false)]
#[derive(Deserialize, Default)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -76,3 +79,11 @@ impl Debug for BindingPluginOptions {
f.debug_struct("BindingPluginOptions").field("name", &self.name).finish_non_exhaustive()
}
}

#[napi_derive::napi(object, object_to_js = false)]
#[derive(Debug, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct BindingPluginWithIndex {
pub index: u32,
pub plugin: BindingPluginOptions,
}
4 changes: 4 additions & 0 deletions crates/rolldown_binding/src/options/plugin/js_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ impl Deref for JsPlugin {
}

impl JsPlugin {
pub(super) fn new(inner: BindingPluginOptions) -> Self {
Self { inner }
}

pub(crate) fn new_boxed(inner: BindingPluginOptions) -> Box<dyn Plugin> {
Box::new(Self { inner })
}
Expand Down
2 changes: 2 additions & 0 deletions crates/rolldown_binding/src/options/plugin/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pub mod binding_plugin_context;
mod binding_plugin_options;
mod js_plugin;
mod parallel_js_plugin;
pub mod types;

pub use binding_plugin_options::*;
pub use js_plugin::*;
pub use parallel_js_plugin::*;

0 comments on commit cff55de

Please sign in to comment.