File size: 5,949 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
use std::io::Write;
use anyhow::Result;
use indoc::writedoc;
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, Vc};
use turbopack_core::{
code_builder::{Code, CodeBuilder},
context::AssetContext,
environment::{ChunkLoading, Environment},
};
use turbopack_ecmascript::utils::StringifyJs;
use crate::{RuntimeType, asset_context::get_runtime_asset_context, embed_js::embed_static_code};
/// Returns the code for the ECMAScript runtime.
#[turbo_tasks::function]
pub async fn get_browser_runtime_code(
environment: ResolvedVc<Environment>,
chunk_base_path: Vc<Option<RcStr>>,
chunk_suffix_path: Vc<Option<RcStr>>,
runtime_type: RuntimeType,
output_root_to_root_path: RcStr,
generate_source_map: bool,
) -> Result<Vc<Code>> {
let asset_context = get_runtime_asset_context(*environment).resolve().await?;
let shared_runtime_utils_code = embed_static_code(
asset_context,
rcstr!("shared/runtime-utils.ts"),
generate_source_map,
);
let mut runtime_base_code = vec!["browser/runtime/base/runtime-base.ts"];
match runtime_type {
RuntimeType::Production => runtime_base_code.push("browser/runtime/base/build-base.ts"),
RuntimeType::Development => {
runtime_base_code.push("browser/runtime/base/dev-base.ts");
}
#[cfg(feature = "test")]
RuntimeType::Dummy => {
panic!("This configuration is not supported in the browser runtime")
}
}
let chunk_loading = &*asset_context
.compile_time_info()
.environment()
.chunk_loading()
.await?;
let mut runtime_backend_code = vec![];
match (chunk_loading, runtime_type) {
(ChunkLoading::Edge, RuntimeType::Development) => {
runtime_backend_code.push("browser/runtime/edge/runtime-backend-edge.ts");
runtime_backend_code.push("browser/runtime/edge/dev-backend-edge.ts");
}
(ChunkLoading::Edge, RuntimeType::Production) => {
runtime_backend_code.push("browser/runtime/edge/runtime-backend-edge.ts");
}
// This case should never be hit.
(ChunkLoading::NodeJs, _) => {
panic!("Node.js runtime is not supported in the browser runtime!")
}
(ChunkLoading::Dom, RuntimeType::Development) => {
runtime_backend_code.push("browser/runtime/dom/runtime-backend-dom.ts");
runtime_backend_code.push("browser/runtime/dom/dev-backend-dom.ts");
}
(ChunkLoading::Dom, RuntimeType::Production) => {
// TODO
runtime_backend_code.push("browser/runtime/dom/runtime-backend-dom.ts");
}
#[cfg(feature = "test")]
(_, RuntimeType::Dummy) => {
panic!("This configuration is not supported in the browser runtime")
}
};
let mut code: CodeBuilder = CodeBuilder::default();
let relative_root_path = output_root_to_root_path;
let chunk_base_path = chunk_base_path.await?;
let chunk_base_path = chunk_base_path.as_ref().map_or_else(|| "", |f| f.as_str());
let chunk_suffix_path = chunk_suffix_path.await?;
let chunk_suffix_path = chunk_suffix_path
.as_ref()
.map_or_else(|| "", |f| f.as_str());
writedoc!(
code,
r#"
(() => {{
if (!Array.isArray(globalThis.TURBOPACK)) {{
return;
}}
const CHUNK_BASE_PATH = {};
const CHUNK_SUFFIX_PATH = {};
const RELATIVE_ROOT_PATH = {};
const RUNTIME_PUBLIC_PATH = {};
"#,
StringifyJs(chunk_base_path),
StringifyJs(chunk_suffix_path),
StringifyJs(relative_root_path.as_str()),
StringifyJs(chunk_base_path),
)?;
code.push_code(&*shared_runtime_utils_code.await?);
for runtime_code in runtime_base_code {
code.push_code(
&*embed_static_code(asset_context, runtime_code.into(), generate_source_map).await?,
);
}
if *environment.supports_commonjs_externals().await? {
code.push_code(
&*embed_static_code(
asset_context,
rcstr!("shared-node/base-externals-utils.ts"),
generate_source_map,
)
.await?,
);
}
if *environment.node_externals().await? {
code.push_code(
&*embed_static_code(
asset_context,
rcstr!("shared-node/node-externals-utils.ts"),
generate_source_map,
)
.await?,
);
}
if *environment.supports_wasm().await? {
code.push_code(
&*embed_static_code(
asset_context,
rcstr!("shared-node/node-wasm-utils.ts"),
generate_source_map,
)
.await?,
);
}
for backend_code in runtime_backend_code {
code.push_code(
&*embed_static_code(asset_context, backend_code.into(), generate_source_map).await?,
);
}
// Registering chunks and chunk lists depends on the BACKEND variable, which is set by the
// specific runtime code, hence it must be appended after it.
writedoc!(
code,
r#"
const chunksToRegister = globalThis.TURBOPACK;
globalThis.TURBOPACK = {{ push: registerChunk }};
chunksToRegister.forEach(registerChunk);
"#
)?;
if matches!(runtime_type, RuntimeType::Development) {
writedoc!(
code,
r#"
const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || [];
chunkListsToRegister.forEach(registerChunkList);
globalThis.TURBOPACK_CHUNK_LISTS = {{ push: registerChunkList }};
"#
)?;
}
writedoc!(
code,
r#"
}})();
"#
)?;
Ok(Code::cell(code.build()))
}
|