Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use asset filename new type #8438

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/rspack_binding_values/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct JsAssetInfoRelated {
impl From<JsAssetInfoRelated> for rspack_core::AssetInfoRelated {
fn from(i: JsAssetInfoRelated) -> Self {
Self {
source_map: i.source_map,
source_map: i.source_map.map(Into::into),
}
}
}
Expand Down Expand Up @@ -83,7 +83,7 @@ pub struct JsAsset {
impl From<rspack_core::AssetInfoRelated> for JsAssetInfoRelated {
fn from(related: rspack_core::AssetInfoRelated) -> Self {
Self {
source_map: related.source_map,
source_map: related.source_map.map(|f| f.to_string()),
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions crates/rspack_binding_values/src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ pub struct JsChunk {

impl JsChunk {
pub fn from(chunk: &rspack_core::Chunk, compilation: &Compilation) -> Self {
let mut files = Vec::from_iter(chunk.files().iter().cloned());
let mut files = Vec::from_iter(chunk.files().iter().map(|file| file.to_string()));
files.sort_unstable();
let mut auxiliary_files = Vec::from_iter(chunk.auxiliary_files().iter().cloned());
let mut auxiliary_files =
Vec::from_iter(chunk.auxiliary_files().iter().map(|file| file.to_string()));
auxiliary_files.sort_unstable();

Self {
Expand Down
27 changes: 16 additions & 11 deletions crates/rspack_binding_values/src/compilation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use entries::JsEntries;
use napi_derive::napi;
use rspack_collections::IdentifierSet;
use rspack_core::rspack_sources::BoxSource;
use rspack_core::AssetFilename;
use rspack_core::AssetInfo;
use rspack_core::ChunkUkey;
use rspack_core::Compilation;
Expand Down Expand Up @@ -124,7 +125,7 @@ impl JsCompilation {

for (filename, asset) in compilation.assets() {
assets.push(JsAsset {
name: filename.clone(),
name: filename.to_string(),
info: asset.info.clone().into(),
});
}
Expand All @@ -136,7 +137,7 @@ impl JsCompilation {
pub fn get_asset(&self, name: String) -> Result<Option<JsAsset>> {
let compilation = self.as_ref()?;

match compilation.assets().get(&name) {
match compilation.assets().get(name.as_str()) {
Some(asset) => Ok(Some(JsAsset {
name,
info: asset.info.clone().into(),
Expand All @@ -151,7 +152,7 @@ impl JsCompilation {

compilation
.assets()
.get(&name)
.get(name.as_str())
.and_then(|v| v.source.as_ref().map(|s| s.to_js_compat_source()))
.transpose()
}
Expand Down Expand Up @@ -267,7 +268,7 @@ impl JsCompilation {
let compilation = self.as_mut()?;

let source: BoxSource = source.into();
match compilation.assets_mut().entry(name) {
match compilation.assets_mut().entry(name.into()) {
std::collections::hash_map::Entry::Occupied(mut e) => e.get_mut().set_source(Some(source)),
std::collections::hash_map::Entry::Vacant(e) => {
e.insert(rspack_core::CompilationAsset::from(source));
Expand All @@ -282,7 +283,7 @@ impl JsCompilation {

compilation
.assets_mut()
.entry(name)
.entry(name.into())
.and_modify(|a| a.set_source(None));
Ok(())
}
Expand All @@ -296,8 +297,7 @@ impl JsCompilation {
.assets()
.iter()
.filter(|(_, asset)| asset.get_source().is_some())
.map(|(filename, _)| filename)
.cloned()
.map(|(filename, _)| filename.to_string())
.collect(),
)
}
Expand All @@ -306,7 +306,7 @@ impl JsCompilation {
pub fn has_asset(&self, name: String) -> Result<bool> {
let compilation = self.as_ref()?;

Ok(compilation.assets().contains_key(&name))
Ok(compilation.assets().contains_key(name.as_str()))
}

#[napi]
Expand All @@ -318,6 +318,7 @@ impl JsCompilation {
module: String,
) -> Result<()> {
let compilation = self.as_mut()?;
let filename: AssetFilename = filename.into();

compilation.emit_asset(
filename.clone(),
Expand All @@ -342,7 +343,7 @@ impl JsCompilation {
let compilation = self.as_mut()?;

compilation.emit_asset(
filename,
filename.into(),
rspack_core::CompilationAsset::new(Some(source.into()), asset_info.into()),
);
Ok(())
Expand All @@ -360,7 +361,7 @@ impl JsCompilation {
pub fn rename_asset(&mut self, filename: String, new_name: String) -> Result<()> {
let compilation = self.as_mut()?;

compilation.rename_asset(&filename, new_name);
compilation.rename_asset(&filename, new_name.into());
Ok(())
}

Expand Down Expand Up @@ -665,7 +666,11 @@ impl JsCompilation {
.into_iter()
.map(|d| d.to_string_lossy().to_string())
.collect(),
assets: res.assets.into_iter().collect(),
assets: res
.assets
.into_iter()
.map(|file| file.to_string())
.collect(),
id: res.id,
};
Ok(js_result)
Expand Down
15 changes: 8 additions & 7 deletions crates/rspack_core/src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use rspack_hash::{RspackHash, RspackHashDigest};
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet, FxHasher};

use crate::{
compare_chunk_group, merge_runtime, sort_group_by_index, ChunkGraph, ChunkGroupOrderKey,
compare_chunk_group, merge_runtime, sort_group_by_index, AssetFilename, ChunkGraph,
ChunkGroupOrderKey,
};
use crate::{ChunkGroupByUkey, ChunkGroupUkey, ChunkUkey, SourceType};
use crate::{Compilation, EntryOptions, Filename, ModuleGraph, RuntimeSpec};
Expand Down Expand Up @@ -57,8 +58,8 @@ pub struct Chunk {
prevent_integration: bool,
groups: UkeySet<ChunkGroupUkey>,
runtime: RuntimeSpec,
files: HashSet<String>,
auxiliary_files: HashSet<String>,
files: HashSet<AssetFilename>,
auxiliary_files: HashSet<AssetFilename>,
chunk_reason: Option<String>,
rendered: bool,
}
Expand Down Expand Up @@ -148,23 +149,23 @@ impl Chunk {
self.runtime = runtime;
}

pub fn files(&self) -> &HashSet<String> {
pub fn files(&self) -> &HashSet<AssetFilename> {
&self.files
}

pub fn add_file(&mut self, file: String) {
pub fn add_file(&mut self, file: AssetFilename) {
self.files.insert(file);
}

pub fn remove_file(&mut self, file: &str) -> bool {
self.files.remove(file)
}

pub fn auxiliary_files(&self) -> &HashSet<String> {
pub fn auxiliary_files(&self) -> &HashSet<AssetFilename> {
&self.auxiliary_files
}

pub fn add_auxiliary_file(&mut self, auxiliary_file: String) {
pub fn add_auxiliary_file(&mut self, auxiliary_file: AssetFilename) {
self.auxiliary_files.insert(auxiliary_file);
}

Expand Down
27 changes: 13 additions & 14 deletions crates/rspack_core/src/chunk_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use rustc_hash::FxHashMap as HashMap;

use crate::{
compare_chunk_group, Chunk, ChunkByUkey, ChunkGroupByUkey, ChunkGroupUkey, DependencyLocation,
DynamicImportFetchPriority, Filename, ModuleLayer,
compare_chunk_group, AssetFilename, Chunk, ChunkByUkey, ChunkGroupByUkey, ChunkGroupUkey,
DependencyLocation, DynamicImportFetchPriority, Filename, ModuleLayer,
};
use crate::{ChunkLoading, ChunkUkey, Compilation};
use crate::{LibraryOptions, ModuleIdentifier, PublicPath};
Expand Down Expand Up @@ -83,18 +83,17 @@
.copied()
}

pub fn get_files(&self, chunk_by_ukey: &ChunkByUkey) -> Vec<String> {
self
.chunks
.iter()
.flat_map(|chunk_ukey| {
chunk_by_ukey
.expect_get(chunk_ukey)
.files()
.iter()
.map(|file| file.to_string())
})
.collect()
pub fn get_files<'a>(
&self,
chunk_by_ukey: &'a ChunkByUkey,
) -> impl Iterator<Item = &'a AssetFilename> + use<'a, '_> {
self.chunks.iter().flat_map(|chunk_ukey| {
chunk_by_ukey
.expect_get(chunk_ukey)
.files()
.iter()

Check failure on line 94 in crates/rspack_core/src/chunk_group.rs

View workflow job for this annotation

GitHub Actions / Rust check

unnecessary map of the identity function
.map(|file| file)
})
}

pub(crate) fn connect_chunk(&mut self, chunk: &mut Chunk) {
Expand Down
Loading
Loading