Skip to content

Commit

Permalink
feat(compilation): add pluginImport and executeModule
Browse files Browse the repository at this point in the history
  • Loading branch information
JSerFeng committed Nov 20, 2023
1 parent d02f4ef commit 4c8c5b1
Show file tree
Hide file tree
Showing 9 changed files with 496 additions and 116 deletions.
481 changes: 367 additions & 114 deletions crates/rspack_core/src/compiler/compilation.rs

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions crates/rspack_core/src/compiler/queue.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::Arc;

use rspack_error::{Diagnostic, Result};
use tokio::sync::mpsc::UnboundedSender;

use crate::{
cache::Cache, BoxDependency, BuildContext, BuildResult, Compilation, CompilerContext,
Expand All @@ -17,13 +18,15 @@ pub enum TaskResult {
Add(Box<AddTaskResult>),
Build(Box<BuildTaskResult>),
ProcessDependencies(Box<ProcessDependenciesResult>),
ImportModule(Box<ImportModuleResult>),
}

#[async_trait::async_trait]
pub trait WorkerTask {
async fn run(self) -> Result<TaskResult>;
}

#[derive(Debug)]
pub struct FactorizeTask {
pub original_module_identifier: Option<ModuleIdentifier>,
pub original_module_context: Option<Box<Context>>,
Expand Down Expand Up @@ -61,6 +64,7 @@ pub struct FactorizeTaskResult {
pub current_profile: Option<Box<ModuleProfile>>,
pub exports_info_related: ExportsInfoRelated,
pub from_cache: bool,
pub original_dependency: Option<DependencyId>,
}

#[async_trait::async_trait]
Expand All @@ -69,7 +73,9 @@ impl WorkerTask for FactorizeTask {
if let Some(current_profile) = &self.current_profile {
current_profile.mark_factory_start();
}

let dependency = self.dependency;
let dependency_id = *dependency.id();

let context = if let Some(context) = dependency.get_context() {
context
Expand Down Expand Up @@ -153,29 +159,34 @@ impl WorkerTask for FactorizeTask {
other_exports_info,
side_effects_info: side_effects_only_info,
},
original_dependency: Some(dependency_id),
})))
}
}

pub type FactorizeQueue = WorkerQueue<FactorizeTask>;

#[derive(Debug)]
pub struct AddTask {
pub original_module_identifier: Option<ModuleIdentifier>,
pub module: Box<dyn Module>,
pub module_graph_module: Box<ModuleGraphModule>,
pub dependencies: Vec<DependencyId>,
pub is_entry: bool,
pub current_profile: Option<Box<ModuleProfile>>,
pub original_dependency: Option<DependencyId>,
}

#[derive(Debug)]
pub enum AddTaskResult {
ModuleReused {
module: Box<dyn Module>,
original_dependency: Option<DependencyId>,
},
ModuleAdded {
module: Box<dyn Module>,
current_profile: Option<Box<ModuleProfile>>,
original_dependency: Option<DependencyId>,
},
}

Expand All @@ -200,6 +211,7 @@ impl AddTask {

return Ok(TaskResult::Add(Box::new(AddTaskResult::ModuleReused {
module: self.module,
original_dependency: self.original_dependency,
})));
}

Expand Down Expand Up @@ -227,6 +239,7 @@ impl AddTask {
Ok(TaskResult::Add(Box::new(AddTaskResult::ModuleAdded {
module: self.module,
current_profile: self.current_profile,
original_dependency: self.original_dependency,
})))
}
}
Expand All @@ -245,13 +258,16 @@ fn set_resolved_module(

pub type AddQueue = WorkerQueue<AddTask>;

#[derive(Debug)]
pub struct BuildTask {
pub module: Box<dyn Module>,
pub resolver_factory: Arc<ResolverFactory>,
pub compiler_options: Arc<CompilerOptions>,
pub plugin_driver: SharedPluginDriver,
pub cache: Arc<Cache>,
pub current_profile: Option<Box<ModuleProfile>>,
pub original_dependency: Option<DependencyId>,
pub task_sender: UnboundedSender<rspack_error::Result<TaskResult>>,
}

#[derive(Debug)]
Expand All @@ -261,6 +277,7 @@ pub struct BuildTaskResult {
pub diagnostics: Vec<Diagnostic>,
pub current_profile: Option<Box<ModuleProfile>>,
pub from_cache: bool,
pub original_dependency: Option<DependencyId>,
}

#[async_trait::async_trait]
Expand Down Expand Up @@ -291,6 +308,7 @@ impl WorkerTask for BuildTask {
resolver_factory: resolver_factory.clone(),
module: Some(module.identifier()),
module_context: module.as_normal_module().and_then(|m| m.get_context()),
task_sender: Some(self.task_sender.clone()),
},
plugin_driver: plugin_driver.clone(),
compiler_options: &compiler_options,
Expand Down Expand Up @@ -327,26 +345,31 @@ impl WorkerTask for BuildTask {
diagnostics,
current_profile: self.current_profile,
from_cache: is_cache_valid,
original_dependency: self.original_dependency,
}))
})
}
}

pub type BuildQueue = WorkerQueue<BuildTask>;

#[derive(Debug)]
pub struct ProcessDependenciesTask {
pub original_module_identifier: ModuleIdentifier,
pub dependencies: Vec<DependencyId>,
pub resolve_options: Option<Box<Resolve>>,
pub original_dependency: Option<DependencyId>,
}

#[derive(Debug)]
pub struct ProcessDependenciesResult {
pub module_identifier: ModuleIdentifier,
pub original_dependency: Option<DependencyId>,
}

