Unverified Commit 1ba2577a authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Turbopack: avoid celling source maps before minify (#76626)

### What?

Avoid using a turbo-tasks function for generating the input source map so the source map is not persistently stored in a cell.
parent 902e5eb9
......@@ -151,6 +151,23 @@ impl GenerateSourceMap for Code {
/// chunk items into a single map file.
#[turbo_tasks::function]
pub async fn generate_source_map(&self) -> Result<Vc<OptionStringifiedSourceMap>> {
Ok(Vc::cell(Some(self.generate_source_map_ref()?)))
}
}
#[turbo_tasks::value_impl]
impl Code {
/// Returns the hash of the source code of this Code.
#[turbo_tasks::function]
pub fn source_code_hash(&self) -> Vc<u64> {
let code = self;
let hash = hash_xxh3_hash64(code.source_code());
Vc::cell(hash)
}
}
impl Code {
pub fn generate_source_map_ref(&self) -> Result<Rope> {
let mut pos = SourcePos::new();
let mut last_byte_pos = 0;
......@@ -173,18 +190,6 @@ impl GenerateSourceMap for Code {
sections.push((pos, map.clone().unwrap_or_else(SourceMap::empty_rope)))
}
let source_map = SourceMap::sections_to_rope(sections)?;
Ok(Vc::cell(Some(source_map)))
}
}
#[turbo_tasks::value_impl]
impl Code {
/// Returns the hash of the source code of this Code.
#[turbo_tasks::function]
pub fn source_code_hash(&self) -> Vc<u64> {
let code = self;
let hash = hash_xxh3_hash64(code.source_code());
Vc::cell(hash)
SourceMap::sections_to_rope(sections)
}
}
......@@ -19,7 +19,6 @@ use turbopack_core::{
reference_type::{CssReferenceSubType, ReferenceType},
resolve::{origin::ResolveOrigin, parse::Request},
source::Source,
source_map::OptionStringifiedSourceMap,
};
use turbopack_ecmascript::{
chunk::{
......@@ -436,12 +435,7 @@ async fn generate_minimal_source_map(filename: String, source: String) -> Result
}
let sm: Arc<SourceMap> = Default::default();
sm.new_source_file(FileName::Custom(filename).into(), source);
let map = generate_js_source_map(
sm,
mappings,
OptionStringifiedSourceMap::none().to_resolved().await?,
)
.await?;
let map = generate_js_source_map(sm, mappings, None).await?;
Ok(map)
}
......
......@@ -966,8 +966,12 @@ async fn gen_content_with_code_gens(
let source_map = if generate_source_map {
Some(
generate_js_source_map(source_map.clone(), mappings, original_source_map)
.await?,
generate_js_source_map(
source_map.clone(),
mappings,
original_source_map.await?.as_ref(),
)
.await?,
)
} else {
None
......
......@@ -21,10 +21,7 @@ use swc_core::{
};
use turbo_tasks::Vc;
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{
code_builder::{Code, CodeBuilder},
source_map::GenerateSourceMap,
};
use turbopack_core::code_builder::{Code, CodeBuilder};
use crate::parse::generate_js_source_map;
......@@ -36,8 +33,11 @@ pub async fn minify(
mangle: bool,
) -> Result<Vc<Code>> {
let path = path.await?;
let source_maps = source_maps.await?.then(|| code.generate_source_map());
let code = code.await?;
let source_maps = source_maps
.await?
.then(|| code.generate_source_map_ref())
.transpose()?;
let cm = Arc::new(SwcSourceMap::new(FilePathMapping::empty()));
let (src, mut src_map_buf) = {
......@@ -117,11 +117,11 @@ pub async fn minify(
};
let mut builder = CodeBuilder::default();
if let Some(original_map) = source_maps {
if let Some(original_map) = source_maps.as_ref() {
src_map_buf.shrink_to_fit();
builder.push_source(
&src.into(),
Some(generate_js_source_map(cm, src_map_buf, original_map.to_resolved().await?).await?),
Some(generate_js_source_map(cm, src_map_buf, Some(original_map)).await?),
);
write!(
......
......@@ -30,7 +30,7 @@ use turbopack_core::{
error::PrettyPrintError,
issue::{Issue, IssueExt, IssueSeverity, IssueStage, OptionStyledString, StyledString},
source::Source,
source_map::{utils::add_default_ignore_list, OptionStringifiedSourceMap},
source_map::utils::add_default_ignore_list,
SOURCE_URL_PROTOCOL,
};
use turbopack_swc_utils::emitter::IssueEmitter;
......@@ -77,9 +77,9 @@ impl PartialEq for ParseResult {
pub async fn generate_js_source_map(
files_map: Arc<swc_core::common::SourceMap>,
mappings: Vec<(BytePos, LineCol)>,
original_source_map: ResolvedVc<OptionStringifiedSourceMap>,
original_source_map: Option<&Rope>,
) -> Result<Rope> {
let input_map = if let Some(original_source_map) = &*original_source_map.await? {
let input_map = if let Some(original_source_map) = original_source_map {
Some(match sourcemap::decode(original_source_map.read())? {
sourcemap::DecodedMap::Regular(source_map) => source_map,
// swc only accepts flattened sourcemaps as input
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment