From 2a02e272d05ef42dfd4662d58f976100eec52fe9 Mon Sep 17 00:00:00 2001 From: Cong-Cong Date: Tue, 19 Nov 2024 10:43:57 +0800 Subject: [PATCH 1/5] chore: prevent unused clone --- crates/rspack_loader_swc/src/lib.rs | 8 ++------ .../src/utils/eval/eval_source.rs | 4 ++-- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/crates/rspack_loader_swc/src/lib.rs b/crates/rspack_loader_swc/src/lib.rs index 8b920b9644c3..8de18aac4c64 100644 --- a/crates/rspack_loader_swc/src/lib.rs +++ b/crates/rspack_loader_swc/src/lib.rs @@ -87,12 +87,8 @@ impl SwcLoader { }; let source = content.into_string_lossy(); - let c = SwcCompiler::new( - resource_path.into_std_path_buf(), - source.clone(), - swc_options, - ) - .map_err(AnyhowError::from)?; + let c = SwcCompiler::new(resource_path.into_std_path_buf(), source, swc_options) + .map_err(AnyhowError::from)?; let built = c .parse(None, |_| { diff --git a/crates/rspack_plugin_javascript/src/utils/eval/eval_source.rs b/crates/rspack_plugin_javascript/src/utils/eval/eval_source.rs index 87e8fd9333c0..7d419bfa13cc 100644 --- a/crates/rspack_plugin_javascript/src/utils/eval/eval_source.rs +++ b/crates/rspack_plugin_javascript/src/utils/eval/eval_source.rs @@ -20,7 +20,7 @@ pub fn eval_source( error_title: T, ) -> Option { let cm: Arc = Default::default(); - let fm = cm.new_source_file(Arc::new(FileName::Anon), source.clone()); + let fm = cm.new_source_file(Arc::new(FileName::Anon), source); let result = parse_file_as_expr( &fm, Syntax::Es(EsSyntax::default()), @@ -38,7 +38,7 @@ pub fn eval_source( span.lo.0.saturating_sub(1) as usize, span.hi.0.saturating_sub(1) as usize, format!("{error_title} warning"), - format!("failed to parse {}", json!(source)), + format!("failed to parse {}", json!(fm.src.as_ref())), ) .with_severity(Severity::Warning), )); From 4b9777e29eb58fbf14c0b8c27543cf27a31881d5 Mon Sep 17 00:00:00 2001 From: Cong-Cong Date: Tue, 19 Nov 2024 11:13:17 +0800 Subject: [PATCH 2/5] perf: prevent to_json --- Cargo.lock | 3 +- Cargo.toml | 2 +- crates/rspack_loader_lightningcss/src/lib.rs | 43 ++++++++++++---- crates/rspack_loader_swc/src/lib.rs | 7 +-- .../src/ast/stringify.rs | 49 ++++++++++++++++--- crates/rspack_plugin_javascript/src/lib.rs | 3 +- .../rspack_plugin_swc_js_minimizer/src/lib.rs | 6 +-- 7 files changed, 83 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1d682236cabb..15a1eb3e2ac6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4613,8 +4613,7 @@ dependencies = [ [[package]] name = "rspack_sources" version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b603e464255d9f8032775b48e98266e01feb2702487a69790723a78dd837da53" +source = "git+https://github.com/web-infra-dev/rspack-sources.git?rev=22635a7c7c9b9cbb6a72c8d8c2879b1985e42cb7#22635a7c7c9b9cbb6a72c8d8c2879b1985e42cb7" dependencies = [ "dashmap 5.5.3", "dyn-clone", diff --git a/Cargo.toml b/Cargo.toml index a9a066fc487e..b3e5dcbe01ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,7 +53,7 @@ rayon = { version = "1.10.0" } regex = { version = "1.10.4" } ropey = "1.6.1" rspack_resolver = { version = "0.3.5", features = ["package_json_raw_json_api"] } -rspack_sources = { version = "=0.3.3" } +rspack_sources = { git = "https://github.com/web-infra-dev/rspack-sources.git", rev = "22635a7c7c9b9cbb6a72c8d8c2879b1985e42cb7" } rustc-hash = { version = "1.1.0" } serde = { version = "1.0.197" } serde_json = { version = "1.0.115" } diff --git a/crates/rspack_loader_lightningcss/src/lib.rs b/crates/rspack_loader_lightningcss/src/lib.rs index 49960a725b29..5b73e97fbcd6 100644 --- a/crates/rspack_loader_lightningcss/src/lib.rs +++ b/crates/rspack_loader_lightningcss/src/lib.rs @@ -9,7 +9,10 @@ use lightningcss::{ targets::{Features, Targets}, traits::IntoOwned, }; -use rspack_core::{rspack_sources::SourceMap, Loader, LoaderContext, RunnerContext}; +use rspack_core::{ + rspack_sources::{encode_mappings, Mapping, OriginalLocation, SourceMap}, + Loader, LoaderContext, RunnerContext, +}; use rspack_error::Result; use rspack_loader_runner::{Identifiable, Identifier}; use tokio::sync::Mutex; @@ -180,14 +183,36 @@ impl LightningCssLoader { .map_err(|_| rspack_error::error!("failed to generate css"))?; if enable_sourcemap { - let source_map = source_map - .to_json(None) - .map_err(|e| rspack_error::error!(e.to_string()))?; - - loader_context.finish_with(( - content.code, - SourceMap::from_json(&source_map).expect("should be able to generate source-map"), - )); + let mappings = encode_mappings(source_map.get_mappings().iter().map(|mapping| Mapping { + generated_line: mapping.generated_line, + generated_column: mapping.generated_column, + original: mapping.original.map(|original| OriginalLocation { + source_index: original.source, + original_line: original.original_line, + original_column: original.original_column, + name_index: original.name, + }), + })); + let rspack_source_map = SourceMap::new( + None, + mappings, + source_map + .get_sources() + .iter() + .map(|source| source.to_string().into()) + .collect::>(), + source_map + .get_sources_content() + .iter() + .map(|source| source.to_string().into()) + .collect::>(), + source_map + .get_names() + .iter() + .map(|source| source.to_string().into()) + .collect::>(), + ); + loader_context.finish_with((content.code, rspack_source_map)); } else { loader_context.finish_with(content.code); } diff --git a/crates/rspack_loader_swc/src/lib.rs b/crates/rspack_loader_swc/src/lib.rs index 8de18aac4c64..e303cbf0a908 100644 --- a/crates/rspack_loader_swc/src/lib.rs +++ b/crates/rspack_loader_swc/src/lib.rs @@ -9,7 +9,7 @@ use std::default::Default; use compiler::{IntoJsAst, SwcCompiler}; use options::SwcCompilerOptionsWithAdditional; pub use options::SwcLoaderJsOptions; -use rspack_core::{rspack_sources::SourceMap, Mode, RunnerContext}; +use rspack_core::{Mode, RunnerContext}; use rspack_error::{error, AnyhowError, Diagnostic, Result}; use rspack_loader_runner::{Identifiable, Identifier, Loader, LoaderContext}; use rspack_plugin_javascript::ast::{self, SourceMapConfig}; @@ -128,11 +128,6 @@ impl SwcLoader { } let ast = c.into_js_ast(program); let TransformOutput { code, map } = ast::stringify(&ast, codegen_options)?; - - let map = map - .map(|m| SourceMap::from_json(&m)) - .transpose() - .map_err(|e| error!(e.to_string()))?; loader_context.finish_with((code, map)); Ok(()) diff --git a/crates/rspack_plugin_javascript/src/ast/stringify.rs b/crates/rspack_plugin_javascript/src/ast/stringify.rs index bba70bc88e4c..2938653beb9c 100644 --- a/crates/rspack_plugin_javascript/src/ast/stringify.rs +++ b/crates/rspack_plugin_javascript/src/ast/stringify.rs @@ -1,6 +1,7 @@ use std::sync::Arc; use rspack_ast::javascript::Ast; +use rspack_core::rspack_sources::{self, encode_mappings, Mapping, OriginalLocation}; use rspack_error::{miette::IntoDiagnostic, Result}; use swc_core::base::config::JsMinifyFormatOptions; use swc_core::base::sourcemap; @@ -105,14 +106,46 @@ pub fn print( }; let map = if source_map_config.enable { - let mut buf = vec![]; - - source_map - .build_source_map_with_config(&src_map_buf, input_source_map, source_map_config) - .to_writer(&mut buf) - .unwrap_or_else(|e| panic!("{}", e.to_string())); - // SAFETY: This buffer is already sanitized - Some(unsafe { String::from_utf8_unchecked(buf) }) + let combined_source_map = + source_map.build_source_map_with_config(&src_map_buf, input_source_map, source_map_config); + // // SAFETY: This buffer is already sanitized + // Some(unsafe { String::from_utf8_unchecked(buf) }) + + let mappings = encode_mappings(combined_source_map.tokens().map(|token| Mapping { + generated_line: token.get_dst_line() + 1, + generated_column: token.get_dst_col(), + original: if token.has_source() { + Some(OriginalLocation { + source_index: token.get_src_id(), + original_line: token.get_src_line() + 1, + original_column: token.get_src_col(), + name_index: if token.has_name() { + Some(token.get_name_id()) + } else { + None + }, + }) + } else { + None + }, + })); + + Some(rspack_sources::SourceMap::new( + combined_source_map.get_file().map(|file| file.to_string()), + mappings, + combined_source_map + .sources() + .map(|source| source.to_string().into()) + .collect::>(), + combined_source_map + .source_contents() + .map(|source| source.unwrap_or_default().to_string().into()) + .collect::>(), + combined_source_map + .names() + .map(|source| source.to_string().into()) + .collect::>(), + )) } else { None }; diff --git a/crates/rspack_plugin_javascript/src/lib.rs b/crates/rspack_plugin_javascript/src/lib.rs index f131cd226a5f..b9a7fb506b11 100644 --- a/crates/rspack_plugin_javascript/src/lib.rs +++ b/crates/rspack_plugin_javascript/src/lib.rs @@ -15,6 +15,7 @@ pub mod utils; pub mod visitors; mod webpack_comment; pub use parser_plugin::*; +use rspack_core::rspack_sources::SourceMap; pub use crate::plugin::infer_async_modules_plugin::InferAsyncModulesPlugin; pub use crate::plugin::*; @@ -22,7 +23,7 @@ pub use crate::plugin::*; #[derive(Debug)] pub struct TransformOutput { pub code: String, - pub map: Option, + pub map: Option, } #[derive(Debug)] diff --git a/crates/rspack_plugin_swc_js_minimizer/src/lib.rs b/crates/rspack_plugin_swc_js_minimizer/src/lib.rs index 743d58f1b60f..a4942555d187 100644 --- a/crates/rspack_plugin_swc_js_minimizer/src/lib.rs +++ b/crates/rspack_plugin_swc_js_minimizer/src/lib.rs @@ -11,7 +11,7 @@ use cow_utils::CowUtils; use once_cell::sync::OnceCell; use rayon::prelude::*; use regex::Regex; -use rspack_core::rspack_sources::{ConcatSource, MapOptions, RawSource, SourceExt, SourceMap}; +use rspack_core::rspack_sources::{ConcatSource, MapOptions, RawSource, SourceExt}; use rspack_core::rspack_sources::{Source, SourceMapSource, SourceMapSourceOptions}; use rspack_core::{ AssetInfo, ChunkUkey, Compilation, CompilationAsset, CompilationParams, CompilationProcessAssets, @@ -241,11 +241,11 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> { return Ok(()) } }; - let source = if let Some(map) = &output.map { + let source = if let Some(source_map) = output.map { SourceMapSource::new(SourceMapSourceOptions { value: output.code, name: filename, - source_map: SourceMap::from_json(map).expect("should be able to generate source-map"), + source_map, original_source: None, inner_source_map: input_source_map, remove_original_source: true, From c6cc7d3f450ae7e0b5bc5a586c9c1f299961d064 Mon Sep 17 00:00:00 2001 From: Cong-Cong Date: Tue, 19 Nov 2024 11:29:01 +0800 Subject: [PATCH 3/5] fix --- crates/rspack_plugin_javascript/src/ast/stringify.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/crates/rspack_plugin_javascript/src/ast/stringify.rs b/crates/rspack_plugin_javascript/src/ast/stringify.rs index 2938653beb9c..f2dd3805799d 100644 --- a/crates/rspack_plugin_javascript/src/ast/stringify.rs +++ b/crates/rspack_plugin_javascript/src/ast/stringify.rs @@ -108,8 +108,6 @@ pub fn print( let map = if source_map_config.enable { let combined_source_map = source_map.build_source_map_with_config(&src_map_buf, input_source_map, source_map_config); - // // SAFETY: This buffer is already sanitized - // Some(unsafe { String::from_utf8_unchecked(buf) }) let mappings = encode_mappings(combined_source_map.tokens().map(|token| Mapping { generated_line: token.get_dst_line() + 1, From 2406dc6f9fd09a218034b3966b6e7d0529630f7a Mon Sep 17 00:00:00 2001 From: Cong-Cong Date: Tue, 19 Nov 2024 19:17:00 +0800 Subject: [PATCH 4/5] feat: use 0.3.4 --- Cargo.lock | 5 +++-- Cargo.toml | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 15a1eb3e2ac6..d4dd75fd2b88 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4612,8 +4612,9 @@ dependencies = [ [[package]] name = "rspack_sources" -version = "0.3.3" -source = "git+https://github.com/web-infra-dev/rspack-sources.git?rev=22635a7c7c9b9cbb6a72c8d8c2879b1985e42cb7#22635a7c7c9b9cbb6a72c8d8c2879b1985e42cb7" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b656e78a61cb22708c1a54466537edd87f4d3a8592cfa267d0a45f386149a2fc" dependencies = [ "dashmap 5.5.3", "dyn-clone", diff --git a/Cargo.toml b/Cargo.toml index b3e5dcbe01ba..9be884035c23 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,7 +53,7 @@ rayon = { version = "1.10.0" } regex = { version = "1.10.4" } ropey = "1.6.1" rspack_resolver = { version = "0.3.5", features = ["package_json_raw_json_api"] } -rspack_sources = { git = "https://github.com/web-infra-dev/rspack-sources.git", rev = "22635a7c7c9b9cbb6a72c8d8c2879b1985e42cb7" } +rspack_sources = { version = "0.3.4" } rustc-hash = { version = "1.1.0" } serde = { version = "1.0.197" } serde_json = { version = "1.0.115" } From 39510cfaed78a65e141848c45204b2fce7bb2e67 Mon Sep 17 00:00:00 2001 From: Cong-Cong Date: Tue, 19 Nov 2024 20:06:13 +0800 Subject: [PATCH 5/5] pin version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 9be884035c23..240496c559f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,7 +53,7 @@ rayon = { version = "1.10.0" } regex = { version = "1.10.4" } ropey = "1.6.1" rspack_resolver = { version = "0.3.5", features = ["package_json_raw_json_api"] } -rspack_sources = { version = "0.3.4" } +rspack_sources = { version = "=0.3.4" } rustc-hash = { version = "1.1.0" } serde = { version = "1.0.197" } serde_json = { version = "1.0.115" }