Skip to content
Draft
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
2 changes: 1 addition & 1 deletion crates/rspack/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,7 @@ impl CompilerOptionsBuilder {
.into_iter()
.for_each(|(name, desc)| {
let entry_options = EntryOptions {
name: Some(name),
name: Some(name.into()),
runtime: desc.runtime.map(EntryRuntime::String),
chunk_loading: desc.chunk_loading,
wasm_loading: desc.wasm_loading,
Expand Down
4 changes: 3 additions & 1 deletion crates/rspack_binding_api/src/allocator.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use napi::{Env, bindgen_prelude::ToNapiValue, sys::napi_env};
use rspack_core::BindingCell;

Expand Down Expand Up @@ -57,7 +59,7 @@ impl rspack_core::NapiAllocator for NapiAllocatorImpl {
fn allocate_assets(
&self,
env: napi_env,
val: &BindingCell<rustc_hash::FxHashMap<String, rspack_core::CompilationAsset>>,
val: &BindingCell<rustc_hash::FxHashMap<Arc<str>, rspack_core::CompilationAsset>>,
) -> napi::Result<napi::sys::napi_value> {
let assets = Assets::new(val.downgrade());
unsafe { ToNapiValue::to_napi_value(env, assets) }
Expand Down
6 changes: 4 additions & 2 deletions crates/rspack_binding_api/src/asset.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use napi::{
Env, JsValue,
bindgen_prelude::{
Expand All @@ -23,7 +25,7 @@ impl From<AssetInfoRelated> for rspack_core::AssetInfoRelated {
Self {
source_map: match i.source_map {
Some(either) => match either {
Either::A(string) => Some(string),
Either::A(string) => Some(Arc::from(string)),
Either::B(_) => None,
},
None => None,
Expand Down Expand Up @@ -208,7 +210,7 @@ pub struct JsAsset {
impl From<rspack_core::AssetInfoRelated> for AssetInfoRelated {
fn from(related: rspack_core::AssetInfoRelated) -> Self {
Self {
source_map: related.source_map.map(Either::A),
source_map: related.source_map.map(|s| Either::A(s.to_string())),
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions crates/rspack_binding_api/src/build_info.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{cell::RefCell, sync::LazyLock};
use std::{
cell::RefCell,
sync::{Arc, LazyLock},
};

use napi::{
Env, JsString, JsValue, Property, PropertyAttributes, Unknown,
Expand All @@ -24,17 +27,17 @@ define_symbols! {
// Record<string, Source>
#[napi]
pub struct Assets {
i: WeakBindingCell<FxHashMap<String, rspack_core::CompilationAsset>>,
i: WeakBindingCell<FxHashMap<Arc<str>, rspack_core::CompilationAsset>>,
}

impl Assets {
pub fn new(i: WeakBindingCell<FxHashMap<String, rspack_core::CompilationAsset>>) -> Self {
pub fn new(i: WeakBindingCell<FxHashMap<Arc<str>, rspack_core::CompilationAsset>>) -> Self {
Self { i }
}

fn with_ref<T>(
&self,
f: impl FnOnce(&FxHashMap<String, rspack_core::CompilationAsset>) -> napi::Result<T>,
f: impl FnOnce(&FxHashMap<Arc<str>, rspack_core::CompilationAsset>) -> napi::Result<T>,
) -> napi::Result<T> {
match self.i.upgrade() {
Some(reference) => f(reference.as_ref()),
Expand Down
8 changes: 4 additions & 4 deletions crates/rspack_binding_api/src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ impl Chunk {
}

#[napi(getter, js_name = "_files")]
pub fn files(&self) -> napi::Result<Vec<&String>> {
pub fn files(&self) -> napi::Result<Vec<&str>> {
let (_, chunk) = self.as_ref()?;
let mut files = Vec::from_iter(chunk.files());
let mut files: Vec<&str> = chunk.files().iter().map(|s| s.as_ref()).collect();
files.sort_unstable();
Ok(files)
}
Expand Down Expand Up @@ -156,9 +156,9 @@ impl Chunk {
}

#[napi(getter, js_name = "_auxiliaryFiles")]
pub fn auxiliary_files(&self) -> napi::Result<Vec<&String>> {
pub fn auxiliary_files(&self) -> napi::Result<Vec<&str>> {
let (_, chunk) = self.as_ref()?;
Ok(chunk.auxiliary_files().iter().collect::<Vec<_>>())
Ok(chunk.auxiliary_files().iter().map(|s| s.as_ref()).collect())
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_binding_api/src/chunk_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl ChunkGroup {
}

#[napi]
pub fn get_files(&self) -> napi::Result<Vec<&String>> {
pub fn get_files(&self) -> napi::Result<Vec<&str>> {
let (compilation, chunk_group) = self.as_ref()?;
Ok(
chunk_group
Expand All @@ -159,7 +159,7 @@ impl ChunkGroup {
.build_chunk_graph_artifact
.chunk_by_ukey
.get(chunk_ukey)
.map(|chunk| chunk.files().iter())
.map(|chunk| chunk.files().iter().map(|s| s.as_ref()))
})
.flatten()
.collect::<Vec<_>>(),
Expand Down
23 changes: 14 additions & 9 deletions crates/rspack_binding_api/src/compilation/entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ impl EntryOptionsDTO {
#[napi]
impl EntryOptionsDTO {
#[napi(getter)]
pub fn name(&self) -> Either<&String, ()> {
self.0.name.as_ref().into()
pub fn name(&self) -> Either<&str, ()> {
self.0.name.as_ref().map(|s| s.as_ref()).into()
}

#[napi(setter)]
pub fn set_name(&mut self, name: Either<String, ()>) {
self.0.name = match name {
Either::A(s) => Some(s),
Either::A(s) => Some(s.into()),
Either::B(_) => None,
};
}
Expand Down Expand Up @@ -252,7 +252,7 @@ impl JsEntries {

#[napi]
pub fn has(&self, key: String) -> bool {
self.compilation.entries.contains_key(&key)
self.compilation.entries.contains_key(key.as_str())
}

#[napi]
Expand All @@ -267,18 +267,18 @@ impl JsEntries {
dto.entry_data.clone()
}
};
self.compilation.entries.insert(key, entry_data);
self.compilation.entries.insert(key.into(), entry_data);
}

#[napi]
pub fn delete(&mut self, key: String) -> bool {
let r = self.compilation.entries.swap_remove(&key);
let r = self.compilation.entries.swap_remove(key.as_str());
r.is_some()
}

#[napi]
pub fn get(&'static mut self, key: String) -> Result<Either<EntryDataDTO, ()>> {
Ok(match self.compilation.entries.get(&key) {
Ok(match self.compilation.entries.get(key.as_str()) {
Some(entry_data) => Either::A(EntryDataDTO {
entry_data: entry_data.clone(),
compilation: self.compilation,
Expand All @@ -288,8 +288,13 @@ impl JsEntries {
}

#[napi]
pub fn keys(&self) -> Vec<&String> {
self.compilation.entries.keys().collect()
pub fn keys(&self) -> Vec<&str> {
self
.compilation
.entries
.keys()
.map(|s| s.as_ref())
.collect()
}

#[napi]
Expand Down
33 changes: 16 additions & 17 deletions crates/rspack_binding_api/src/compilation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod dependencies;
mod diagnostics;
pub mod entries;

use std::{cell::RefCell, path::Path, ptr::NonNull};
use std::{cell::RefCell, path::Path, ptr::NonNull, sync::Arc};

use chunks::Chunks;
pub use code_generation_results::*;
Expand Down Expand Up @@ -163,7 +163,7 @@ impl JsCompilation {
for (filename, asset) in compilation.assets() {
let info = asset.info.reflector();
assets.push(JsAsset {
name: filename.clone(),
name: filename.to_string(),
info,
});
}
Expand All @@ -175,7 +175,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) => {
let info = asset.info.reflector();
Ok(Some(JsAsset { name, info }))
Expand All @@ -190,7 +190,7 @@ impl JsCompilation {

compilation
.assets()
.get(&name)
.get(name.as_str())
.and_then(|v| {
v.source
.as_ref()
Expand Down Expand Up @@ -266,7 +266,7 @@ impl JsCompilation {
.build_chunk_graph_artifact
.named_chunks
.keys()
.cloned()
.map(|s| s.to_string())
.collect::<Vec<_>>(),
)
}
Expand All @@ -279,7 +279,7 @@ impl JsCompilation {
compilation
.build_chunk_graph_artifact
.named_chunks
.get(&name)
.get(name.as_str())
.and_then(|c| {
compilation
.build_chunk_graph_artifact
Expand All @@ -291,15 +291,15 @@ impl JsCompilation {
}

#[napi]
pub fn get_named_chunk_group_keys(&self) -> Result<Vec<String>> {
pub fn get_named_chunk_group_keys(&self) -> Result<Vec<&str>> {
let compilation = self.as_ref()?;

Ok(
compilation
.build_chunk_graph_artifact
.named_chunk_groups
.keys()
.cloned()
.map(|s| s.as_ref())
.collect::<Vec<_>>(),
)
}
Expand All @@ -311,7 +311,7 @@ impl JsCompilation {
compilation
.build_chunk_graph_artifact
.named_chunk_groups
.get(&name)
.get(name.as_str())
.map(|ukey| ChunkGroupWrapper::new(*ukey, compilation)),
)
}
Expand All @@ -321,7 +321,8 @@ impl JsCompilation {
let compilation = self.as_mut()?;

let source: BoxSource = source.try_into()?;
match compilation.assets_mut().entry(name) {
let name_key = Arc::from(name);
match compilation.assets_mut().entry(name_key) {
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 @@ -334,10 +335,9 @@ impl JsCompilation {
pub fn delete_asset_source(&mut self, name: String) -> Result<()> {
let compilation = self.as_mut()?;

compilation
.assets_mut()
.entry(name)
.and_modify(|a| a.set_source(None));
if let Some(asset) = compilation.assets_mut().get_mut(name.as_str()) {
asset.set_source(None);
}
Ok(())
}

Expand All @@ -350,8 +350,7 @@ impl JsCompilation {
.assets()
.iter()
.filter(|(_, asset)| asset.get_source().is_some())
.map(|(filename, _)| filename)
.cloned()
.map(|(filename, _)| filename.to_string())
.collect(),
)
}
Expand All @@ -360,7 +359,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 Down
2 changes: 1 addition & 1 deletion crates/rspack_binding_api/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ impl Module {
};

module.build_info_mut().assets.insert(
filename,
filename.into(),
rspack_core::CompilationAsset {
source: Some(source.try_into()?),
info: asset_info,
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_binding_api/src/options/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub struct JsEntryOptions {
impl From<JsEntryOptions> for EntryOptions {
fn from(value: JsEntryOptions) -> Self {
Self {
name: value.name,
name: value.name.map(Into::into),
runtime: value.runtime.map(|r| JsEntryRuntimeWrapper(r).into()),
chunk_loading: value.chunk_loading.map(Into::into),
wasm_loading: value.wasm_loading.map(Into::into),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use napi::bindgen_prelude::Object;
use napi_derive::napi;
use rspack_error::{Error, ToStringResultToRspackResultExt};
Expand Down Expand Up @@ -36,7 +38,7 @@ impl<'a> TryFrom<RawContextReplacementPluginOptions<'a>> for ContextReplacementP
for key in keys {
let value = raw.get::<String>(&key).to_rspack_result()?;
if let Some(value) = value {
map.insert(key, value);
map.insert(Arc::from(key), value);
}
}
Some(map)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ impl From<RawInfo> for Info {
development: value.development,
hot_module_replacement: value.hot_module_replacement,
related: value.related.map(|r| Related {
source_map: r.source_map,
source_map: r.source_map.map(Into::into),
}),
version: value.version,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl From<RawDllEntryPluginOptions> for DllEntryPluginOptions {
} = value;

Self {
name,
name: name.into(),
context: context.into(),
entries,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl From<RawContainerPluginOptions> for ContainerPluginOptions {
fn from(value: RawContainerPluginOptions) -> Self {
let share_scope = into_share_scope(value.share_scope);
Self {
name: value.name,
name: value.name.into(),
share_scope,
library: value.library.into(),
runtime: value.runtime.map(|r| JsEntryRuntimeWrapper(r).into()),
Expand Down Expand Up @@ -163,7 +163,7 @@ impl From<RawCollectShareEntryPluginOptions> for CollectSharedEntryPluginOptions
.into_iter()
.map(|provide| {
let (key, consume_options): (String, ConsumeOptions) = provide.into();
(key, std::sync::Arc::new(consume_options))
(key, Arc::new(consume_options))
})
.collect(),
filename: value.filename,
Expand All @@ -184,7 +184,7 @@ pub struct RawSharedContainerPluginOptions {
impl From<RawSharedContainerPluginOptions> for SharedContainerPluginOptions {
fn from(value: RawSharedContainerPluginOptions) -> Self {
SharedContainerPluginOptions {
name: value.name,
name: value.name.into(),
request: value.request,
version: value.version,
library: value.library.into(),
Expand Down
6 changes: 3 additions & 3 deletions crates/rspack_binding_api/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ impl From<rspack_core::Resource> for ResolveRequest {
value.description_data.map(|data| data.into_parts()).unzip();
Self {
path: value.path.to_string(),
query: value.query,
fragment: value.fragment,
description_file_data: description_file_data.map(std::sync::Arc::unwrap_or_clone),
query: value.query.to_string(),
fragment: value.fragment.to_string(),
description_file_data: description_file_data.map(Arc::unwrap_or_clone),
description_file_path: description_file_path.map(|path| path.to_string_lossy().into_owned()),
file_dependencies: vec![],
missing_dependencies: vec![],
Expand Down
Loading
Loading