Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci-rust.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ jobs:
--exclude rspack_node \
--exclude rspack_binding_builder \
--exclude rspack_binding_builder_macros \
--exclude rspack_binding_builder_testing \
--exclude rspack_binding_build \
--exclude rspack_napi \
-- --nocapture
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reusable-build-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ jobs:
if: ${{ inputs.target == 'wasm32-wasip1-threads' }}
run: |
unset CC_x86_64_unknown_linux_gnu && unset CC # for jemallocator to compile
DISABLE_PLUGIN=1 RUST_TARGET=wasm32-wasip1-threads pnpm --filter @rspack/binding build:${{ inputs.profile }}
DISABLE_PLUGIN=1 RUST_TARGET=wasm32-wasip1-threads pnpm build:binding:${{ inputs.profile }}

- name: Diff artifact
run: |
Expand Down
14 changes: 14 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 @@ -120,6 +120,7 @@ rspack_base64 = { version = "0.2.0", path = "crates/rsp
rspack_binding_api = { version = "0.2.0", path = "crates/rspack_binding_api" }
rspack_binding_build = { version = "0.2.0", path = "crates/rspack_binding_build" }
rspack_binding_builder = { version = "0.2.0", path = "crates/rspack_binding_builder" }
rspack_binding_builder_macros = { version = "0.2.0", path = "crates/rspack_binding_builder_macros" }
rspack_browserslist = { version = "0.2.0", path = "crates/rspack_browserslist" }
rspack_builtin = { version = "0.2.0", path = "crates/rspack_builtin" }
rspack_cacheable = { version = "0.2.0", path = "crates/rspack_cacheable" }
Expand Down
4 changes: 3 additions & 1 deletion crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export type RawLazyCompilationTest = RegExp | ((module: Module) => boolean);

export type AssetInfo = KnownAssetInfo & Record<string, any>;

export type CustomPluginName = string;

export const MODULE_IDENTIFIER_SYMBOL: unique symbol;

export const COMPILATION_HOOKS_MAP_SYMBOL: unique symbol;
Expand Down Expand Up @@ -439,7 +441,7 @@ export declare class Sources {
}

export interface BuiltinPlugin {
name: BuiltinPluginName
name: BuiltinPluginName | CustomPluginName
options: unknown
canInherentFromParent?: boolean
}
Expand Down
2 changes: 2 additions & 0 deletions crates/node_binding/scripts/banner.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export type RawLazyCompilationTest = RegExp | ((module: Module) => boolean);

export type AssetInfo = KnownAssetInfo & Record<string, any>;

export type CustomPluginName = string;

export const MODULE_IDENTIFIER_SYMBOL: unique symbol;

export const COMPILATION_HOOKS_MAP_SYMBOL: unique symbol;
Expand Down
38 changes: 22 additions & 16 deletions crates/rspack_binding_api/src/raw_options/raw_builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::cell::RefCell;

use napi::{
bindgen_prelude::{FromNapiValue, JsObjectValue, Object},
Env, Unknown,
Either, Env, Unknown,
};
use napi_derive::napi;
use raw_dll::{RawDllReferenceAgencyPluginOptions, RawFlagAllModulesAsUsedPluginOptions};
Expand Down Expand Up @@ -223,23 +223,22 @@ pub enum BuiltinPluginName {
ModuleInfoHeaderPlugin,
HttpUriPlugin,
CssChunkingPlugin,
// Custom(String),
}

pub type CustomPluginBuilder =
for<'a> fn(env: Env, options: Unknown<'a>) -> napi::Result<BoxPlugin>;

thread_local! {
static CUSTOMED_PLUGINS_CTOR: RefCell<HashMap<String, CustomPluginBuilder>> = RefCell::new(HashMap::default());
static CUSTOMED_PLUGINS_CTOR: RefCell<HashMap<CustomPluginName, CustomPluginBuilder>> = RefCell::new(HashMap::default());
}

