Skip to content
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
30 changes: 28 additions & 2 deletions turbopack/crates/turbopack-json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -130,16 +131,41 @@ 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)?;
format!("{TURBOPACK_EXPORT_VALUE}(JSON.parse({js_str_content}));")
} 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?)],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is uri escaping necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a comment

we don't do this is (yet) in the other place where these turbopack:// urls are generated, bgw had tried to improve this in #84362

"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())
Expand Down

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

Loading