Skip to content

Commit d5a6535

Browse files
committed
avoid celling source maps
1 parent fa6b98e commit d5a6535

File tree

5 files changed

+35
-32
lines changed

5 files changed

+35
-32
lines changed

turbopack/crates/turbopack-core/src/code_builder.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,23 @@ impl GenerateSourceMap for Code {
151151
/// chunk items into a single map file.
152152
#[turbo_tasks::function]
153153
pub async fn generate_source_map(&self) -> Result<Vc<OptionStringifiedSourceMap>> {
154+
Ok(Vc::cell(Some(self.generate_source_map_ref()?)))
155+
}
156+
}
157+
158+
#[turbo_tasks::value_impl]
159+
impl Code {
160+
/// Returns the hash of the source code of this Code.
161+
#[turbo_tasks::function]
162+
pub fn source_code_hash(&self) -> Vc<u64> {
163+
let code = self;
164+
let hash = hash_xxh3_hash64(code.source_code());
165+
Vc::cell(hash)
166+
}
167+
}
168+
169+
impl Code {
170+
pub fn generate_source_map_ref(&self) -> Result<Rope> {
154171
let mut pos = SourcePos::new();
155172
let mut last_byte_pos = 0;
156173

@@ -173,18 +190,6 @@ impl GenerateSourceMap for Code {
173190
sections.push((pos, map.clone().unwrap_or_else(SourceMap::empty_rope)))
174191
}
175192

176-
let source_map = SourceMap::sections_to_rope(sections)?;
177-
Ok(Vc::cell(Some(source_map)))
178-
}
179-
}
180-
181-
#[turbo_tasks::value_impl]
182-
impl Code {
183-
/// Returns the hash of the source code of this Code.
184-
#[turbo_tasks::function]
185-
pub fn source_code_hash(&self) -> Vc<u64> {
186-
let code = self;
187-
let hash = hash_xxh3_hash64(code.source_code());
188-
Vc::cell(hash)
193+
SourceMap::sections_to_rope(sections)
189194
}
190195
}

turbopack/crates/turbopack-css/src/module_asset.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use turbopack_core::{
1919
reference_type::{CssReferenceSubType, ReferenceType},
2020
resolve::{origin::ResolveOrigin, parse::Request},
2121
source::Source,
22-
source_map::OptionStringifiedSourceMap,
2322
};
2423
use turbopack_ecmascript::{
2524
chunk::{
@@ -436,12 +435,7 @@ async fn generate_minimal_source_map(filename: String, source: String) -> Result
436435
}
437436
let sm: Arc<SourceMap> = Default::default();
438437
sm.new_source_file(FileName::Custom(filename).into(), source);
439-
let map = generate_js_source_map(
440-
sm,
441-
mappings,
442-
OptionStringifiedSourceMap::none().to_resolved().await?,
443-
)
444-
.await?;
438+
let map = generate_js_source_map(sm, mappings, None).await?;
445439
Ok(map)
446440
}
447441

turbopack/crates/turbopack-ecmascript/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -966,8 +966,12 @@ async fn gen_content_with_code_gens(
966966

967967
let source_map = if generate_source_map {
968968
Some(
969-
generate_js_source_map(source_map.clone(), mappings, original_source_map)
970-
.await?,
969+
generate_js_source_map(
970+
source_map.clone(),
971+
mappings,
972+
original_source_map.await?.as_ref(),
973+
)
974+
.await?,
971975
)
972976
} else {
973977
None

turbopack/crates/turbopack-ecmascript/src/minify.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ use swc_core::{
2121
};
2222
use turbo_tasks::Vc;
2323
use turbo_tasks_fs::FileSystemPath;
24-
use turbopack_core::{
25-
code_builder::{Code, CodeBuilder},
26-
source_map::GenerateSourceMap,
27-
};
24+
use turbopack_core::code_builder::{Code, CodeBuilder};
2825

2926
use crate::parse::generate_js_source_map;
3027

@@ -36,8 +33,11 @@ pub async fn minify(
3633
mangle: bool,
3734
) -> Result<Vc<Code>> {
3835
let path = path.await?;
39-
let source_maps = source_maps.await?.then(|| code.generate_source_map());
4036
let code = code.await?;
37+
let source_maps = source_maps
38+
.await?
39+
.then(|| code.generate_source_map_ref())
40+
.transpose()?;
4141

4242
let cm = Arc::new(SwcSourceMap::new(FilePathMapping::empty()));
4343
let (src, mut src_map_buf) = {
@@ -117,11 +117,11 @@ pub async fn minify(
117117
};
118118

119119
let mut builder = CodeBuilder::default();
120-
if let Some(original_map) = source_maps {
120+
if let Some(original_map) = source_maps.as_ref() {
121121
src_map_buf.shrink_to_fit();
122122
builder.push_source(
123123
&src.into(),
124-
Some(generate_js_source_map(cm, src_map_buf, original_map.to_resolved().await?).await?),
124+
Some(generate_js_source_map(cm, src_map_buf, Some(original_map)).await?),
125125
);
126126

127127
write!(

turbopack/crates/turbopack-ecmascript/src/parse.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use turbopack_core::{
3030
error::PrettyPrintError,
3131
issue::{Issue, IssueExt, IssueSeverity, IssueStage, OptionStyledString, StyledString},
3232
source::Source,
33-
source_map::{utils::add_default_ignore_list, OptionStringifiedSourceMap},
33+
source_map::utils::add_default_ignore_list,
3434
SOURCE_URL_PROTOCOL,
3535
};
3636
use turbopack_swc_utils::emitter::IssueEmitter;
@@ -77,9 +77,9 @@ impl PartialEq for ParseResult {
7777
pub async fn generate_js_source_map(
7878
files_map: Arc<swc_core::common::SourceMap>,
7979
mappings: Vec<(BytePos, LineCol)>,
80-
original_source_map: ResolvedVc<OptionStringifiedSourceMap>,
80+
original_source_map: Option<&Rope>,
8181
) -> Result<Rope> {
82-
let input_map = if let Some(original_source_map) = &*original_source_map.await? {
82+
let input_map = if let Some(original_source_map) = original_source_map {
8383
Some(match sourcemap::decode(original_source_map.read())? {
8484
sourcemap::DecodedMap::Regular(source_map) => source_map,
8585
// swc only accepts flattened sourcemaps as input

0 commit comments

Comments
 (0)