-
Notifications
You must be signed in to change notification settings - Fork 503
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add plugin adapter * chore: add bundler with_plugins
- Loading branch information
Showing
20 changed files
with
494 additions
and
94 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
crates/rolldown_node_binding/src/options/input_options/plugin.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use derivative::Derivative; | ||
use napi::JsFunction; | ||
use serde::Deserialize; | ||
|
||
#[napi_derive::napi(object)] | ||
#[derive(Deserialize, Default, Derivative)] | ||
#[serde(rename_all = "camelCase")] | ||
#[derivative(Debug)] | ||
pub struct PluginOptions { | ||
pub name: String, | ||
|
||
#[derivative(Debug = "ignore")] | ||
#[serde(skip_deserializing)] | ||
#[napi( | ||
ts_type = "(specifier: string, importer?: string) => Promise<undefined | ResolveIdResult>" | ||
)] | ||
pub resolve_id: Option<JsFunction>, | ||
|
||
#[derivative(Debug = "ignore")] | ||
#[serde(skip_deserializing)] | ||
#[napi(ts_type = "(id: string) => Promise<undefined | SourceResult>")] | ||
pub load: Option<JsFunction>, | ||
|
||
#[derivative(Debug = "ignore")] | ||
#[serde(skip_deserializing)] | ||
#[napi(ts_type = "(id: string, code: string) => Promise<undefined | SourceResult>")] | ||
pub transform: Option<JsFunction>, | ||
} | ||
|
||
#[napi_derive::napi(object)] | ||
#[derive(Deserialize, Default, Derivative)] | ||
#[serde(rename_all = "camelCase")] | ||
#[derivative(Debug)] | ||
pub struct ResolveIdResult { | ||
pub id: String, | ||
pub external: Option<bool>, | ||
} | ||
|
||
impl From<ResolveIdResult> for rolldown_plugin::ResolveIdResult { | ||
fn from(value: ResolveIdResult) -> Self { | ||
rolldown_plugin::ResolveIdResult { | ||
id: value.id, | ||
external: value.external, | ||
} | ||
} | ||
} | ||
|
||
#[napi_derive::napi(object)] | ||
#[derive(Deserialize, Default, Derivative)] | ||
#[serde(rename_all = "camelCase")] | ||
#[derivative(Debug)] | ||
pub struct SourceResult { | ||
pub code: String, | ||
} | ||
|
||
impl From<SourceResult> for rolldown_plugin::SourceResult { | ||
fn from(value: SourceResult) -> Self { | ||
rolldown_plugin::SourceResult { code: value.code } | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
crates/rolldown_node_binding/src/options/input_options/plugin_adapter.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
use crate::utils::napi_error_ext::NapiErrorExt; | ||
use crate::utils::JsCallback; | ||
use derivative::Derivative; | ||
use rolldown_plugin::{BoxPlugin, Plugin, PluginName}; | ||
|
||
use super::plugin::{PluginOptions, ResolveIdResult, SourceResult}; | ||
|
||
pub type ResolveIdCallback = JsCallback<(String, Option<String>), Option<ResolveIdResult>>; | ||
pub type LoadCallback = JsCallback<(String, Option<String>), Option<SourceResult>>; | ||
pub type TransformCallback = JsCallback<(String, String), Option<SourceResult>>; | ||
|
||
#[derive(Derivative)] | ||
#[derivative(Debug)] | ||
pub struct JsAdapterPlugin { | ||
pub name: String, | ||
#[derivative(Debug = "ignore")] | ||
resolve_id_fn: Option<ResolveIdCallback>, | ||
#[derivative(Debug = "ignore")] | ||
load_fn: Option<LoadCallback>, | ||
#[derivative(Debug = "ignore")] | ||
transform_fn: Option<TransformCallback>, | ||
} | ||
|
||
impl JsAdapterPlugin { | ||
pub fn new(option: PluginOptions) -> napi::Result<Self> { | ||
let resolve_id_fn = option | ||
.resolve_id | ||
.as_ref() | ||
.map(ResolveIdCallback::new) | ||
.transpose()?; | ||
let load_fn = option | ||
.resolve_id | ||
.as_ref() | ||
.map(LoadCallback::new) | ||
.transpose()?; | ||
let transform_fn = option | ||
.transform | ||
.as_ref() | ||
.map(TransformCallback::new) | ||
.transpose()?; | ||
Ok(JsAdapterPlugin { | ||
name: option.name, | ||
resolve_id_fn, | ||
load_fn, | ||
transform_fn, | ||
}) | ||
} | ||
|
||
pub fn new_boxed(option: PluginOptions) -> napi::Result<BoxPlugin> { | ||
Ok(Box::new(Self::new(option)?)) | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl Plugin for JsAdapterPlugin { | ||
fn name(&self) -> PluginName { | ||
std::borrow::Cow::Borrowed(&self.name) | ||
} | ||
|
||
async fn resolve_id( | ||
&self, | ||
_ctx: &mut rolldown_plugin::Context, | ||
args: &rolldown_plugin::ResolveIdArgs, | ||
) -> rolldown_plugin::ResolveIdReturn { | ||
if let Some(cb) = &self.resolve_id_fn { | ||
let res = cb | ||
.call_async(( | ||
args.source.to_string(), | ||
args.importer.map(|s| s.to_string()), | ||
)) | ||
.await | ||
.map_err(|e| e.into_bundle_error())?; | ||
|
||
Ok(res.map(Into::into)) | ||
} else { | ||
Ok(None) | ||
} | ||
} | ||
|
||
async fn load( | ||
&self, | ||
_ctx: &mut rolldown_plugin::Context, | ||
args: &rolldown_plugin::LoadArgs, | ||
) -> rolldown_plugin::LoadReturn { | ||
if let Some(cb) = &self.load_fn { | ||
let res = cb | ||
.call_async((args.id.to_string(), None)) | ||
.await | ||
.map_err(|e| e.into_bundle_error())?; | ||
Ok(res.map(Into::into)) | ||
} else { | ||
Ok(None) | ||
} | ||
} | ||
|
||
async fn transform( | ||
&self, | ||
_ctx: &mut rolldown_plugin::Context, | ||
args: &rolldown_plugin::TransformArgs, | ||
) -> rolldown_plugin::TransformReturn { | ||
if let Some(cb) = &self.transform_fn { | ||
let res = cb | ||
.call_async((args.code.to_string(), args.id.to_string())) | ||
.await | ||
.map_err(|e| e.into_bundle_error())?; | ||
Ok(res.map(Into::into)) | ||
} else { | ||
Ok(None) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
mod napi_error_ext; | ||
pub mod napi_error_ext; | ||
use std::sync::atomic::AtomicBool; | ||
|
||
use napi::Env; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
description = "rolldown_plugin" | ||
edition = "2021" | ||
license = "Apache-2.0" | ||
name = "rolldown_plugin" | ||
version = "0.0.1" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
anyhow = { workspace = true } | ||
async-trait = { workspace = true } | ||
rustc-hash = { workspace = true } | ||
rolldown_common = { version = "0.0.1", path = "../rolldown_common" } | ||
rolldown_error = { version = "0.0.1", path = "../rolldown_error" } |
Oops, something went wrong.