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: source map #7647

Merged
merged 5 commits into from
Aug 29, 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
17 changes: 3 additions & 14 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 @@ -47,7 +47,7 @@ proc-macro2 = { version = "1.0.79" }
quote = { version = "1.0.35" }
rayon = { version = "1.10.0" }
regex = { version = "1.10.4" }
rspack_sources = { version = "=0.2.17" }
rspack_sources = { version = "=0.3.0" }
rustc-hash = { version = "1.1.0" }
schemars = { version = "0.8.16" }
serde = { version = "1.0.197" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ impl From<RawCopyPattern> for CopyPattern {

fn convert_to_enum(input: Either<String, Buffer>) -> RawSource {
match input {
Either::A(s) => RawSource::Source(s),
Either::B(b) => RawSource::Buffer(b.to_vec()),
Either::A(s) => RawSource::from(s),
Either::B(b) => RawSource::from(b.to_vec()),
}
}

Expand Down
52 changes: 32 additions & 20 deletions crates/rspack_binding_values/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ pub struct JsCompatSource {
pub struct CompatSource {
pub is_raw: bool,
pub is_buffer: bool,
pub source: Vec<u8>,
pub map: Option<Vec<u8>>,
pub buffer: Vec<u8>,
pub value_as_string: String,
pub map: Option<SourceMap>,
}

impl FromNapiValue for CompatSource {
Expand All @@ -38,7 +39,7 @@ impl std::hash::Hash for CompatSource {
"__CompatSource".hash(state);
self.is_raw.hash(state);
self.is_buffer.hash(state);
self.source.hash(state);
self.buffer.hash(state);
self.map.hash(state);
}
}
Expand All @@ -47,57 +48,68 @@ impl PartialEq for CompatSource {
fn eq(&self, other: &Self) -> bool {
self.is_raw == other.is_raw
&& self.is_buffer == other.is_buffer
&& self.source == other.source
&& self.buffer == other.buffer
&& self.map == other.map
}
}

impl From<JsCompatSource> for CompatSource {
fn from(source: JsCompatSource) -> Self {
let buffer = source.source.to_vec();
let map = source
.map
.as_ref()
.and_then(|m| SourceMap::from_slice(m).ok());
let value_as_string = String::from_utf8_lossy(&buffer).to_string();
Self {
is_raw: source.is_raw,
is_buffer: source.is_buffer,
source: source.source.into(),
map: source.map.map(Into::into),
buffer,
value_as_string,
map,
}
}
}

impl StreamChunks for CompatSource {
impl<'a> StreamChunks<'a> for CompatSource {
fn stream_chunks(
&self,
&'a self,
options: &MapOptions,
on_chunk: OnChunk,
on_source: OnSource,
on_name: OnName,
on_chunk: OnChunk<'_, 'a>,
on_source: OnSource<'_, 'a>,
on_name: OnName<'_, 'a>,
) -> GeneratedInfo {
stream_chunks_default(self, options, on_chunk, on_source, on_name)
stream_chunks_default(
&self.value_as_string,
self.map.as_ref(),
options,
on_chunk,
on_source,
on_name,
)
}
}

impl Source for CompatSource {
fn source(&self) -> Cow<str> {
// Use UTF-8 lossy for any sources, including `RawSource` as a workaround for not supporting either `Buffer` or `String` in `Source`.
String::from_utf8_lossy(&self.source)
Cow::Borrowed(&self.value_as_string)
}

fn buffer(&self) -> Cow<[u8]> {
Cow::Borrowed(self.source.as_ref())
Cow::Borrowed(&self.buffer)
}

fn size(&self) -> usize {
self.source.len()
self.buffer.len()
}

fn map(&self, _options: &MapOptions) -> Option<SourceMap> {
self
.map
.as_ref()
.and_then(|m| SourceMap::from_slice(m).ok())
self.map.clone()
}

fn to_writer(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()> {
writer.write_all(&self.source)
writer.write_all(&self.buffer)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/normal_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ impl Diagnosable for NormalModule {
impl NormalModule {
fn create_source(&self, content: Content, source_map: Option<SourceMap>) -> Result<BoxSource> {
if content.is_buffer() {
return Ok(RawSource::Buffer(content.into_bytes()).boxed());
return Ok(RawSource::from(content.into_bytes()).boxed());
}
let source_map_kind = self.get_source_map_kind();
if source_map_kind.enabled()
Expand Down
16 changes: 5 additions & 11 deletions crates/rspack_plugin_copy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ use futures::future::BoxFuture;
use glob::{MatchOptions, Pattern as GlobPattern};
use regex::Regex;
use rspack_core::{
rspack_sources::RawSource, AssetInfo, AssetInfoRelated, Compilation, CompilationAsset,
CompilationLogger, CompilationProcessAssets, FilenameTemplate, Logger, PathData, Plugin,
rspack_sources::{RawSource, Source},
AssetInfo, AssetInfoRelated, Compilation, CompilationAsset, CompilationLogger,
CompilationProcessAssets, FilenameTemplate, Logger, PathData, Plugin,
};
use rspack_error::{Diagnostic, DiagnosticError, Error, ErrorExt, Result};
use rspack_hash::{HashDigest, HashFunction, HashSalt, RspackHash, RspackHashDigest};
Expand Down Expand Up @@ -148,14 +149,7 @@ impl CopyRspackPlugin {
salt: &HashSalt,
) -> RspackHashDigest {
let mut hasher = RspackHash::with_salt(function, salt);
match &source {
RawSource::Buffer(buffer) => {
buffer.hash(&mut hasher);
}
RawSource::Source(source) => {
source.hash(&mut hasher);
}
}
source.buffer().hash(&mut hasher);
hasher.digest(digest)
}

Expand Down Expand Up @@ -290,7 +284,7 @@ impl CopyRspackPlugin {
}
};

let mut source = RawSource::Buffer(source_vec.clone());
let mut source = RawSource::from(source_vec.clone());

if let Some(transform) = &pattern.transform {
match transform {
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_hmr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> {
filename,
CompilationAsset::new(
Some(
RawSource::Source(
RawSource::from(
serde_json::json!({
"c": c,
"r": r,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,18 @@ fn render_chunk(
let mut source = ConcatSource::default();

if matches!(chunk.kind, ChunkKind::HotUpdate) {
source.add(RawSource::Source(format!(
source.add(RawSource::from(format!(
"{}[{}]('{}', ",
global_object,
serde_json::to_string(hot_update_global).map_err(|e| error!(e.to_string()))?,
chunk.expect_id()
)));
source.add(render_source.source.clone());
if has_runtime_modules {
source.add(RawSource::Source(",".to_string()));
source.add(RawSource::from(",".to_string()));
source.add(render_chunk_runtime_modules(compilation, chunk_ukey)?);
}
source.add(RawSource::Source(")".to_string()));
source.add(RawSource::from(")".to_string()));
} else {
let chunk_loading_global = &compilation.options.output.chunk_loading_global;

Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_swc_js_minimizer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> {
.contains_key(filename)
{
ConcatSource::new([
RawSource::Source(banner).boxed(),
RawSource::from(banner).boxed(),
RawSource::from("\n").boxed(),
source
]).boxed()
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_swc_js_minimizer/src/minify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ pub fn minify(
.insert(
filename.to_string(),
ExtractedCommentsInfo {
source: RawSource::Source(extracted_comments.join("\n\n")).boxed(),
source: RawSource::from(extracted_comments.join("\n\n")).boxed(),
comments_file_name: extract_comments.filename.to_string(),
},
);
Expand Down
Loading