Before

1use std::io::Write;
2
3use anyhow::Result;
4use indoc::writedoc;
5use turbo_rcstr::{RcStr, rcstr};
6use turbo_tasks::{ResolvedVc, Vc};
7use turbopack_core::{
8 code_builder::{Code, CodeBuilder},
9 context::AssetContext,
10 environment::{ChunkLoading, Environment},
11};
12use turbopack_ecmascript::utils::StringifyJs;
13
14use crate::{
15 ChunkSuffix, RuntimeType, asset_context::get_runtime_asset_context, embed_js::embed_static_code,
16};
17
18/// Returns the code for the ECMAScript runtime.
19#[turbo_tasks::function]
20pub async fn get_browser_runtime_code(
21 environment: ResolvedVc<Environment>,
22 chunk_base_path: Vc<Option<RcStr>>,
23 chunk_suffix: Vc<ChunkSuffix>,
24 runtime_type: RuntimeType,
25 output_root_to_root_path: RcStr,
26 generate_source_map: bool,
27) -> Result<Vc<Code>> {
28 let asset_context = get_runtime_asset_context(*environment).resolve().await?;
29
…lines 30–78 unchanged (49 lines)
79 };
80
81 let mut code: CodeBuilder = CodeBuilder::default();
82 let relative_root_path = output_root_to_root_path;
83 let chunk_base_path = chunk_base_path.await?;
84 let chunk_base_path = chunk_base_path.as_ref().map_or_else(|| "", |f| f.as_str());
85 let chunk_suffix = chunk_suffix.await?;
86
87 writedoc!(
88 code,
89 r#"
90 (() => {{
91 if (!Array.isArray(globalThis.TURBOPACK)) {{
…lines 92–97 unchanged (6 lines)
98 "#,
99 StringifyJs(chunk_base_path),
100 StringifyJs(relative_root_path.as_str()),
101 StringifyJs(chunk_base_path),
102 )?;
103
104 match &*chunk_suffix {
105 ChunkSuffix::None => {
106 writedoc!(
107 code,
108 r#"
109 const CHUNK_SUFFIX = "";
110 "#
111 )?;
112 }
113 ChunkSuffix::Constant(suffix) => {
114 writedoc!(
115 code,
116 r#"
117 const CHUNK_SUFFIX = {};
118 "#,
119 StringifyJs(suffix.as_str())
120 )?;
121 }
122 ChunkSuffix::FromScriptSrc => {
123 if chunk_loading == &ChunkLoading::Edge {
124 panic!("ChunkSuffix::FromScriptSrc is not supported in Edge runtimes");
125 }
126 writedoc!(
127 code,
128 r#"
129 const CHUNK_SUFFIX = getChunkSuffixFromScriptSrc();
130 "#
131 )?;
132 }
133 }
134
135 code.push_code(&*shared_runtime_utils_code.await?);
136 for runtime_code in runtime_base_code {
137 code.push_code(
138 &*embed_static_code(asset_context, runtime_code.into(), generate_source_map).await?,
139 );
…lines 140–220 unchanged (81 lines)

After

1use std::io::Write;
2
3use anyhow::Result;
4use indoc::writedoc;
5use turbo_rcstr::{RcStr, rcstr};
6use turbo_tasks::{ResolvedVc, Vc};
7use turbopack_core::{
8 chunk::AssetSuffix,
9 code_builder::{Code, CodeBuilder},
10 context::AssetContext,
11 environment::{ChunkLoading, Environment},
12};
13use turbopack_ecmascript::utils::StringifyJs;
14
15use crate::{RuntimeType, asset_context::get_runtime_asset_context, embed_js::embed_static_code};
16
17/// Returns the code for the ECMAScript runtime.
18#[turbo_tasks::function]
19pub async fn get_browser_runtime_code(
20 environment: ResolvedVc<Environment>,
21 chunk_base_path: Vc<Option<RcStr>>,
22 asset_suffix: Vc<AssetSuffix>,
23 runtime_type: RuntimeType,
24 output_root_to_root_path: RcStr,
25 generate_source_map: bool,
26) -> Result<Vc<Code>> {
27 let asset_context = get_runtime_asset_context(*environment).resolve().await?;
28
…lines 29–77 unchanged (49 lines)
78 };
79
80 let mut code: CodeBuilder = CodeBuilder::default();
81 let relative_root_path = output_root_to_root_path;
82 let chunk_base_path = chunk_base_path.await?;
83 let chunk_base_path = chunk_base_path.as_ref().map_or_else(|| "", |f| f.as_str());
84 let asset_suffix = asset_suffix.await?;
85
86 writedoc!(
87 code,
88 r#"
89 (() => {{
90 if (!Array.isArray(globalThis.TURBOPACK)) {{
…lines 91–96 unchanged (6 lines)
97 "#,
98 StringifyJs(chunk_base_path),
99 StringifyJs(relative_root_path.as_str()),
100 StringifyJs(chunk_base_path),
101 )?;
102
103 match &*asset_suffix {
104 AssetSuffix::None => {
105 writedoc!(
106 code,
107 r#"
108 const ASSET_SUFFIX = "";
109 "#
110 )?;
111 }
112 AssetSuffix::Constant(suffix) => {
113 writedoc!(
114 code,
115 r#"
116 const ASSET_SUFFIX = {};
117 "#,
118 StringifyJs(suffix.as_str())
119 )?;
120 }
121 AssetSuffix::Inferred => {
122 if chunk_loading == &ChunkLoading::Edge {
123 panic!("AssetSuffix::Inferred is not supported in Edge runtimes");
124 }
125 writedoc!(
126 code,
127 r#"
128 const ASSET_SUFFIX = getAssetSuffixFromScriptSrc();
129 "#
130 )?;
131 }
132 AssetSuffix::FromGlobal(global_name) => {
133 writedoc!(
134 code,
135 r#"
136 const ASSET_SUFFIX = globalThis[{}] || "";
137 "#,
138 StringifyJs(global_name)
139 )?;
140 }
141 }
142
143 code.push_code(&*shared_runtime_utils_code.await?);
144 for runtime_code in runtime_base_code {
145 code.push_code(
146 &*embed_static_code(asset_context, runtime_code.into(), generate_source_map).await?,
…lines 147–228 unchanged (82 lines)

Before

source_file
use_declaration
use "use"
scoped_identifier
scoped_identifier
identifier "std"
:: "::"
identifier "io"
:: "::"
identifier "Write"
; ";"
use_declaration
use "use"
scoped_identifier
identifier "anyhow"
:: "::"
identifier "Result"
; ";"
use_declaration
use "use"
scoped_identifier
identifier "indoc"
:: "::"
identifier "writedoc"
; ";"
use_declaration
use "use"
scoped_use_list
identifier "turbo_rcstr"
:: "::"
use_list
{ "{"
identifier "RcStr"
, ","
identifier "rcstr"
} "}"
; ";"
use_declaration
use "use"
scoped_use_list
identifier "turbo_tasks"
:: "::"
use_list
{ "{"
identifier "ResolvedVc"
, ","
identifier "Vc"
} "}"
; ";"
use_declaration
use "use"
scoped_use_list
identifier "turbopack_core"
:: "::"
use_list
{ "{"
scoped_use_list
identifier "code_builder"
:: "::"
use_list
{ "{"
identifier "Code"
, ","
identifier "CodeBuilder"
} "}"
, ","
scoped_identifier
identifier "context"
:: "::"
identifier "AssetContext"
, ","
scoped_use_list
identifier "environment"
:: "::"
use_list
{ "{"
identifier "ChunkLoading"
, ","
identifier "Environment"
} "}"
, ","
} "}"
; ";"
use_declaration
use "use"
scoped_identifier
scoped_identifier
identifier "turbopack_ecmascript"
:: "::"
identifier "utils"
:: "::"
identifier "StringifyJs"
; ";"
use_declaration
use "use"
scoped_use_list
crate "crate"
:: "::"
use_list
{ "{"
identifier "ChunkSuffix"
, ","
identifier "RuntimeType"
, ","
scoped_identifier
identifier "asset_context"
:: "::"
identifier "get_runtime_asset_context"
, ","
scoped_identifier
identifier "embed_js"
:: "::"
identifier "embed_static_code"
, ","
} "}"
; ";"
line_comment
// "//"
outer_doc_comment_marker
/ "/"
doc_comment " Returns the code for the ECMAScript runtime.\n"
attribute_item
# "#"
[ "["
attribute
scoped_identifier
identifier "turbo_tasks"
:: "::"
identifier "function"
] "]"
function_item
visibility_modifier
pub "pub"
function_modifiers
async "async"
fn "fn"
identifier "get_browser_runtime_code"
parameters
( "("
parameter
identifier "environment"
: ":"
generic_type
type_identifier "ResolvedVc"
type_arguments
< "<"
type_identifier "Environment"
> ">"
, ","
parameter
identifier "chunk_base_path"
: ":"
generic_type
type_identifier "Vc"
type_arguments
< "<"
generic_type
type_identifier "Option"
type_arguments
< "<"
type_identifier "RcStr"
> ">"
> ">"
, ","
parameter
identifier "chunk_suffix"
: ":"
generic_type
type_identifier "Vc"
type_arguments
< "<"
type_identifier "ChunkSuffix"
> ">"
, ","
parameter
identifier "runtime_type"
: ":"
type_identifier "RuntimeType"
, ","
parameter
identifier "output_root_to_root_path"
: ":"
type_identifier "RcStr"
, ","
parameter
identifier "generate_source_map"
: ":"
primitive_type "bool"
, ","
) ")"
-> "->"
generic_type
type_identifier "Result"
type_arguments
< "<"
generic_type
type_identifier "Vc"
type_arguments
< "<"
type_identifier "Code"
> ">"
> ">"
block
{ "{"
let_declaration (+25 nodes collapsed)
let_declaration (+25 nodes collapsed)
let_declaration
let "let"
mutable_specifier "mut"
identifier "runtime_base_code"
= "="
macro_invocation
identifier "vec"
! "!"
token_tree
[ "["
string_literal
" "\""
string_content "browser/runtime/base/runtime-base.ts"
" "\""
] "]"
; ";"
expression_statement (+86 nodes collapsed)
let_declaration (+36 nodes collapsed)
let_declaration
let "let"
mutable_specifier "mut"
identifier "runtime_backend_code"
= "="
macro_invocation
identifier "vec"
! "!"
token_tree
[ "["
] "]"
; ";"
expression_statement (+235 nodes collapsed)
empty_statement
; ";"
let_declaration
let "let"
mutable_specifier "mut"
identifier "code"
: ":"
type_identifier "CodeBuilder"
= "="
call_expression
scoped_identifier
identifier "CodeBuilder"
:: "::"
identifier "default"
arguments
( "("
) ")"
; ";"
let_declaration
let "let"
identifier "relative_root_path"
= "="
identifier "output_root_to_root_path"
; ";"
let_declaration
let "let"
identifier "chunk_base_path"
= "="
try_expression
await_expression
identifier "chunk_base_path"
. "."
await "await"
? "?"
; ";"
let_declaration (+41 nodes collapsed)
let_declaration
let "let"
identifier "chunk_suffix"
= "="
try_expression
await_expression
identifier "chunk_suffix"
. "."
await "await"
? "?"
; ";"
expression_statement (+38 nodes collapsed)
expression_statement
match_expression
match "match"
reference_expression
& "&"
unary_expression
* "*"
identifier "chunk_suffix"
match_block
{ "{"
match_arm
match_pattern
scoped_identifier
identifier "ChunkSuffix"
:: "::"
identifier "None"
=> "=>"
block
{ "{"
expression_statement
try_expression
macro_invocation
identifier "writedoc"
! "!"
token_tree
( "("
identifier "code"
, ","
raw_string_literal
string_content "const CHUNK_SUFFIX = \"\";\n "
) ")"
? "?"
; ";"
} "}"
match_arm
match_pattern
tuple_struct_pattern
scoped_identifier
identifier "ChunkSuffix"
:: "::"
identifier "Constant"
( "("
identifier "suffix"
) ")"
=> "=>"
block
{ "{"
expression_statement
try_expression
macro_invocation
identifier "writedoc"
! "!"
token_tree
( "("
identifier "code"
, ","
raw_string_literal
string_content "const CHUNK_SUFFIX = {};\n "
, ","
identifier "StringifyJs"
token_tree
( "("
identifier "suffix"
. "."
identifier "as_str"
token_tree
( "("
) ")"
) ")"
) ")"
? "?"
; ";"
} "}"
match_arm
match_pattern
scoped_identifier
identifier "ChunkSuffix"
:: "::"
identifier "FromScriptSrc"
=> "=>"
block
{ "{"
expression_statement
if_expression
if "if"
binary_expression
identifier "chunk_loading"
== "=="
reference_expression
& "&"
scoped_identifier
identifier "ChunkLoading"
:: "::"
identifier "Edge"
block
{ "{"
expression_statement
macro_invocation
identifier "panic"
! "!"
token_tree
( "("
string_literal
" "\""
string_content "ChunkSuffix::FromScriptSrc is not supported in Edge runtimes"
" "\""
) ")"
; ";"
} "}"
expression_statement
try_expression
macro_invocation
identifier "writedoc"
! "!"
token_tree
( "("
identifier "code"
, ","
raw_string_literal
string_content "const CHUNK_SUFFIX = getChunkSuffixFromScriptSrc();\n "...
) ")"
? "?"
; ";"
} "}"
} "}"
expression_statement
call_expression
field_expression
identifier "code"
. "."
field_identifier "push_code"
arguments
( "("
reference_expression
& "&"
unary_expression
* "*"
try_expression
await_expression
identifier "shared_runtime_utils_code"
. "."
await "await"
? "?"
) ")"
; ";"
expression_statement (+46 nodes collapsed)
expression_statement (+61 nodes collapsed)
expression_statement (+61 nodes collapsed)
expression_statement (+61 nodes collapsed)
expression_statement (+46 nodes collapsed)
line_comment
// "//"
line_comment
// "//"
expression_statement
try_expression
macro_invocation
identifier "writedoc"
! "!"
token_tree
( "("
identifier "code"
, ","
raw_string_literal
string_content "const chunksToRegister = globalThis.TURBOPACK;\n g"...
) ")"
? "?"
; ";"
expression_statement (+31 nodes collapsed)
expression_statement
try_expression
macro_invocation
identifier "writedoc"
! "!"
token_tree
( "("
identifier "code"
, ","
raw_string_literal
string_content "}})();\n "
) ")"
? "?"
; ";"
call_expression (+21 nodes collapsed)
} "}"
line_comment
// "//"
outer_doc_comment_marker
/ "/"
doc_comment " Returns the code for the ECMAScript worker entrypoint boots"...
function_item (+70 nodes collapsed)

After

source_file
use_declaration
use "use"
scoped_identifier
scoped_identifier
identifier "std"
:: "::"
identifier "io"
:: "::"
identifier "Write"
; ";"
use_declaration
use "use"
scoped_identifier
identifier "anyhow"
:: "::"
identifier "Result"
; ";"
use_declaration
use "use"
scoped_identifier
identifier "indoc"
:: "::"
identifier "writedoc"
; ";"
use_declaration
use "use"
scoped_use_list
identifier "turbo_rcstr"
:: "::"
use_list
{ "{"
identifier "RcStr"
, ","
identifier "rcstr"
} "}"
; ";"
use_declaration
use "use"
scoped_use_list
identifier "turbo_tasks"
:: "::"
use_list
{ "{"
identifier "ResolvedVc"
, ","
identifier "Vc"
} "}"
; ";"
use_declaration
use "use"
scoped_use_list
identifier "turbopack_core"
:: "::"
use_list
{ "{"
scoped_identifier
identifier "chunk"
:: "::"
identifier "AssetSuffix"
, ","
scoped_use_list
identifier "code_builder"
:: "::"
use_list
{ "{"
identifier "Code"
, ","
identifier "CodeBuilder"
} "}"
, ","
scoped_identifier
identifier "context"
:: "::"
identifier "AssetContext"
, ","
scoped_use_list
identifier "environment"
:: "::"
use_list
{ "{"
identifier "ChunkLoading"
, ","
identifier "Environment"
} "}"
, ","
} "}"
; ";"
use_declaration
use "use"
scoped_identifier
scoped_identifier
identifier "turbopack_ecmascript"
:: "::"
identifier "utils"
:: "::"
identifier "StringifyJs"
; ";"
use_declaration
use "use"
scoped_use_list
crate "crate"
:: "::"
use_list
{ "{"
identifier "RuntimeType"
, ","
scoped_identifier
identifier "asset_context"
:: "::"
identifier "get_runtime_asset_context"
, ","
scoped_identifier
identifier "embed_js"
:: "::"
identifier "embed_static_code"
} "}"
; ";"
line_comment
// "//"
outer_doc_comment_marker
/ "/"
doc_comment " Returns the code for the ECMAScript runtime.\n"
attribute_item
# "#"
[ "["
attribute
scoped_identifier
identifier "turbo_tasks"
:: "::"
identifier "function"
] "]"
function_item
visibility_modifier
pub "pub"
function_modifiers
async "async"
fn "fn"
identifier "get_browser_runtime_code"
parameters
( "("
parameter
identifier "environment"
: ":"
generic_type
type_identifier "ResolvedVc"
type_arguments
< "<"
type_identifier "Environment"
> ">"
, ","
parameter
identifier "chunk_base_path"
: ":"
generic_type
type_identifier "Vc"
type_arguments
< "<"
generic_type
type_identifier "Option"
type_arguments
< "<"
type_identifier "RcStr"
> ">"
> ">"
, ","
parameter
identifier "asset_suffix"
: ":"
generic_type
type_identifier "Vc"
type_arguments
< "<"
type_identifier "AssetSuffix"
> ">"
, ","
parameter
identifier "runtime_type"
: ":"
type_identifier "RuntimeType"
, ","
parameter
identifier "output_root_to_root_path"
: ":"
type_identifier "RcStr"
, ","
parameter
identifier "generate_source_map"
: ":"
primitive_type "bool"
, ","
) ")"
-> "->"
generic_type
type_identifier "Result"
type_arguments
< "<"
generic_type
type_identifier "Vc"
type_arguments
< "<"
type_identifier "Code"
> ">"
> ">"
block
{ "{"
let_declaration (+25 nodes collapsed)
let_declaration (+25 nodes collapsed)
let_declaration
let "let"
mutable_specifier "mut"
identifier "runtime_base_code"
= "="
macro_invocation
identifier "vec"
! "!"
token_tree
[ "["
string_literal
" "\""
string_content "browser/runtime/base/runtime-base.ts"
" "\""
] "]"
; ";"
expression_statement (+86 nodes collapsed)
let_declaration (+36 nodes collapsed)
let_declaration
let "let"
mutable_specifier "mut"
identifier "runtime_backend_code"
= "="
macro_invocation
identifier "vec"
! "!"
token_tree
[ "["
] "]"
; ";"
expression_statement (+235 nodes collapsed)
empty_statement
; ";"
let_declaration
let "let"
mutable_specifier "mut"
identifier "code"
: ":"
type_identifier "CodeBuilder"
= "="
call_expression
scoped_identifier
identifier "CodeBuilder"
:: "::"
identifier "default"
arguments
( "("
) ")"
; ";"
let_declaration
let "let"
identifier "relative_root_path"
= "="
identifier "output_root_to_root_path"
; ";"
let_declaration
let "let"
identifier "chunk_base_path"
= "="
try_expression
await_expression
identifier "chunk_base_path"
. "."
await "await"
? "?"
; ";"
let_declaration (+41 nodes collapsed)
let_declaration
let "let"
identifier "asset_suffix"
= "="
try_expression
await_expression
identifier "asset_suffix"
. "."
await "await"
? "?"
; ";"
expression_statement (+38 nodes collapsed)
expression_statement
match_expression
match "match"
reference_expression
& "&"
unary_expression
* "*"
identifier "asset_suffix"
match_block
{ "{"
match_arm
match_pattern
scoped_identifier
identifier "AssetSuffix"
:: "::"
identifier "None"
=> "=>"
block
{ "{"
expression_statement
try_expression
macro_invocation
identifier "writedoc"
! "!"
token_tree
( "("
identifier "code"
, ","
raw_string_literal
string_content "const ASSET_SUFFIX = \"\";\n "
) ")"
? "?"
; ";"
} "}"
match_arm
match_pattern
tuple_struct_pattern
scoped_identifier
identifier "AssetSuffix"
:: "::"
identifier "Constant"
( "("
identifier "suffix"
) ")"
=> "=>"
block
{ "{"
expression_statement
try_expression
macro_invocation
identifier "writedoc"
! "!"
token_tree
( "("
identifier "code"
, ","
raw_string_literal
string_content "const ASSET_SUFFIX = {};\n "
, ","
identifier "StringifyJs"
token_tree
( "("
identifier "suffix"
. "."
identifier "as_str"
token_tree
( "("
) ")"
) ")"
) ")"
? "?"
; ";"
} "}"
match_arm
match_pattern
scoped_identifier
identifier "AssetSuffix"
:: "::"
identifier "Inferred"
=> "=>"
block
{ "{"
expression_statement
if_expression
if "if"
binary_expression
identifier "chunk_loading"
== "=="
reference_expression
& "&"
scoped_identifier
identifier "ChunkLoading"
:: "::"
identifier "Edge"
block
{ "{"
expression_statement
macro_invocation
identifier "panic"
! "!"
token_tree
( "("
string_literal
" "\""
string_content "AssetSuffix::Inferred is not supported in Edge runtimes"
" "\""
) ")"
; ";"
} "}"
expression_statement
try_expression
macro_invocation
identifier "writedoc"
! "!"
token_tree
( "("
identifier "code"
, ","
raw_string_literal
string_content "const ASSET_SUFFIX = getAssetSuffixFromScriptSrc();\n "...
) ")"
? "?"
; ";"
} "}"
match_arm
match_pattern
tuple_struct_pattern
scoped_identifier
identifier "AssetSuffix"
:: "::"
identifier "FromGlobal"
( "("
identifier "global_name"
) ")"
=> "=>"
block
{ "{"
expression_statement
try_expression
macro_invocation
identifier "writedoc"
! "!"
token_tree
( "("
identifier "code"
, ","
raw_string_literal
string_content "const ASSET_SUFFIX = globalThis[{}] || \"\";\n "
, ","
identifier "StringifyJs"
token_tree
( "("
identifier "global_name"
) ")"
) ")"
? "?"
; ";"
} "}"
} "}"
expression_statement
call_expression
field_expression
identifier "code"
. "."
field_identifier "push_code"
arguments
( "("
reference_expression
& "&"
unary_expression
* "*"
try_expression
await_expression
identifier "shared_runtime_utils_code"
. "."
await "await"
? "?"
) ")"
; ";"
expression_statement (+46 nodes collapsed)
expression_statement (+61 nodes collapsed)
expression_statement (+61 nodes collapsed)
expression_statement (+61 nodes collapsed)
expression_statement (+46 nodes collapsed)
line_comment
// "//"
line_comment
// "//"
expression_statement
try_expression
macro_invocation
identifier "writedoc"
! "!"
token_tree
( "("
identifier "code"
, ","
raw_string_literal
string_content "const chunksToRegister = globalThis.TURBOPACK;\n g"...
) ")"
? "?"
; ";"
expression_statement (+31 nodes collapsed)
expression_statement
try_expression
macro_invocation
identifier "writedoc"
! "!"
token_tree
( "("
identifier "code"
, ","
raw_string_literal
string_content "}})();\n "
) ")"
? "?"
; ";"
call_expression (+21 nodes collapsed)
} "}"
line_comment
// "//"
outer_doc_comment_marker
/ "/"
doc_comment " Returns the code for the ECMAScript worker entrypoint boots"...
function_item (+70 nodes collapsed)