Skip to content

Commit

Permalink
feat(core): integrate rspack_resolver with input_filesystem (web-infr…
Browse files Browse the repository at this point in the history
  • Loading branch information
hardfist authored Sep 22, 2024
1 parent e58ac85 commit 45a3997
Show file tree
Hide file tree
Showing 30 changed files with 243 additions and 150 deletions.
11 changes: 7 additions & 4 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 @@ -48,6 +48,7 @@ proc-macro2 = { version = "1.0.79" }
quote = { version = "1.0.35" }
rayon = { version = "1.10.0" }
regex = { version = "1.10.4" }
rspack_resolver = { version = "0.3.3", features = ["package_json_raw_json_api"] }
ropey = "1.6.1"
rspack_sources = { version = "=0.3.2" }
rustc-hash = { version = "1.1.0" }
Expand Down
1 change: 1 addition & 0 deletions crates/node_binding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ rspack_binding_values = { version = "0.1.0", path = "../rspack_binding_values
rspack_collections = { version = "0.1.0", path = "../rspack_collections" }
rspack_core = { version = "0.1.0", path = "../rspack_core" }
rspack_error = { version = "0.1.0", path = "../rspack_error" }
rspack_fs = { version = "0.1.0", path = "../rspack_fs" }
rspack_fs_node = { version = "0.1.0", path = "../rspack_fs_node" }
rspack_hash = { version = "0.1.0", path = "../rspack_hash" }
rspack_hook = { version = "0.1.0", path = "../rspack_hook" }
Expand Down
5 changes: 3 additions & 2 deletions crates/node_binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ impl Rspack {
let rspack = rspack_core::Compiler::new(
compiler_options,
plugins,
Box::new(
Some(Box::new(
AsyncNodeWritableFileSystem::new(output_filesystem)
.map_err(|e| Error::from_reason(format!("Failed to create writable filesystem: {e}",)))?,
),
)),
None,
Some(resolver_factory),
Some(loader_resolver_factory),
);
Expand Down
15 changes: 13 additions & 2 deletions crates/node_binding/src/resolver_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::sync::Arc;

use napi_derive::napi;
use rspack_core::{Resolve, ResolverFactory};
use rspack_fs::{NativeFileSystem, ReadableFileSystem};

use crate::{
raw_resolve::{
Expand All @@ -14,23 +15,30 @@ use crate::{
pub struct JsResolverFactory {
pub(crate) resolver_factory: Option<Arc<ResolverFactory>>,
pub(crate) loader_resolver_factory: Option<Arc<ResolverFactory>>,
pub(crate) input_filesystem: Arc<dyn ReadableFileSystem>,
}

#[napi]
impl JsResolverFactory {
#[napi(constructor)]
pub fn new() -> napi::Result<Self> {
let input_filesystem = Arc::new(NativeFileSystem {});
Ok(Self {
resolver_factory: None,
loader_resolver_factory: None,
input_filesystem,
})
}

pub fn get_resolver_factory(&mut self, resolve_options: Resolve) -> Arc<ResolverFactory> {
match &self.resolver_factory {
Some(resolver_factory) => resolver_factory.clone(),

None => {
let resolver_factory = Arc::new(ResolverFactory::new(resolve_options));
let resolver_factory = Arc::new(ResolverFactory::new(
resolve_options,
self.input_filesystem.clone(),
));
self.resolver_factory = Some(resolver_factory.clone());
resolver_factory
}
Expand All @@ -41,7 +49,10 @@ impl JsResolverFactory {
match &self.loader_resolver_factory {
Some(resolver_factory) => resolver_factory.clone(),
None => {
let resolver_factory = Arc::new(ResolverFactory::new(resolve_options));
let resolver_factory = Arc::new(ResolverFactory::new(
resolve_options,
self.input_filesystem.clone(),
));
self.loader_resolver_factory = Some(resolver_factory.clone());
resolver_factory
}
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ regex = { workspace = true }
rspack_ast = { version = "0.1.0", path = "../rspack_ast" }
rspack_collections = { version = "0.1.0", path = "../rspack_collections" }
rspack_error = { version = "0.1.0", path = "../rspack_error" }
rspack_fs = { version = "0.1.0", path = "../rspack_fs", features = ["async", "rspack-error"] }
rspack_fs = { version = "0.1.0", path = "../rspack_fs" }
rspack_futures = { version = "0.1.0", path = "../rspack_futures" }
rspack_hash = { version = "0.1.0", path = "../rspack_hash" }
rspack_hook = { version = "0.1.0", path = "../rspack_hook" }
rspack_loader_runner = { version = "0.1.0", path = "../rspack_loader_runner" }
rspack_macros = { version = "0.1.0", path = "../rspack_macros" }
rspack_paths = { version = "0.1.0", path = "../rspack_paths" }
rspack_regex = { version = "0.1.0", path = "../rspack_regex" }
rspack_resolver = { version = "0.3.1", features = ["package_json_raw_json_api"] }
rspack_resolver = { workspace = true }
rspack_sources = { workspace = true }
rspack_util = { version = "0.1.0", path = "../rspack_util" }
rustc-hash = { workspace = true }
Expand Down
10 changes: 8 additions & 2 deletions crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ use std::{
};

use dashmap::DashSet;
use derivative::Derivative;
use indexmap::{IndexMap, IndexSet};
use itertools::Itertools;
use rayon::prelude::*;
use rspack_collections::{
Identifiable, Identifier, IdentifierDashMap, IdentifierMap, IdentifierSet, UkeySet,
};
use rspack_error::{error, Diagnostic, Result, Severity};
use rspack_fs::ReadableFileSystem;
use rspack_futures::FuturesResults;
use rspack_hash::{RspackHash, RspackHashDigest};
use rspack_hook::define_hook;
Expand Down Expand Up @@ -129,8 +131,8 @@ impl Default for CompilationId {
type ValueCacheVersions = HashMap<String, String>;

static COMPILATION_ID: AtomicU32 = AtomicU32::new(0);

#[derive(Debug)]
#[derive(Derivative)]
#[derivative(Debug)]
pub struct Compilation {
/// get_compilation_hooks(compilation.id)
id: CompilationId,
Expand Down Expand Up @@ -193,6 +195,8 @@ pub struct Compilation {
pub modified_files: HashSet<PathBuf>,
pub removed_files: HashSet<PathBuf>,
make_artifact: MakeArtifact,
#[derivative(Debug = "ignore")]
pub input_filesystem: Arc<dyn ReadableFileSystem>,
}

impl Compilation {
Expand Down Expand Up @@ -228,6 +232,7 @@ impl Compilation {
module_executor: Option<ModuleExecutor>,
modified_files: HashSet<PathBuf>,
removed_files: HashSet<PathBuf>,
input_filesystem: Arc<dyn ReadableFileSystem>,
) -> Self {
let mutations = options.new_incremental_enabled().then(Mutations::default);
Self {
Expand Down Expand Up @@ -285,6 +290,7 @@ impl Compilation {
make_artifact: Default::default(),
modified_files,
removed_files,
input_filesystem,
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/rspack_core/src/compiler/hmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ impl Compiler {
Some(ModuleExecutor::default()),
modified_files,
removed_files,
self.input_filesystem.clone(),
);

if let Some(state) = self.options.get_incremental_rebuild_make_state() {
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_core/src/compiler/make/repair/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ impl Task<MakeTaskContext> for AddTask {
resolver_factory: context.resolver_factory.clone(),
compiler_options: context.compiler_options.clone(),
plugin_driver: context.plugin_driver.clone(),
fs: context.fs.clone(),
})])
}
}
Expand Down
10 changes: 9 additions & 1 deletion crates/rspack_core/src/compiler/make/repair/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::{collections::VecDeque, sync::Arc};

use derivative::Derivative;
use rspack_error::{Diagnostic, IntoTWithDiagnosticArray};
use rspack_fs::ReadableFileSystem;

use super::{process_dependencies::ProcessDependenciesTask, MakeTaskContext};
use crate::{
Expand All @@ -10,13 +12,17 @@ use crate::{
SharedPluginDriver,
};

#[derive(Debug)]
#[derive(Derivative)]
#[derivative(Debug)]

pub struct BuildTask {
pub module: Box<dyn Module>,
pub current_profile: Option<Box<ModuleProfile>>,
pub resolver_factory: Arc<ResolverFactory>,
pub compiler_options: Arc<CompilerOptions>,
pub plugin_driver: SharedPluginDriver,
#[derivative(Debug = "ignore")]
pub fs: Arc<dyn ReadableFileSystem>,
}

#[async_trait::async_trait]
Expand All @@ -31,6 +37,7 @@ impl Task<MakeTaskContext> for BuildTask {
plugin_driver,
current_profile,
mut module,
fs,
} = *self;
if let Some(current_profile) = &current_profile {
current_profile.mark_building_start();
Expand All @@ -53,6 +60,7 @@ impl Task<MakeTaskContext> for BuildTask {
},
plugin_driver: plugin_driver.clone(),
compiler_options: &compiler_options,
fs: fs.clone(),
},
None,
)
Expand Down
4 changes: 4 additions & 0 deletions crates/rspack_core/src/compiler/make/repair/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod process_dependencies;
use std::sync::Arc;

use rspack_error::Result;
use rspack_fs::ReadableFileSystem;
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};

use super::MakeArtifact;
Expand All @@ -21,6 +22,7 @@ use crate::{
pub struct MakeTaskContext {
// compilation info
pub plugin_driver: SharedPluginDriver,
pub fs: Arc<dyn ReadableFileSystem>,
pub compiler_options: Arc<CompilerOptions>,
pub resolver_factory: Arc<ResolverFactory>,
pub loader_resolver_factory: Arc<ResolverFactory>,
Expand All @@ -41,6 +43,7 @@ impl MakeTaskContext {
old_cache: compilation.old_cache.clone(),
unaffected_modules_cache: compilation.unaffected_modules_cache.clone(),
dependency_factories: compilation.dependency_factories.clone(),
fs: compilation.input_filesystem.clone(),
artifact,
}
}
Expand Down Expand Up @@ -68,6 +71,7 @@ impl MakeTaskContext {
None,
Default::default(),
Default::default(),
self.fs.clone(),
);
compilation.dependency_factories = self.dependency_factories.clone();
compilation.swap_make_artifact(&mut self.artifact);
Expand Down
33 changes: 27 additions & 6 deletions crates/rspack_core/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use std::sync::Arc;

use derivative::Derivative;
use rspack_error::Result;
use rspack_fs::AsyncWritableFileSystem;
use rspack_fs::{
AsyncNativeFileSystem, AsyncWritableFileSystem, NativeFileSystem, ReadableFileSystem,
};
use rspack_futures::FuturesResults;
use rspack_hook::define_hook;
use rspack_paths::{Utf8Path, Utf8PathBuf};
Expand Down Expand Up @@ -55,6 +57,8 @@ pub struct Compiler {
pub options: Arc<CompilerOptions>,
#[derivative(Debug = "ignore")]
pub output_filesystem: Box<dyn AsyncWritableFileSystem + Send + Sync>,
#[derivative(Debug = "ignore")]
pub input_filesystem: Arc<dyn ReadableFileSystem>,
pub compilation: Compilation,
pub plugin_driver: SharedPluginDriver,
pub resolver_factory: Arc<ResolverFactory>,
Expand All @@ -71,7 +75,9 @@ impl Compiler {
pub fn new(
options: CompilerOptions,
plugins: Vec<BoxPlugin>,
output_filesystem: Box<dyn AsyncWritableFileSystem + Send + Sync>,
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>>,
// no need to pass resolve_factory in rust api
resolver_factory: Option<Arc<ResolverFactory>>,
loader_resolver_factory: Option<Arc<ResolverFactory>>,
Expand All @@ -82,14 +88,26 @@ impl Compiler {
debug_info.with_context(options.context.to_string());
}
}
let resolver_factory =
resolver_factory.unwrap_or_else(|| Arc::new(ResolverFactory::new(options.resolve.clone())));
let loader_resolver_factory = loader_resolver_factory
.unwrap_or_else(|| Arc::new(ResolverFactory::new(options.resolve_loader.clone())));
let input_filesystem = input_filesystem.unwrap_or_else(|| Arc::new(NativeFileSystem {}));

let resolver_factory = resolver_factory.unwrap_or_else(|| {
Arc::new(ResolverFactory::new(
options.resolve.clone(),
input_filesystem.clone(),
))
});
let loader_resolver_factory = loader_resolver_factory.unwrap_or_else(|| {
Arc::new(ResolverFactory::new(
options.resolve_loader.clone(),
input_filesystem.clone(),
))
});
let (plugin_driver, options) = PluginDriver::new(options, 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();
let output_filesystem = output_filesystem.unwrap_or_else(|| Box::new(AsyncNativeFileSystem {}));

Self {
options: options.clone(),
compilation: Compilation::new(
Expand All @@ -103,6 +121,7 @@ impl Compiler {
Some(module_executor),
Default::default(),
Default::default(),
input_filesystem.clone(),
),
output_filesystem,
plugin_driver,
Expand All @@ -111,6 +130,7 @@ impl Compiler {
old_cache,
emitted_asset_versions: Default::default(),
unaffected_modules_cache,
input_filesystem,
}
}

Expand Down Expand Up @@ -140,6 +160,7 @@ impl Compiler {
Some(module_executor),
Default::default(),
Default::default(),
self.input_filesystem.clone(),
),
);

Expand Down
3 changes: 1 addition & 2 deletions crates/rspack_core/src/external_module.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::borrow::Cow;
use std::iter;
use std::{borrow::Cow, iter};

use rspack_collections::{Identifiable, Identifier};
use rspack_error::{error, impl_empty_diagnosable_trait, Diagnostic, Result};
Expand Down
Loading

0 comments on commit 45a3997

Please sign in to comment.