silverwolf-x's picture
download
raw
7.26 kB
use std::io::Write;
use anyhow::Result;
use indoc::writedoc;
use turbo_rcstr::rcstr;
use turbo_tasks::{ResolvedVc, ValueToString, Vc, turbobail};
use turbo_tasks_fs::{File, FileContent, FileSystem, FileSystemPath};
use turbopack_core::{
asset::{Asset, AssetContent},
chunk::ChunkingContext,
code_builder::{Code, CodeBuilder},
ident::AssetIdent,
output::{OutputAsset, OutputAssetsReference, OutputAssetsWithReferenced},
source_map::{GenerateSourceMap, SourceMapAsset},
};
use turbopack_ecmascript::utils::StringifyJs;
use turbopack_ecmascript_runtime::RuntimeType;
use crate::NodeJsChunkingContext;
/// An Ecmascript chunk that contains the Node.js runtime code.
#[turbo_tasks::value(shared)]
#[derive(ValueToString)]
#[value_to_string("Ecmascript Build Node Runtime Chunk")]
pub(crate) struct EcmascriptBuildNodeRuntimeChunk {
chunking_context: ResolvedVc<NodeJsChunkingContext>,
}
#[turbo_tasks::value_impl]
impl EcmascriptBuildNodeRuntimeChunk {
/// Creates a new [`Vc<EcmascriptBuildNodeRuntimeChunk>`].
#[turbo_tasks::function]
pub fn new(chunking_context: ResolvedVc<NodeJsChunkingContext>) -> Vc<Self> {
EcmascriptBuildNodeRuntimeChunk { chunking_context }.cell()
}
#[turbo_tasks::function]
async fn code(self: Vc<Self>) -> Result<Vc<Code>> {
let this = self.await?;
let output_root_to_root_path = this.chunking_context.output_root_to_root_path().await?;
let output_root = this.chunking_context.output_root().await?;
let generate_source_map = *this
.chunking_context
.reference_chunk_source_maps(Vc::upcast(self))
.await?;
let runtime_path = self.path().await?;
let runtime_public_path = if let Some(path) = output_root.get_path_to(&runtime_path) {
path
} else {
turbobail!("runtime path {runtime_path} is not in output root {output_root}");
};
let mut code = CodeBuilder::default();
let asset_prefix = this.chunking_context.asset_prefix().await?;
let asset_prefix = asset_prefix.as_deref().unwrap_or("/");
// Get the list of global variable names to forward to workers
let worker_forwarded_globals =
Vc::upcast::<Box<dyn ChunkingContext>>(*this.chunking_context)
.worker_forwarded_globals()
.await?;
writedoc!(
code,
r#"
var RUNTIME_PUBLIC_PATH = {};
var RELATIVE_ROOT_PATH = {};
var ASSET_PREFIX = {};
var WORKER_FORWARDED_GLOBALS = {};
"#,
StringifyJs(runtime_public_path),
StringifyJs(output_root_to_root_path.as_str()),
StringifyJs(asset_prefix),
StringifyJs(&*worker_forwarded_globals),
)?;
// Add preamble to read forwarded globals from workerData (for worker_threads)
if !worker_forwarded_globals.is_empty() {
writedoc!(
code,
r#"
// Apply forwarded globals from workerData if running in a worker thread
if (typeof require !== 'undefined') {{
try {{
var {{ workerData }} = require('worker_threads');
if (workerData?.__turbopack_globals__) {{
Object.assign(globalThis, workerData.__turbopack_globals__);
// Remove internal data so it's not visible to user code
delete workerData.__turbopack_globals__;
}}
}} catch (_) {{
// Not in a worker thread context, ignore
}}
}}
"#,
)?;
}
let asset_context =
turbopack::get_runtime_asset_context(this.chunking_context.environment());
match *this.chunking_context.runtime_type().await? {
RuntimeType::Development => {
let runtime_code = turbopack_ecmascript_runtime::get_nodejs_runtime_code(
asset_context,
RuntimeType::Development,
generate_source_map,
);
code.push_code(&*runtime_code.await?);
}
RuntimeType::Production => {
let runtime_code = turbopack_ecmascript_runtime::get_nodejs_runtime_code(
asset_context,
RuntimeType::Production,
generate_source_map,
);
code.push_code(&*runtime_code.await?);
}
#[cfg(feature = "test")]
RuntimeType::Dummy => {
let runtime_code = turbopack_ecmascript_runtime::get_dummy_runtime_code();
code.push_code(&runtime_code);
}
}
Ok(Code::cell(code.build()))
}
#[turbo_tasks::function]
async fn ident_for_path(self: Vc<Self>) -> Result<Vc<AssetIdent>> {
Ok(AssetIdent::from_path(
turbopack_ecmascript_runtime::embed_fs()
.root()
.await?
.join("runtime.js")?,
))
}
#[turbo_tasks::function]
async fn source_map(self: Vc<Self>) -> Result<Vc<SourceMapAsset>> {
let this = self.await?;
Ok(SourceMapAsset::new(
Vc::upcast(*this.chunking_context),
self.ident_for_path(),
Vc::upcast(self),
))
}
}
#[turbo_tasks::value_impl]
impl OutputAssetsReference for EcmascriptBuildNodeRuntimeChunk {
#[turbo_tasks::function]
async fn references(self: Vc<Self>) -> Result<Vc<OutputAssetsWithReferenced>> {
let this = self.await?;
let mut references = vec![];
if *this
.chunking_context
.reference_chunk_source_maps(Vc::upcast(self))
.await?
{
references.push(ResolvedVc::upcast(self.source_map().to_resolved().await?))
}
Ok(OutputAssetsWithReferenced::from_assets(Vc::cell(
references,
)))
}
}
#[turbo_tasks::value_impl]
impl OutputAsset for EcmascriptBuildNodeRuntimeChunk {
#[turbo_tasks::function]
async fn path(self: Vc<Self>) -> Result<Vc<FileSystemPath>> {
let this = self.await?;
let ident = self.ident_for_path();
Ok(this
.chunking_context
.chunk_path(Some(Vc::upcast(self)), ident, None, rcstr!(".js")))
}
}
#[turbo_tasks::value_impl]
impl Asset for EcmascriptBuildNodeRuntimeChunk {
#[turbo_tasks::function]
async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> {
Ok(AssetContent::file(
FileContent::Content(File::from(
self.code()
.to_rope_with_magic_comments(|| self.source_map())
.await?,
))
.cell(),
))
}
}
#[turbo_tasks::value_impl]
impl GenerateSourceMap for EcmascriptBuildNodeRuntimeChunk {
#[turbo_tasks::function]
fn generate_source_map(self: Vc<Self>) -> Vc<FileContent> {
self.code().generate_source_map()
}
}

Xet Storage Details

Size:
7.26 kB
·
Xet hash:
22db1d5a6ea9be7a32ad71ce25d570e945a969c398322083beb09de9c01afdc2

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.