Skip to content

Commit

Permalink
ref: re impl in 0.5.9
Browse files Browse the repository at this point in the history
  • Loading branch information
JiangWeixian committed Mar 30, 2024
1 parent 1178612 commit 4297cec
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 52 deletions.
118 changes: 68 additions & 50 deletions crates/rspack_plugin_rsc/src/plugin/rsc_client_entry_rspack_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ use std::time::Instant;

use async_trait::async_trait;
use rspack_core::rspack_sources::{RawSource, SourceExt};
use rspack_core::{AssetInfo, Compilation, CompilationAsset, Module, ModuleType, Plugin};
use rspack_core::{
ApplyContext, AssetInfo, Compilation, CompilationAsset, CompilerOptions, Module, ModuleType,
Plugin, PluginContext,
};
use rspack_error::Result;
use rspack_hook::{plugin, plugin_hook, AsyncSeries};
use serde_json::to_string;

#[plugin]
#[derive(Debug, Default, Clone)]
pub struct RSCClientEntryRspackPlugin {}

impl RSCClientEntryRspackPlugin {
pub fn new() -> Self {
Self {}
Self::new_inner()
}
fn filter_client_components(
&self,

Check failure on line 22 in crates/rspack_plugin_rsc/src/plugin/rsc_client_entry_rspack_plugin.rs

View workflow job for this annotation

GitHub Actions / Rust check

parameter is only used in recursion
Expand Down Expand Up @@ -59,57 +63,71 @@ impl RSCClientEntryRspackPlugin {
}
}

#[async_trait]
impl Plugin for RSCClientEntryRspackPlugin {
async fn finish_make(&self, compilation: &mut Compilation) -> Result<()> {
let now = Instant::now();
let mut client_imports: HashMap<String, HashSet<String>> = HashMap::new();
for (name, entry) in &compilation.entries {
let mut collected_client_imports: HashSet<String> = HashSet::new();
let mut visited_modules: HashSet<String> = HashSet::new();
let mg = compilation.get_module_graph();
let entry_module = mg
.get_module_by_dependency_id(&entry.dependencies[0])
.expect("should exist");
self.filter_client_components(
compilation,
entry_module,
&mut visited_modules,
&mut collected_client_imports,
);
client_imports.insert(String::from(name), collected_client_imports);
}
// all other entries depend on this entry
let main_name = "server-entry";
let cc = client_imports.clone();
let main = cc.get(main_name).unwrap();
for (name, value) in client_imports.iter_mut() {
if name != main_name {
for import in main {
value.remove(import.as_str());
}
#[plugin_hook(AsyncSeries<Compilation> for RSCClientEntryRspackPlugin)]
async fn finish_make(&self, compilation: &mut Compilation) -> Result<()> {
let now = Instant::now();
let mut client_imports: HashMap<String, HashSet<String>> = HashMap::new();
for (name, entry) in &compilation.entries {
let mut collected_client_imports: HashSet<String> = HashSet::new();
let mut visited_modules: HashSet<String> = HashSet::new();
let mg = compilation.get_module_graph();
let entry_module = mg
.get_module_by_dependency_id(&entry.dependencies[0])
.expect("should exist");
self.filter_client_components(
compilation,
entry_module,
&mut visited_modules,
&mut collected_client_imports,
);
client_imports.insert(String::from(name), collected_client_imports);
}
// all other entries depend on this entry
let main_name = "server-entry";
let cc = client_imports.clone();
let main = cc.get(main_name).unwrap();

Check failure on line 88 in crates/rspack_plugin_rsc/src/plugin/rsc_client_entry_rspack_plugin.rs

View workflow job for this annotation

GitHub Actions / Rust check

used `unwrap()` on an `Option` value
for (name, value) in client_imports.iter_mut() {
if name != main_name {
for import in main {
value.remove(import.as_str());
}
let content = to_string(&value);
match content {
Ok(content) => {
compilation.assets_mut().insert(
format!("[{}]_client_imports.json", name),
CompilationAsset {
source: Some(RawSource::from(content).boxed()),
info: AssetInfo {
immutable: false,
..AssetInfo::default()
},
}
let content = to_string(&value);
match content {

Check failure on line 96 in crates/rspack_plugin_rsc/src/plugin/rsc_client_entry_rspack_plugin.rs

View workflow job for this annotation

GitHub Actions / Rust check

you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
Ok(content) => {
compilation.assets_mut().insert(
format!("[{}]_client_imports.json", name),
CompilationAsset {
source: Some(RawSource::from(content).boxed()),
info: AssetInfo {
immutable: false,
..AssetInfo::default()
},
);
}
Err(_) => (),
},
);
}
Err(_) => (),
}
tracing::debug!(
"collect all client imports took {} ms.",
now.elapsed().as_millis()
);
}
tracing::debug!(
"collect all client imports took {} ms.",
now.elapsed().as_millis()
);
Ok(())
}

#[async_trait]
impl Plugin for RSCClientEntryRspackPlugin {
fn apply(
&self,
ctx: PluginContext<&mut ApplyContext>,
_options: &mut CompilerOptions,
) -> Result<()> {
ctx
.context
.compiler_hooks
.finish_make
.tap(finish_make::new(self));
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl RSCClientReferenceManifest {
})
.collect::<Vec<_>>();
for chunk in &chunk_group.chunks {
let chunk_modules = compilation.chunk_graph.get_chunk_modules(chunk, mg);
let chunk_modules = compilation.chunk_graph.get_chunk_modules(chunk, &mg);
for module in chunk_modules {
let module_id = compilation.chunk_graph.get_module_id(module.identifier());
let resolved_data = module

Check failure on line 87 in crates/rspack_plugin_rsc/src/plugin/rsc_client_reference_manifest_rspack_plugin.rs

View workflow job for this annotation

GitHub Actions / Rust check

using `Option.and_then(|x| Some(y))`, which is more succinctly expressed as `map(|x| y)`
Expand All @@ -101,7 +101,7 @@ impl RSCClientReferenceManifest {
if let Some(module_id) = module_id {
let exports_info = mg.get_exports_info(&module.identifier());
let module_exported_keys = exports_info.get_ordered_exports().filter_map(|id| {
let info = id.get_export_info(mg);
let info = id.get_export_info(&mg);
if let Some(provided) = info.provided {
match provided {
ExportInfoProvided::True => Some(info.name.clone()),
Expand Down

0 comments on commit 4297cec

Please sign in to comment.