Skip to content

Commit

Permalink
fix: import-module should use separate plugin driver
Browse files Browse the repository at this point in the history
  • Loading branch information
JSerFeng committed Sep 24, 2024
1 parent 7cddc17 commit 1859fa8
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 9 deletions.
1 change: 1 addition & 0 deletions crates/node_binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ impl Rspack {
let rspack = rspack_core::Compiler::new(
compiler_options,
plugins,
rspack_binding_options::buildtime_plugins::buildtime_plugins(),
Some(Box::new(
AsyncNodeWritableFileSystem::new(output_filesystem)
.map_err(|e| Error::from_reason(format!("Failed to create writable filesystem: {e}",)))?,
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_binding_options/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
mod options;
mod plugins;
pub use options::*;
pub use plugins::buildtime_plugins;
pub(crate) use plugins::*;
15 changes: 15 additions & 0 deletions crates/rspack_binding_options/src/plugins/buildtime_plugins.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use rspack_core::{BoxPlugin, ChunkLoading, ChunkLoadingType, PluginExt};
use rspack_plugin_javascript::JsPlugin;
use rspack_plugin_runtime::{
CommonJsChunkLoadingPlugin, RuntimePlugin, StartupChunkDependenciesPlugin,
};

pub fn buildtime_plugins() -> Vec<BoxPlugin> {
vec![
RuntimePlugin::default().boxed(),
JsPlugin::default().boxed(),
// StartupChunkDependenciesPlugin::new(ChunkLoading::Enable(ChunkLoadingType::Require), false)
// .boxed(),
// CommonJsChunkLoadingPlugin::new(false).boxed(),
]
}
1 change: 1 addition & 0 deletions crates/rspack_binding_options/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ mod js_loader;

pub use context_replacement::*;
pub(super) use js_loader::{JsLoaderRspackPlugin, JsLoaderRunner};
pub mod buildtime_plugins;
6 changes: 6 additions & 0 deletions crates/rspack_core/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub struct Compiler {
pub input_filesystem: Arc<dyn ReadableFileSystem>,
pub compilation: Compilation,
pub plugin_driver: SharedPluginDriver,
pub buildtime_plugin_driver: SharedPluginDriver,
pub resolver_factory: Arc<ResolverFactory>,
pub loader_resolver_factory: Arc<ResolverFactory>,
pub old_cache: Arc<OldCache>,
Expand All @@ -75,6 +76,7 @@ impl Compiler {
pub fn new(
options: CompilerOptions,
plugins: Vec<BoxPlugin>,
buildtime_plugins: Vec<BoxPlugin>,
output_filesystem: Option<Box<dyn AsyncWritableFileSystem + Send + Sync>>,
// only supports passing input_filesystem in rust api, no support for js api
input_filesystem: Option<Arc<dyn ReadableFileSystem + Send + Sync>>,
Expand Down Expand Up @@ -102,8 +104,11 @@ impl Compiler {
input_filesystem.clone(),
))
});

let options = Arc::new(options);
let plugin_driver = PluginDriver::new(options.clone(), plugins, resolver_factory.clone());
let buildtime_plugin_driver =
PluginDriver::new(options.clone(), buildtime_plugins, resolver_factory.clone());
let old_cache = Arc::new(OldCache::new(options.clone()));
let unaffected_modules_cache = Arc::new(UnaffectedModulesCache::default());
let module_executor = ModuleExecutor::default();
Expand All @@ -126,6 +131,7 @@ impl Compiler {
),
output_filesystem,
plugin_driver,
buildtime_plugin_driver,
resolver_factory,
loader_resolver_factory,
old_cache,
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_core/src/compiler/module_executor/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ impl Task<MakeTaskContext> for EntryTask {
)
})
.clone(),
original_module_identifier: None,
original_module_identifier: dep.original_module,
original_module_source: None,
issuer: None,
issuer_layer: None,
original_module_context: None,
original_module_context: Some(Box::new(dep.context.clone())),
dependencies: vec![dep],
resolve_options: None,
options: context.compiler_options.clone(),
Expand Down
9 changes: 5 additions & 4 deletions crates/rspack_core/src/compiler/module_executor/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ impl Task<MakeTaskContext> for ExecuteTask {
let id = EXECUTE_MODULE_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed);

let mg = compilation.get_module_graph_mut();
let entry_module_identifier = mg
.get_module_by_dependency_id(&entry_dep_id)
.expect("should have module")
.identifier();
let Some(entry_module_identifier) = mg.get_module_by_dependency_id(&entry_dep_id) else {
return Err(rspack_error::error!("entry module not found"));
};

let entry_module_identifier = entry_module_identifier.identifier();
let mut queue = vec![entry_module_identifier];
let mut modules = IdentifierSet::default();

Expand Down
1 change: 1 addition & 0 deletions crates/rspack_core/src/compiler/module_executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ impl ModuleExecutor {
let dep = LoaderImportDependency::new(
request.clone(),
original_module_context.unwrap_or(Context::from("")),
original_module_identifier,
);
let dep_id = *dep.id();
v.insert(dep_id);
Expand Down
8 changes: 5 additions & 3 deletions crates/rspack_core/src/dependency/loader_import.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
use super::AffectType;
use crate::{
AsContextDependency, AsDependencyTemplate, Context, Dependency, DependencyCategory, DependencyId,
DependencyType, ModuleDependency,
DependencyType, ModuleDependency, ModuleIdentifier,
};

#[derive(Debug, Hash, PartialEq, Eq, Clone)]
pub struct LoaderImportDependency {
id: DependencyId,
context: Context,
pub context: Context,
pub original_module: Option<ModuleIdentifier>,
request: String,
}

impl LoaderImportDependency {
pub fn new(request: String, context: Context) -> Self {
pub fn new(request: String, context: Context, original_module: Option<ModuleIdentifier>) -> Self {
Self {
request,
context,
original_module,
id: DependencyId::new(),
}
}
Expand Down

0 comments on commit 1859fa8

Please sign in to comment.