Skip to content

Commit

Permalink
move to builtinplugin
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk committed Jul 8, 2024
1 parent 1f6ec58 commit 6275ad1
Show file tree
Hide file tree
Showing 29 changed files with 265 additions and 413 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

12 changes: 2 additions & 10 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class JsStats {
}

export class Rspack {
constructor(options: RawOptions, builtinPlugins: Array<BuiltinPlugin>, parserPlugins: Array<JsParserPlugin>, registerJsTaps: RegisterJsTaps, outputFilesystem: ThreadsafeNodeFS)
constructor(options: RawOptions, builtinPlugins: Array<BuiltinPlugin>, registerJsTaps: RegisterJsTaps, outputFilesystem: ThreadsafeNodeFS)
setNonSkippableRegisters(kinds: Array<RegisterJsTapKind>): void
/** Build with the given option passed to the constructor */
build(callback: (err: null | Error) => void): void
Expand Down Expand Up @@ -111,6 +111,7 @@ export interface BuiltinPlugin {
}

export enum BuiltinPluginName {
DefinePlugin = 'DefinePlugin',
ProvidePlugin = 'ProvidePlugin',
BannerPlugin = 'BannerPlugin',
IgnorePlugin = 'IgnorePlugin',
Expand Down Expand Up @@ -435,15 +436,6 @@ export interface JsOriginRecord {
request: string
}

export interface JsParserPlugin {
name: JsParserPluginName
options: unknown
}

export enum JsParserPluginName {
DefinePlugin = 'DefinePlugin'
}

export interface JsPathData {
filename?: string
hash?: string
Expand Down
8 changes: 1 addition & 7 deletions crates/node_binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,16 @@ impl Rspack {
env: Env,
options: RawOptions,
builtin_plugins: Vec<BuiltinPlugin>,
parser_plugins: Vec<JsParserPlugin>,
register_js_taps: RegisterJsTaps,
output_filesystem: ThreadsafeNodeFS,
) -> Result<Self> {
tracing::info!("raw_options: {:#?}", &options);

let mut plugins = Vec::new();
let mut js_parser_plugins = Vec::new();
let js_plugin = JsHooksAdapterPlugin::from_js_hooks(env, register_js_taps)?;
plugins.push(js_plugin.clone().boxed());
for pp in parser_plugins {
pp.append_to(&mut js_parser_plugins, &mut plugins)
.map_err(|e| Error::from_reason(format!("{e}")))?;
}
for bp in builtin_plugins {
bp.append_to(env, &mut js_parser_plugins, &mut plugins)
bp.append_to(env, &mut plugins)
.map_err(|e| Error::from_reason(format!("{e}")))?;
}

Expand Down
53 changes: 10 additions & 43 deletions crates/rspack_binding_options/src/options/raw_builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ mod raw_size_limits;
mod raw_swc_js_minimizer;
mod raw_to_be_deprecated;

use std::sync::Arc;

use napi::{bindgen_prelude::FromNapiValue, Env, JsUnknown};
use napi_derive::napi;
use raw_lightning_css_minimizer::RawLightningCssMinimizerRspackPluginOptions;
Expand Down Expand Up @@ -45,9 +43,9 @@ use rspack_plugin_hmr::HotModuleReplacementPlugin;
use rspack_plugin_html::HtmlRspackPlugin;
use rspack_plugin_ignore::IgnorePlugin;
use rspack_plugin_javascript::{
api_plugin::APIPlugin, define_plugin::DefinePlugin, BoxJavascriptParserPlugin,
FlagDependencyExportsPlugin, FlagDependencyUsagePlugin, InferAsyncModulesPlugin, JsPlugin,
MangleExportsPlugin, ModuleConcatenationPlugin, SideEffectsFlagPlugin,
api_plugin::APIPlugin, define_plugin::DefinePlugin, FlagDependencyExportsPlugin,
FlagDependencyUsagePlugin, InferAsyncModulesPlugin, JsPlugin, MangleExportsPlugin,
ModuleConcatenationPlugin, SideEffectsFlagPlugin,
};
use rspack_plugin_json::JsonPlugin;
use rspack_plugin_library::enable_library_plugin;
Expand Down Expand Up @@ -103,6 +101,7 @@ use crate::{
#[derive(Debug)]
pub enum BuiltinPluginName {
// webpack also have these plugins
DefinePlugin,
ProvidePlugin,
BannerPlugin,
IgnorePlugin,
Expand Down Expand Up @@ -188,14 +187,13 @@ pub struct BuiltinPlugin {
}

impl BuiltinPlugin {
pub fn append_to(
self,
env: Env,
parser_plugins: &mut Vec<BoxJavascriptParserPlugin>,
plugins: &mut Vec<BoxPlugin>,
) -> rspack_error::Result<()> {
pub fn append_to(self, env: Env, plugins: &mut Vec<BoxPlugin>) -> rspack_error::Result<()> {
match self.name {
// webpack also have these plugins
BuiltinPluginName::DefinePlugin => {
let plugin = DefinePlugin::new(downcast_into(self.options)?).boxed();
plugins.push(plugin);
}
BuiltinPluginName::ProvidePlugin => {
let plugin = ProvidePlugin::new(downcast_into::<Provide>(self.options)?).boxed();
plugins.push(plugin);
Expand Down Expand Up @@ -371,9 +369,7 @@ impl BuiltinPlugin {
BuiltinPluginName::InferAsyncModulesPlugin => {
plugins.push(InferAsyncModulesPlugin::default().boxed())
}
BuiltinPluginName::JavascriptModulesPlugin => {
plugins.push(JsPlugin::new(std::mem::take(parser_plugins)).boxed())
}
BuiltinPluginName::JavascriptModulesPlugin => plugins.push(JsPlugin::default().boxed()),
BuiltinPluginName::AsyncWebAssemblyModulesPlugin => {
plugins.push(AsyncWasmPlugin::default().boxed())
}
Expand Down Expand Up @@ -516,35 +512,6 @@ impl BuiltinPlugin {
}
}

#[napi(string_enum)]
#[derive(Debug)]
pub enum JsParserPluginName {
DefinePlugin,
}

#[napi(object)]
pub struct JsParserPlugin {
pub name: JsParserPluginName,
pub options: JsUnknown,
}

impl JsParserPlugin {
pub fn append_to(
self,
plugins: &mut Vec<BoxJavascriptParserPlugin>,
builtin_plugins: &mut Vec<Box<dyn Plugin>>,
) -> rspack_error::Result<()> {
match self.name {
JsParserPluginName::DefinePlugin => {
let plugin = DefinePlugin::new(downcast_into(self.options)?);
plugins.push(Arc::new(plugin.clone()));
builtin_plugins.push(Box::new(plugin));
}
};
Ok(())
}
}

fn downcast_into<T: FromNapiValue + 'static>(o: JsUnknown) -> Result<T> {
rspack_napi::downcast_into(o).into_rspack_result()
}
Expand Down
4 changes: 3 additions & 1 deletion crates/rspack_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ pub use resolver::*;
pub mod concatenated_module;
pub mod reserved_names;

pub use rspack_loader_runner::{get_scheme, ResourceData, Scheme, BUILTIN_LOADER_PREFIX};
pub use rspack_loader_runner::{
get_scheme, AdditionalData, ResourceData, Scheme, BUILTIN_LOADER_PREFIX,
};
pub use rspack_macros::{impl_runtime_module, impl_source_map_config};
pub use rspack_sources;

Expand Down
15 changes: 11 additions & 4 deletions crates/rspack_core/src/normal_module_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ use crate::{
stringify_loaders_and_resource, BoxLoader, BoxModule, CompilerOptions, Context,
DependencyCategory, FuncUseCtx, GeneratorOptions, ModuleExt, ModuleFactory,
ModuleFactoryCreateData, ModuleFactoryResult, ModuleIdentifier, ModuleRule, ModuleRuleEnforce,
ModuleRuleUse, ModuleRuleUseLoader, ModuleType, NormalModule, ParserOptions, RawModule, Resolve,
ResolveArgs, ResolveOptionsWithDependencyType, ResolveResult, Resolver, ResolverFactory,
ResourceData, ResourceParsedData, RunnerContext, SharedPluginDriver,
ModuleRuleUse, ModuleRuleUseLoader, ModuleType, NormalModule, ParserAndGenerator, ParserOptions,
RawModule, Resolve, ResolveArgs, ResolveOptionsWithDependencyType, ResolveResult, Resolver,
ResolverFactory, ResourceData, ResourceParsedData, RunnerContext, SharedPluginDriver,
};

define_hook!(NormalModuleFactoryBeforeResolve: AsyncSeriesBail(data: &mut ModuleFactoryCreateData) -> bool);
Expand All @@ -26,6 +26,7 @@ define_hook!(NormalModuleFactoryResolveForScheme: AsyncSeriesBail(data: &mut Mod
define_hook!(NormalModuleFactoryAfterResolve: AsyncSeriesBail(data: &mut ModuleFactoryCreateData, create_data: &mut NormalModuleCreateData) -> bool);
define_hook!(NormalModuleFactoryCreateModule: AsyncSeriesBail(data: &mut ModuleFactoryCreateData, create_data: &mut NormalModuleCreateData) -> BoxModule);
define_hook!(NormalModuleFactoryModule: AsyncSeries(data: &mut ModuleFactoryCreateData, create_data: &mut NormalModuleCreateData, module: &mut BoxModule));
define_hook!(NormalModuleFactoryParser: SyncSeries(module_type: &ModuleType, parser: &mut dyn ParserAndGenerator, parser_options: Option<&ParserOptions>));
define_hook!(NormalModuleFactoryResolveLoader: AsyncSeriesBail(context: &Context, resolver: &Resolver, l: &ModuleRuleUseLoader) -> BoxLoader);

pub enum NormalModuleFactoryResolveResult {
Expand All @@ -42,6 +43,7 @@ pub struct NormalModuleFactoryHooks {
pub after_resolve: NormalModuleFactoryAfterResolveHook,
pub create_module: NormalModuleFactoryCreateModuleHook,
pub module: NormalModuleFactoryModuleHook,
pub parser: NormalModuleFactoryParserHook,
/// Webpack resolves loaders in `NormalModuleFactory`,
/// Rspack resolves it when normalizing configuration.
/// So this hook is used to resolve inline loader (inline loader requests).
Expand Down Expand Up @@ -500,7 +502,7 @@ impl NormalModuleFactory {
resolved_generator_options,
);
let resolved_side_effects = self.calculate_side_effects(&resolved_module_rules);
let resolved_parser_and_generator = self
let mut resolved_parser_and_generator = self
.plugin_driver
.registered_parser_and_generator_builder
.get(&resolved_module_type)
Expand All @@ -513,6 +515,11 @@ impl NormalModuleFactory {
resolved_parser_options.as_ref(),
resolved_generator_options.as_ref(),
);
self.plugin_driver.normal_module_factory_hooks.parser.call(
&resolved_module_type,
resolved_parser_and_generator.as_mut(),
resolved_parser_options.as_ref(),
)?;

let mut create_data = {
let mut create_data = NormalModuleCreateData {
Expand Down
18 changes: 17 additions & 1 deletion crates/rspack_core/src/parser_and_generator.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::any::Any;
use std::fmt::Debug;

use derivative::Derivative;
use rspack_error::{Result, TWithDiagnosticArray};
use rspack_loader_runner::{AdditionalData, ResourceData};
use rspack_sources::BoxSource;
use rspack_util::ext::AsAny;
use rspack_util::source_map::SourceMapKind;
use swc_core::common::Span;

Expand Down Expand Up @@ -80,7 +82,7 @@ pub struct GenerateContext<'a> {
pub concatenation_scope: Option<&'a mut ConcatenationScope>,
}

pub trait ParserAndGenerator: Send + Sync + Debug {
pub trait ParserAndGenerator: Send + Sync + Debug + AsAny {
/// The source types that the generator can generate (the source types you can make requests for)
fn source_types(&self) -> &[SourceType];
/// Parse the source and return the dependencies and the ast or source
Expand All @@ -102,3 +104,17 @@ pub trait ParserAndGenerator: Send + Sync + Debug {
_cg: &ChunkGraph,
) -> Option<String>;
}

impl dyn ParserAndGenerator + '_ {
pub fn downcast_ref<D: Any>(&self) -> Option<&D> {
self.as_any().downcast_ref::<D>()
}

pub fn downcast_mut<D: Any>(&mut self) -> Option<&mut D> {
self.as_any_mut().downcast_mut::<D>()
}

pub fn is<D: Any>(&self) -> bool {
self.downcast_ref::<D>().is_some()
}
}
29 changes: 15 additions & 14 deletions crates/rspack_plugin_extract_css/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ repository = "https://github.com/web-infra-dev/rspack"
version = "0.1.0"

[dependencies]
async-trait = { workspace = true }
once_cell = { workspace = true }
regex = { workspace = true }
rspack_core = { path = "../rspack_core" }
rspack_error = { path = "../rspack_error" }
rspack_hash = { path = "../rspack_hash" }
rspack_hook = { path = "../rspack_hook" }
rspack_identifier = { path = "../rspack_identifier" }
rspack_plugin_css = { path = "../rspack_plugin_css" }
rspack_plugin_runtime = { path = "../rspack_plugin_runtime" }
rspack_util = { path = "../rspack_util" }
rustc-hash = { workspace = true }
serde_json = { workspace = true }
ustr = { workspace = true }
async-trait = { workspace = true }
once_cell = { workspace = true }
regex = { workspace = true }
rspack_core = { path = "../rspack_core" }
rspack_error = { path = "../rspack_error" }
rspack_hash = { path = "../rspack_hash" }
rspack_hook = { path = "../rspack_hook" }
rspack_identifier = { path = "../rspack_identifier" }
rspack_plugin_css = { path = "../rspack_plugin_css" }
rspack_plugin_javascript = { path = "../rspack_plugin_javascript" }
rspack_plugin_runtime = { path = "../rspack_plugin_runtime" }
rspack_util = { path = "../rspack_util" }
rustc-hash = { workspace = true }
serde_json = { workspace = true }
ustr = { workspace = true }
4 changes: 2 additions & 2 deletions crates/rspack_plugin_extract_css/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![feature(let_chains)]
pub mod css_dependency;
mod css_module;
mod parser_and_generator;
pub use parser_and_generator::{CssExtractJsonData, CssExtractJsonDataList};
mod parser_plugin;
pub use parser_plugin::{CssExtractJsonData, CssExtractJsonDataList};
pub mod plugin;
mod runtime;
Loading

0 comments on commit 6275ad1

Please sign in to comment.