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

perf: reduce source map json stringify #8468

Merged
merged 5 commits into from
Nov 20, 2024
Merged
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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = { version = "=0.3.4" }
rustc-hash = { version = "1.1.0" }
serde = { version = "1.0.197" }
serde_json = { version = "1.0.115" }
Expand Down
43 changes: 34 additions & 9 deletions crates/rspack_loader_lightningcss/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<Vec<_>>(),
source_map
.get_sources_content()
.iter()
.map(|source| source.to_string().into())
.collect::<Vec<_>>(),
source_map
.get_names()
.iter()
.map(|source| source.to_string().into())
.collect::<Vec<_>>(),
);
loader_context.finish_with((content.code, rspack_source_map));
} else {
loader_context.finish_with(content.code);
}
Expand Down
15 changes: 3 additions & 12 deletions crates/rspack_loader_swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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, |_| {
Expand Down Expand Up @@ -132,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(())
Expand Down
47 changes: 39 additions & 8 deletions crates/rspack_plugin_javascript/src/ast/stringify.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -105,14 +106,44 @@ 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);

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::<Vec<_>>(),
combined_source_map
.source_contents()
.map(|source| source.unwrap_or_default().to_string().into())
.collect::<Vec<_>>(),
combined_source_map
.names()
.map(|source| source.to_string().into())
.collect::<Vec<_>>(),
))
} else {
None
};
Expand Down
3 changes: 2 additions & 1 deletion crates/rspack_plugin_javascript/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ 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::*;

#[derive(Debug)]
pub struct TransformOutput {
pub code: String,
pub map: Option<String>,
pub map: Option<SourceMap>,
}

#[derive(Debug)]
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_plugin_javascript/src/utils/eval/eval_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn eval_source<T: Display>(
error_title: T,
) -> Option<BasicEvaluatedExpression> {
let cm: Arc<swc_core::common::SourceMap> = 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()),
Expand All @@ -38,7 +38,7 @@ pub fn eval_source<T: Display>(
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),
));
Expand Down
6 changes: 3 additions & 3 deletions crates/rspack_plugin_swc_js_minimizer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Loading