#[doc(hidden)]
#[allow(clippy::result_unit_err)]
pub fn register_custom_plugin(
name: &'static str,
name: String,
plugin_builder: CustomPluginBuilder,
) -> std::result::Result<(), ()> {
CUSTOMED_PLUGINS_CTOR.with_borrow_mut(|ctors| match ctors.entry(name.to_string()) {
CUSTOMED_PLUGINS_CTOR.with_borrow_mut(|ctors| match ctors.entry(name) {
std::collections::hash_map::Entry::Occupied(_) => Err(()),
std::collections::hash_map::Entry::Vacant(vacant_entry) => {
vacant_entry.insert(plugin_builder);
Expand All @@ -248,9 +247,11 @@ pub fn register_custom_plugin(
})
}

type CustomPluginName = String;

#[napi(object)]
pub struct BuiltinPlugin<'a> {
pub name: BuiltinPluginName,
pub name: Either<BuiltinPluginName, CustomPluginName>,
pub options: Unknown<'a>,
pub can_inherent_from_parent: Option<bool>,
}
Expand All @@ -262,7 +263,20 @@ impl<'a> BuiltinPlugin<'a> {
compiler_object: &mut Object,
plugins: &mut Vec<BoxPlugin>,
) -> napi::Result<()> {
match self.name {
let name = match self.name {
Either::A(name) => name,
Either::B(name) => {
CUSTOMED_PLUGINS_CTOR.with_borrow(|ctors| {
let ctor = ctors.get(&name).ok_or_else(|| {
napi::Error::from_reason(format!("Expected plugin installed '{name}'"))
})?;
plugins.push(ctor(env, self.options)?);
Ok::<_, napi::Error>(())
})?;
return Ok(());
}
};
match name {
// webpack also have these plugins
BuiltinPluginName::DefinePlugin => {
let plugin = DefinePlugin::new(
Expand Down Expand Up @@ -762,15 +776,7 @@ impl<'a> BuiltinPlugin<'a> {
let options = downcast_into::<CssChunkingPluginOptions>(self.options)
.map_err(|report| napi::Error::from_reason(report.to_string()))?;
plugins.push(CssChunkingPlugin::new(options.into()).boxed());
} // BuiltinPluginName::Custom(ref name) => {
// CUSTOMED_PLUGINS_CTOR.with_borrow(|ctors| {
// let ctor = ctors.get(name).ok_or_else(|| {
// napi::Error::from_reason(format!("Expected plugin installed '{name}'"))
// })?;
// plugins.push(ctor(env, self.options)?);
// Ok::<_, napi::Error>(())
// })?;
// }
}
}
Ok(())
}
Expand Down
49 changes: 5 additions & 44 deletions crates/rspack_binding_builder_macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,10 @@
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::quote;
use syn::{
parse::{Parse, ParseStream},
parse_macro_input, Expr, Ident, LitStr, Token,
};

struct RegisterPluginInput {
name: LitStr,
_comma: Token![,],
plugin: Expr,
}
mod register_plugin;

impl Parse for RegisterPluginInput {
fn parse(input: ParseStream) -> syn::Result<Self> {
Ok(RegisterPluginInput {
name: input.parse()?,
_comma: input.parse()?,
plugin: input.parse()?,
})
}
}
use proc_macro::TokenStream;
use syn::parse_macro_input;

#[proc_macro]
pub fn register_plugin(input: TokenStream) -> TokenStream {
let RegisterPluginInput { name, plugin, .. } = parse_macro_input!(input as RegisterPluginInput);

let plugin_register_ident: Ident =
Ident::new(&format!("register_{}", name.value()), Span::call_site());

let expanded = quote! {
#[napi]
fn #plugin_register_ident() {
fn register<'a>(
env: Env,
options: Unknown<'a>,
) -> Result<rspack_core::BoxPlugin> {
(#plugin)(env, options)
}
match rspack_binding_builder::register_custom_plugin(#name, register as rspack_binding_builder::CustomPluginBuilder) {
Ok(_) => {}
Err(err) => panic!("Cannot register plugins under the same name"),
}
}
};

TokenStream::from(expanded)
let input = parse_macro_input!(input as register_plugin::RegisterPluginInput);
input.expand().into()
}
47 changes: 47 additions & 0 deletions crates/rspack_binding_builder_macros/src/register_plugin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use proc_macro2::{Ident, Span, TokenStream};
use quote::quote;
use syn::{
parse::{Parse, ParseStream},
Expr, LitStr, Token,
};

pub struct RegisterPluginInput {
name: LitStr,
plugin: Expr,
}

impl Parse for RegisterPluginInput {
fn parse(input: ParseStream) -> syn::Result<Self> {
let name = input.parse()?;
<Token![,]>::parse(input)?;
let plugin = input.parse()?;
Ok(RegisterPluginInput { name, plugin })
}
}

impl RegisterPluginInput {
pub fn expand(self) -> TokenStream {
let RegisterPluginInput { name, plugin } = self;

let plugin_register_ident: Ident =
Ident::new(&format!("register_{}", name.value()), Span::call_site());

let expanded = quote! {
#[napi]
fn #plugin_register_ident() -> napi::Result<()> {
fn register<'a>(
env: Env,
options: Unknown<'a>,
) -> Result<rspack_core::BoxPlugin> {
(#plugin)(env, options)
}
let name = #name.to_string();
rspack_binding_builder::register_custom_plugin(name, register as rspack_binding_builder::CustomPluginBuilder).map_err(|e| {
napi::Error::from_reason(format!("Cannot register plugins under the same name: {}", #name))
})
}
};

expanded
}
}
3 changes: 3 additions & 0 deletions crates/rspack_binding_builder_testing/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*.node
/*.wasm
/*.d.ts
32 changes: 32 additions & 0 deletions crates/rspack_binding_builder_testing/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[package]
description = "rspack node builder testing"
edition.workspace = true
license = "MIT"
name = "rspack_binding_builder_testing"
repository = "https://github.com/web-infra-dev/rspack"
version = "0.2.0"

[lib]
crate-type = ["cdylib"]


[package.metadata.cargo-shear]
# `napi-derive` uses absolute path to `napi`
ignored = ["napi"]

[features]
plugin = ["rspack_binding_builder/plugin"]

[dependencies]
rspack_binding_builder = { workspace = true }
rspack_binding_builder_macros = { workspace = true }

rspack_core = { workspace = true }
rspack_error = { workspace = true }
rspack_napi = { workspace = true }

napi = { workspace = true, features = ["async", "tokio_rt", "serde-json", "anyhow", "napi7", "compat-mode"] }
napi-derive = { workspace = true, features = ["compat-mode"] }

[build-dependencies]
rspack_binding_build = { workspace = true }
Loading
Loading