pub type ProcessDependenciesQueue = WorkerQueue<ProcessDependenciesTask>;

#[derive(Debug)]
pub struct CleanTask {
pub module_identifier: ModuleIdentifier,
}
Expand Down Expand Up @@ -396,3 +419,17 @@ impl CleanTask {
}

pub type CleanQueue = WorkerQueue<CleanTask>;

#[derive(Debug)]
pub struct ImportModuleResult {
pub request: String,
pub sender: UnboundedSender<Result<String>>,
pub original_module: Option<ModuleIdentifier>,
pub original_module_context: Option<Box<Context>>,
pub options: ImportModuleOption,
}

#[derive(Debug)]
pub struct ImportModuleOption {
pub public_path: String,
}
45 changes: 45 additions & 0 deletions crates/rspack_core/src/dependency/loader_import_dependency.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use crate::{
AsContextDependency, AsDependencyTemplate, Dependency, DependencyId, ModuleDependency,
};

#[derive(Debug, Hash, PartialEq, Eq, Clone)]
pub struct LoaderImportDependency {
request: String,
id: DependencyId,
}

impl LoaderImportDependency {
pub fn new(request: String) -> Self {
Self {
request,
id: DependencyId::new(),
}
}
}

impl AsDependencyTemplate for LoaderImportDependency {}
impl AsContextDependency for LoaderImportDependency {}

impl Dependency for LoaderImportDependency {
fn dependency_debug_name(&self) -> &'static str {
"LoaderImportDependency"
}

fn id(&self) -> &crate::DependencyId {
&self.id
}
}

impl ModuleDependency for LoaderImportDependency {
fn request(&self) -> &str {
&self.request
}

fn user_request(&self) -> &str {
&self.request
}

fn set_request(&mut self, request: String) {
self.request = request
}
}
2 changes: 2 additions & 0 deletions crates/rspack_core/src/dependency/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod dependency_trait;
mod dependency_type;
mod entry;
mod import_dependency_trait;
mod loader_import_dependency;
mod module_dependency;
mod runtime_requirements_dependency;
mod runtime_template;
Expand All @@ -24,6 +25,7 @@ pub use dependency_trait::*;
pub use dependency_type::DependencyType;
pub use entry::*;
pub use import_dependency_trait::ImportDependencyTrait;
pub use loader_import_dependency::LoaderImportDependency;
pub use module_dependency::*;
pub use runtime_requirements_dependency::RuntimeRequirementsDependency;
pub use runtime_template::*;
Expand Down
42 changes: 41 additions & 1 deletion crates/rspack_core/src/loader/loader_runner.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,55 @@
use std::sync::Arc;

use rspack_error::{Error, Result};
pub use rspack_loader_runner::{run_loaders, Content, Loader, LoaderContext};
use tokio::sync::mpsc::unbounded_channel;

use crate::{CompilerOptions, Context, ModuleIdentifier, ResolverFactory};
use crate::{CompilerOptions, Context, ModuleIdentifier, ResolverFactory, TaskResult, TaskSender};

#[derive(Debug, Clone)]
pub struct CompilerContext {
pub options: Arc<CompilerOptions>,
pub resolver_factory: Arc<ResolverFactory>,
pub module: Option<ModuleIdentifier>, // current module
pub module_context: Option<Box<Context>>, // current module context
pub task_sender: Option<TaskSender>,
}

impl CompilerContext {
pub async fn import_module(
&self,
request: String,
public_path: String,
original_module: Option<ModuleIdentifier>,
original_module_context: Option<Box<Context>>,
) -> Result<String> {
let (tx, mut rx) = unbounded_channel();
self

Check failure on line 27 in crates/rspack_core/src/loader/loader_runner.rs

View workflow job for this annotation

GitHub Actions / Rust check

used `unwrap()` on a `Result` value

Check failure on line 27 in crates/rspack_core/src/loader/loader_runner.rs

View workflow job for this annotation

GitHub Actions / Rust check

used `unwrap()` on an `Option` value
.task_sender
.as_ref()
.unwrap()
.send(Ok(TaskResult::ImportModule(Box::new(
crate::ImportModuleResult {
request,
sender: tx,
original_module,
original_module_context,
options: crate::ImportModuleOption { public_path },
},
))))
.unwrap();

let res = rx.recv().await;

match res {
Some(Ok(res)) => Ok(res),
Some(Err(e)) => Err(e),
None => Err(Error::InternalError(rspack_error::InternalError::new(
"Failed to call importModule".into(),
rspack_error::Severity::Error,
))),
}
}
}

pub type LoaderRunnerContext = CompilerContext;
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_core/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ impl StartTimeAggregate {
}
}

#[derive(Debug)]
pub struct CacheCount {
label: &'static str,
hit: u32,
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/utils/queue.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::VecDeque;

#[derive(Default)]
#[derive(Default, Debug)]
pub struct WorkerQueue<T> {
inner: VecDeque<T>,
}
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_loader_sass/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ async fn loader_test(actual: impl AsRef<Path>, expected: impl AsRef<Path>) {
resolver_factory: Default::default(),
module: None,
module_context: None,
task_sender: None,
},
)
.await
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_loader_swc/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ async fn loader_test(actual: impl AsRef<Path>, expected: impl AsRef<Path>) {
resolver_factory: Default::default(),
module: None,
module_context: None,
task_sender: None,
},
)
.await
Expand Down

0 comments on commit 4c8c5b1

Please sign in to comment.