Unverified Commit e0f676b6 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: sourcemaps for JSON modules (#84611)

Closes PACK-5637

Generate sourcemaps for JSON modules, which better attributes bundle sizes in tools such as source-map-explorer

(These should never show up in stack traces)

<img width="1509" height="502" alt="Bildschirmfoto 2025-10-07 um 17 03 00" src="https://github.com/user-attachments/assets/0c41bade-36cd-417a-b32d-f5cc3dfe3a7a" />

<img width="374" height="470" alt="Bildschirmfoto 2025-10-07 um 15 01 14" src="https://github.com/user-attachments/assets/a93d322e-cca2-4624-ba6c-d23febc3fa87" />
parent 642ae3e4
......@@ -18,6 +18,7 @@ use turbo_tasks_fs::{FileContent, FileJsonContent, glob::Glob};
use turbopack_core::{
asset::{Asset, AssetContent},
chunk::{ChunkItem, ChunkType, ChunkableModule, ChunkingContext},
code_builder::CodeBuilder,
ident::AssetIdent,
module::Module,
module_graph::ModuleGraph,
......@@ -130,7 +131,10 @@ impl EcmascriptChunkItem for JsonChunkItem {
match &*data {
FileJsonContent::Content(data) => {
let data_str = data.to_string();
let inner_code = if data_str.len() > 10_000 {
let mut code = CodeBuilder::default();
let source_code = if data_str.len() > 10_000 {
// Only use JSON.parse if the content is larger than 10kb
// https://v8.dev/blog/cost-of-javascript-2019#json
let js_str_content = serde_json::to_string(&data_str)?;
......@@ -138,8 +142,30 @@ impl EcmascriptChunkItem for JsonChunkItem {
} else {
format!("{TURBOPACK_EXPORT_VALUE}({data_str});")
};
let source_code = source_code.into();
let source_map = serde_json::json!({
"version": 3,
// TODO: Encode using `urlencoding`, so that these
// are valid URLs. However, `project_trace_source_operation` (and
// `uri_from_file`) need to handle percent encoding correctly first.
//
// See turbopack/crates/turbopack-core/src/source_map/utils.rs as well
"sources": [format!("turbopack:///{}", self.module.ident().path().to_string().await?)],
"sourcesContent": [&data_str],
"names": [],
// Maps 0:0 in the output code to 0:0 in the `source_code`. Sufficient for
// bundle analyzers to attribute the bytes in the output chunks
"mappings": "AAAA",
})
.to_string()
.into();
code.push_source(&source_code, Some(source_map));
let code = code.build();
Ok(EcmascriptChunkItemContent {
inner_code: inner_code.into(),
source_map: Some(code.generate_source_map_ref(None)),
inner_code: code.into_source_code(),
..Default::default()
}
.into())
......
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