diff --git a/crates/turbopack-core/src/chunk/chunking_context.rs b/crates/turbopack-core/src/chunk/chunking_context.rs new file mode 100644 index 0000000000000..8ab901d7ae7ae --- /dev/null +++ b/crates/turbopack-core/src/chunk/chunking_context.rs @@ -0,0 +1,55 @@ +use std::fmt::Debug; + +use anyhow::Result; +use turbo_tasks::primitives::{BoolVc, StringVc}; +use turbo_tasks_fs::FileSystemPathVc; + +use super::{ChunkVc, EvaluatableAssetsVc}; +use crate::{ + asset::{AssetVc, AssetsVc}, + environment::EnvironmentVc, + ident::AssetIdentVc, +}; + +/// A context for the chunking that influences the way chunks are created +#[turbo_tasks::value_trait] +pub trait ChunkingContext { + fn context_path(&self) -> FileSystemPathVc; + fn output_root(&self) -> FileSystemPathVc; + + // TODO remove this, a chunking context should not be bound to a specific + // environment since this can change due to transitions in the module graph + fn environment(&self) -> EnvironmentVc; + + // TODO(alexkirsz) Remove this from the chunking context. This should be at the + // discretion of chunking context implementors. However, we currently use this + // in a couple of places in `turbopack-css`, so we need to remove that + // dependency first. + fn chunk_path(&self, ident: AssetIdentVc, extension: &str) -> FileSystemPathVc; + + // TODO(alexkirsz) Remove this from the chunking context. + /// Reference Source Map Assets for chunks + fn reference_chunk_source_maps(&self, chunk: AssetVc) -> BoolVc; + + fn can_be_in_same_chunk(&self, asset_a: AssetVc, asset_b: AssetVc) -> BoolVc; + + fn asset_path(&self, content_hash: &str, extension: &str) -> FileSystemPathVc; + + fn is_hot_module_replacement_enabled(&self) -> BoolVc { + BoolVc::cell(false) + } + + fn layer(&self) -> StringVc { + StringVc::cell("".to_string()) + } + + fn with_layer(&self, layer: &str) -> ChunkingContextVc; + + fn chunk_group(&self, entry: ChunkVc) -> AssetsVc; + + fn evaluated_chunk_group( + &self, + entry: ChunkVc, + evaluatable_assets: EvaluatableAssetsVc, + ) -> AssetsVc; +} diff --git a/crates/turbopack-core/src/chunk/evaluate.rs b/crates/turbopack-core/src/chunk/evaluate.rs index 2b1da53e63575..aa16ab1836655 100644 --- a/crates/turbopack-core/src/chunk/evaluate.rs +++ b/crates/turbopack-core/src/chunk/evaluate.rs @@ -1,9 +1,9 @@ use anyhow::{bail, Result}; use turbo_tasks::{Value, ValueToString}; -use super::{ChunkVc, ChunkableAsset, ChunkableAssetVc, ChunkingContext, ChunkingContextVc}; +use super::{ChunkableAsset, ChunkableAssetVc}; use crate::{ - asset::{Asset, AssetVc, AssetsVc}, + asset::{Asset, AssetVc}, context::{AssetContext, AssetContextVc}, reference_type::{EntryReferenceSubType, ReferenceType}, }; @@ -52,15 +52,3 @@ impl EvaluatableAssetsVc { Ok(EvaluatableAssets(entries).cell()) } } - -/// Trait for chunking contexts which can generate evaluated chunks. -#[turbo_tasks::value_trait] -pub trait EvaluateChunkingContext: ChunkingContext { - /// Create a chunk that evaluates the given entries. - fn evaluate_chunk( - &self, - entry_chunk: ChunkVc, - other_assets: AssetsVc, - evaluatable_assets: EvaluatableAssetsVc, - ) -> AssetVc; -} diff --git a/crates/turbopack-core/src/chunk/mod.rs b/crates/turbopack-core/src/chunk/mod.rs index 505828b15472f..494e3a644f1ca 100644 --- a/crates/turbopack-core/src/chunk/mod.rs +++ b/crates/turbopack-core/src/chunk/mod.rs @@ -1,5 +1,6 @@ pub mod availability_info; pub mod available_assets; +pub(crate) mod chunking_context; pub(crate) mod evaluate; pub mod optimize; @@ -14,10 +15,7 @@ use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; use turbo_tasks::{ debug::ValueDebugFormat, - graph::{ - GraphTraversal, GraphTraversalResult, ReverseTopological, SkipDuplicates, Visit, - VisitControlFlow, - }, + graph::{GraphTraversal, GraphTraversalResult, ReverseTopological, Visit, VisitControlFlow}, primitives::{BoolVc, StringVc}, trace::TraceRawVcs, TryJoinIterExt, Value, ValueToString, ValueToStringVc, @@ -25,14 +23,14 @@ use turbo_tasks::{ use turbo_tasks_fs::FileSystemPathVc; use turbo_tasks_hash::DeterministicHash; -pub use self::evaluate::{ - EvaluatableAsset, EvaluatableAssetVc, EvaluatableAssets, EvaluatableAssetsVc, - EvaluateChunkingContext, EvaluateChunkingContextVc, +use self::availability_info::AvailabilityInfo; +pub use self::{ + chunking_context::{ChunkingContext, ChunkingContextVc}, + evaluate::{EvaluatableAsset, EvaluatableAssetVc, EvaluatableAssets, EvaluatableAssetsVc}, + optimize::optimize, }; -use self::{availability_info::AvailabilityInfo, optimize::optimize}; use crate::{ asset::{Asset, AssetVc, AssetsVc}, - environment::EnvironmentVc, ident::AssetIdentVc, reference::{AssetReference, AssetReferenceVc, AssetReferencesVc}, resolve::{PrimaryResolveResult, ResolveResult, ResolveResultVc}, @@ -77,44 +75,6 @@ impl ModuleId { #[turbo_tasks::value(transparent, shared)] pub struct ModuleIds(Vec); -/// A context for the chunking that influences the way chunks are created -#[turbo_tasks::value_trait] -pub trait ChunkingContext { - fn context_path(&self) -> FileSystemPathVc; - fn output_root(&self) -> FileSystemPathVc; - - // TODO remove this, a chunking context should not be bound to a specific - // environment since this can change due to transitions in the module graph - fn environment(&self) -> EnvironmentVc; - - // TODO(alexkirsz) Remove this from the chunking context. This should be at the - // discretion of chunking context implementors. However, we currently use this - // in a couple of places in `turbopack-css`, so we need to remove that - // dependency first. - fn chunk_path(&self, ident: AssetIdentVc, extension: &str) -> FileSystemPathVc; - - // TODO(alexkirsz) Remove this from the chunking context. - /// Reference Source Map Assets for chunks - fn reference_chunk_source_maps(&self, chunk: AssetVc) -> BoolVc; - - fn can_be_in_same_chunk(&self, asset_a: AssetVc, asset_b: AssetVc) -> BoolVc; - - fn asset_path(&self, content_hash: &str, extension: &str) -> FileSystemPathVc; - - fn is_hot_module_replacement_enabled(&self) -> BoolVc { - BoolVc::cell(false) - } - - fn layer(&self) -> StringVc { - StringVc::cell("".to_string()) - } - - fn with_layer(&self, layer: &str) -> ChunkingContextVc; - - /// Generates an output chunk asset from an intermediate chunk asset. - fn generate_chunk(&self, chunk: ChunkVc) -> AssetVc; -} - /// An [Asset] that can be converted into a [Chunk]. #[turbo_tasks::value_trait] pub trait ChunkableAsset: Asset { @@ -134,176 +94,9 @@ pub trait ChunkableAsset: Asset { } } -#[turbo_tasks::value] -pub struct ChunkGroup { - chunking_context: ChunkingContextVc, - entry: ChunkVc, - evaluatable_assets: EvaluatableAssetsVc, -} - #[turbo_tasks::value(transparent)] pub struct Chunks(Vec); -#[turbo_tasks::value_impl] -impl ChunkGroupVc { - /// Creates a chunk group from an asset as entrypoint - #[turbo_tasks::function] - pub fn from_asset( - asset: ChunkableAssetVc, - chunking_context: ChunkingContextVc, - availability_info: Value, - ) -> Self { - Self::from_chunk( - chunking_context, - asset.as_chunk(chunking_context, availability_info), - ) - } - - /// Creates a chunk group from a chunk as entrypoint - #[turbo_tasks::function] - pub fn from_chunk(chunking_context: ChunkingContextVc, entry: ChunkVc) -> Self { - Self::cell(ChunkGroup { - chunking_context, - entry, - evaluatable_assets: EvaluatableAssetsVc::empty(), - }) - } - - /// Creates a chunk group from a chunk as entrypoint, with the given - /// evaluated entries to be appended. - /// - /// `main_entry` will always be evaluated after all entries in - /// `other_entries` are evaluated. - #[turbo_tasks::function] - pub fn evaluated( - chunking_context: ChunkingContextVc, - main_entry: EvaluatableAssetVc, - other_entries: EvaluatableAssetsVc, - ) -> Self { - Self::cell(ChunkGroup { - chunking_context, - entry: main_entry.as_root_chunk(chunking_context), - // The main entry should always be *appended* to other entries, in order to ensure - // it's only evaluated once all other entries are evaluated. - evaluatable_assets: other_entries.with_entry(main_entry), - }) - } - - /// Returns the entry chunk of this chunk group. - #[turbo_tasks::function] - pub async fn entry(self) -> Result { - Ok(self.await?.entry) - } - - /// Lists all chunks that are in this chunk group. - /// These chunks need to be loaded to fulfill that chunk group. - /// All chunks should be loaded in parallel. - #[turbo_tasks::function] - pub async fn chunks(self) -> Result { - let this = self.await?; - let evaluatable_assets = this.evaluatable_assets.await?; - - let mut entry_chunks: HashSet<_> = evaluatable_assets - .iter() - .map({ - let chunking_context = this.chunking_context; - move |evaluatable_asset| async move { - Ok(evaluatable_asset - .as_root_chunk(chunking_context) - .resolve() - .await?) - } - }) - .try_join() - .await? - .into_iter() - .collect(); - - entry_chunks.insert(this.entry.resolve().await?); - - let chunks: Vec<_> = GraphTraversal::, _>>::visit( - entry_chunks.into_iter(), - get_chunk_children, - ) - .await - .completed()? - .into_inner() - .into_iter() - .collect(); - - let chunks = ChunksVc::cell(chunks); - let chunks = optimize(chunks, self); - let mut assets: Vec = chunks - .await? - .iter() - .map(|chunk| this.chunking_context.generate_chunk(*chunk)) - .collect(); - - if !evaluatable_assets.is_empty() { - if let Some(evaluate_chunking_context) = - EvaluateChunkingContextVc::resolve_from(&this.chunking_context).await? - { - assets.push(evaluate_chunking_context.evaluate_chunk( - this.entry, - AssetsVc::cell(assets.clone()), - this.evaluatable_assets, - )); - } - } - - Ok(AssetsVc::cell(assets)) - } -} - -/// Computes the list of all chunk children of a given chunk. -async fn get_chunk_children(parent: ChunkVc) -> Result + Send> { - Ok(parent - .references() - .await? - .iter() - .copied() - .map(reference_to_chunks) - .try_join() - .await? - .into_iter() - .flatten()) -} - -/// Get all parallel chunks from a parallel chunk reference. -async fn reference_to_chunks(r: AssetReferenceVc) -> Result + Send> { - let mut result = Vec::new(); - if let Some(pc) = ParallelChunkReferenceVc::resolve_from(r).await? { - if *pc.is_loaded_in_parallel().await? { - result = r - .resolve_reference() - .await? - .primary - .iter() - .map(|r| async move { - Ok(if let PrimaryResolveResult::Asset(a) = r { - ChunkVc::resolve_from(a).await? - } else { - None - }) - }) - .try_join() - .await?; - } - } - Ok(result.into_iter().flatten()) -} - -#[turbo_tasks::value_impl] -impl ValueToString for ChunkGroup { - #[turbo_tasks::function] - async fn to_string(&self) -> Result { - Ok(StringVc::cell(format!( - "group for {}", - self.entry.path().to_string().await? - ))) - } -} - /// A chunk is one type of asset. /// It usually contains multiple chunk items. /// There is an optional trait [ParallelChunkReference] that @@ -429,22 +222,32 @@ impl ParallelChunkReference for ChunkReference { /// A reference to multiple chunks from a [ChunkGroup] #[turbo_tasks::value] pub struct ChunkGroupReference { - chunk_group: ChunkGroupVc, + chunking_context: ChunkingContextVc, + entry: ChunkVc, } #[turbo_tasks::value_impl] impl ChunkGroupReferenceVc { #[turbo_tasks::function] - pub fn new(chunk_group: ChunkGroupVc) -> Self { - Self::cell(ChunkGroupReference { chunk_group }) + pub fn new(chunking_context: ChunkingContextVc, entry: ChunkVc) -> Self { + Self::cell(ChunkGroupReference { + chunking_context, + entry, + }) + } + + #[turbo_tasks::function] + async fn chunks(self) -> Result { + let this = self.await?; + Ok(this.chunking_context.chunk_group(this.entry)) } } #[turbo_tasks::value_impl] impl AssetReference for ChunkGroupReference { #[turbo_tasks::function] - async fn resolve_reference(&self) -> Result { - let set = self.chunk_group.chunks().await?.clone_value(); + async fn resolve_reference(self_vc: ChunkGroupReferenceVc) -> Result { + let set = self_vc.chunks().await?.clone_value(); Ok(ResolveResult::assets(set).into()) } } @@ -454,8 +257,8 @@ impl ValueToString for ChunkGroupReference { #[turbo_tasks::function] async fn to_string(&self) -> Result { Ok(StringVc::cell(format!( - "chunk group {}", - self.chunk_group.to_string().await? + "chunk group ({})", + self.entry.ident().to_string().await? ))) } } @@ -463,7 +266,7 @@ impl ValueToString for ChunkGroupReference { pub struct ChunkContentResult { pub chunk_items: Vec, pub chunks: Vec, - pub async_chunk_groups: Vec, + pub async_chunk_group_entries: Vec, pub external_asset_references: Vec, pub availability_info: AvailabilityInfo, } @@ -515,7 +318,7 @@ enum ChunkContentGraphNode { Chunk(ChunkVc), // Chunk groups that are referenced from the current chunk, but // not loaded in parallel - AsyncChunkGroup(ChunkGroupVc), + AsyncChunkGroup { entry: ChunkVc }, ExternalAssetReference(AssetReferenceVc), } @@ -640,11 +443,10 @@ where ChunkingType::Separate => { graph_nodes.push(( Some((asset, chunking_type)), - ChunkContentGraphNode::AsyncChunkGroup(ChunkGroupVc::from_asset( - chunkable_asset, - context.chunking_context, - context.availability_info, - )), + ChunkContentGraphNode::AsyncChunkGroup { + entry: chunkable_asset + .as_chunk(context.chunking_context, context.availability_info), + }, )); } ChunkingType::SeparateAsync => { @@ -804,7 +606,7 @@ where let mut chunk_items = Vec::new(); let mut chunks = Vec::new(); - let mut async_chunk_groups = Vec::new(); + let mut async_chunk_group_entries = Vec::new(); let mut external_asset_references = Vec::new(); for graph_node in graph_nodes { @@ -816,8 +618,8 @@ where ChunkContentGraphNode::Chunk(chunk) => { chunks.push(chunk); } - ChunkContentGraphNode::AsyncChunkGroup(async_chunk_group) => { - async_chunk_groups.push(async_chunk_group); + ChunkContentGraphNode::AsyncChunkGroup { entry } => { + async_chunk_group_entries.push(entry); } ChunkContentGraphNode::ExternalAssetReference(reference) => { external_asset_references.push(reference); @@ -828,7 +630,7 @@ where Ok(Some(ChunkContentResult { chunk_items, chunks, - async_chunk_groups, + async_chunk_group_entries, external_asset_references, availability_info: availability_info.into_value(), })) diff --git a/crates/turbopack-core/src/chunk/optimize.rs b/crates/turbopack-core/src/chunk/optimize.rs index 6481de931e016..de3019ab95ea7 100644 --- a/crates/turbopack-core/src/chunk/optimize.rs +++ b/crates/turbopack-core/src/chunk/optimize.rs @@ -10,7 +10,7 @@ use indexmap::{IndexMap, IndexSet}; use turbo_tasks::TryJoinIterExt; use turbo_tasks_fs::{FileSystemPathOptionVc, FileSystemPathVc}; -use super::{ChunkGroupVc, ChunkVc, ChunksVc}; +use super::{ChunkVc, ChunksVc}; use crate::{ asset::{Asset, AssetVc}, chunk::Chunk, @@ -19,7 +19,7 @@ use crate::{ /// A functor to optimize a set of chunks. #[turbo_tasks::value_trait] pub trait ChunkOptimizer { - fn optimize(&self, chunks: ChunksVc, chunk_group: ChunkGroupVc) -> ChunksVc; + fn optimize(&self, chunks: ChunksVc) -> ChunksVc; } /// Trait to mark a chunk as optimizable. @@ -30,7 +30,7 @@ pub trait OptimizableChunk: Chunk + Asset { /// Optimize all chunks that implement [OptimizableChunk]. #[turbo_tasks::function] -pub async fn optimize(chunks: ChunksVc, chunk_group: ChunkGroupVc) -> Result { +pub async fn optimize(chunks: ChunksVc) -> Result { let chunks = chunks.await?; let mut by_optimizer = IndexMap::<_, Vec<_>>::new(); for (chunk, optimizer) in chunks @@ -57,7 +57,7 @@ pub async fn optimize(chunks: ChunksVc, chunk_group: ChunkGroupVc) -> Result, pub chunks: Vec, - pub async_chunk_groups: Vec, + pub async_chunk_group_entries: Vec, pub external_asset_references: Vec, } @@ -206,7 +206,7 @@ impl From> for CssChunkContentResult { CssChunkContentResult { chunk_items: from.chunk_items, chunks: from.chunks, - async_chunk_groups: from.async_chunk_groups, + async_chunk_group_entries: from.async_chunk_group_entries, external_asset_references: from.external_asset_references, } } @@ -231,26 +231,26 @@ async fn css_chunk_content( let mut all_chunk_items = IndexSet::::new(); let mut all_chunks = IndexSet::::new(); - let mut all_async_chunk_groups = IndexSet::::new(); + let mut all_async_chunk_group_entries = IndexSet::::new(); let mut all_external_asset_references = IndexSet::::new(); for content in contents { let CssChunkContentResult { chunk_items, chunks, - async_chunk_groups, + async_chunk_group_entries, external_asset_references, } = &*content.await?; all_chunk_items.extend(chunk_items.iter().copied()); all_chunks.extend(chunks.iter().copied()); - all_async_chunk_groups.extend(async_chunk_groups.iter().copied()); + all_async_chunk_group_entries.extend(async_chunk_group_entries.iter().copied()); all_external_asset_references.extend(external_asset_references.iter().copied()); } Ok(CssChunkContentResult { chunk_items: all_chunk_items.into_iter().collect(), chunks: all_chunks.into_iter().collect(), - async_chunk_groups: all_async_chunk_groups.into_iter().collect(), + async_chunk_group_entries: all_async_chunk_group_entries.into_iter().collect(), external_asset_references: all_external_asset_references.into_iter().collect(), } .cell()) @@ -351,8 +351,8 @@ impl Asset for CssChunk { for chunk in content.chunks.iter() { references.push(ChunkReferenceVc::new_parallel(*chunk).into()); } - for chunk_group in content.async_chunk_groups.iter() { - references.push(ChunkGroupReferenceVc::new(*chunk_group).into()); + for entry in content.async_chunk_group_entries.iter() { + references.push(ChunkGroupReferenceVc::new(this.context, *entry).into()); } if *this .context diff --git a/crates/turbopack-css/src/chunk/optimize.rs b/crates/turbopack-css/src/chunk/optimize.rs index 123641d82041e..924c0d3d21a91 100644 --- a/crates/turbopack-css/src/chunk/optimize.rs +++ b/crates/turbopack-css/src/chunk/optimize.rs @@ -3,7 +3,7 @@ use indexmap::IndexSet; use turbo_tasks::{TryJoinIterExt, Value}; use turbopack_core::chunk::{ optimize::{ChunkOptimizer, ChunkOptimizerVc}, - ChunkGroupVc, ChunkVc, ChunkingContextVc, ChunksVc, + ChunkVc, ChunkingContextVc, ChunksVc, }; use super::{CssChunkPlaceablesVc, CssChunkVc}; @@ -22,7 +22,7 @@ impl CssChunkOptimizerVc { #[turbo_tasks::value_impl] impl ChunkOptimizer for CssChunkOptimizer { #[turbo_tasks::function] - async fn optimize(&self, chunks: ChunksVc, _chunk_group: ChunkGroupVc) -> Result { + async fn optimize(&self, chunks: ChunksVc) -> Result { // The CSS optimizer works under the constraint that the order in which // CSS chunks are loaded must be preserved, as CSS rules // precedence is determined by the order in which they are diff --git a/crates/turbopack-dev-server/src/html.rs b/crates/turbopack-dev-server/src/html.rs index 376584fedb6bf..d73c89faff61c 100644 --- a/crates/turbopack-dev-server/src/html.rs +++ b/crates/turbopack-dev-server/src/html.rs @@ -4,8 +4,7 @@ use turbo_tasks::primitives::StringVc; use turbo_tasks_fs::{File, FileSystemPathVc}; use turbo_tasks_hash::{encode_hex, Xxh3Hash64Hasher}; use turbopack_core::{ - asset::{Asset, AssetContentVc, AssetVc}, - chunk::ChunkGroupVc, + asset::{Asset, AssetContentVc, AssetVc, AssetsVc}, ident::AssetIdentVc, reference::{AssetReferencesVc, SingleAssetReferenceVc}, version::{Version, VersionVc, VersionedContent, VersionedContentVc}, @@ -18,7 +17,7 @@ use turbopack_core::{ #[derive(Clone)] pub struct DevHtmlAsset { path: FileSystemPathVc, - chunk_groups: Vec, + chunk_groups: Vec, body: Option, } @@ -43,7 +42,7 @@ impl Asset for DevHtmlAsset { async fn references(&self) -> Result { let mut references = Vec::new(); for chunk_group in &self.chunk_groups { - let chunks = chunk_group.chunks().await?; + let chunks = chunk_group.await?; for chunk in chunks.iter() { references.push( SingleAssetReferenceVc::new(*chunk, dev_html_chunk_reference_description()) @@ -62,7 +61,7 @@ impl Asset for DevHtmlAsset { impl DevHtmlAssetVc { /// Create a new dev HTML asset. - pub fn new(path: FileSystemPathVc, chunk_groups: Vec) -> Self { + pub fn new(path: FileSystemPathVc, chunk_groups: Vec) -> Self { DevHtmlAsset { path, chunk_groups, @@ -74,7 +73,7 @@ impl DevHtmlAssetVc { /// Create a new dev HTML asset. pub fn new_with_body( path: FileSystemPathVc, - chunk_groups: Vec, + chunk_groups: Vec, body: String, ) -> Self { DevHtmlAsset { @@ -112,7 +111,7 @@ impl DevHtmlAssetVc { let mut chunk_paths = vec![]; for chunk_group in &this.chunk_groups { - for chunk in chunk_group.chunks().await?.iter() { + for chunk in chunk_group.await?.iter() { let chunk_path = &*chunk.ident().path().await?; if let Some(relative_path) = context_path.get_path_to(chunk_path) { chunk_paths.push(format!("/{relative_path}")); diff --git a/crates/turbopack-dev/js/src/runtime.dom.js b/crates/turbopack-dev/js/src/runtime.dom.js index 806ce7bf34224..5d52114d138d7 100644 --- a/crates/turbopack-dev/js/src/runtime.dom.js +++ b/crates/turbopack-dev/js/src/runtime.dom.js @@ -16,11 +16,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-dev/js/src/runtime.js b/crates/turbopack-dev/js/src/runtime.js index c8223e09c3259..5581dde60fb16 100644 --- a/crates/turbopack-dev/js/src/runtime.js +++ b/crates/turbopack-dev/js/src/runtime.js @@ -6,6 +6,7 @@ /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -318,7 +319,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1175,6 +1175,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1239,39 +1243,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1301,3 +1296,16 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; diff --git a/crates/turbopack-dev/js/types/backend.d.ts b/crates/turbopack-dev/js/types/backend.d.ts index dd1908899a5ed..1bd7a997aa4cd 100644 --- a/crates/turbopack-dev/js/types/backend.d.ts +++ b/crates/turbopack-dev/js/types/backend.d.ts @@ -11,7 +11,6 @@ declare global { declare const RUNTIME_PARAMS: DevRuntimeParams; declare const getFirstModuleChunk: GetFirstModuleChunk; declare const getOrInstantiateRuntimeModule: GetOrInstantiateRuntimeModule; - declare const registerChunkListAndMarkAsRuntime: RegisterChunkListAndMarkAsRuntime; declare const SourceTypeRuntime: SourceType.Runtime; declare const SourceTypeParent: SourceType.Parent; declare const SourceTypeUpdate: SourceType.Update; diff --git a/crates/turbopack-dev/js/types/index.d.ts b/crates/turbopack-dev/js/types/index.d.ts index 99bd6bb27997c..583789580b463 100644 --- a/crates/turbopack-dev/js/types/index.d.ts +++ b/crates/turbopack-dev/js/types/index.d.ts @@ -22,6 +22,11 @@ export type ChunkRegistration = [ chunkModules: ChunkModule[], DevRuntimeParams | undefined ]; +export type ChunkList = { + path: ChunkPath; + chunks: ChunkPath[]; + source: "entry" | "dynamic"; +}; interface Module { exports: Exports; @@ -121,6 +126,7 @@ export interface TurbopackGlobals { TURBOPACK_CHUNK_UPDATE_LISTENERS?: | ChunkUpdateProvider | [ChunkPath, UpdateCallback][]; + TURBOPACK_CHUNK_LISTS?: ChunkList[]; } export type GetFirstModuleChunk = (moduleId: ModuleId) => ChunkPath | null; @@ -128,10 +134,6 @@ export type GetOrInstantiateRuntimeModule = ( moduleId: ModuleId, chunkPath: ChunkPath ) => Module; -export type RegisterChunkListAndMarkAsRuntime = ( - chunkListPath: ChunkPath, - chunkPaths: ChunkPath[] -) => void; export interface Loader { promise: Promise; @@ -160,6 +162,7 @@ declare global { | ChunkUpdateProvider | [ChunkPath, UpdateCallback][] | undefined; + var TURBOPACK_CHUNK_LISTS: ChunkList[]; var $RefreshHelpers$: RefreshRuntimeGlobals["$RefreshHelpers$"]; var $RefreshReg$: RefreshRuntimeGlobals["$RefreshReg$"]; diff --git a/crates/turbopack-dev/js/types/runtime.d.ts b/crates/turbopack-dev/js/types/runtime.d.ts index 123cbc0515fa2..f0c5d9ce2f8a2 100644 --- a/crates/turbopack-dev/js/types/runtime.d.ts +++ b/crates/turbopack-dev/js/types/runtime.d.ts @@ -21,5 +21,4 @@ export type ModuleEffect = export type DevRuntimeParams = { otherChunks: ChunkPath[]; runtimeModuleIds: ModuleId[]; - chunkListPath: ChunkPath; }; diff --git a/crates/turbopack-dev/src/chunking_context.rs b/crates/turbopack-dev/src/chunking_context.rs index c7791cbbc938e..21b836dbb76aa 100644 --- a/crates/turbopack-dev/src/chunking_context.rs +++ b/crates/turbopack-dev/src/chunking_context.rs @@ -1,21 +1,25 @@ use std::fmt::Write; use anyhow::Result; +use indexmap::IndexSet; use turbo_tasks::{ + graph::{GraphTraversal, ReverseTopological, SkipDuplicates}, primitives::{BoolVc, StringVc}, - Value, ValueToString, + TryJoinIterExt, Value, ValueToString, }; use turbo_tasks_fs::FileSystemPathVc; use turbo_tasks_hash::{encode_hex, hash_xxh3_hash64, DeterministicHash, Xxh3Hash64Hasher}; use turbopack_core::{ asset::{Asset, AssetVc, AssetsVc}, chunk::{ - availability_info::AvailabilityInfo, ChunkVc, ChunkableAssetVc, ChunkingContext, - ChunkingContextVc, EvaluatableAssetsVc, EvaluateChunkingContext, EvaluateChunkingContextVc, + availability_info::AvailabilityInfo, optimize, ChunkVc, ChunkableAsset, ChunkableAssetVc, + ChunkingContext, ChunkingContextVc, ChunksVc, EvaluatableAssetsVc, ParallelChunkReference, + ParallelChunkReferenceVc, }, environment::EnvironmentVc, ident::{AssetIdent, AssetIdentVc}, - resolve::ModulePart, + reference::{AssetReference, AssetReferenceVc}, + resolve::{ModulePart, PrimaryResolveResult}, }; use turbopack_ecmascript::chunk::{ EcmascriptChunkItemVc, EcmascriptChunkVc, EcmascriptChunkingContext, @@ -25,6 +29,7 @@ use turbopack_ecmascript::chunk::{ use crate::ecmascript::{ chunk::EcmascriptDevChunkVc, evaluate::chunk::EcmascriptDevEvaluateChunkVc, + list::asset::{EcmascriptDevChunkListSource, EcmascriptDevChunkListVc}, manifest::{chunk_asset::DevManifestChunkAssetVc, loader_item::DevManifestLoaderItemVc}, }; @@ -118,11 +123,6 @@ impl DevChunkingContextVc { } } -#[turbo_tasks::function] -fn chunk_list_modifier() -> StringVc { - StringVc::cell("chunk list".to_string()) -} - #[turbo_tasks::value_impl] impl DevChunkingContextVc { #[turbo_tasks::function] @@ -131,12 +131,38 @@ impl DevChunkingContextVc { } #[turbo_tasks::function] - pub async fn chunk_list_path( - self, - entry_chunk_ident: AssetIdentVc, - ) -> Result { - let ident = entry_chunk_ident.with_modifier(chunk_list_modifier()); - Ok(self.chunk_path(ident, ".json")) + pub(crate) async fn generate_chunk( + self_vc: DevChunkingContextVc, + chunk: ChunkVc, + ) -> Result { + Ok( + if let Some(ecmascript_chunk) = EcmascriptChunkVc::resolve_from(chunk).await? { + EcmascriptDevChunkVc::new(self_vc, ecmascript_chunk).into() + } else { + chunk.into() + }, + ) + } + + #[turbo_tasks::function] + fn generate_evaluate_chunk( + self_vc: DevChunkingContextVc, + entry_chunk: ChunkVc, + other_chunks: AssetsVc, + evaluatable_assets: EvaluatableAssetsVc, + ) -> AssetVc { + EcmascriptDevEvaluateChunkVc::new(self_vc, entry_chunk, other_chunks, evaluatable_assets) + .into() + } + + #[turbo_tasks::function] + fn generate_chunk_list_register_chunk( + self_vc: DevChunkingContextVc, + entry_chunk: ChunkVc, + other_chunks: AssetsVc, + source: Value, + ) -> AssetVc { + EcmascriptDevChunkListVc::new(self_vc, entry_chunk, other_chunks, source).into() } } @@ -343,14 +369,68 @@ impl ChunkingContext for DevChunkingContext { } #[turbo_tasks::function] - async fn generate_chunk(self_vc: DevChunkingContextVc, chunk: ChunkVc) -> Result { - Ok( - if let Some(ecmascript_chunk) = EcmascriptChunkVc::resolve_from(chunk).await? { - EcmascriptDevChunkVc::new(self_vc, ecmascript_chunk).into() - } else { - chunk.into() - }, - ) + async fn chunk_group(self_vc: DevChunkingContextVc, entry_chunk: ChunkVc) -> Result { + let chunks = get_optimized_parallel_chunks([entry_chunk]).await?; + + let mut assets: Vec = chunks + .await? + .iter() + .map(|chunk| self_vc.generate_chunk(*chunk)) + .collect(); + + assets.push(self_vc.generate_chunk_list_register_chunk( + entry_chunk, + AssetsVc::cell(assets.clone()), + Value::new(EcmascriptDevChunkListSource::Dynamic), + )); + + Ok(AssetsVc::cell(assets)) + } + + #[turbo_tasks::function] + async fn evaluated_chunk_group( + self_vc: DevChunkingContextVc, + entry_chunk: ChunkVc, + evaluatable_assets: EvaluatableAssetsVc, + ) -> Result { + let evaluatable_assets_ref = evaluatable_assets.await?; + + let mut entry_assets: IndexSet<_> = evaluatable_assets_ref + .iter() + .map({ + move |evaluatable_asset| async move { + Ok(evaluatable_asset + .as_root_chunk(self_vc.into()) + .resolve() + .await?) + } + }) + .try_join() + .await? + .into_iter() + .collect(); + + entry_assets.insert(entry_chunk.resolve().await?); + + let chunks = get_optimized_parallel_chunks(entry_assets).await?; + + let mut assets: Vec = chunks + .await? + .iter() + .map(|chunk| self_vc.generate_chunk(*chunk)) + .collect(); + + let other_assets = AssetsVc::cell(assets.clone()); + + assets.push(self_vc.generate_chunk_list_register_chunk( + entry_chunk, + other_assets, + Value::new(EcmascriptDevChunkListSource::Entry), + )); + + assets.push(self_vc.generate_evaluate_chunk(entry_chunk, other_assets, evaluatable_assets)); + + Ok(AssetsVc::cell(assets)) } } @@ -367,16 +447,60 @@ impl EcmascriptChunkingContext for DevChunkingContext { } } -#[turbo_tasks::value_impl] -impl EvaluateChunkingContext for DevChunkingContext { - #[turbo_tasks::function] - fn evaluate_chunk( - self_vc: DevChunkingContextVc, - entry_chunk: ChunkVc, - other_chunks: AssetsVc, - evaluatable_assets: EvaluatableAssetsVc, - ) -> AssetVc { - EcmascriptDevEvaluateChunkVc::new(self_vc, entry_chunk, other_chunks, evaluatable_assets) - .into() +async fn get_optimized_parallel_chunks(entries: I) -> Result +where + I: IntoIterator, +{ + let chunks: Vec<_> = GraphTraversal::, _>>::visit( + entries, + get_chunk_children, + ) + .await + .completed()? + .into_inner() + .into_iter() + .collect(); + + let chunks = ChunksVc::cell(chunks); + let chunks = optimize(chunks); + + Ok(chunks) +} + +/// Computes the list of all chunk children of a given chunk. +async fn get_chunk_children(parent: ChunkVc) -> Result + Send> { + Ok(parent + .references() + .await? + .iter() + .copied() + .map(reference_to_chunks) + .try_join() + .await? + .into_iter() + .flatten()) +} + +/// Get all parallel chunks from a parallel chunk reference. +async fn reference_to_chunks(r: AssetReferenceVc) -> Result + Send> { + let mut result = Vec::new(); + if let Some(pc) = ParallelChunkReferenceVc::resolve_from(r).await? { + if *pc.is_loaded_in_parallel().await? { + result = r + .resolve_reference() + .await? + .primary + .iter() + .map(|r| async move { + Ok(if let PrimaryResolveResult::Asset(a) = r { + ChunkVc::resolve_from(a).await? + } else { + None + }) + }) + .try_join() + .await?; + } } + Ok(result.into_iter().flatten()) } diff --git a/crates/turbopack-dev/src/ecmascript/content.rs b/crates/turbopack-dev/src/ecmascript/content.rs index e97e3456f128c..6df7f43da5007 100644 --- a/crates/turbopack-dev/src/ecmascript/content.rs +++ b/crates/turbopack-dev/src/ecmascript/content.rs @@ -2,11 +2,10 @@ use std::io::Write; use anyhow::{bail, Result}; use indoc::writedoc; -use serde::Serialize; use turbo_tasks_fs::File; use turbopack_core::{ asset::{Asset, AssetContentVc}, - chunk::{ChunkingContext, ModuleId, ModuleIdReadRef}, + chunk::{ChunkingContext, ModuleId}, code_builder::{CodeBuilder, CodeVc}, source_map::{GenerateSourceMap, GenerateSourceMapVc, OptionSourceMapVc}, version::{ @@ -159,18 +158,3 @@ impl GenerateSourceMap for EcmascriptDevChunkContent { Ok(OptionSourceMapVc::cell(None)) } } - -#[derive(Debug, Clone, Default, Serialize)] -#[serde(rename_all = "camelCase")] -struct EcmascriptDevChunkRuntimeParams { - /// Other chunks in the chunk group this chunk belongs to, if any. Does not - /// include the chunk itself. - /// - /// These chunks must be loaed before the runtime modules can be - /// instantiated. - other_chunks: Vec, - /// List of module IDs that this chunk should instantiate when executed. - runtime_module_ids: Vec, - /// Path to the chunk list that this chunk should register itself with. - chunk_list_path: Option, -} diff --git a/crates/turbopack-dev/src/ecmascript/evaluate/chunk.rs b/crates/turbopack-dev/src/ecmascript/evaluate/chunk.rs index 92780d47ffc4d..085797b669cab 100644 --- a/crates/turbopack-dev/src/ecmascript/evaluate/chunk.rs +++ b/crates/turbopack-dev/src/ecmascript/evaluate/chunk.rs @@ -1,6 +1,6 @@ use std::io::Write; -use anyhow::{bail, Context, Result}; +use anyhow::{bail, Result}; use indoc::writedoc; use serde::Serialize; use turbo_tasks::{primitives::StringVc, TryJoinIterExt, Value, ValueToString, ValueToStringVc}; @@ -21,17 +21,17 @@ use turbopack_ecmascript::{ utils::StringifyJs, }; -use crate::{ecmascript::list::reference::ChunkListReferenceVc, DevChunkingContextVc}; +use crate::DevChunkingContextVc; /// An Ecmascript chunk that: /// * Contains the Turbopack dev runtime code; and /// * Evaluates a list of runtime entries. #[turbo_tasks::value(shared)] pub(crate) struct EcmascriptDevEvaluateChunk { - pub(super) chunking_context: DevChunkingContextVc, - pub(super) entry_chunk: ChunkVc, - pub(super) other_chunks: AssetsVc, - pub(super) evaluatable_assets: EvaluatableAssetsVc, + chunking_context: DevChunkingContextVc, + entry_chunk: ChunkVc, + other_chunks: AssetsVc, + evaluatable_assets: EvaluatableAssetsVc, } #[turbo_tasks::value_impl] @@ -104,16 +104,7 @@ impl EcmascriptDevEvaluateChunkVc { .flatten() .collect(); - let chunk_list_path = this - .chunking_context - .chunk_list_path(this.entry_chunk.ident()) - .await?; - let chunk_list_path = output_root - .get_path_to(&*chunk_list_path) - .context("chunk list path is not in output root")?; - let params = EcmascriptDevChunkRuntimeParams { - chunk_list_path, other_chunks: other_chunks_paths, runtime_module_ids, }; @@ -221,13 +212,8 @@ impl Asset for EcmascriptDevEvaluateChunk { } #[turbo_tasks::function] - async fn references(self_vc: EcmascriptDevEvaluateChunkVc) -> Result { - let this = self_vc.await?; - Ok(AssetReferencesVc::cell(vec![ - ChunkListReferenceVc::new(this.chunking_context, this.entry_chunk, this.other_chunks) - .into(), - SourceMapAssetReferenceVc::new(self_vc.into()).into(), - ])) + fn references(self_vc: EcmascriptDevEvaluateChunkVc) -> AssetReferencesVc { + AssetReferencesVc::cell(vec![SourceMapAssetReferenceVc::new(self_vc.into()).into()]) } #[turbo_tasks::function] @@ -247,7 +233,7 @@ impl GenerateSourceMap for EcmascriptDevEvaluateChunk { #[derive(Debug, Clone, Default, Serialize)] #[serde(rename_all = "camelCase")] -struct EcmascriptDevChunkRuntimeParams<'a> { +struct EcmascriptDevChunkRuntimeParams { /// Other chunks in the chunk group this chunk belongs to, if any. Does not /// include the chunk itself. /// @@ -256,6 +242,4 @@ struct EcmascriptDevChunkRuntimeParams<'a> { other_chunks: Vec, /// List of module IDs that this chunk should instantiate when executed. runtime_module_ids: Vec, - /// Path to the chunk list that this chunk should register itself with. - chunk_list_path: &'a str, } diff --git a/crates/turbopack-dev/src/ecmascript/list/asset.rs b/crates/turbopack-dev/src/ecmascript/list/asset.rs index 73e1a8683c1c9..88ac08e37ecdf 100644 --- a/crates/turbopack-dev/src/ecmascript/list/asset.rs +++ b/crates/turbopack-dev/src/ecmascript/list/asset.rs @@ -1,5 +1,6 @@ use anyhow::Result; -use turbo_tasks::primitives::StringVc; +use serde::Serialize; +use turbo_tasks::{primitives::StringVc, Value, ValueToString, ValueToStringVc}; use turbopack_core::{ asset::{Asset, AssetContentVc, AssetVc, AssetsVc}, chunk::{ChunkVc, ChunkingContext}, @@ -8,70 +9,93 @@ use turbopack_core::{ version::{VersionedContent, VersionedContentVc}, }; -use super::content::ChunkListContentVc; +use super::content::EcmascriptDevChunkListContentVc; use crate::DevChunkingContextVc; /// An asset that represents a list of chunks that exist together in a chunk /// group, and should be *updated* together. /// -/// A chunk list has no actual content: all it does is merge updates from its -/// chunks into a single update when possible. This is useful for keeping track -/// of changes that affect more than one chunk, or affect the chunk group, e.g.: +/// The chunk list's content registers itself as a Turbopack chunk and a chunk +/// list. +/// +/// Then, on updates, it merges updates from its chunks into a single update +/// when possible. This is useful for keeping track of changes that affect more +/// than one chunk, or affect the chunk group, e.g.: /// * moving a module from one chunk to another; /// * changing a chunk's path. #[turbo_tasks::value(shared)] -pub(super) struct ChunkListAsset { - chunking_context: DevChunkingContextVc, - entry_chunk: ChunkVc, - other_chunks: AssetsVc, +pub(crate) struct EcmascriptDevChunkList { + pub(super) chunking_context: DevChunkingContextVc, + pub(super) entry_chunk: ChunkVc, + pub(super) chunks: AssetsVc, + pub(super) source: EcmascriptDevChunkListSource, } #[turbo_tasks::value_impl] -impl ChunkListAssetVc { - /// Creates a new [`ChunkListAsset`]. +impl EcmascriptDevChunkListVc { + /// Creates a new [`EcmascriptDevChunkListVc`]. #[turbo_tasks::function] pub fn new( chunking_context: DevChunkingContextVc, entry_chunk: ChunkVc, - other_chunks: AssetsVc, + chunks: AssetsVc, + source: Value, ) -> Self { - ChunkListAsset { + EcmascriptDevChunkList { chunking_context, entry_chunk, - other_chunks, + chunks, + source: source.into_value(), } .cell() } #[turbo_tasks::function] - async fn content(self) -> Result { - let this = &*self.await?; - Ok(ChunkListContentVc::new( - this.chunking_context.output_root(), - this.other_chunks, - )) + fn own_content(self) -> EcmascriptDevChunkListContentVc { + EcmascriptDevChunkListContentVc::new(self) } } +#[turbo_tasks::value_impl] +impl ValueToString for EcmascriptDevChunkList { + #[turbo_tasks::function] + async fn to_string(&self) -> Result { + Ok(StringVc::cell("Ecmascript Dev Chunk List".to_string())) + } +} + +#[turbo_tasks::function] +fn modifier() -> StringVc { + StringVc::cell("ecmascript dev chunk list".to_string()) +} + #[turbo_tasks::function] fn chunk_list_chunk_reference_description() -> StringVc { StringVc::cell("chunk list chunk".to_string()) } #[turbo_tasks::value_impl] -impl Asset for ChunkListAsset { +impl Asset for EcmascriptDevChunkList { #[turbo_tasks::function] async fn ident(&self) -> Result { + let mut ident = self.entry_chunk.ident().await?.clone_value(); + + ident.add_modifier(modifier()); + + for chunk in &*self.chunks.await? { + ident.add_modifier(chunk.ident().to_string()); + } + + let ident = AssetIdentVc::new(Value::new(ident)); Ok(AssetIdentVc::from_path( - self.chunking_context - .chunk_list_path(self.entry_chunk.ident()), + self.chunking_context.chunk_path(ident, ".js"), )) } #[turbo_tasks::function] async fn references(&self) -> Result { Ok(AssetReferencesVc::cell( - self.other_chunks + self.chunks .await? .iter() .map(|chunk| { @@ -83,12 +107,33 @@ impl Asset for ChunkListAsset { } #[turbo_tasks::function] - fn content(self_vc: ChunkListAssetVc) -> AssetContentVc { - self_vc.content().content() + fn content(self_vc: EcmascriptDevChunkListVc) -> AssetContentVc { + self_vc.own_content().content() } #[turbo_tasks::function] - fn versioned_content(self_vc: ChunkListAssetVc) -> VersionedContentVc { - self_vc.content().into() + fn versioned_content(self_vc: EcmascriptDevChunkListVc) -> VersionedContentVc { + self_vc.own_content().into() } } + +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "camelCase")] +struct EcmascriptDevChunkListParams<'a> { + /// Path to the chunk list to register. + path: &'a str, + /// All chunks that belong to the chunk list. + chunks: Vec, + /// Where this chunk list is from. + source: EcmascriptDevChunkListSource, +} + +#[derive(Debug, Clone, Copy, Ord, PartialOrd, Hash)] +#[turbo_tasks::value(serialization = "auto_for_input")] +#[serde(rename_all = "camelCase")] +pub enum EcmascriptDevChunkListSource { + /// The chunk list is from a runtime entry. + Entry, + /// The chunk list is from a dynamic import. + Dynamic, +} diff --git a/crates/turbopack-dev/src/ecmascript/list/content.rs b/crates/turbopack-dev/src/ecmascript/list/content.rs index da3fcd582bb5a..01e69b0bc4716 100644 --- a/crates/turbopack-dev/src/ecmascript/list/content.rs +++ b/crates/turbopack-dev/src/ecmascript/list/content.rs @@ -1,43 +1,57 @@ -use anyhow::Result; +use std::io::Write; + +use anyhow::{Context, Result}; use indexmap::IndexMap; +use indoc::writedoc; +use serde::Serialize; use turbo_tasks::{IntoTraitRef, TryJoinIterExt}; -use turbo_tasks_fs::{FileContent, FileSystemPathReadRef, FileSystemPathVc}; +use turbo_tasks_fs::File; use turbopack_core::{ - asset::{Asset, AssetContent, AssetContentVc, AssetsVc}, + asset::{Asset, AssetContentVc}, + chunk::ChunkingContext, + code_builder::{CodeBuilder, CodeVc}, version::{ MergeableVersionedContent, MergeableVersionedContentVc, UpdateVc, VersionVc, VersionedContent, VersionedContentMerger, VersionedContentVc, VersionedContentsVc, }, }; +use turbopack_ecmascript::utils::StringifyJs; use super::{ + asset::{EcmascriptDevChunkListSource, EcmascriptDevChunkListVc}, update::update_chunk_list, - version::{ChunkListVersion, ChunkListVersionVc}, + version::{EcmascriptDevChunkListVersion, EcmascriptDevChunkListVersionVc}, }; -/// Contents of a [`super::asset::ChunkListAsset`]. +/// Contents of an [`EcmascriptDevChunkList`]. #[turbo_tasks::value] -pub(super) struct ChunkListContent { - pub server_root: FileSystemPathReadRef, - pub chunks_contents: IndexMap, +pub(super) struct EcmascriptDevChunkListContent { + chunk_list_path: String, + pub(super) chunks_contents: IndexMap, + source: EcmascriptDevChunkListSource, } #[turbo_tasks::value_impl] -impl ChunkListContentVc { - /// Creates a new [`ChunkListContent`]. +impl EcmascriptDevChunkListContentVc { + /// Creates a new [`EcmascriptDevChunkListContent`]. #[turbo_tasks::function] - pub async fn new(server_root: FileSystemPathVc, chunks: AssetsVc) -> Result { - let server_root = server_root.await?; - Ok(ChunkListContent { - server_root: server_root.clone(), - chunks_contents: chunks + pub async fn new(chunk_list: EcmascriptDevChunkListVc) -> Result { + let chunk_list_ref = chunk_list.await?; + let output_root = chunk_list_ref.chunking_context.output_root().await?; + Ok(EcmascriptDevChunkListContent { + chunk_list_path: output_root + .get_path_to(&*chunk_list.ident().path().await?) + .context("chunk list path not in output root")? + .to_string(), + chunks_contents: chunk_list_ref + .chunks .await? .iter() .map(|chunk| { - let server_root = server_root.clone(); + let output_root = output_root.clone(); async move { Ok(( - server_root + output_root .get_path_to(&*chunk.ident().path().await?) .map(|path| path.to_string()), chunk.versioned_content(), @@ -49,13 +63,14 @@ impl ChunkListContentVc { .into_iter() .filter_map(|(path, content)| path.map(|path| (path, content))) .collect(), + source: chunk_list_ref.source, } .cell()) } /// Computes the version of this content. #[turbo_tasks::function] - pub async fn version(self) -> Result { + pub async fn version(self) -> Result { let this = self.await?; let mut by_merger = IndexMap::<_, Vec<_>>::new(); @@ -95,24 +110,67 @@ impl ChunkListContentVc { .into_iter() .collect(); - Ok(ChunkListVersion { by_path, by_merger }.cell()) + Ok(EcmascriptDevChunkListVersion { by_path, by_merger }.cell()) + } + + #[turbo_tasks::function] + pub(super) async fn code(self) -> Result { + let this = self.await?; + + let params = EcmascriptDevChunkListParams { + path: &this.chunk_list_path, + chunks: this.chunks_contents.keys().map(|s| s.as_str()).collect(), + source: this.source, + }; + + let mut code = CodeBuilder::default(); + + // When loaded, JS chunks must register themselves with the `TURBOPACK` global + // variable. Similarly, we register the chunk list with the + // `TURBOPACK_CHUNK_LISTS` global variable. + writedoc!( + code, + r#" + (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + {}, + {{}}, + ]); + (globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({:#}); + "#, + StringifyJs(&this.chunk_list_path), + StringifyJs(¶ms), + )?; + + Ok(CodeVc::cell(code.build())) } } #[turbo_tasks::value_impl] -impl VersionedContent for ChunkListContent { +impl VersionedContent for EcmascriptDevChunkListContent { #[turbo_tasks::function] - fn content(&self) -> AssetContentVc { - AssetContentVc::cell(AssetContent::File(FileContent::NotFound.into())) + async fn content(self_vc: EcmascriptDevChunkListContentVc) -> Result { + let code = self_vc.code().await?; + Ok(File::from(code.source_code().clone()).into()) } #[turbo_tasks::function] - fn version(self_vc: ChunkListContentVc) -> VersionVc { + fn version(self_vc: EcmascriptDevChunkListContentVc) -> VersionVc { self_vc.version().into() } #[turbo_tasks::function] - fn update(self_vc: ChunkListContentVc, from_version: VersionVc) -> UpdateVc { + fn update(self_vc: EcmascriptDevChunkListContentVc, from_version: VersionVc) -> UpdateVc { update_chunk_list(self_vc, from_version) } } + +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "camelCase")] +struct EcmascriptDevChunkListParams<'a> { + /// Path to the chunk list to register. + path: &'a str, + /// All chunks that belong to the chunk list. + chunks: Vec<&'a str>, + /// Where this chunk list is from. + source: EcmascriptDevChunkListSource, +} diff --git a/crates/turbopack-dev/src/ecmascript/list/mod.rs b/crates/turbopack-dev/src/ecmascript/list/mod.rs index 829280a02890e..cb3fddcd2276a 100644 --- a/crates/turbopack-dev/src/ecmascript/list/mod.rs +++ b/crates/turbopack-dev/src/ecmascript/list/mod.rs @@ -1,5 +1,4 @@ pub(crate) mod asset; pub(crate) mod content; -pub(crate) mod reference; pub(crate) mod update; pub(crate) mod version; diff --git a/crates/turbopack-dev/src/ecmascript/list/reference.rs b/crates/turbopack-dev/src/ecmascript/list/reference.rs deleted file mode 100644 index 9a6b201409c13..0000000000000 --- a/crates/turbopack-dev/src/ecmascript/list/reference.rs +++ /dev/null @@ -1,79 +0,0 @@ -use anyhow::Result; -use turbo_tasks::{primitives::StringVc, ValueToString, ValueToStringVc}; -use turbopack_core::{ - asset::{Asset, AssetsVc}, - chunk::{ - ChunkVc, ChunkableAssetReference, ChunkableAssetReferenceVc, ChunkingType, - ChunkingTypeOptionVc, - }, - reference::{AssetReference, AssetReferenceVc}, - resolve::{ResolveResult, ResolveResultVc}, -}; - -use super::asset::ChunkListAssetVc; -use crate::DevChunkingContextVc; - -/// A reference to a [`ChunkListAsset`]. -/// -/// This is the only way to create a [`ChunkListAsset`]. The asset itself will -/// live under the provided path. -/// -/// [`ChunkListAsset`]: super::asset::ChunkListAsset -#[turbo_tasks::value] -pub struct ChunkListReference { - chunking_context: DevChunkingContextVc, - entry_chunk: ChunkVc, - other_chunks: AssetsVc, -} - -#[turbo_tasks::value_impl] -impl ChunkListReferenceVc { - /// Creates a new [`ChunkListReference`]. - #[turbo_tasks::function] - pub fn new( - chunking_context: DevChunkingContextVc, - entry_chunk: ChunkVc, - other_chunks: AssetsVc, - ) -> Self { - ChunkListReference { - chunking_context, - entry_chunk, - other_chunks, - } - .cell() - } -} - -#[turbo_tasks::value_impl] -impl ValueToString for ChunkListReference { - #[turbo_tasks::function] - async fn to_string(&self) -> Result { - Ok(StringVc::cell(format!( - "referenced chunk list {}", - self.chunking_context - .chunk_list_path(self.entry_chunk.ident()) - .to_string() - .await? - ))) - } -} - -#[turbo_tasks::value_impl] -impl AssetReference for ChunkListReference { - #[turbo_tasks::function] - fn resolve_reference(&self) -> ResolveResultVc { - ResolveResult::asset( - ChunkListAssetVc::new(self.chunking_context, self.entry_chunk, self.other_chunks) - .into(), - ) - .cell() - } -} - -#[turbo_tasks::value_impl] -impl ChunkableAssetReference for ChunkListReference { - #[turbo_tasks::function] - fn chunking_type(&self) -> ChunkingTypeOptionVc { - ChunkingTypeOptionVc::cell(Some(ChunkingType::Separate)) - } -} diff --git a/crates/turbopack-dev/src/ecmascript/list/update.rs b/crates/turbopack-dev/src/ecmascript/list/update.rs index 90197992d4b62..251dcc5b0cb20 100644 --- a/crates/turbopack-dev/src/ecmascript/list/update.rs +++ b/crates/turbopack-dev/src/ecmascript/list/update.rs @@ -9,7 +9,7 @@ use turbopack_core::version::{ UpdateVc, VersionVc, VersionedContent, VersionedContentMerger, VersionedContentsVc, }; -use super::{content::ChunkListContentVc, version::ChunkListVersionVc}; +use super::{content::EcmascriptDevChunkListContentVc, version::EcmascriptDevChunkListVersionVc}; /// Update of a chunk list from one version to another. #[derive(Serialize)] @@ -50,19 +50,20 @@ impl<'a> ChunkListUpdate<'a> { /// Computes the update of a chunk list from one version to another. #[turbo_tasks::function] pub(super) async fn update_chunk_list( - content: ChunkListContentVc, + content: EcmascriptDevChunkListContentVc, from_version: VersionVc, ) -> Result { let to_version = content.version(); - let from_version = if let Some(from) = ChunkListVersionVc::resolve_from(from_version).await? { - from - } else { - // It's likely `from_version` is `NotFoundVersion`. - return Ok(Update::Total(TotalUpdate { - to: to_version.as_version().into_trait_ref().await?, - }) - .cell()); - }; + let from_version = + if let Some(from) = EcmascriptDevChunkListVersionVc::resolve_from(from_version).await? { + from + } else { + // It's likely `from_version` is `NotFoundVersion`. + return Ok(Update::Total(TotalUpdate { + to: to_version.as_version().into_trait_ref().await?, + }) + .cell()); + }; let to = to_version.await?; let from = from_version.await?; diff --git a/crates/turbopack-dev/src/ecmascript/list/version.rs b/crates/turbopack-dev/src/ecmascript/list/version.rs index 6d79b55ae48df..1c787a7f49d09 100644 --- a/crates/turbopack-dev/src/ecmascript/list/version.rs +++ b/crates/turbopack-dev/src/ecmascript/list/version.rs @@ -4,11 +4,11 @@ use turbo_tasks::{primitives::StringVc, TraitRef, TryJoinIterExt}; use turbo_tasks_hash::{encode_hex, Xxh3Hash64Hasher}; use turbopack_core::version::{Version, VersionVc, VersionedContentMergerVc}; -/// The version of a [`ChunkListContent`]. +/// The version of a [`EcmascriptDevChunkListContent`]. /// -/// [`ChunkListContent`]: super::content::ChunkListContent +/// [`EcmascriptDevChunkListContent`]: super::content::EcmascriptDevChunkListContent #[turbo_tasks::value(shared)] -pub(super) struct ChunkListVersion { +pub(super) struct EcmascriptDevChunkListVersion { /// A map from chunk path to its version. #[turbo_tasks(trace_ignore)] pub by_path: IndexMap>, @@ -18,7 +18,7 @@ pub(super) struct ChunkListVersion { } #[turbo_tasks::value_impl] -impl Version for ChunkListVersion { +impl Version for EcmascriptDevChunkListVersion { #[turbo_tasks::function] async fn id(&self) -> Result { let by_path = { diff --git a/crates/turbopack-dev/src/ecmascript/manifest/chunk_asset.rs b/crates/turbopack-dev/src/ecmascript/manifest/chunk_asset.rs index 446d0799ea6fa..207aa14ebc83d 100644 --- a/crates/turbopack-dev/src/ecmascript/manifest/chunk_asset.rs +++ b/crates/turbopack-dev/src/ecmascript/manifest/chunk_asset.rs @@ -1,12 +1,10 @@ -use std::iter::once; - use anyhow::{bail, Result}; use turbo_tasks::{primitives::StringVc, Value}; use turbopack_core::{ - asset::{Asset, AssetContentVc, AssetVc}, + asset::{Asset, AssetContentVc, AssetVc, AssetsVc}, chunk::{ - availability_info::AvailabilityInfo, ChunkGroupVc, ChunkVc, ChunkableAsset, - ChunkableAssetVc, ChunkingContext, ChunkingContextVc, + availability_info::AvailabilityInfo, ChunkVc, ChunkableAsset, ChunkableAssetVc, + ChunkingContext, ChunkingContextVc, }, ident::AssetIdentVc, reference::{AssetReferencesVc, SingleAssetReferenceVc}, @@ -17,7 +15,7 @@ use turbopack_ecmascript::chunk::{ }; use super::chunk_item::DevManifestChunkItem; -use crate::{ecmascript::list::reference::ChunkListReferenceVc, DevChunkingContextVc}; +use crate::DevChunkingContextVc; #[turbo_tasks::function] fn modifier() -> StringVc { @@ -57,15 +55,20 @@ impl DevManifestChunkAssetVc { } #[turbo_tasks::function] - pub(super) async fn chunk_group(self) -> Result { + pub(super) async fn entry_chunk(self) -> Result { let this = self.await?; - Ok(ChunkGroupVc::from_asset( - this.asset, + Ok(this.asset.as_chunk( this.chunking_context.into(), Value::new(this.availability_info), )) } + #[turbo_tasks::function] + pub(super) async fn chunks(self) -> Result { + let this = self.await?; + Ok(this.chunking_context.chunk_group(self.entry_chunk())) + } + #[turbo_tasks::function] pub async fn manifest_chunk(self) -> Result { let this = self.await?; @@ -95,9 +98,7 @@ impl Asset for DevManifestChunkAsset { #[turbo_tasks::function] async fn references(self_vc: DevManifestChunkAssetVc) -> Result { - let this = self_vc.await?; - let chunk_group = self_vc.chunk_group(); - let chunks = chunk_group.chunks(); + let chunks = self_vc.chunks(); Ok(AssetReferencesVc::cell( chunks @@ -108,16 +109,6 @@ impl Asset for DevManifestChunkAsset { SingleAssetReferenceVc::new(chunk, dev_manifest_chunk_reference_description()) .into() }) - .chain(once( - // This creates the chunk list corresponding to the manifest chunk's chunk - // group. - ChunkListReferenceVc::new( - this.chunking_context, - chunk_group.entry(), - chunk_group.chunks(), - ) - .into(), - )) .collect(), )) } diff --git a/crates/turbopack-dev/src/ecmascript/manifest/chunk_item.rs b/crates/turbopack-dev/src/ecmascript/manifest/chunk_item.rs index 05c5d9580386c..85e6e42116e40 100644 --- a/crates/turbopack-dev/src/ecmascript/manifest/chunk_item.rs +++ b/crates/turbopack-dev/src/ecmascript/manifest/chunk_item.rs @@ -1,7 +1,6 @@ -use anyhow::{anyhow, Result}; +use anyhow::Result; use indexmap::IndexSet; use indoc::formatdoc; -use serde::Serialize; use turbopack_core::{ asset::Asset, chunk::{ChunkItem, ChunkItemVc, ChunkingContext}, @@ -37,8 +36,7 @@ impl EcmascriptChunkItem for DevManifestChunkItem { #[turbo_tasks::function] async fn content(&self) -> Result { - let chunk_group = self.manifest.chunk_group(); - let chunks = chunk_group.chunks().await?; + let chunks = self.manifest.chunks().await?; let output_root = self.context.output_root().await?; let mut chunk_server_paths = IndexSet::new(); @@ -56,29 +54,11 @@ impl EcmascriptChunkItem for DevManifestChunkItem { }; } - let chunk_list_path = self - .context - .chunk_list_path(chunk_group.entry().ident()) - .await?; - let chunk_list_path = output_root - .get_path_to(&chunk_list_path) - .ok_or(anyhow!("chunk list path is not in output root"))?; - - #[derive(Serialize)] - #[serde(rename_all = "camelCase")] - struct ManifestChunkExport<'a> { - chunks: IndexSet, - list: &'a str, - } - let code = formatdoc! { r#" __turbopack_export_value__({:#}); "#, - StringifyJs(&ManifestChunkExport { - chunks: chunk_server_paths, - list: chunk_list_path, - }) + StringifyJs(&chunk_server_paths) }; Ok(EcmascriptChunkItemContent { diff --git a/crates/turbopack-dev/src/ecmascript/manifest/loader_item.rs b/crates/turbopack-dev/src/ecmascript/manifest/loader_item.rs index ac010627d3de9..05af7f861b5f7 100644 --- a/crates/turbopack-dev/src/ecmascript/manifest/loader_item.rs +++ b/crates/turbopack-dev/src/ecmascript/manifest/loader_item.rs @@ -136,8 +136,7 @@ impl EcmascriptChunkItem for DevManifestLoaderItem { __turbopack_export_value__((__turbopack_import__) => {{ return __turbopack_load__({chunk_server_path}).then(() => {{ return __turbopack_require__({item_id}); - }}).then(({{ chunks, list }}) => {{ - __turbopack_register_chunk_list__(list, chunks); + }}).then((chunks) => {{ return Promise.all(chunks.map((chunk_path) => __turbopack_load__(chunk_path))); }}).then(() => {{ return __turbopack_import__({dynamic_id}); diff --git a/crates/turbopack-dev/src/ecmascript/module_factory.rs b/crates/turbopack-dev/src/ecmascript/module_factory.rs index d78f7ad74f2d6..843305c2daf31 100644 --- a/crates/turbopack-dev/src/ecmascript/module_factory.rs +++ b/crates/turbopack-dev/src/ecmascript/module_factory.rs @@ -15,7 +15,6 @@ pub(super) async fn module_factory(content: EcmascriptChunkItemContentVc) -> Res "v: __turbopack_export_value__", "c: __turbopack_cache__", "l: __turbopack_load__", - "k: __turbopack_register_chunk_list__", "j: __turbopack_cjs__", "g: global", // HACK diff --git a/crates/turbopack-dev/src/lib.rs b/crates/turbopack-dev/src/lib.rs index 41b0adfd3c525..e4e59970022c9 100644 --- a/crates/turbopack-dev/src/lib.rs +++ b/crates/turbopack-dev/src/lib.rs @@ -5,7 +5,6 @@ pub(crate) mod chunking_context; pub(crate) mod ecmascript; pub use chunking_context::{DevChunkingContext, DevChunkingContextBuilder, DevChunkingContextVc}; -pub use ecmascript::list::reference::{ChunkListReference, ChunkListReferenceVc}; pub fn register() { turbo_tasks::register(); diff --git a/crates/turbopack-ecmascript/src/chunk/content.rs b/crates/turbopack-ecmascript/src/chunk/content.rs index 5bac4d84ff12e..d6c0cfca6f82f 100644 --- a/crates/turbopack-ecmascript/src/chunk/content.rs +++ b/crates/turbopack-ecmascript/src/chunk/content.rs @@ -4,7 +4,7 @@ use turbo_tasks::Value; use turbopack_core::{ chunk::{ availability_info::AvailabilityInfo, chunk_content, chunk_content_split, - ChunkContentResult, ChunkGroupVc, ChunkVc, + ChunkContentResult, ChunkVc, }, reference::AssetReferenceVc, }; @@ -19,7 +19,7 @@ use super::{ pub struct EcmascriptChunkContent { pub chunk_items: Vec, pub chunks: Vec, - pub async_chunk_groups: Vec, + pub async_chunk_group_entries: Vec, pub external_asset_references: Vec, pub availability_info: AvailabilityInfo, } @@ -29,7 +29,7 @@ impl From> for EcmascriptChunkContent EcmascriptChunkContent { chunk_items: from.chunk_items, chunks: from.chunks, - async_chunk_groups: from.async_chunk_groups, + async_chunk_group_entries: from.async_chunk_group_entries, external_asset_references: from.external_asset_references, availability_info: from.availability_info, } @@ -80,27 +80,27 @@ async fn ecmascript_chunk_content_internal( let mut all_chunk_items = IndexSet::::new(); let mut all_chunks = IndexSet::::new(); - let mut all_async_chunk_groups = IndexSet::::new(); + let mut all_async_chunk_group_entries = IndexSet::::new(); let mut all_external_asset_references = IndexSet::::new(); for content in contents { let EcmascriptChunkContent { chunk_items, chunks, - async_chunk_groups, + async_chunk_group_entries, external_asset_references, availability_info: _, } = &*content.await?; all_chunk_items.extend(chunk_items.iter().copied()); all_chunks.extend(chunks.iter().copied()); - all_async_chunk_groups.extend(async_chunk_groups.iter().copied()); + all_async_chunk_group_entries.extend(async_chunk_group_entries.iter().copied()); all_external_asset_references.extend(external_asset_references.iter().copied()); } Ok(EcmascriptChunkContent { chunk_items: all_chunk_items.into_iter().collect(), chunks: all_chunks.into_iter().collect(), - async_chunk_groups: all_async_chunk_groups.into_iter().collect(), + async_chunk_group_entries: all_async_chunk_group_entries.into_iter().collect(), external_asset_references: all_external_asset_references.into_iter().collect(), availability_info: availability_info.into_value(), } diff --git a/crates/turbopack-ecmascript/src/chunk/mod.rs b/crates/turbopack-ecmascript/src/chunk/mod.rs index a28e41e834511..7b6ca24c49eaa 100644 --- a/crates/turbopack-ecmascript/src/chunk/mod.rs +++ b/crates/turbopack-ecmascript/src/chunk/mod.rs @@ -372,8 +372,8 @@ impl Asset for EcmascriptChunk { for chunk in content.chunks.iter() { references.push(ChunkReferenceVc::new_parallel(*chunk).into()); } - for chunk_group in content.async_chunk_groups.iter() { - references.push(ChunkGroupReferenceVc::new(*chunk_group).into()); + for entry in content.async_chunk_group_entries.iter() { + references.push(ChunkGroupReferenceVc::new(this.context.into(), *entry).into()); } Ok(AssetReferencesVc::cell(references)) diff --git a/crates/turbopack-ecmascript/src/chunk/optimize.rs b/crates/turbopack-ecmascript/src/chunk/optimize.rs index d212c74e9f9eb..d3a2b9f56327e 100644 --- a/crates/turbopack-ecmascript/src/chunk/optimize.rs +++ b/crates/turbopack-ecmascript/src/chunk/optimize.rs @@ -8,7 +8,7 @@ use turbo_tasks::{TryJoinIterExt, Value}; use turbo_tasks_fs::FileSystemPathOptionVc; use turbopack_core::chunk::{ optimize::{optimize_by_common_parent, ChunkOptimizer, ChunkOptimizerVc}, - ChunkGroupVc, ChunkVc, ChunksVc, + ChunkVc, ChunksVc, }; use super::{EcmascriptChunkPlaceablesVc, EcmascriptChunkVc, EcmascriptChunkingContextVc}; @@ -27,9 +27,9 @@ impl EcmascriptChunkOptimizerVc { #[turbo_tasks::value_impl] impl ChunkOptimizer for EcmascriptChunkOptimizer { #[turbo_tasks::function] - async fn optimize(&self, chunks: ChunksVc, chunk_group: ChunkGroupVc) -> Result { + async fn optimize(&self, chunks: ChunksVc) -> Result { optimize_by_common_parent(chunks, get_common_parent, |local, children| { - optimize_ecmascript(local, children, chunk_group) + optimize_ecmascript(local, children) }) .await } @@ -373,11 +373,7 @@ async fn merge_by_size( /// Chunk optimization for ecmascript chunks. #[turbo_tasks::function] -async fn optimize_ecmascript( - local: Option, - children: Vec, - _chunk_group: ChunkGroupVc, -) -> Result { +async fn optimize_ecmascript(local: Option, children: Vec) -> Result { let mut chunks = Vec::<(EcmascriptChunkVc, Option)>::new(); // TODO optimize let mut unoptimized_count = 0; diff --git a/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs b/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs index cffcf6bad3071..8a474712880d6 100644 --- a/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs +++ b/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs @@ -3,10 +3,10 @@ use indexmap::IndexSet; use turbo_tasks::{primitives::StringVc, TryJoinIterExt, Value, ValueToString}; use turbo_tasks_fs::FileSystemPathVc; use turbopack_core::{ - asset::{Asset, AssetContentVc, AssetVc}, + asset::{Asset, AssetContentVc, AssetVc, AssetsVc}, chunk::{ - availability_info::AvailabilityInfo, ChunkGroupVc, ChunkItem, ChunkItemVc, ChunkVc, - ChunkableAsset, ChunkableAssetVc, ChunkingContextVc, EvaluatableAssetsVc, + availability_info::AvailabilityInfo, ChunkItem, ChunkItemVc, ChunkVc, ChunkableAsset, + ChunkableAssetVc, ChunkingContext, ChunkingContextVc, EvaluatableAssetsVc, }, ident::AssetIdentVc, introspect::{ @@ -44,27 +44,22 @@ pub struct ChunkGroupFilesAsset { #[turbo_tasks::value_impl] impl ChunkGroupFilesAssetVc { #[turbo_tasks::function] - async fn chunk_group(self) -> Result { + async fn chunks(self) -> Result { let this = self.await?; - let chunk_group = + Ok( if let Some(ecma) = EcmascriptModuleAssetVc::resolve_from(this.asset).await? { - ChunkGroupVc::evaluated( - this.chunking_context, - ecma.into(), + this.chunking_context.evaluated_chunk_group( + ecma.as_root_chunk(this.chunking_context), this.runtime_entries .clone() - .unwrap_or_else(EvaluatableAssetsVc::empty), + .unwrap_or_else(EvaluatableAssetsVc::empty) + .with_entry(ecma.into()), ) } else { - ChunkGroupVc::from_asset( - this.asset, - this.chunking_context, - Value::new(AvailabilityInfo::Root { - current_availability_root: this.asset.into(), - }), - ) - }; - Ok(chunk_group) + this.chunking_context + .chunk_group(this.asset.as_root_chunk(this.chunking_context)) + }, + ) } } @@ -87,7 +82,7 @@ impl Asset for ChunkGroupFilesAsset { #[turbo_tasks::function] async fn references(self_vc: ChunkGroupFilesAssetVc) -> Result { - let chunks = self_vc.chunk_group().chunks(); + let chunks = self_vc.chunks(); Ok(AssetReferencesVc::cell( chunks @@ -166,7 +161,7 @@ impl EcmascriptChunkItem for ChunkGroupFilesChunkItem { #[turbo_tasks::function] async fn content(&self) -> Result { - let chunks = self.inner.chunk_group().chunks(); + let chunks = self.inner.chunks(); let client_root = self.client_root.await?; let chunks_paths = chunks .await? @@ -224,7 +219,7 @@ impl Introspectable for ChunkGroupFilesAsset { async fn children(self_vc: ChunkGroupFilesAssetVc) -> Result { let mut children = IndexSet::new(); let chunk_ty = StringVc::cell("chunk".to_string()); - for &chunk in self_vc.chunk_group().chunks().await?.iter() { + for &chunk in self_vc.chunks().await?.iter() { children.insert((chunk_ty, IntrospectableAssetVc::new(chunk.into()))); } children.insert(( diff --git a/crates/turbopack-node/src/bootstrap.rs b/crates/turbopack-node/src/bootstrap.rs index dd6054b64d8f7..d164feab80848 100644 --- a/crates/turbopack-node/src/bootstrap.rs +++ b/crates/turbopack-node/src/bootstrap.rs @@ -4,8 +4,7 @@ use anyhow::Result; use turbo_tasks::primitives::StringVc; use turbo_tasks_fs::{File, FileSystemPathVc}; use turbopack_core::{ - asset::{Asset, AssetContentVc, AssetVc}, - chunk::ChunkGroupVc, + asset::{Asset, AssetContentVc, AssetVc, AssetsVc}, ident::AssetIdentVc, reference::{AssetReferencesVc, SingleAssetReferenceVc}, }; @@ -14,7 +13,7 @@ use turbopack_ecmascript::utils::StringifyJs; #[turbo_tasks::value(shared)] pub(super) struct NodeJsBootstrapAsset { pub(super) path: FileSystemPathVc, - pub(super) chunk_group: ChunkGroupVc, + pub(super) chunks: AssetsVc, } #[turbo_tasks::function] @@ -37,7 +36,7 @@ impl Asset for NodeJsBootstrapAsset { // but until then this is a simple hack to make it work for now let mut output = "Error.stackTraceLimit = 100;\nglobal.self = global;\n".to_string(); - for chunk in self.chunk_group.chunks().await?.iter() { + for chunk in self.chunks.await?.iter() { let path = &*chunk.ident().path().await?; if let Some(p) = context_path.get_relative_path_to(path) { if p.ends_with(".js") { @@ -51,7 +50,7 @@ impl Asset for NodeJsBootstrapAsset { #[turbo_tasks::function] async fn references(&self) -> Result { - let chunks = self.chunk_group.chunks().await?; + let chunks = self.chunks.await?; let mut references = Vec::new(); for chunk in chunks.iter() { references.push( diff --git a/crates/turbopack-node/src/evaluate.rs b/crates/turbopack-node/src/evaluate.rs index 5cc3b9df80e16..a18353a19b4f0 100644 --- a/crates/turbopack-node/src/evaluate.rs +++ b/crates/turbopack-node/src/evaluate.rs @@ -24,7 +24,7 @@ use turbo_tasks_fs::{ }; use turbopack_core::{ asset::{Asset, AssetVc}, - chunk::{ChunkGroupVc, ChunkingContext, ChunkingContextVc, EvaluatableAssetsVc}, + chunk::{ChunkableAsset, ChunkingContext, ChunkingContextVc, EvaluatableAssetsVc}, context::{AssetContext, AssetContextVc}, ident::AssetIdentVc, issue::{Issue, IssueSeverity, IssueSeverityVc, IssueVc}, @@ -182,10 +182,9 @@ pub async fn get_evaluate_pool( let bootstrap = NodeJsBootstrapAsset { path, - chunk_group: ChunkGroupVc::evaluated( - chunking_context, - entry_module.into(), - runtime_entries, + chunks: chunking_context.evaluated_chunk_group( + entry_module.as_root_chunk(chunking_context), + runtime_entries.with_entry(entry_module.into()), ), } .cell() diff --git a/crates/turbopack-node/src/lib.rs b/crates/turbopack-node/src/lib.rs index feb80175ac651..cfeba27c8d6ea 100644 --- a/crates/turbopack-node/src/lib.rs +++ b/crates/turbopack-node/src/lib.rs @@ -18,7 +18,7 @@ use turbo_tasks_fs::{to_sys_path, File, FileContent, FileSystemPathVc}; use turbopack_core::{ asset::{Asset, AssetVc, AssetsSetVc}, chunk::{ - ChunkGroupVc, ChunkingContext, ChunkingContextVc, EvaluatableAssetVc, EvaluatableAssetsVc, + ChunkableAsset, ChunkingContext, ChunkingContextVc, EvaluatableAssetVc, EvaluatableAssetsVc, }, reference::primary_referenced_assets, source_map::GenerateSourceMapVc, @@ -264,10 +264,13 @@ pub async fn get_intermediate_asset( main_entry: EvaluatableAssetVc, other_entries: EvaluatableAssetsVc, ) -> Result { - let chunk_group = ChunkGroupVc::evaluated(chunking_context, main_entry, other_entries); + let chunks = chunking_context.evaluated_chunk_group( + main_entry.as_root_chunk(chunking_context), + other_entries.with_entry(main_entry), + ); Ok(NodeJsBootstrapAsset { path: chunking_context.chunk_path(main_entry.ident(), ".js"), - chunk_group, + chunks, } .cell() .into()) diff --git a/crates/turbopack-tests/tests/snapshot.rs b/crates/turbopack-tests/tests/snapshot.rs index 62b71e2922037..b9c167f9dd2bb 100644 --- a/crates/turbopack-tests/tests/snapshot.rs +++ b/crates/turbopack-tests/tests/snapshot.rs @@ -28,7 +28,7 @@ use turbopack::{ use turbopack_core::{ asset::{Asset, AssetVc}, chunk::{ - ChunkGroupVc, ChunkableAsset, ChunkableAssetVc, EvaluatableAssetVc, EvaluatableAssetsVc, + ChunkableAsset, ChunkableAssetVc, ChunkingContext, EvaluatableAssetVc, EvaluatableAssetsVc, }, compile_time_defines, compile_time_info::CompileTimeInfo, @@ -253,16 +253,14 @@ async fn run_test(resource: &str) -> Result { .map(|module| async move { if let Some(ecmascript) = EcmascriptModuleAssetVc::resolve_from(module).await? { // TODO: Load runtime entries from snapshots - Ok(ChunkGroupVc::evaluated( - chunking_context, - ecmascript.into(), - runtime_entries.unwrap_or_else(EvaluatableAssetsVc::empty), + Ok(chunking_context.evaluated_chunk_group( + ecmascript.as_root_chunk(chunking_context), + runtime_entries + .unwrap_or_else(EvaluatableAssetsVc::empty) + .with_entry(ecmascript.into()), )) } else if let Some(chunkable) = ChunkableAssetVc::resolve_from(module).await? { - Ok(ChunkGroupVc::from_chunk( - chunking_context, - chunkable.as_root_chunk(chunking_context), - )) + Ok(chunking_context.chunk_group(chunkable.as_root_chunk(chunking_context))) } else { // TODO convert into a serve-able asset Err(anyhow!( @@ -276,8 +274,8 @@ async fn run_test(resource: &str) -> Result { let mut seen = HashSet::new(); let mut queue = VecDeque::with_capacity(32); - for chunk_group in chunk_groups { - for chunk in &*chunk_group.chunks().await? { + for chunks in chunk_groups { + for chunk in &*chunks.await? { queue.push_back(*chunk); } } diff --git a/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/20803_bar_index_c8a3ce.js b/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/20803_bar_index_c8a3ce.js index aade09872750d..f0c6edec122ab 100644 --- a/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/20803_bar_index_c8a3ce.js +++ b/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/20803_bar_index_c8a3ce.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/20803_bar_index_c8a3ce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/basic/async_chunk/input/node_modules/bar/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/basic/async_chunk/input/node_modules/bar/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { __turbopack_esm__({ "bar": ()=>bar diff --git a/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/20803_foo_index_5f9e1e.js b/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/20803_foo_index_5f9e1e.js index f73cdb22ff5bd..8c202fb984c26 100644 --- a/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/20803_foo_index_5f9e1e.js +++ b/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/20803_foo_index_5f9e1e.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/20803_foo_index_5f9e1e.js", { -"[project]/crates/turbopack-tests/tests/snapshot/basic/async_chunk/input/node_modules/foo/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/basic/async_chunk/input/node_modules/foo/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { __turbopack_esm__({ "foo": ()=>foo diff --git a/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_import_571bb9.js b/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_import_571bb9.js index babc99391f570..42399307c437c 100644 --- a/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_import_571bb9.js +++ b/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_import_571bb9.js @@ -1,14 +1,12 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_import_571bb9.js", { -"[project]/crates/turbopack-tests/tests/snapshot/basic/async_chunk/input/import.js (ecmascript, manifest chunk)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/basic/async_chunk/input/import.js (ecmascript, manifest chunk)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { -__turbopack_export_value__({ - "chunks": [ - "output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_import_a29514.js", - "output/20803_foo_index_5f9e1e.js" - ], - "list": "output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_import.js_f60aac._.json" -}); +__turbopack_export_value__([ + "output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_import_a29514.js", + "output/20803_foo_index_5f9e1e.js", + "output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_import_75e817.js" +]); })()), }]); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_import_75e817.js b/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_import_75e817.js new file mode 100644 index 0000000000000..d4b63dc3fec58 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_import_75e817.js @@ -0,0 +1,12 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_import_75e817.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_import_75e817.js", + "chunks": [ + "output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_import_a29514.js", + "output/20803_foo_index_5f9e1e.js" + ], + "source": "dynamic" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_import_a29514.js b/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_import_a29514.js index dbc2b4753d0f1..92c03eb16f275 100644 --- a/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_import_a29514.js +++ b/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_import_a29514.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_import_a29514.js", { -"[project]/crates/turbopack-tests/tests/snapshot/basic/async_chunk/input/import.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/basic/async_chunk/input/import.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2f$async_chunk$2f$input$2f$node_modules$2f$foo$2f$index$2e$js__$28$ecmascript$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic/async_chunk/input/node_modules/foo/index.js (ecmascript)"); var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2f$async_chunk$2f$input$2f$node_modules$2f$bar$2f$index$2e$js__$28$ecmascript$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic/async_chunk/input/node_modules/bar/index.js (ecmascript)"); diff --git a/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_index_0d348e.js b/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_index_0d348e.js index 8c9e67a286ca0..b214a8062c71e 100644 --- a/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_index_0d348e.js +++ b/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_index_0d348e.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_index_0d348e.js", {}, - {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_index_b53fce.js","output/20803_bar_index_c8a3ce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/basic/async_chunk/input/index.js (ecmascript)"],"chunkListPath":"output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_index.js_f5704b._.json"} + {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_index_b53fce.js","output/20803_bar_index_c8a3ce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/basic/async_chunk/input/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_index_b53fce.js index 2a5530ef75c88..6e88edf9567a4 100644 --- a/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_index_b53fce.js @@ -1,12 +1,11 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_index_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/basic/async_chunk/input/import.js (ecmascript, manifest chunk, loader)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/basic/async_chunk/input/import.js (ecmascript, manifest chunk, loader)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { __turbopack_export_value__((__turbopack_import__) => { return __turbopack_load__("output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_import_571bb9.js").then(() => { return __turbopack_require__("[project]/crates/turbopack-tests/tests/snapshot/basic/async_chunk/input/import.js (ecmascript, manifest chunk)"); - }).then(({ chunks, list }) => { - __turbopack_register_chunk_list__(list, chunks); + }).then((chunks) => { return Promise.all(chunks.map((chunk_path) => __turbopack_load__(chunk_path))); }).then(() => { return __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic/async_chunk/input/import.js (ecmascript)"); @@ -14,11 +13,11 @@ __turbopack_export_value__((__turbopack_import__) => { }); })()), -"[project]/crates/turbopack-tests/tests/snapshot/basic/async_chunk/input/shared.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/snapshot/basic/async_chunk/input/shared.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { }.call(this) }), -"[project]/crates/turbopack-tests/tests/snapshot/basic/async_chunk/input/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/basic/async_chunk/input/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2f$async_chunk$2f$input$2f$node_modules$2f$bar$2f$index$2e$js__$28$ecmascript$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic/async_chunk/input/node_modules/bar/index.js (ecmascript)"); var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2f$async_chunk$2f$input$2f$shared$2e$js__$28$ecmascript$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic/async_chunk/input/shared.js (ecmascript)"); diff --git a/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_index_b53fce.js.map b/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_index_b53fce.js.map index bd8c3e6f628b7..769c661f5b711 100644 --- a/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_index_b53fce.js.map +++ b/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_index_b53fce.js.map @@ -1,8 +1,8 @@ { "version": 3, "sections": [ - {"offset": {"line": 18, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}}, - {"offset": {"line": 18, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 22, "column": 0}, "map": {"version":3,"sources":["/turbopack/[project]/crates/turbopack-tests/tests/snapshot/basic/async_chunk/input/index.js"],"sourcesContent":["import { bar } from \"bar\";\nimport \"./shared\";\n\nbar(true);\n\nimport(\"./import\").then(({ foo }) => {\n foo(true);\n});\n"],"names":[],"mappings":";;;;;AAGA,sMAAI,IAAI;AAER,sKAAmB,IAAI,CAAC,CAAC,EAAE,IAAG,EAAE,GAAK;IACnC,IAAI,IAAI;AACV"}}, - {"offset": {"line": 31, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}] + {"offset": {"line": 17, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}}, + {"offset": {"line": 17, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, + {"offset": {"line": 21, "column": 0}, "map": {"version":3,"sources":["/turbopack/[project]/crates/turbopack-tests/tests/snapshot/basic/async_chunk/input/index.js"],"sourcesContent":["import { bar } from \"bar\";\nimport \"./shared\";\n\nbar(true);\n\nimport(\"./import\").then(({ foo }) => {\n foo(true);\n});\n"],"names":[],"mappings":";;;;;AAGA,sMAAI,IAAI;AAER,sKAAmB,IAAI,CAAC,CAAC,EAAE,IAAG,EAAE,GAAK;IACnC,IAAI,IAAI;AACV"}}, + {"offset": {"line": 30, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}] } \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_index_fccff1.js b/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_index_fccff1.js new file mode 100644 index 0000000000000..042566cdcaf1d --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_index_fccff1.js @@ -0,0 +1,12 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_index_fccff1.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_index_fccff1.js", + "chunks": [ + "output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_index_b53fce.js", + "output/20803_bar_index_c8a3ce.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/basic/chunked/output/39e84_foo_index_4427e1.js b/crates/turbopack-tests/tests/snapshot/basic/chunked/output/39e84_foo_index_4427e1.js index 08d3e8bbdcb4f..193c7220aee6a 100644 --- a/crates/turbopack-tests/tests/snapshot/basic/chunked/output/39e84_foo_index_4427e1.js +++ b/crates/turbopack-tests/tests/snapshot/basic/chunked/output/39e84_foo_index_4427e1.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/39e84_foo_index_4427e1.js", { -"[project]/crates/turbopack-tests/tests/snapshot/basic/chunked/input/node_modules/foo/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/basic/chunked/input/node_modules/foo/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { __turbopack_esm__({ "foo": ()=>foo diff --git a/crates/turbopack-tests/tests/snapshot/basic/chunked/output/crates_turbopack-tests_tests_snapshot_basic_chunked_input_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/basic/chunked/output/crates_turbopack-tests_tests_snapshot_basic_chunked_input_index_b53fce.js index d5a4fdbde847c..dc97f617a094f 100644 --- a/crates/turbopack-tests/tests/snapshot/basic/chunked/output/crates_turbopack-tests_tests_snapshot_basic_chunked_input_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/basic/chunked/output/crates_turbopack-tests_tests_snapshot_basic_chunked_input_index_b53fce.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/crates_turbopack-tests_tests_snapshot_basic_chunked_input_index_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/basic/chunked/input/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/basic/chunked/input/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2f$chunked$2f$input$2f$node_modules$2f$foo$2f$index$2e$js__$28$ecmascript$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic/chunked/input/node_modules/foo/index.js (ecmascript)"); "__TURBOPACK__ecmascript__hoisting__location__"; diff --git a/crates/turbopack-tests/tests/snapshot/basic/chunked/output/crates_turbopack-tests_tests_snapshot_basic_chunked_input_index_d9f5b7.js b/crates/turbopack-tests/tests/snapshot/basic/chunked/output/crates_turbopack-tests_tests_snapshot_basic_chunked_input_index_d9f5b7.js new file mode 100644 index 0000000000000..34471ad880882 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic/chunked/output/crates_turbopack-tests_tests_snapshot_basic_chunked_input_index_d9f5b7.js @@ -0,0 +1,12 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/crates_turbopack-tests_tests_snapshot_basic_chunked_input_index_d9f5b7.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/crates_turbopack-tests_tests_snapshot_basic_chunked_input_index_d9f5b7.js", + "chunks": [ + "output/crates_turbopack-tests_tests_snapshot_basic_chunked_input_index_b53fce.js", + "output/39e84_foo_index_4427e1.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/basic/chunked/output/crates_turbopack-tests_tests_snapshot_basic_chunked_input_index_e77e9f.js b/crates/turbopack-tests/tests/snapshot/basic/chunked/output/crates_turbopack-tests_tests_snapshot_basic_chunked_input_index_e77e9f.js index eb972d334a385..2ff114b59100b 100644 --- a/crates/turbopack-tests/tests/snapshot/basic/chunked/output/crates_turbopack-tests_tests_snapshot_basic_chunked_input_index_e77e9f.js +++ b/crates/turbopack-tests/tests/snapshot/basic/chunked/output/crates_turbopack-tests_tests_snapshot_basic_chunked_input_index_e77e9f.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/crates_turbopack-tests_tests_snapshot_basic_chunked_input_index_e77e9f.js", {}, - {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_basic_chunked_input_index_b53fce.js","output/39e84_foo_index_4427e1.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/basic/chunked/input/index.js (ecmascript)"],"chunkListPath":"output/crates_turbopack-tests_tests_snapshot_basic_chunked_input_index.js_f5704b._.json"} + {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_basic_chunked_input_index_b53fce.js","output/39e84_foo_index_4427e1.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/basic/chunked/input/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/basic/shebang/output/crates_turbopack-tests_tests_snapshot_basic_shebang_input_index_b1f0c2.js b/crates/turbopack-tests/tests/snapshot/basic/shebang/output/crates_turbopack-tests_tests_snapshot_basic_shebang_input_index_b1f0c2.js index 22e8d1b4e582e..0814d610f3c17 100644 --- a/crates/turbopack-tests/tests/snapshot/basic/shebang/output/crates_turbopack-tests_tests_snapshot_basic_shebang_input_index_b1f0c2.js +++ b/crates/turbopack-tests/tests/snapshot/basic/shebang/output/crates_turbopack-tests_tests_snapshot_basic_shebang_input_index_b1f0c2.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/crates_turbopack-tests_tests_snapshot_basic_shebang_input_index_b1f0c2.js", {}, - {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_basic_shebang_input_index_b53fce.js","output/d1787_foo_index_dd389c.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/basic/shebang/input/index.js (ecmascript)"],"chunkListPath":"output/crates_turbopack-tests_tests_snapshot_basic_shebang_input_index.js_f5704b._.json"} + {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_basic_shebang_input_index_b53fce.js","output/d1787_foo_index_dd389c.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/basic/shebang/input/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/basic/shebang/output/crates_turbopack-tests_tests_snapshot_basic_shebang_input_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/basic/shebang/output/crates_turbopack-tests_tests_snapshot_basic_shebang_input_index_b53fce.js index dafdc96bd4185..dc959c70ed90a 100644 --- a/crates/turbopack-tests/tests/snapshot/basic/shebang/output/crates_turbopack-tests_tests_snapshot_basic_shebang_input_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/basic/shebang/output/crates_turbopack-tests_tests_snapshot_basic_shebang_input_index_b53fce.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/crates_turbopack-tests_tests_snapshot_basic_shebang_input_index_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/basic/shebang/input/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/basic/shebang/input/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2f$shebang$2f$input$2f$node_modules$2f$foo$2f$index$2e$js__$28$ecmascript$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic/shebang/input/node_modules/foo/index.js (ecmascript)"); "__TURBOPACK__ecmascript__hoisting__location__"; diff --git a/crates/turbopack-tests/tests/snapshot/basic/shebang/output/crates_turbopack-tests_tests_snapshot_basic_shebang_input_index_d00151.js b/crates/turbopack-tests/tests/snapshot/basic/shebang/output/crates_turbopack-tests_tests_snapshot_basic_shebang_input_index_d00151.js new file mode 100644 index 0000000000000..0a4d896ccd11f --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic/shebang/output/crates_turbopack-tests_tests_snapshot_basic_shebang_input_index_d00151.js @@ -0,0 +1,12 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/crates_turbopack-tests_tests_snapshot_basic_shebang_input_index_d00151.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/crates_turbopack-tests_tests_snapshot_basic_shebang_input_index_d00151.js", + "chunks": [ + "output/crates_turbopack-tests_tests_snapshot_basic_shebang_input_index_b53fce.js", + "output/d1787_foo_index_dd389c.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/basic/shebang/output/d1787_foo_index_dd389c.js b/crates/turbopack-tests/tests/snapshot/basic/shebang/output/d1787_foo_index_dd389c.js index 990bdf7f3cc4c..d7988d4c6f10f 100644 --- a/crates/turbopack-tests/tests/snapshot/basic/shebang/output/d1787_foo_index_dd389c.js +++ b/crates/turbopack-tests/tests/snapshot/basic/shebang/output/d1787_foo_index_dd389c.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/d1787_foo_index_dd389c.js", { -"[project]/crates/turbopack-tests/tests/snapshot/basic/shebang/input/node_modules/foo/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/basic/shebang/input/node_modules/foo/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { __turbopack_esm__({ "foo": ()=>foo diff --git a/crates/turbopack-tests/tests/snapshot/comptime/define/output/crates_turbopack-tests_tests_snapshot_comptime_define_input_index_047644.js b/crates/turbopack-tests/tests/snapshot/comptime/define/output/crates_turbopack-tests_tests_snapshot_comptime_define_input_index_047644.js new file mode 100644 index 0000000000000..030a8b4a21ece --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/comptime/define/output/crates_turbopack-tests_tests_snapshot_comptime_define_input_index_047644.js @@ -0,0 +1,11 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/crates_turbopack-tests_tests_snapshot_comptime_define_input_index_047644.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/crates_turbopack-tests_tests_snapshot_comptime_define_input_index_047644.js", + "chunks": [ + "output/crates_turbopack-tests_tests_snapshot_comptime_define_input_index_b53fce.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/comptime/define/output/crates_turbopack-tests_tests_snapshot_comptime_define_input_index_6b0d2b.js b/crates/turbopack-tests/tests/snapshot/comptime/define/output/crates_turbopack-tests_tests_snapshot_comptime_define_input_index_6b0d2b.js index 72d9656a3ea38..97afbe19c6652 100644 --- a/crates/turbopack-tests/tests/snapshot/comptime/define/output/crates_turbopack-tests_tests_snapshot_comptime_define_input_index_6b0d2b.js +++ b/crates/turbopack-tests/tests/snapshot/comptime/define/output/crates_turbopack-tests_tests_snapshot_comptime_define_input_index_6b0d2b.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/crates_turbopack-tests_tests_snapshot_comptime_define_input_index_6b0d2b.js", {}, - {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_comptime_define_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/comptime/define/input/index.js (ecmascript)"],"chunkListPath":"output/crates_turbopack-tests_tests_snapshot_comptime_define_input_index.js_f5704b._.json"} + {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_comptime_define_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/comptime/define/input/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/comptime/define/output/crates_turbopack-tests_tests_snapshot_comptime_define_input_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/comptime/define/output/crates_turbopack-tests_tests_snapshot_comptime_define_input_index_b53fce.js index b06852527f229..57ab598b34692 100644 --- a/crates/turbopack-tests/tests/snapshot/comptime/define/output/crates_turbopack-tests_tests_snapshot_comptime_define_input_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/comptime/define/output/crates_turbopack-tests_tests_snapshot_comptime_define_input_index_b53fce.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/crates_turbopack-tests_tests_snapshot_comptime_define_input_index_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/comptime/define/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/snapshot/comptime/define/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { if ("TURBOPACK compile-time truthy", 1) { console.log('DEFINED_VALUE'); diff --git a/crates/turbopack-tests/tests/snapshot/css/absolute-uri-import/output/crates_turbopack-tests_tests_snapshot_css_absolute-uri-import_input_index_082e10.js b/crates/turbopack-tests/tests/snapshot/css/absolute-uri-import/output/crates_turbopack-tests_tests_snapshot_css_absolute-uri-import_input_index_082e10.js index 8fb7a00ad27d8..4710cc4fc1920 100644 --- a/crates/turbopack-tests/tests/snapshot/css/absolute-uri-import/output/crates_turbopack-tests_tests_snapshot_css_absolute-uri-import_input_index_082e10.js +++ b/crates/turbopack-tests/tests/snapshot/css/absolute-uri-import/output/crates_turbopack-tests_tests_snapshot_css_absolute-uri-import_input_index_082e10.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/crates_turbopack-tests_tests_snapshot_css_absolute-uri-import_input_index_082e10.js", {}, - {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_css_absolute-uri-import_input_index.css","output/crates_turbopack-tests_tests_snapshot_css_absolute-uri-import_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/css/absolute-uri-import/input/index.js (ecmascript)"],"chunkListPath":"output/79fb1_turbopack-tests_tests_snapshot_css_absolute-uri-import_input_index.js_f5704b._.json"} + {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_css_absolute-uri-import_input_index.css","output/crates_turbopack-tests_tests_snapshot_css_absolute-uri-import_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/css/absolute-uri-import/input/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/css/absolute-uri-import/output/crates_turbopack-tests_tests_snapshot_css_absolute-uri-import_input_index_2e2744.js b/crates/turbopack-tests/tests/snapshot/css/absolute-uri-import/output/crates_turbopack-tests_tests_snapshot_css_absolute-uri-import_input_index_2e2744.js new file mode 100644 index 0000000000000..3c88707fff253 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/css/absolute-uri-import/output/crates_turbopack-tests_tests_snapshot_css_absolute-uri-import_input_index_2e2744.js @@ -0,0 +1,12 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/crates_turbopack-tests_tests_snapshot_css_absolute-uri-import_input_index_2e2744.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/crates_turbopack-tests_tests_snapshot_css_absolute-uri-import_input_index_2e2744.js", + "chunks": [ + "output/crates_turbopack-tests_tests_snapshot_css_absolute-uri-import_input_index.css", + "output/crates_turbopack-tests_tests_snapshot_css_absolute-uri-import_input_index_b53fce.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/css/absolute-uri-import/output/crates_turbopack-tests_tests_snapshot_css_absolute-uri-import_input_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/css/absolute-uri-import/output/crates_turbopack-tests_tests_snapshot_css_absolute-uri-import_input_index_b53fce.js index f628e6be989be..ef3fb890d5d2a 100644 --- a/crates/turbopack-tests/tests/snapshot/css/absolute-uri-import/output/crates_turbopack-tests_tests_snapshot_css_absolute-uri-import_input_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/css/absolute-uri-import/output/crates_turbopack-tests_tests_snapshot_css_absolute-uri-import_input_index_b53fce.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/crates_turbopack-tests_tests_snapshot_css_absolute-uri-import_input_index_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/css/absolute-uri-import/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/snapshot/css/absolute-uri-import/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { ; diff --git a/crates/turbopack-tests/tests/snapshot/css/css/output/8697f_foo_style.module.css_a724a8._.js b/crates/turbopack-tests/tests/snapshot/css/css/output/8697f_foo_style.module.css_a724a8._.js index 14492c7078e3a..f19d6b32ce48d 100644 --- a/crates/turbopack-tests/tests/snapshot/css/css/output/8697f_foo_style.module.css_a724a8._.js +++ b/crates/turbopack-tests/tests/snapshot/css/css/output/8697f_foo_style.module.css_a724a8._.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/8697f_foo_style.module.css_a724a8._.js", { -"[project]/crates/turbopack-tests/tests/snapshot/css/css/input/node_modules/foo/style.module.css (css, css module)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/css/css/input/node_modules/foo/style.module.css (css, css module)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { __turbopack_export_value__({ "foo-module-style": "foo-module-style__style__abf9e738", diff --git a/crates/turbopack-tests/tests/snapshot/css/css/output/crates_turbopack-tests_tests_snapshot_css_css_input_index_2081e1.js b/crates/turbopack-tests/tests/snapshot/css/css/output/crates_turbopack-tests_tests_snapshot_css_css_input_index_2081e1.js index ad2ca994357ca..7f791923baba2 100644 --- a/crates/turbopack-tests/tests/snapshot/css/css/output/crates_turbopack-tests_tests_snapshot_css_css_input_index_2081e1.js +++ b/crates/turbopack-tests/tests/snapshot/css/css/output/crates_turbopack-tests_tests_snapshot_css_css_input_index_2081e1.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/crates_turbopack-tests_tests_snapshot_css_css_input_index_2081e1.js", {}, - {"otherChunks":["output/8697f_foo_style.css","output/crates_turbopack-tests_tests_snapshot_css_css_input_style.css","output/8697f_foo_style.module_b5a149.css","output/crates_turbopack-tests_tests_snapshot_css_css_input_style.module_b5a149.css","output/crates_turbopack-tests_tests_snapshot_css_css_input_index_b53fce.js","output/8697f_foo_style.module.css_a724a8._.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/css/css/input/index.js (ecmascript)"],"chunkListPath":"output/crates_turbopack-tests_tests_snapshot_css_css_input_index.js_f5704b._.json"} + {"otherChunks":["output/8697f_foo_style.css","output/crates_turbopack-tests_tests_snapshot_css_css_input_style.css","output/8697f_foo_style.module_b5a149.css","output/crates_turbopack-tests_tests_snapshot_css_css_input_style.module_b5a149.css","output/crates_turbopack-tests_tests_snapshot_css_css_input_index_b53fce.js","output/8697f_foo_style.module.css_a724a8._.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/css/css/input/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/css/css/output/crates_turbopack-tests_tests_snapshot_css_css_input_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/css/css/output/crates_turbopack-tests_tests_snapshot_css_css_input_index_b53fce.js index 4214564ac3e16..3ab7afde55eec 100644 --- a/crates/turbopack-tests/tests/snapshot/css/css/output/crates_turbopack-tests_tests_snapshot_css_css_input_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/css/css/output/crates_turbopack-tests_tests_snapshot_css_css_input_index_b53fce.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/crates_turbopack-tests_tests_snapshot_css_css_input_index_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/css/css/input/style.module.css (css, css module)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/css/css/input/style.module.css (css, css module)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { __turbopack_export_value__({ "another-composed-module-style": "another-composed-module-style__style__9bcf751c" + " " + __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/css/css/input/node_modules/foo/style.module.css (css, css module)")["foo-module-style"], @@ -10,7 +10,7 @@ __turbopack_export_value__({ }); })()), -"[project]/crates/turbopack-tests/tests/snapshot/css/css/input/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/css/css/input/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$css$2f$css$2f$input$2f$node_modules$2f$foo$2f$style$2e$module$2e$css__$28$css$2c$__css__module$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/css/css/input/node_modules/foo/style.module.css (css, css module)"); var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$css$2f$css$2f$input$2f$style$2e$module$2e$css__$28$css$2c$__css__module$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/css/css/input/style.module.css (css, css module)"); diff --git a/crates/turbopack-tests/tests/snapshot/css/css/output/crates_turbopack-tests_tests_snapshot_css_css_input_index_c9488c.js b/crates/turbopack-tests/tests/snapshot/css/css/output/crates_turbopack-tests_tests_snapshot_css_css_input_index_c9488c.js new file mode 100644 index 0000000000000..4f2bb2d86fdab --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/css/css/output/crates_turbopack-tests_tests_snapshot_css_css_input_index_c9488c.js @@ -0,0 +1,16 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/crates_turbopack-tests_tests_snapshot_css_css_input_index_c9488c.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/crates_turbopack-tests_tests_snapshot_css_css_input_index_c9488c.js", + "chunks": [ + "output/8697f_foo_style.css", + "output/crates_turbopack-tests_tests_snapshot_css_css_input_style.css", + "output/8697f_foo_style.module_b5a149.css", + "output/crates_turbopack-tests_tests_snapshot_css_css_input_style.module_b5a149.css", + "output/crates_turbopack-tests_tests_snapshot_css_css_input_index_b53fce.js", + "output/8697f_foo_style.module.css_a724a8._.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/emotion/emotion/output/63a02_@emotion_react_index_a1c0c3.js b/crates/turbopack-tests/tests/snapshot/emotion/emotion/output/63a02_@emotion_react_index_a1c0c3.js index 626f28df2fb2d..4bc6731deedf9 100644 --- a/crates/turbopack-tests/tests/snapshot/emotion/emotion/output/63a02_@emotion_react_index_a1c0c3.js +++ b/crates/turbopack-tests/tests/snapshot/emotion/emotion/output/63a02_@emotion_react_index_a1c0c3.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/63a02_@emotion_react_index_a1c0c3.js", { -"[project]/crates/turbopack-tests/tests/node_modules/@emotion/react/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/node_modules/@emotion/react/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { "purposefully empty stub"; "@emtion/react/index.js"; diff --git a/crates/turbopack-tests/tests/snapshot/emotion/emotion/output/63a02_@emotion_react_jsx-dev-runtime_73b602.js b/crates/turbopack-tests/tests/snapshot/emotion/emotion/output/63a02_@emotion_react_jsx-dev-runtime_73b602.js index 36eae97ff7fa6..2829f2b3fdbbd 100644 --- a/crates/turbopack-tests/tests/snapshot/emotion/emotion/output/63a02_@emotion_react_jsx-dev-runtime_73b602.js +++ b/crates/turbopack-tests/tests/snapshot/emotion/emotion/output/63a02_@emotion_react_jsx-dev-runtime_73b602.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/63a02_@emotion_react_jsx-dev-runtime_73b602.js", { -"[project]/crates/turbopack-tests/tests/node_modules/@emotion/react/jsx-dev-runtime.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/node_modules/@emotion/react/jsx-dev-runtime.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { "purposefully empty stub"; "@emtion/react/jsx-dev-runtime.js"; diff --git a/crates/turbopack-tests/tests/snapshot/emotion/emotion/output/63a02_@emotion_styled_index_fd4de2.js b/crates/turbopack-tests/tests/snapshot/emotion/emotion/output/63a02_@emotion_styled_index_fd4de2.js index 4218a287c0716..6714b3b467c33 100644 --- a/crates/turbopack-tests/tests/snapshot/emotion/emotion/output/63a02_@emotion_styled_index_fd4de2.js +++ b/crates/turbopack-tests/tests/snapshot/emotion/emotion/output/63a02_@emotion_styled_index_fd4de2.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/63a02_@emotion_styled_index_fd4de2.js", { -"[project]/crates/turbopack-tests/tests/node_modules/@emotion/styled/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/node_modules/@emotion/styled/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { "purposefully empty stub"; "@emtion/styled/index.js"; diff --git a/crates/turbopack-tests/tests/snapshot/emotion/emotion/output/crates_turbopack-tests_tests_snapshot_emotion_emotion_input_index_32d078.js b/crates/turbopack-tests/tests/snapshot/emotion/emotion/output/crates_turbopack-tests_tests_snapshot_emotion_emotion_input_index_32d078.js new file mode 100644 index 0000000000000..d4d43472312a7 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/emotion/emotion/output/crates_turbopack-tests_tests_snapshot_emotion_emotion_input_index_32d078.js @@ -0,0 +1,14 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/crates_turbopack-tests_tests_snapshot_emotion_emotion_input_index_32d078.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/crates_turbopack-tests_tests_snapshot_emotion_emotion_input_index_32d078.js", + "chunks": [ + "output/63a02_@emotion_react_jsx-dev-runtime_73b602.js", + "output/63a02_@emotion_react_index_a1c0c3.js", + "output/63a02_@emotion_styled_index_fd4de2.js", + "output/crates_turbopack-tests_tests_snapshot_emotion_emotion_input_index_b53fce.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/emotion/emotion/output/crates_turbopack-tests_tests_snapshot_emotion_emotion_input_index_b080c4.js b/crates/turbopack-tests/tests/snapshot/emotion/emotion/output/crates_turbopack-tests_tests_snapshot_emotion_emotion_input_index_b080c4.js index 52a748909d12a..5a823ebacc101 100644 --- a/crates/turbopack-tests/tests/snapshot/emotion/emotion/output/crates_turbopack-tests_tests_snapshot_emotion_emotion_input_index_b080c4.js +++ b/crates/turbopack-tests/tests/snapshot/emotion/emotion/output/crates_turbopack-tests_tests_snapshot_emotion_emotion_input_index_b080c4.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/crates_turbopack-tests_tests_snapshot_emotion_emotion_input_index_b080c4.js", {}, - {"otherChunks":["output/63a02_@emotion_react_jsx-dev-runtime_73b602.js","output/63a02_@emotion_react_index_a1c0c3.js","output/63a02_@emotion_styled_index_fd4de2.js","output/crates_turbopack-tests_tests_snapshot_emotion_emotion_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/emotion/emotion/input/index.js (ecmascript)"],"chunkListPath":"output/crates_turbopack-tests_tests_snapshot_emotion_emotion_input_index.js_f5704b._.json"} + {"otherChunks":["output/63a02_@emotion_react_jsx-dev-runtime_73b602.js","output/63a02_@emotion_react_index_a1c0c3.js","output/63a02_@emotion_styled_index_fd4de2.js","output/crates_turbopack-tests_tests_snapshot_emotion_emotion_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/emotion/emotion/input/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/emotion/emotion/output/crates_turbopack-tests_tests_snapshot_emotion_emotion_input_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/emotion/emotion/output/crates_turbopack-tests_tests_snapshot_emotion_emotion_input_index_b53fce.js index 8b07b8c06bc72..46ab6a63aa546 100644 --- a/crates/turbopack-tests/tests/snapshot/emotion/emotion/output/crates_turbopack-tests_tests_snapshot_emotion_emotion_input_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/emotion/emotion/output/crates_turbopack-tests_tests_snapshot_emotion_emotion_input_index_b53fce.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/crates_turbopack-tests_tests_snapshot_emotion_emotion_input_index_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/emotion/emotion/input/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/emotion/emotion/input/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$node_modules$2f40$emotion$2f$react$2f$jsx$2d$dev$2d$runtime$2e$js__$28$ecmascript$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/node_modules/@emotion/react/jsx-dev-runtime.js (ecmascript)"); var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$node_modules$2f40$emotion$2f$react$2f$index$2e$js__$28$ecmascript$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/node_modules/@emotion/react/index.js (ecmascript)"); diff --git a/crates/turbopack-tests/tests/snapshot/env/env/output/crates_turbopack-tests_tests_snapshot_env_env_input_.env_.env_b53fce.js b/crates/turbopack-tests/tests/snapshot/env/env/output/crates_turbopack-tests_tests_snapshot_env_env_input_.env_.env_b53fce.js index 40b01a0d2a8aa..d2e8e94954f91 100644 --- a/crates/turbopack-tests/tests/snapshot/env/env/output/crates_turbopack-tests_tests_snapshot_env_env_input_.env_.env_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/env/env/output/crates_turbopack-tests_tests_snapshot_env_env_input_.env_.env_b53fce.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/crates_turbopack-tests_tests_snapshot_env_env_input_.env_.env_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/env/env/input/.env/.env.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/snapshot/env/env/input/.env/.env.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { const env = process.env = { ...process.env diff --git a/crates/turbopack-tests/tests/snapshot/env/env/output/crates_turbopack-tests_tests_snapshot_env_env_input_index_29a23f.js b/crates/turbopack-tests/tests/snapshot/env/env/output/crates_turbopack-tests_tests_snapshot_env_env_input_index_29a23f.js index e377af4e702a4..ab2df3fe2c1a4 100644 --- a/crates/turbopack-tests/tests/snapshot/env/env/output/crates_turbopack-tests_tests_snapshot_env_env_input_index_29a23f.js +++ b/crates/turbopack-tests/tests/snapshot/env/env/output/crates_turbopack-tests_tests_snapshot_env_env_input_index_29a23f.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/crates_turbopack-tests_tests_snapshot_env_env_input_index_29a23f.js", {}, - {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_env_env_input_index_b53fce.js","output/crates_turbopack-tests_tests_snapshot_env_env_input_.env_.env_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/env/env/input/.env/.env.js (ecmascript)","[project]/crates/turbopack-tests/tests/snapshot/env/env/input/index.js (ecmascript)"],"chunkListPath":"output/crates_turbopack-tests_tests_snapshot_env_env_input_index.js_f5704b._.json"} + {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_env_env_input_index_b53fce.js","output/crates_turbopack-tests_tests_snapshot_env_env_input_.env_.env_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/env/env/input/.env/.env.js (ecmascript)","[project]/crates/turbopack-tests/tests/snapshot/env/env/input/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/env/env/output/crates_turbopack-tests_tests_snapshot_env_env_input_index_936aa9.js b/crates/turbopack-tests/tests/snapshot/env/env/output/crates_turbopack-tests_tests_snapshot_env_env_input_index_936aa9.js new file mode 100644 index 0000000000000..d2b6f6222469d --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/env/env/output/crates_turbopack-tests_tests_snapshot_env_env_input_index_936aa9.js @@ -0,0 +1,12 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/crates_turbopack-tests_tests_snapshot_env_env_input_index_936aa9.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/crates_turbopack-tests_tests_snapshot_env_env_input_index_936aa9.js", + "chunks": [ + "output/crates_turbopack-tests_tests_snapshot_env_env_input_index_b53fce.js", + "output/crates_turbopack-tests_tests_snapshot_env_env_input_.env_.env_b53fce.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/env/env/output/crates_turbopack-tests_tests_snapshot_env_env_input_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/env/env/output/crates_turbopack-tests_tests_snapshot_env_env_input_index_b53fce.js index 601f3f623e2b9..84b58cd1bb230 100644 --- a/crates/turbopack-tests/tests/snapshot/env/env/output/crates_turbopack-tests_tests_snapshot_env_env_input_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/env/env/output/crates_turbopack-tests_tests_snapshot_env_env_input_index_b53fce.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/crates_turbopack-tests_tests_snapshot_env_env_input_index_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/env/env/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/snapshot/env/env/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { console.log(process.env.FOOBAR); diff --git a/crates/turbopack-tests/tests/snapshot/evaluated_entrry/runtime_entry/output/a587c_tests_snapshot_evaluated_entrry_runtime_entry_input_index_994876.js b/crates/turbopack-tests/tests/snapshot/evaluated_entrry/runtime_entry/output/a587c_tests_snapshot_evaluated_entrry_runtime_entry_input_index_994876.js new file mode 100644 index 0000000000000..7a88abcd147fe --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/evaluated_entrry/runtime_entry/output/a587c_tests_snapshot_evaluated_entrry_runtime_entry_input_index_994876.js @@ -0,0 +1,11 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/a587c_tests_snapshot_evaluated_entrry_runtime_entry_input_index_994876.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/a587c_tests_snapshot_evaluated_entrry_runtime_entry_input_index_994876.js", + "chunks": [ + "output/a587c_tests_snapshot_evaluated_entrry_runtime_entry_input_index_b53fce.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/evaluated_entrry/runtime_entry/output/a587c_tests_snapshot_evaluated_entrry_runtime_entry_input_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/evaluated_entrry/runtime_entry/output/a587c_tests_snapshot_evaluated_entrry_runtime_entry_input_index_b53fce.js index 53c2ae385b508..216ef11c92e44 100644 --- a/crates/turbopack-tests/tests/snapshot/evaluated_entrry/runtime_entry/output/a587c_tests_snapshot_evaluated_entrry_runtime_entry_input_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/evaluated_entrry/runtime_entry/output/a587c_tests_snapshot_evaluated_entrry_runtime_entry_input_index_b53fce.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/a587c_tests_snapshot_evaluated_entrry_runtime_entry_input_index_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/evaluated_entrry/runtime_entry/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/snapshot/evaluated_entrry/runtime_entry/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { console.log("hello world"); diff --git a/crates/turbopack-tests/tests/snapshot/evaluated_entrry/runtime_entry/output/a587c_tests_snapshot_evaluated_entrry_runtime_entry_input_index_f59cc7.js b/crates/turbopack-tests/tests/snapshot/evaluated_entrry/runtime_entry/output/a587c_tests_snapshot_evaluated_entrry_runtime_entry_input_index_f59cc7.js index 01a2a7e5d089f..b3469bee2b1d1 100644 --- a/crates/turbopack-tests/tests/snapshot/evaluated_entrry/runtime_entry/output/a587c_tests_snapshot_evaluated_entrry_runtime_entry_input_index_f59cc7.js +++ b/crates/turbopack-tests/tests/snapshot/evaluated_entrry/runtime_entry/output/a587c_tests_snapshot_evaluated_entrry_runtime_entry_input_index_f59cc7.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/a587c_tests_snapshot_evaluated_entrry_runtime_entry_input_index_f59cc7.js", {}, - {"otherChunks":["output/a587c_tests_snapshot_evaluated_entrry_runtime_entry_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/evaluated_entrry/runtime_entry/input/index.js (ecmascript)"],"chunkListPath":"output/a587c_tests_snapshot_evaluated_entrry_runtime_entry_input_index.js_f5704b._.json"} + {"otherChunks":["output/a587c_tests_snapshot_evaluated_entrry_runtime_entry_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/evaluated_entrry/runtime_entry/input/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/example/example/output/crates_turbopack-tests_tests_snapshot_example_example_input_index_3d0aac.js b/crates/turbopack-tests/tests/snapshot/example/example/output/crates_turbopack-tests_tests_snapshot_example_example_input_index_3d0aac.js new file mode 100644 index 0000000000000..c5474019c3a4a --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/example/example/output/crates_turbopack-tests_tests_snapshot_example_example_input_index_3d0aac.js @@ -0,0 +1,11 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/crates_turbopack-tests_tests_snapshot_example_example_input_index_3d0aac.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/crates_turbopack-tests_tests_snapshot_example_example_input_index_3d0aac.js", + "chunks": [ + "output/crates_turbopack-tests_tests_snapshot_example_example_input_index_b53fce.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/example/example/output/crates_turbopack-tests_tests_snapshot_example_example_input_index_78b6bf.js b/crates/turbopack-tests/tests/snapshot/example/example/output/crates_turbopack-tests_tests_snapshot_example_example_input_index_78b6bf.js index f9085cbba9ea1..9d98b664df396 100644 --- a/crates/turbopack-tests/tests/snapshot/example/example/output/crates_turbopack-tests_tests_snapshot_example_example_input_index_78b6bf.js +++ b/crates/turbopack-tests/tests/snapshot/example/example/output/crates_turbopack-tests_tests_snapshot_example_example_input_index_78b6bf.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/crates_turbopack-tests_tests_snapshot_example_example_input_index_78b6bf.js", {}, - {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_example_example_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/example/example/input/index.js (ecmascript)"],"chunkListPath":"output/crates_turbopack-tests_tests_snapshot_example_example_input_index.js_f5704b._.json"} + {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_example_example_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/example/example/input/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/example/example/output/crates_turbopack-tests_tests_snapshot_example_example_input_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/example/example/output/crates_turbopack-tests_tests_snapshot_example_example_input_index_b53fce.js index b3a159b654d64..bdf1af30cc91a 100644 --- a/crates/turbopack-tests/tests/snapshot/example/example/output/crates_turbopack-tests_tests_snapshot_example_example_input_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/example/example/output/crates_turbopack-tests_tests_snapshot_example_example_input_index_b53fce.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/crates_turbopack-tests_tests_snapshot_example_example_input_index_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/example/example/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/snapshot/example/example/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { console.log("hello world"); diff --git a/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-2_input_index_289ae7.js b/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-2_input_index_289ae7.js index 0faaa299f7d40..c9a4f941dc4d2 100644 --- a/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-2_input_index_289ae7.js +++ b/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-2_input_index_289ae7.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-2_input_index_289ae7.js", {}, - {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-2_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/input/index.js (ecmascript)"],"chunkListPath":"output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-2_input_index.js_f5704b._.json"} + {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-2_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/input/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-2_input_index_33e920.js b/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-2_input_index_33e920.js new file mode 100644 index 0000000000000..e0b1b6f974561 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-2_input_index_33e920.js @@ -0,0 +1,11 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-2_input_index_33e920.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-2_input_index_33e920.js", + "chunks": [ + "output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-2_input_index_b53fce.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-2_input_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-2_input_index_b53fce.js index 9f050a025aeba..59bea737abd28 100644 --- a/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-2_input_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-2_input_index_b53fce.js @@ -1,11 +1,11 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-2_input_index_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/input/commonjs.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/input/commonjs.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { exports.hello = "World"; }.call(this) }), -"[project]/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/input/c.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/input/c.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { __turbopack_esm__({}); var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$export$2d$alls$2f$cjs$2d$2$2f$input$2f$commonjs$2e$js__$28$ecmascript$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/input/commonjs.js (ecmascript)"); @@ -14,7 +14,7 @@ __turbopack_cjs__(__TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turb ; })()), -"[project]/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/input/b.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/input/b.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { __turbopack_esm__({}); var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$export$2d$alls$2f$cjs$2d$2$2f$input$2f$c$2e$js__$28$ecmascript$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/input/c.js (ecmascript)"); @@ -23,7 +23,7 @@ __turbopack_cjs__(__TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turb ; })()), -"[project]/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$export$2d$alls$2f$cjs$2d$2$2f$input$2f$b$2e$js__$28$ecmascript$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/input/b.js (ecmascript)"); "__TURBOPACK__ecmascript__hoisting__location__"; diff --git a/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-script_input_index_3e96b7.js b/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-script_input_index_3e96b7.js index ce676e5af3495..ccb7e25588e0b 100644 --- a/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-script_input_index_3e96b7.js +++ b/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-script_input_index_3e96b7.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-script_input_index_3e96b7.js", {}, - {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-script_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/input/index.js (ecmascript)"],"chunkListPath":"output/79fb1_turbopack-tests_tests_snapshot_export-alls_cjs-script_input_index.js_f5704b._.json"} + {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-script_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/input/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-script_input_index_9e47bb.js b/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-script_input_index_9e47bb.js new file mode 100644 index 0000000000000..b366483b433e3 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-script_input_index_9e47bb.js @@ -0,0 +1,11 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-script_input_index_9e47bb.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-script_input_index_9e47bb.js", + "chunks": [ + "output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-script_input_index_b53fce.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-script_input_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-script_input_index_b53fce.js index 27088a654cd23..1079d951b1c77 100644 --- a/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-script_input_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-script_input_index_b53fce.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-script_input_index_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/input/exported.cjs (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/input/exported.cjs (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { module.exports = { foo: 1, @@ -8,7 +8,7 @@ module.exports = { }; }.call(this) }), -"[project]/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/input/mod.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/input/mod.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { __turbopack_esm__({}); var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$export$2d$alls$2f$cjs$2d$script$2f$input$2f$exported$2e$cjs__$28$ecmascript$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/input/exported.cjs (ecmascript)"); @@ -18,7 +18,7 @@ __turbopack_cjs__(__TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turb console.log('Hoist test'); })()), -"[project]/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$export$2d$alls$2f$cjs$2d$script$2f$input$2f$mod$2e$js__$28$ecmascript$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/input/mod.js (ecmascript)"); "__TURBOPACK__ecmascript__hoisting__location__"; diff --git a/crates/turbopack-tests/tests/snapshot/import-meta/cjs/output/crates_turbopack-tests_tests_snapshot_import-meta_cjs_input_index_537553.js b/crates/turbopack-tests/tests/snapshot/import-meta/cjs/output/crates_turbopack-tests_tests_snapshot_import-meta_cjs_input_index_537553.js index 735ca5abb927b..bedcb59c76a95 100644 --- a/crates/turbopack-tests/tests/snapshot/import-meta/cjs/output/crates_turbopack-tests_tests_snapshot_import-meta_cjs_input_index_537553.js +++ b/crates/turbopack-tests/tests/snapshot/import-meta/cjs/output/crates_turbopack-tests_tests_snapshot_import-meta_cjs_input_index_537553.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/crates_turbopack-tests_tests_snapshot_import-meta_cjs_input_index_537553.js", {}, - {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_import-meta_cjs_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/import-meta/cjs/input/index.js (ecmascript)"],"chunkListPath":"output/crates_turbopack-tests_tests_snapshot_import-meta_cjs_input_index.js_f5704b._.json"} + {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_import-meta_cjs_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/import-meta/cjs/input/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/import-meta/cjs/output/crates_turbopack-tests_tests_snapshot_import-meta_cjs_input_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/import-meta/cjs/output/crates_turbopack-tests_tests_snapshot_import-meta_cjs_input_index_b53fce.js index 0daf49029c542..452cf1867a76c 100644 --- a/crates/turbopack-tests/tests/snapshot/import-meta/cjs/output/crates_turbopack-tests_tests_snapshot_import-meta_cjs_input_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/import-meta/cjs/output/crates_turbopack-tests_tests_snapshot_import-meta_cjs_input_index_b53fce.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/crates_turbopack-tests_tests_snapshot_import-meta_cjs_input_index_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/import-meta/cjs/input/mod.cjs (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/snapshot/import-meta/cjs/input/mod.cjs (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { const __TURBOPACK__import$2e$meta__ = { url: "file:///ROOT/crates/turbopack-tests/tests/snapshot/import-meta/cjs/input/mod.cjs" @@ -9,7 +9,7 @@ const __TURBOPACK__import$2e$meta__ = { console.log(__TURBOPACK__import$2e$meta__.url); }.call(this) }), -"[project]/crates/turbopack-tests/tests/snapshot/import-meta/cjs/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/snapshot/import-meta/cjs/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$import$2d$meta$2f$cjs$2f$input$2f$mod$2e$cjs__$28$ecmascript$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/import-meta/cjs/input/mod.cjs (ecmascript)"); "__TURBOPACK__ecmascript__hoisting__location__"; diff --git a/crates/turbopack-tests/tests/snapshot/import-meta/cjs/output/crates_turbopack-tests_tests_snapshot_import-meta_cjs_input_index_c15b1f.js b/crates/turbopack-tests/tests/snapshot/import-meta/cjs/output/crates_turbopack-tests_tests_snapshot_import-meta_cjs_input_index_c15b1f.js new file mode 100644 index 0000000000000..8dce3ca4aeae6 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/import-meta/cjs/output/crates_turbopack-tests_tests_snapshot_import-meta_cjs_input_index_c15b1f.js @@ -0,0 +1,11 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/crates_turbopack-tests_tests_snapshot_import-meta_cjs_input_index_c15b1f.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/crates_turbopack-tests_tests_snapshot_import-meta_cjs_input_index_c15b1f.js", + "chunks": [ + "output/crates_turbopack-tests_tests_snapshot_import-meta_cjs_input_index_b53fce.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/import-meta/esm-multiple/output/79fb1_turbopack-tests_tests_snapshot_import-meta_esm-multiple_input_index_2082c1.js b/crates/turbopack-tests/tests/snapshot/import-meta/esm-multiple/output/79fb1_turbopack-tests_tests_snapshot_import-meta_esm-multiple_input_index_2082c1.js new file mode 100644 index 0000000000000..2a9a6fee7822b --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/import-meta/esm-multiple/output/79fb1_turbopack-tests_tests_snapshot_import-meta_esm-multiple_input_index_2082c1.js @@ -0,0 +1,11 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/79fb1_turbopack-tests_tests_snapshot_import-meta_esm-multiple_input_index_2082c1.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/79fb1_turbopack-tests_tests_snapshot_import-meta_esm-multiple_input_index_2082c1.js", + "chunks": [ + "output/79fb1_turbopack-tests_tests_snapshot_import-meta_esm-multiple_input_index_b53fce.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/import-meta/esm-multiple/output/79fb1_turbopack-tests_tests_snapshot_import-meta_esm-multiple_input_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/import-meta/esm-multiple/output/79fb1_turbopack-tests_tests_snapshot_import-meta_esm-multiple_input_index_b53fce.js index 0e646dfcf0b91..0c039b2bf8709 100644 --- a/crates/turbopack-tests/tests/snapshot/import-meta/esm-multiple/output/79fb1_turbopack-tests_tests_snapshot_import-meta_esm-multiple_input_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/import-meta/esm-multiple/output/79fb1_turbopack-tests_tests_snapshot_import-meta_esm-multiple_input_index_b53fce.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/79fb1_turbopack-tests_tests_snapshot_import-meta_esm-multiple_input_index_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/import-meta/esm-multiple/input/mod.mjs (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/snapshot/import-meta/esm-multiple/input/mod.mjs (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { const __TURBOPACK__import$2e$meta__ = { url: "file:///ROOT/crates/turbopack-tests/tests/snapshot/import-meta/esm-multiple/input/mod.mjs" @@ -16,7 +16,7 @@ foo(); bar(); }.call(this) }), -"[project]/crates/turbopack-tests/tests/snapshot/import-meta/esm-multiple/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/snapshot/import-meta/esm-multiple/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$import$2d$meta$2f$esm$2d$multiple$2f$input$2f$mod$2e$mjs__$28$ecmascript$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/import-meta/esm-multiple/input/mod.mjs (ecmascript)"); "__TURBOPACK__ecmascript__hoisting__location__"; diff --git a/crates/turbopack-tests/tests/snapshot/import-meta/esm-multiple/output/79fb1_turbopack-tests_tests_snapshot_import-meta_esm-multiple_input_index_c00392.js b/crates/turbopack-tests/tests/snapshot/import-meta/esm-multiple/output/79fb1_turbopack-tests_tests_snapshot_import-meta_esm-multiple_input_index_c00392.js index 24c20084681ad..6e2a34a4db131 100644 --- a/crates/turbopack-tests/tests/snapshot/import-meta/esm-multiple/output/79fb1_turbopack-tests_tests_snapshot_import-meta_esm-multiple_input_index_c00392.js +++ b/crates/turbopack-tests/tests/snapshot/import-meta/esm-multiple/output/79fb1_turbopack-tests_tests_snapshot_import-meta_esm-multiple_input_index_c00392.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/79fb1_turbopack-tests_tests_snapshot_import-meta_esm-multiple_input_index_c00392.js", {}, - {"otherChunks":["output/79fb1_turbopack-tests_tests_snapshot_import-meta_esm-multiple_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/import-meta/esm-multiple/input/index.js (ecmascript)"],"chunkListPath":"output/79fb1_turbopack-tests_tests_snapshot_import-meta_esm-multiple_input_index.js_f5704b._.json"} + {"otherChunks":["output/79fb1_turbopack-tests_tests_snapshot_import-meta_esm-multiple_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/import-meta/esm-multiple/input/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/import-meta/esm-mutable/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-mutable_input_index_6c9201.js b/crates/turbopack-tests/tests/snapshot/import-meta/esm-mutable/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-mutable_input_index_6c9201.js index 613c38c0677c0..30f2ce1d77401 100644 --- a/crates/turbopack-tests/tests/snapshot/import-meta/esm-mutable/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-mutable_input_index_6c9201.js +++ b/crates/turbopack-tests/tests/snapshot/import-meta/esm-mutable/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-mutable_input_index_6c9201.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/crates_turbopack-tests_tests_snapshot_import-meta_esm-mutable_input_index_6c9201.js", {}, - {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_import-meta_esm-mutable_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/import-meta/esm-mutable/input/index.js (ecmascript)"],"chunkListPath":"output/79fb1_turbopack-tests_tests_snapshot_import-meta_esm-mutable_input_index.js_f5704b._.json"} + {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_import-meta_esm-mutable_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/import-meta/esm-mutable/input/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/import-meta/esm-mutable/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-mutable_input_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/import-meta/esm-mutable/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-mutable_input_index_b53fce.js index 39417e5c5ade9..1f89daa3367bc 100644 --- a/crates/turbopack-tests/tests/snapshot/import-meta/esm-mutable/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-mutable_input_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/import-meta/esm-mutable/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-mutable_input_index_b53fce.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/crates_turbopack-tests_tests_snapshot_import-meta_esm-mutable_input_index_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/import-meta/esm-mutable/input/mod.mjs (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/snapshot/import-meta/esm-mutable/input/mod.mjs (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { const __TURBOPACK__import$2e$meta__ = { url: "file:///ROOT/crates/turbopack-tests/tests/snapshot/import-meta/esm-mutable/input/mod.mjs" @@ -9,7 +9,7 @@ const __TURBOPACK__import$2e$meta__ = { __TURBOPACK__import$2e$meta__.foo = 1; }.call(this) }), -"[project]/crates/turbopack-tests/tests/snapshot/import-meta/esm-mutable/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/snapshot/import-meta/esm-mutable/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$import$2d$meta$2f$esm$2d$mutable$2f$input$2f$mod$2e$mjs__$28$ecmascript$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/import-meta/esm-mutable/input/mod.mjs (ecmascript)"); "__TURBOPACK__ecmascript__hoisting__location__"; diff --git a/crates/turbopack-tests/tests/snapshot/import-meta/esm-mutable/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-mutable_input_index_f31da1.js b/crates/turbopack-tests/tests/snapshot/import-meta/esm-mutable/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-mutable_input_index_f31da1.js new file mode 100644 index 0000000000000..0df6fb47a84aa --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/import-meta/esm-mutable/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-mutable_input_index_f31da1.js @@ -0,0 +1,11 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/crates_turbopack-tests_tests_snapshot_import-meta_esm-mutable_input_index_f31da1.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/crates_turbopack-tests_tests_snapshot_import-meta_esm-mutable_input_index_f31da1.js", + "chunks": [ + "output/crates_turbopack-tests_tests_snapshot_import-meta_esm-mutable_input_index_b53fce.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/import-meta/esm-object/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-object_input_index_6fcf7d.js b/crates/turbopack-tests/tests/snapshot/import-meta/esm-object/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-object_input_index_6fcf7d.js index 9f00ea2e67cd8..4575eb0ff1a30 100644 --- a/crates/turbopack-tests/tests/snapshot/import-meta/esm-object/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-object_input_index_6fcf7d.js +++ b/crates/turbopack-tests/tests/snapshot/import-meta/esm-object/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-object_input_index_6fcf7d.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/crates_turbopack-tests_tests_snapshot_import-meta_esm-object_input_index_6fcf7d.js", {}, - {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_import-meta_esm-object_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/import-meta/esm-object/input/index.js (ecmascript)"],"chunkListPath":"output/79fb1_turbopack-tests_tests_snapshot_import-meta_esm-object_input_index.js_f5704b._.json"} + {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_import-meta_esm-object_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/import-meta/esm-object/input/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/import-meta/esm-object/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-object_input_index_94bd94.js b/crates/turbopack-tests/tests/snapshot/import-meta/esm-object/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-object_input_index_94bd94.js new file mode 100644 index 0000000000000..87dda707b6f25 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/import-meta/esm-object/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-object_input_index_94bd94.js @@ -0,0 +1,11 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/crates_turbopack-tests_tests_snapshot_import-meta_esm-object_input_index_94bd94.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/crates_turbopack-tests_tests_snapshot_import-meta_esm-object_input_index_94bd94.js", + "chunks": [ + "output/crates_turbopack-tests_tests_snapshot_import-meta_esm-object_input_index_b53fce.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/import-meta/esm-object/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-object_input_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/import-meta/esm-object/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-object_input_index_b53fce.js index 84f5d269b9771..7e25b43123881 100644 --- a/crates/turbopack-tests/tests/snapshot/import-meta/esm-object/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-object_input_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/import-meta/esm-object/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-object_input_index_b53fce.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/crates_turbopack-tests_tests_snapshot_import-meta_esm-object_input_index_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/import-meta/esm-object/input/mod.mjs (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/snapshot/import-meta/esm-object/input/mod.mjs (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { const __TURBOPACK__import$2e$meta__ = { url: "file:///ROOT/crates/turbopack-tests/tests/snapshot/import-meta/esm-object/input/mod.mjs" @@ -9,7 +9,7 @@ const __TURBOPACK__import$2e$meta__ = { console.log(__TURBOPACK__import$2e$meta__); }.call(this) }), -"[project]/crates/turbopack-tests/tests/snapshot/import-meta/esm-object/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/snapshot/import-meta/esm-object/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$import$2d$meta$2f$esm$2d$object$2f$input$2f$mod$2e$mjs__$28$ecmascript$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/import-meta/esm-object/input/mod.mjs (ecmascript)"); "__TURBOPACK__ecmascript__hoisting__location__"; diff --git a/crates/turbopack-tests/tests/snapshot/import-meta/esm/output/crates_turbopack-tests_tests_snapshot_import-meta_esm_input_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/import-meta/esm/output/crates_turbopack-tests_tests_snapshot_import-meta_esm_input_index_b53fce.js index 1fe9a06b893a3..463afbd2b4e16 100644 --- a/crates/turbopack-tests/tests/snapshot/import-meta/esm/output/crates_turbopack-tests_tests_snapshot_import-meta_esm_input_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/import-meta/esm/output/crates_turbopack-tests_tests_snapshot_import-meta_esm_input_index_b53fce.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/crates_turbopack-tests_tests_snapshot_import-meta_esm_input_index_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/import-meta/esm/input/mod.mjs (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/snapshot/import-meta/esm/input/mod.mjs (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { const __TURBOPACK__import$2e$meta__ = { url: "file:///ROOT/crates/turbopack-tests/tests/snapshot/import-meta/esm/input/mod.mjs" @@ -9,7 +9,7 @@ const __TURBOPACK__import$2e$meta__ = { console.log(__TURBOPACK__import$2e$meta__.url); }.call(this) }), -"[project]/crates/turbopack-tests/tests/snapshot/import-meta/esm/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/snapshot/import-meta/esm/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$import$2d$meta$2f$esm$2f$input$2f$mod$2e$mjs__$28$ecmascript$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/import-meta/esm/input/mod.mjs (ecmascript)"); "__TURBOPACK__ecmascript__hoisting__location__"; diff --git a/crates/turbopack-tests/tests/snapshot/import-meta/esm/output/crates_turbopack-tests_tests_snapshot_import-meta_esm_input_index_c4c88a.js b/crates/turbopack-tests/tests/snapshot/import-meta/esm/output/crates_turbopack-tests_tests_snapshot_import-meta_esm_input_index_c4c88a.js index c525ae6a14f91..e2a0f44786ee9 100644 --- a/crates/turbopack-tests/tests/snapshot/import-meta/esm/output/crates_turbopack-tests_tests_snapshot_import-meta_esm_input_index_c4c88a.js +++ b/crates/turbopack-tests/tests/snapshot/import-meta/esm/output/crates_turbopack-tests_tests_snapshot_import-meta_esm_input_index_c4c88a.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/crates_turbopack-tests_tests_snapshot_import-meta_esm_input_index_c4c88a.js", {}, - {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_import-meta_esm_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/import-meta/esm/input/index.js (ecmascript)"],"chunkListPath":"output/crates_turbopack-tests_tests_snapshot_import-meta_esm_input_index.js_f5704b._.json"} + {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_import-meta_esm_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/import-meta/esm/input/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/import-meta/esm/output/crates_turbopack-tests_tests_snapshot_import-meta_esm_input_index_e40b1b.js b/crates/turbopack-tests/tests/snapshot/import-meta/esm/output/crates_turbopack-tests_tests_snapshot_import-meta_esm_input_index_e40b1b.js new file mode 100644 index 0000000000000..c819efac57ff1 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/import-meta/esm/output/crates_turbopack-tests_tests_snapshot_import-meta_esm_input_index_e40b1b.js @@ -0,0 +1,11 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/crates_turbopack-tests_tests_snapshot_import-meta_esm_input_index_e40b1b.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/crates_turbopack-tests_tests_snapshot_import-meta_esm_input_index_e40b1b.js", + "chunks": [ + "output/crates_turbopack-tests_tests_snapshot_import-meta_esm_input_index_b53fce.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/import-meta/url/output/crates_turbopack-tests_tests_snapshot_import-meta_url_input_index_630266.js b/crates/turbopack-tests/tests/snapshot/import-meta/url/output/crates_turbopack-tests_tests_snapshot_import-meta_url_input_index_630266.js new file mode 100644 index 0000000000000..b245f17e96d54 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/import-meta/url/output/crates_turbopack-tests_tests_snapshot_import-meta_url_input_index_630266.js @@ -0,0 +1,11 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/crates_turbopack-tests_tests_snapshot_import-meta_url_input_index_630266.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/crates_turbopack-tests_tests_snapshot_import-meta_url_input_index_630266.js", + "chunks": [ + "output/crates_turbopack-tests_tests_snapshot_import-meta_url_input_index_b53fce.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/import-meta/url/output/crates_turbopack-tests_tests_snapshot_import-meta_url_input_index_988b57.js b/crates/turbopack-tests/tests/snapshot/import-meta/url/output/crates_turbopack-tests_tests_snapshot_import-meta_url_input_index_988b57.js index 51c04d6ed24a7..20981fff91960 100644 --- a/crates/turbopack-tests/tests/snapshot/import-meta/url/output/crates_turbopack-tests_tests_snapshot_import-meta_url_input_index_988b57.js +++ b/crates/turbopack-tests/tests/snapshot/import-meta/url/output/crates_turbopack-tests_tests_snapshot_import-meta_url_input_index_988b57.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/crates_turbopack-tests_tests_snapshot_import-meta_url_input_index_988b57.js", {}, - {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_import-meta_url_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/import-meta/url/input/index.js (ecmascript)"],"chunkListPath":"output/crates_turbopack-tests_tests_snapshot_import-meta_url_input_index.js_f5704b._.json"} + {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_import-meta_url_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/import-meta/url/input/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/import-meta/url/output/crates_turbopack-tests_tests_snapshot_import-meta_url_input_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/import-meta/url/output/crates_turbopack-tests_tests_snapshot_import-meta_url_input_index_b53fce.js index a627fae225845..c38d58e13cce6 100644 --- a/crates/turbopack-tests/tests/snapshot/import-meta/url/output/crates_turbopack-tests_tests_snapshot_import-meta_url_input_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/import-meta/url/output/crates_turbopack-tests_tests_snapshot_import-meta_url_input_index_b53fce.js @@ -1,10 +1,10 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/crates_turbopack-tests_tests_snapshot_import-meta_url_input_index_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/import-meta/url/input/asset.txt (static)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/import-meta/url/input/asset.txt (static)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { __turbopack_export_value__("/crates/turbopack-tests/tests/snapshot/import-meta/url/static/05254cf29a922ae2.txt"); })()), -"[project]/crates/turbopack-tests/tests/snapshot/import-meta/url/input/mod.mjs (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/snapshot/import-meta/url/input/mod.mjs (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { const __TURBOPACK__import$2e$meta__ = { url: "file:///ROOT/crates/turbopack-tests/tests/snapshot/import-meta/url/input/mod.mjs" @@ -15,7 +15,7 @@ console.log(assetUrl); fetch(assetUrl).then((res)=>res.text()).then(console.log); }.call(this) }), -"[project]/crates/turbopack-tests/tests/snapshot/import-meta/url/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/snapshot/import-meta/url/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$import$2d$meta$2f$url$2f$input$2f$mod$2e$mjs__$28$ecmascript$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/import-meta/url/input/mod.mjs (ecmascript)"); "__TURBOPACK__ecmascript__hoisting__location__"; diff --git a/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_index_3121b1.js b/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_index_3121b1.js new file mode 100644 index 0000000000000..eb4d393ce2ef6 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_index_3121b1.js @@ -0,0 +1,11 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_index_3121b1.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_index_3121b1.js", + "chunks": [ + "output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_index_b53fce.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_index_45c162.js b/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_index_45c162.js index 297bbad87a19a..1fc1ce4f9da59 100644 --- a/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_index_45c162.js +++ b/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_index_45c162.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_index_45c162.js", {}, - {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/imports/dynamic/input/index.js (ecmascript)"],"chunkListPath":"output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_index.js_f5704b._.json"} + {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/imports/dynamic/input/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_index_b53fce.js index eec2658afb875..b9b5e7c727229 100644 --- a/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_index_b53fce.js @@ -1,12 +1,11 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_index_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/imports/dynamic/input/vercel.mjs (ecmascript, manifest chunk, loader)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/imports/dynamic/input/vercel.mjs (ecmascript, manifest chunk, loader)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { __turbopack_export_value__((__turbopack_import__) => { return __turbopack_load__("output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_vercel.mjs_4e74d3._.js").then(() => { return __turbopack_require__("[project]/crates/turbopack-tests/tests/snapshot/imports/dynamic/input/vercel.mjs (ecmascript, manifest chunk)"); - }).then(({ chunks, list }) => { - __turbopack_register_chunk_list__(list, chunks); + }).then((chunks) => { return Promise.all(chunks.map((chunk_path) => __turbopack_load__(chunk_path))); }).then(() => { return __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/imports/dynamic/input/vercel.mjs (ecmascript)"); @@ -14,7 +13,7 @@ __turbopack_export_value__((__turbopack_import__) => { }); })()), -"[project]/crates/turbopack-tests/tests/snapshot/imports/dynamic/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/snapshot/imports/dynamic/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { __turbopack_require__("[project]/crates/turbopack-tests/tests/snapshot/imports/dynamic/input/vercel.mjs (ecmascript, manifest chunk, loader)")(__turbopack_import__).then(console.log); diff --git a/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_index_b53fce.js.map b/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_index_b53fce.js.map index ec815f0ecd23f..3d018eebf12bd 100644 --- a/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_index_b53fce.js.map +++ b/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_index_b53fce.js.map @@ -1,6 +1,6 @@ { "version": 3, "sections": [ - {"offset": {"line": 18, "column": 0}, "map": {"version":3,"sources":["/turbopack/[project]/crates/turbopack-tests/tests/snapshot/imports/dynamic/input/index.js"],"sourcesContent":["import(\"./vercel.mjs\").then(console.log);\n"],"names":[],"mappings":"AAAA,qKAAuB,IAAI,CAAC,QAAQ,GAAG"}}, - {"offset": {"line": 19, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}] + {"offset": {"line": 17, "column": 0}, "map": {"version":3,"sources":["/turbopack/[project]/crates/turbopack-tests/tests/snapshot/imports/dynamic/input/index.js"],"sourcesContent":["import(\"./vercel.mjs\").then(console.log);\n"],"names":[],"mappings":"AAAA,qKAAuB,IAAI,CAAC,QAAQ,GAAG"}}, + {"offset": {"line": 18, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}] } \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_vercel.mjs_1e2f57._.js b/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_vercel.mjs_1e2f57._.js new file mode 100644 index 0000000000000..1b2b157d7effd --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_vercel.mjs_1e2f57._.js @@ -0,0 +1,11 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_vercel.mjs_1e2f57._.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_vercel.mjs_1e2f57._.js", + "chunks": [ + "output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_vercel.mjs_536504._.js" + ], + "source": "dynamic" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_vercel.mjs_4e74d3._.js b/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_vercel.mjs_4e74d3._.js index 5f7d0619e5820..b93845f45cbe5 100644 --- a/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_vercel.mjs_4e74d3._.js +++ b/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_vercel.mjs_4e74d3._.js @@ -1,13 +1,11 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_vercel.mjs_4e74d3._.js", { -"[project]/crates/turbopack-tests/tests/snapshot/imports/dynamic/input/vercel.mjs (ecmascript, manifest chunk)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/imports/dynamic/input/vercel.mjs (ecmascript, manifest chunk)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { -__turbopack_export_value__({ - "chunks": [ - "output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_vercel.mjs_536504._.js" - ], - "list": "output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_vercel.mjs_17bc56._.json" -}); +__turbopack_export_value__([ + "output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_vercel.mjs_536504._.js", + "output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_vercel.mjs_1e2f57._.js" +]); })()), }]); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_vercel.mjs_536504._.js b/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_vercel.mjs_536504._.js index c3e2a5a6b53bb..6128657832ec7 100644 --- a/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_vercel.mjs_536504._.js +++ b/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_vercel.mjs_536504._.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_vercel.mjs_536504._.js", { -"[project]/crates/turbopack-tests/tests/snapshot/imports/dynamic/input/vercel.mjs (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/imports/dynamic/input/vercel.mjs (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { __turbopack_esm__({ "default": ()=>__TURBOPACK__default__export__ diff --git a/crates/turbopack-tests/tests/snapshot/imports/json/output/crates_turbopack-tests_tests_snapshot_imports_json_input_index_961ae2.js b/crates/turbopack-tests/tests/snapshot/imports/json/output/crates_turbopack-tests_tests_snapshot_imports_json_input_index_961ae2.js index 2271062bc93d8..a9c6b3b455f73 100644 --- a/crates/turbopack-tests/tests/snapshot/imports/json/output/crates_turbopack-tests_tests_snapshot_imports_json_input_index_961ae2.js +++ b/crates/turbopack-tests/tests/snapshot/imports/json/output/crates_turbopack-tests_tests_snapshot_imports_json_input_index_961ae2.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/crates_turbopack-tests_tests_snapshot_imports_json_input_index_961ae2.js", {}, - {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_imports_json_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/imports/json/input/index.js (ecmascript)"],"chunkListPath":"output/crates_turbopack-tests_tests_snapshot_imports_json_input_index.js_f5704b._.json"} + {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_imports_json_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/imports/json/input/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/imports/json/output/crates_turbopack-tests_tests_snapshot_imports_json_input_index_af3240.js b/crates/turbopack-tests/tests/snapshot/imports/json/output/crates_turbopack-tests_tests_snapshot_imports_json_input_index_af3240.js new file mode 100644 index 0000000000000..452d8b38bf89e --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/imports/json/output/crates_turbopack-tests_tests_snapshot_imports_json_input_index_af3240.js @@ -0,0 +1,11 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/crates_turbopack-tests_tests_snapshot_imports_json_input_index_af3240.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/crates_turbopack-tests_tests_snapshot_imports_json_input_index_af3240.js", + "chunks": [ + "output/crates_turbopack-tests_tests_snapshot_imports_json_input_index_b53fce.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/imports/json/output/crates_turbopack-tests_tests_snapshot_imports_json_input_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/imports/json/output/crates_turbopack-tests_tests_snapshot_imports_json_input_index_b53fce.js index 8b300b9c40ef8..e89a80e487645 100644 --- a/crates/turbopack-tests/tests/snapshot/imports/json/output/crates_turbopack-tests_tests_snapshot_imports_json_input_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/imports/json/output/crates_turbopack-tests_tests_snapshot_imports_json_input_index_b53fce.js @@ -5,11 +5,11 @@ throw new Error("An error occurred while generating the chunk item [project]/crates/turbopack-tests/tests/snapshot/imports/json/input/invalid.json (json)\n\nCaused by:\n- Unable to make a module from invalid JSON: expected `,` or `}` at line 3 column 26\n\nDebug info:\n- An error occurred while generating the chunk item [project]/crates/turbopack-tests/tests/snapshot/imports/json/input/invalid.json (json)\n- Execution of module_factory failed\n- Execution of JsonChunkItem::content failed\n- Unable to make a module from invalid JSON: expected `,` or `}` at line 3 column 26\n at nested.?\n 1 | {\n 2 | \"nested\": {\n | v\n 3 + \"this-is\": \"invalid\" // lint-staged will remove trailing commas, so here's a comment\n | ^\n 4 | }\n 5 | }"); }}), -"[project]/crates/turbopack-tests/tests/snapshot/imports/json/input/package.json (json)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/imports/json/input/package.json (json)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { __turbopack_export_value__(JSON.parse("{\"name\":\"json-snapshot\"}")); })()), -"[project]/crates/turbopack-tests/tests/snapshot/imports/json/input/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/imports/json/input/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$imports$2f$json$2f$input$2f$package$2e$json__$28$json$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/imports/json/input/package.json (json)"); var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$imports$2f$json$2f$input$2f$invalid$2e$json__$28$json$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/imports/json/input/invalid.json (json)"); diff --git a/crates/turbopack-tests/tests/snapshot/imports/resolve_error_cjs/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_cjs_input_index_9ebfba.js b/crates/turbopack-tests/tests/snapshot/imports/resolve_error_cjs/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_cjs_input_index_9ebfba.js new file mode 100644 index 0000000000000..180e651df5725 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/imports/resolve_error_cjs/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_cjs_input_index_9ebfba.js @@ -0,0 +1,11 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_cjs_input_index_9ebfba.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_cjs_input_index_9ebfba.js", + "chunks": [ + "output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_cjs_input_index_b53fce.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/imports/resolve_error_cjs/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_cjs_input_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/imports/resolve_error_cjs/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_cjs_input_index_b53fce.js index 2063f21841d7d..460d288ab81b8 100644 --- a/crates/turbopack-tests/tests/snapshot/imports/resolve_error_cjs/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_cjs_input_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/imports/resolve_error_cjs/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_cjs_input_index_b53fce.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_cjs_input_index_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/imports/resolve_error_cjs/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/snapshot/imports/resolve_error_cjs/input/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { const dne = __turbopack_require__((()=>{ const e = new Error("Cannot find module 'does-not-exist/path'"); diff --git a/crates/turbopack-tests/tests/snapshot/imports/resolve_error_cjs/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_cjs_input_index_f8412b.js b/crates/turbopack-tests/tests/snapshot/imports/resolve_error_cjs/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_cjs_input_index_f8412b.js index 9fd9e070be8ac..e40d1114f7768 100644 --- a/crates/turbopack-tests/tests/snapshot/imports/resolve_error_cjs/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_cjs_input_index_f8412b.js +++ b/crates/turbopack-tests/tests/snapshot/imports/resolve_error_cjs/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_cjs_input_index_f8412b.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_cjs_input_index_f8412b.js", {}, - {"otherChunks":["output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_cjs_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/imports/resolve_error_cjs/input/index.js (ecmascript)"],"chunkListPath":"output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_cjs_input_index.js_f5704b._.json"} + {"otherChunks":["output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_cjs_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/imports/resolve_error_cjs/input/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/imports/resolve_error_esm/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_esm_input_index_0b3e45.js b/crates/turbopack-tests/tests/snapshot/imports/resolve_error_esm/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_esm_input_index_0b3e45.js index 4165bd9cc924b..0fd6383966e24 100644 --- a/crates/turbopack-tests/tests/snapshot/imports/resolve_error_esm/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_esm_input_index_0b3e45.js +++ b/crates/turbopack-tests/tests/snapshot/imports/resolve_error_esm/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_esm_input_index_0b3e45.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_esm_input_index_0b3e45.js", {}, - {"otherChunks":["output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_esm_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/imports/resolve_error_esm/input/index.js (ecmascript)"],"chunkListPath":"output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_esm_input_index.js_f5704b._.json"} + {"otherChunks":["output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_esm_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/imports/resolve_error_esm/input/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/imports/resolve_error_esm/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_esm_input_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/imports/resolve_error_esm/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_esm_input_index_b53fce.js index 5e7f546e68a21..2028b5c516b3e 100644 --- a/crates/turbopack-tests/tests/snapshot/imports/resolve_error_esm/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_esm_input_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/imports/resolve_error_esm/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_esm_input_index_b53fce.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_esm_input_index_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/imports/resolve_error_esm/input/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/imports/resolve_error_esm/input/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { (()=>{ const e = new Error("Cannot find module 'does-not-exist/path'"); diff --git a/crates/turbopack-tests/tests/snapshot/imports/resolve_error_esm/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_esm_input_index_effdbf.js b/crates/turbopack-tests/tests/snapshot/imports/resolve_error_esm/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_esm_input_index_effdbf.js new file mode 100644 index 0000000000000..79b1ab4448134 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/imports/resolve_error_esm/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_esm_input_index_effdbf.js @@ -0,0 +1,11 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_esm_input_index_effdbf.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_esm_input_index_effdbf.js", + "chunks": [ + "output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_esm_input_index_b53fce.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/79fb1_turbopack-tests_tests_snapshot_imports_static-and-dynamic_input_index_a1d5a5.js b/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/79fb1_turbopack-tests_tests_snapshot_imports_static-and-dynamic_input_index_a1d5a5.js new file mode 100644 index 0000000000000..1aa2d0ddb7964 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/79fb1_turbopack-tests_tests_snapshot_imports_static-and-dynamic_input_index_a1d5a5.js @@ -0,0 +1,11 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/79fb1_turbopack-tests_tests_snapshot_imports_static-and-dynamic_input_index_a1d5a5.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/79fb1_turbopack-tests_tests_snapshot_imports_static-and-dynamic_input_index_a1d5a5.js", + "chunks": [ + "output/79fb1_turbopack-tests_tests_snapshot_imports_static-and-dynamic_input_index_b53fce.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/79fb1_turbopack-tests_tests_snapshot_imports_static-and-dynamic_input_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/79fb1_turbopack-tests_tests_snapshot_imports_static-and-dynamic_input_index_b53fce.js index 464c8440bf64a..dcb656b6429a8 100644 --- a/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/79fb1_turbopack-tests_tests_snapshot_imports_static-and-dynamic_input_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/79fb1_turbopack-tests_tests_snapshot_imports_static-and-dynamic_input_index_b53fce.js @@ -1,12 +1,11 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/79fb1_turbopack-tests_tests_snapshot_imports_static-and-dynamic_input_index_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/input/vercel.mjs (ecmascript, manifest chunk, loader)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/input/vercel.mjs (ecmascript, manifest chunk, loader)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { __turbopack_export_value__((__turbopack_import__) => { return __turbopack_load__("output/a587c_tests_snapshot_imports_static-and-dynamic_input_vercel.mjs_10fe7a._.js").then(() => { return __turbopack_require__("[project]/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/input/vercel.mjs (ecmascript, manifest chunk)"); - }).then(({ chunks, list }) => { - __turbopack_register_chunk_list__(list, chunks); + }).then((chunks) => { return Promise.all(chunks.map((chunk_path) => __turbopack_load__(chunk_path))); }).then(() => { return __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/input/vercel.mjs (ecmascript)"); @@ -14,7 +13,7 @@ __turbopack_export_value__((__turbopack_import__) => { }); })()), -"[project]/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/input/vercel.mjs (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/input/vercel.mjs (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { __turbopack_esm__({ "default": ()=>__TURBOPACK__default__export__ @@ -22,7 +21,7 @@ __turbopack_esm__({ const __TURBOPACK__default__export__ = "turbopack"; })()), -"[project]/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/input/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/input/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$imports$2f$static$2d$and$2d$dynamic$2f$input$2f$vercel$2e$mjs__$28$ecmascript$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/input/vercel.mjs (ecmascript)"); "__TURBOPACK__ecmascript__hoisting__location__"; diff --git a/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/79fb1_turbopack-tests_tests_snapshot_imports_static-and-dynamic_input_index_b53fce.js.map b/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/79fb1_turbopack-tests_tests_snapshot_imports_static-and-dynamic_input_index_b53fce.js.map index 10eaa9afd77d1..5df6e17c5561a 100644 --- a/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/79fb1_turbopack-tests_tests_snapshot_imports_static-and-dynamic_input_index_b53fce.js.map +++ b/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/79fb1_turbopack-tests_tests_snapshot_imports_static-and-dynamic_input_index_b53fce.js.map @@ -1,8 +1,8 @@ { "version": 3, "sections": [ - {"offset": {"line": 18, "column": 0}, "map": {"version":3,"sources":["/turbopack/[project]/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/input/vercel.mjs"],"sourcesContent":["export default \"turbopack\";\n"],"names":[],"mappings":";;;uCAAe"}}, - {"offset": {"line": 22, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 26, "column": 0}, "map": {"version":3,"sources":["/turbopack/[project]/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/input/index.js"],"sourcesContent":["import img from \"./vercel.mjs\";\nconsole.log(img);\n\nimport(\"./vercel.mjs\").then(console.log);\n"],"names":[],"mappings":";;;AACA,QAAQ,GAAG;AAEX,gLAAuB,IAAI,CAAC,QAAQ,GAAG"}}, - {"offset": {"line": 31, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}] + {"offset": {"line": 17, "column": 0}, "map": {"version":3,"sources":["/turbopack/[project]/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/input/vercel.mjs"],"sourcesContent":["export default \"turbopack\";\n"],"names":[],"mappings":";;;uCAAe"}}, + {"offset": {"line": 21, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, + {"offset": {"line": 25, "column": 0}, "map": {"version":3,"sources":["/turbopack/[project]/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/input/index.js"],"sourcesContent":["import img from \"./vercel.mjs\";\nconsole.log(img);\n\nimport(\"./vercel.mjs\").then(console.log);\n"],"names":[],"mappings":";;;AACA,QAAQ,GAAG;AAEX,gLAAuB,IAAI,CAAC,QAAQ,GAAG"}}, + {"offset": {"line": 30, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}] } \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/79fb1_turbopack-tests_tests_snapshot_imports_static-and-dynamic_input_index_ec8693.js b/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/79fb1_turbopack-tests_tests_snapshot_imports_static-and-dynamic_input_index_ec8693.js index 704259bec3e03..75d6a6284287a 100644 --- a/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/79fb1_turbopack-tests_tests_snapshot_imports_static-and-dynamic_input_index_ec8693.js +++ b/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/79fb1_turbopack-tests_tests_snapshot_imports_static-and-dynamic_input_index_ec8693.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/79fb1_turbopack-tests_tests_snapshot_imports_static-and-dynamic_input_index_ec8693.js", {}, - {"otherChunks":["output/79fb1_turbopack-tests_tests_snapshot_imports_static-and-dynamic_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/input/index.js (ecmascript)"],"chunkListPath":"output/79fb1_turbopack-tests_tests_snapshot_imports_static-and-dynamic_input_index.js_f5704b._.json"} + {"otherChunks":["output/79fb1_turbopack-tests_tests_snapshot_imports_static-and-dynamic_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/input/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/a587c_tests_snapshot_imports_static-and-dynamic_input_vercel.mjs_10fe7a._.js b/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/a587c_tests_snapshot_imports_static-and-dynamic_input_vercel.mjs_10fe7a._.js index 1c48080c76b77..b77421666ea99 100644 --- a/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/a587c_tests_snapshot_imports_static-and-dynamic_input_vercel.mjs_10fe7a._.js +++ b/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/a587c_tests_snapshot_imports_static-and-dynamic_input_vercel.mjs_10fe7a._.js @@ -1,13 +1,11 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/a587c_tests_snapshot_imports_static-and-dynamic_input_vercel.mjs_10fe7a._.js", { -"[project]/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/input/vercel.mjs (ecmascript, manifest chunk)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/input/vercel.mjs (ecmascript, manifest chunk)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { -__turbopack_export_value__({ - "chunks": [ - "output/a587c_tests_snapshot_imports_static-and-dynamic_input_vercel.mjs_26aaf6._.js" - ], - "list": "output/a587c_tests_snapshot_imports_static-and-dynamic_input_vercel.mjs_e8af7f._.json" -}); +__turbopack_export_value__([ + "output/a587c_tests_snapshot_imports_static-and-dynamic_input_vercel.mjs_26aaf6._.js", + "output/a587c_tests_snapshot_imports_static-and-dynamic_input_vercel.mjs_b21398._.js" +]); })()), }]); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/a587c_tests_snapshot_imports_static-and-dynamic_input_vercel.mjs_26aaf6._.js b/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/a587c_tests_snapshot_imports_static-and-dynamic_input_vercel.mjs_26aaf6._.js index d3ea7bf940f79..e02777a0b0d0d 100644 --- a/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/a587c_tests_snapshot_imports_static-and-dynamic_input_vercel.mjs_26aaf6._.js +++ b/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/a587c_tests_snapshot_imports_static-and-dynamic_input_vercel.mjs_26aaf6._.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/a587c_tests_snapshot_imports_static-and-dynamic_input_vercel.mjs_26aaf6._.js", { -"[project]/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/input/vercel.mjs (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/input/vercel.mjs (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { __turbopack_esm__({ "default": ()=>__TURBOPACK__default__export__ diff --git a/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/a587c_tests_snapshot_imports_static-and-dynamic_input_vercel.mjs_b21398._.js b/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/a587c_tests_snapshot_imports_static-and-dynamic_input_vercel.mjs_b21398._.js new file mode 100644 index 0000000000000..70d747ea1832c --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/a587c_tests_snapshot_imports_static-and-dynamic_input_vercel.mjs_b21398._.js @@ -0,0 +1,11 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/a587c_tests_snapshot_imports_static-and-dynamic_input_vercel.mjs_b21398._.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/a587c_tests_snapshot_imports_static-and-dynamic_input_vercel.mjs_b21398._.js", + "chunks": [ + "output/a587c_tests_snapshot_imports_static-and-dynamic_input_vercel.mjs_26aaf6._.js" + ], + "source": "dynamic" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/imports/static/output/crates_turbopack-tests_tests_snapshot_imports_static_input_index_885269.js b/crates/turbopack-tests/tests/snapshot/imports/static/output/crates_turbopack-tests_tests_snapshot_imports_static_input_index_885269.js index 384dc129095cf..228942fc34550 100644 --- a/crates/turbopack-tests/tests/snapshot/imports/static/output/crates_turbopack-tests_tests_snapshot_imports_static_input_index_885269.js +++ b/crates/turbopack-tests/tests/snapshot/imports/static/output/crates_turbopack-tests_tests_snapshot_imports_static_input_index_885269.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/crates_turbopack-tests_tests_snapshot_imports_static_input_index_885269.js", {}, - {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_imports_static_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/imports/static/input/index.js (ecmascript)"],"chunkListPath":"output/crates_turbopack-tests_tests_snapshot_imports_static_input_index.js_f5704b._.json"} + {"otherChunks":["output/crates_turbopack-tests_tests_snapshot_imports_static_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/imports/static/input/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/imports/static/output/crates_turbopack-tests_tests_snapshot_imports_static_input_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/imports/static/output/crates_turbopack-tests_tests_snapshot_imports_static_input_index_b53fce.js index 488d2d6260f55..151847d1011fb 100644 --- a/crates/turbopack-tests/tests/snapshot/imports/static/output/crates_turbopack-tests_tests_snapshot_imports_static_input_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/imports/static/output/crates_turbopack-tests_tests_snapshot_imports_static_input_index_b53fce.js @@ -1,10 +1,10 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/crates_turbopack-tests_tests_snapshot_imports_static_input_index_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/imports/static/input/vercel.svg (static)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/imports/static/input/vercel.svg (static)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { __turbopack_export_value__("/crates/turbopack-tests/tests/snapshot/imports/static/static/957b9b162f8447f9.svg"); })()), -"[project]/crates/turbopack-tests/tests/snapshot/imports/static/input/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/imports/static/input/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$imports$2f$static$2f$input$2f$vercel$2e$svg__$28$static$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/imports/static/input/vercel.svg (static)"); "__TURBOPACK__ecmascript__hoisting__location__"; diff --git a/crates/turbopack-tests/tests/snapshot/imports/static/output/crates_turbopack-tests_tests_snapshot_imports_static_input_index_da04c9.js b/crates/turbopack-tests/tests/snapshot/imports/static/output/crates_turbopack-tests_tests_snapshot_imports_static_input_index_da04c9.js new file mode 100644 index 0000000000000..9addc18a51c3a --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/imports/static/output/crates_turbopack-tests_tests_snapshot_imports_static_input_index_da04c9.js @@ -0,0 +1,11 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/crates_turbopack-tests_tests_snapshot_imports_static_input_index_da04c9.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/crates_turbopack-tests_tests_snapshot_imports_static_input_index_da04c9.js", + "chunks": [ + "output/crates_turbopack-tests_tests_snapshot_imports_static_input_index_b53fce.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/node/node_protocol_external/output/79fb1_turbopack-tests_tests_snapshot_node_node_protocol_external_input_index_667edf.js b/crates/turbopack-tests/tests/snapshot/node/node_protocol_external/output/79fb1_turbopack-tests_tests_snapshot_node_node_protocol_external_input_index_667edf.js index a97140d5dd7cc..ce2d61aad427d 100644 --- a/crates/turbopack-tests/tests/snapshot/node/node_protocol_external/output/79fb1_turbopack-tests_tests_snapshot_node_node_protocol_external_input_index_667edf.js +++ b/crates/turbopack-tests/tests/snapshot/node/node_protocol_external/output/79fb1_turbopack-tests_tests_snapshot_node_node_protocol_external_input_index_667edf.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/79fb1_turbopack-tests_tests_snapshot_node_node_protocol_external_input_index_667edf.js", {}, - {"otherChunks":["output/79fb1_turbopack-tests_tests_snapshot_node_node_protocol_external_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/node/node_protocol_external/input/index.js (ecmascript)"],"chunkListPath":"output/a587c_tests_snapshot_node_node_protocol_external_input_index.js_f5704b._.json"} + {"otherChunks":["output/79fb1_turbopack-tests_tests_snapshot_node_node_protocol_external_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/node/node_protocol_external/input/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/node/node_protocol_external/output/79fb1_turbopack-tests_tests_snapshot_node_node_protocol_external_input_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/node/node_protocol_external/output/79fb1_turbopack-tests_tests_snapshot_node_node_protocol_external_input_index_b53fce.js index 974e23471c01a..ac40b59f825c4 100644 --- a/crates/turbopack-tests/tests/snapshot/node/node_protocol_external/output/79fb1_turbopack-tests_tests_snapshot_node_node_protocol_external_input_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/node/node_protocol_external/output/79fb1_turbopack-tests_tests_snapshot_node_node_protocol_external_input_index_b53fce.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/79fb1_turbopack-tests_tests_snapshot_node_node_protocol_external_input_index_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/node/node_protocol_external/input/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/node/node_protocol_external/input/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { var __TURBOPACK__external__node$3a$fs__ = __turbopack_external_require__("node:fs", true); "__TURBOPACK__ecmascript__hoisting__location__"; diff --git a/crates/turbopack-tests/tests/snapshot/node/node_protocol_external/output/79fb1_turbopack-tests_tests_snapshot_node_node_protocol_external_input_index_e13bab.js b/crates/turbopack-tests/tests/snapshot/node/node_protocol_external/output/79fb1_turbopack-tests_tests_snapshot_node_node_protocol_external_input_index_e13bab.js new file mode 100644 index 0000000000000..b1c91dd75962f --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/node/node_protocol_external/output/79fb1_turbopack-tests_tests_snapshot_node_node_protocol_external_input_index_e13bab.js @@ -0,0 +1,11 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/79fb1_turbopack-tests_tests_snapshot_node_node_protocol_external_input_index_e13bab.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/79fb1_turbopack-tests_tests_snapshot_node_node_protocol_external_input_index_e13bab.js", + "chunks": [ + "output/79fb1_turbopack-tests_tests_snapshot_node_node_protocol_external_input_index_b53fce.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/styled_components/styled_components/output/63a02_styled-components_index_a35c8c.js b/crates/turbopack-tests/tests/snapshot/styled_components/styled_components/output/63a02_styled-components_index_a35c8c.js index 0c8f07ed068a1..bd10b0645ab61 100644 --- a/crates/turbopack-tests/tests/snapshot/styled_components/styled_components/output/63a02_styled-components_index_a35c8c.js +++ b/crates/turbopack-tests/tests/snapshot/styled_components/styled_components/output/63a02_styled-components_index_a35c8c.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/63a02_styled-components_index_a35c8c.js", { -"[project]/crates/turbopack-tests/tests/node_modules/styled-components/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/node_modules/styled-components/index.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { "purposefully empty stub"; "styled-components/index.js"; diff --git a/crates/turbopack-tests/tests/snapshot/styled_components/styled_components/output/a587c_tests_snapshot_styled_components_styled_components_input_index_4a0400.js b/crates/turbopack-tests/tests/snapshot/styled_components/styled_components/output/a587c_tests_snapshot_styled_components_styled_components_input_index_4a0400.js new file mode 100644 index 0000000000000..de21ff42e8f28 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/styled_components/styled_components/output/a587c_tests_snapshot_styled_components_styled_components_input_index_4a0400.js @@ -0,0 +1,12 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/a587c_tests_snapshot_styled_components_styled_components_input_index_4a0400.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/a587c_tests_snapshot_styled_components_styled_components_input_index_4a0400.js", + "chunks": [ + "output/63a02_styled-components_index_a35c8c.js", + "output/a587c_tests_snapshot_styled_components_styled_components_input_index_b53fce.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/styled_components/styled_components/output/a587c_tests_snapshot_styled_components_styled_components_input_index_afc482.js b/crates/turbopack-tests/tests/snapshot/styled_components/styled_components/output/a587c_tests_snapshot_styled_components_styled_components_input_index_afc482.js index 5023e544bbe55..dfb9b3b1d6400 100644 --- a/crates/turbopack-tests/tests/snapshot/styled_components/styled_components/output/a587c_tests_snapshot_styled_components_styled_components_input_index_afc482.js +++ b/crates/turbopack-tests/tests/snapshot/styled_components/styled_components/output/a587c_tests_snapshot_styled_components_styled_components_input_index_afc482.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/a587c_tests_snapshot_styled_components_styled_components_input_index_afc482.js", {}, - {"otherChunks":["output/63a02_styled-components_index_a35c8c.js","output/a587c_tests_snapshot_styled_components_styled_components_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/styled_components/styled_components/input/index.js (ecmascript)"],"chunkListPath":"output/a587c_tests_snapshot_styled_components_styled_components_input_index.js_f5704b._.json"} + {"otherChunks":["output/63a02_styled-components_index_a35c8c.js","output/a587c_tests_snapshot_styled_components_styled_components_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/styled_components/styled_components/input/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/styled_components/styled_components/output/a587c_tests_snapshot_styled_components_styled_components_input_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/styled_components/styled_components/output/a587c_tests_snapshot_styled_components_styled_components_input_index_b53fce.js index 55b4c3c683866..fefb9949b74f3 100644 --- a/crates/turbopack-tests/tests/snapshot/styled_components/styled_components/output/a587c_tests_snapshot_styled_components_styled_components_input_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/styled_components/styled_components/output/a587c_tests_snapshot_styled_components_styled_components_input_index_b53fce.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/a587c_tests_snapshot_styled_components_styled_components_input_index_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/styled_components/styled_components/input/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/styled_components/styled_components/input/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$node_modules$2f$styled$2d$components$2f$index$2e$js__$28$ecmascript$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/node_modules/styled-components/index.js (ecmascript)"); "__TURBOPACK__ecmascript__hoisting__location__"; diff --git a/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/output/63a02_react_jsx-dev-runtime_7d1be7.js b/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/output/63a02_react_jsx-dev-runtime_7d1be7.js index 3a702609bf681..0d59b1422c9ab 100644 --- a/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/output/63a02_react_jsx-dev-runtime_7d1be7.js +++ b/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/output/63a02_react_jsx-dev-runtime_7d1be7.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/63a02_react_jsx-dev-runtime_7d1be7.js", { -"[project]/crates/turbopack-tests/tests/node_modules/react/jsx-dev-runtime.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/node_modules/react/jsx-dev-runtime.js (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { "purposefully empty stub"; "react/jsx-dev-runtime.js"; diff --git a/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/output/7b7bf_third_party_component_index_8e9ad8.js b/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/output/7b7bf_third_party_component_index_8e9ad8.js index ad0ae22242d43..ca28b5afb4ed9 100644 --- a/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/output/7b7bf_third_party_component_index_8e9ad8.js +++ b/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/output/7b7bf_third_party_component_index_8e9ad8.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/7b7bf_third_party_component_index_8e9ad8.js", { -"[project]/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/input/node_modules/third_party_component/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/input/node_modules/third_party_component/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { __turbopack_esm__({ "default": ()=>ThirdPartyComponent diff --git a/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/output/8562f_snapshot_swc_transforms_mono_transforms_input_packages_component_index_b0e6f8.js b/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/output/8562f_snapshot_swc_transforms_mono_transforms_input_packages_component_index_b0e6f8.js index c17390839bc16..6657801422a89 100644 --- a/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/output/8562f_snapshot_swc_transforms_mono_transforms_input_packages_component_index_b0e6f8.js +++ b/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/output/8562f_snapshot_swc_transforms_mono_transforms_input_packages_component_index_b0e6f8.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/8562f_snapshot_swc_transforms_mono_transforms_input_packages_component_index_b0e6f8.js", { -"[project]/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/input/packages/component/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/input/packages/component/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { __turbopack_esm__({ "default": ()=>MyApp diff --git a/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/output/a587c_tests_snapshot_swc_transforms_mono_transforms_input_packages_app_index_4a3d65.js b/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/output/a587c_tests_snapshot_swc_transforms_mono_transforms_input_packages_app_index_4a3d65.js index 11abd46e15ba1..e4c777403dad8 100644 --- a/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/output/a587c_tests_snapshot_swc_transforms_mono_transforms_input_packages_app_index_4a3d65.js +++ b/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/output/a587c_tests_snapshot_swc_transforms_mono_transforms_input_packages_app_index_4a3d65.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/a587c_tests_snapshot_swc_transforms_mono_transforms_input_packages_app_index_4a3d65.js", {}, - {"otherChunks":["output/63a02_react_jsx-dev-runtime_7d1be7.js","output/8562f_snapshot_swc_transforms_mono_transforms_input_packages_component_index_b0e6f8.js","output/a587c_tests_snapshot_swc_transforms_mono_transforms_input_packages_app_index_b53fce.js","output/7b7bf_third_party_component_index_8e9ad8.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/input/packages/app/index.js (ecmascript)"],"chunkListPath":"output/8562f_snapshot_swc_transforms_mono_transforms_input_packages_app_index.js_f5704b._.json"} + {"otherChunks":["output/63a02_react_jsx-dev-runtime_7d1be7.js","output/8562f_snapshot_swc_transforms_mono_transforms_input_packages_component_index_b0e6f8.js","output/a587c_tests_snapshot_swc_transforms_mono_transforms_input_packages_app_index_b53fce.js","output/7b7bf_third_party_component_index_8e9ad8.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/input/packages/app/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/output/a587c_tests_snapshot_swc_transforms_mono_transforms_input_packages_app_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/output/a587c_tests_snapshot_swc_transforms_mono_transforms_input_packages_app_index_b53fce.js index 29a55e057a385..dcec06373730c 100644 --- a/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/output/a587c_tests_snapshot_swc_transforms_mono_transforms_input_packages_app_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/output/a587c_tests_snapshot_swc_transforms_mono_transforms_input_packages_app_index_b53fce.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/a587c_tests_snapshot_swc_transforms_mono_transforms_input_packages_app_index_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/input/packages/app/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/input/packages/app/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$swc_transforms$2f$mono_transforms$2f$input$2f$packages$2f$component$2f$index$2e$js__$28$ecmascript$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/input/packages/component/index.js (ecmascript)"); var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$swc_transforms$2f$mono_transforms$2f$input$2f$node_modules$2f$third_party_component$2f$index$2e$js__$28$ecmascript$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/input/node_modules/third_party_component/index.js (ecmascript)"); diff --git a/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/output/a587c_tests_snapshot_swc_transforms_mono_transforms_input_packages_app_index_d54d58.js b/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/output/a587c_tests_snapshot_swc_transforms_mono_transforms_input_packages_app_index_d54d58.js new file mode 100644 index 0000000000000..74d56b96d15da --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/output/a587c_tests_snapshot_swc_transforms_mono_transforms_input_packages_app_index_d54d58.js @@ -0,0 +1,14 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/a587c_tests_snapshot_swc_transforms_mono_transforms_input_packages_app_index_d54d58.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/a587c_tests_snapshot_swc_transforms_mono_transforms_input_packages_app_index_d54d58.js", + "chunks": [ + "output/63a02_react_jsx-dev-runtime_7d1be7.js", + "output/8562f_snapshot_swc_transforms_mono_transforms_input_packages_component_index_b0e6f8.js", + "output/a587c_tests_snapshot_swc_transforms_mono_transforms_input_packages_app_index_b53fce.js", + "output/7b7bf_third_party_component_index_8e9ad8.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/63a02_@swc_helpers_src__class_call_check.mjs_f682d0._.js b/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/63a02_@swc_helpers_src__class_call_check.mjs_f682d0._.js index b8c1eaa9ca7b5..21d24915430cd 100644 --- a/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/63a02_@swc_helpers_src__class_call_check.mjs_f682d0._.js +++ b/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/63a02_@swc_helpers_src__class_call_check.mjs_f682d0._.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/63a02_@swc_helpers_src__class_call_check.mjs_f682d0._.js", { -"[project]/crates/turbopack-tests/tests/node_modules/@swc/helpers/src/_class_call_check.mjs (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { +"[project]/crates/turbopack-tests/tests/node_modules/@swc/helpers/src/_class_call_check.mjs (ecmascript)": (function({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname, m: module, e: exports }) { !function() { "purposefully empty stub"; "@swc/helpers/src/_class_call_check.mjs"; diff --git a/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_21f811.js b/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_21f811.js new file mode 100644 index 0000000000000..84ef63cbfae38 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_21f811.js @@ -0,0 +1,12 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_21f811.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_21f811.js", + "chunks": [ + "output/63a02_@swc_helpers_src__class_call_check.mjs_f682d0._.js", + "output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_b53fce.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_b53fce.js index 58ef4db6d832f..fe1d8975b8cc4 100644 --- a/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_b53fce.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/input/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/input/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$node_modules$2f40$swc$2f$helpers$2f$src$2f$_class_call_check$2e$mjs__$28$ecmascript$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/node_modules/@swc/helpers/src/_class_call_check.mjs (ecmascript)"); "__TURBOPACK__ecmascript__hoisting__location__"; diff --git a/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_e2f660.js b/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_e2f660.js index 12217ff04da83..bdc5bbd496ce4 100644 --- a/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_e2f660.js +++ b/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_e2f660.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_e2f660.js", {}, - {"otherChunks":["output/63a02_@swc_helpers_src__class_call_check.mjs_f682d0._.js","output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/input/index.js (ecmascript)"],"chunkListPath":"output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index.js_f5704b._.json"} + {"otherChunks":["output/63a02_@swc_helpers_src__class_call_check.mjs_f682d0._.js","output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/input/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/typescript/jsconfig-baseurl/output/79fb1_turbopack-tests_tests_snapshot_typescript_jsconfig-baseurl_input_index_8f1e58.js b/crates/turbopack-tests/tests/snapshot/typescript/jsconfig-baseurl/output/79fb1_turbopack-tests_tests_snapshot_typescript_jsconfig-baseurl_input_index_8f1e58.js index ad9aa23b13d06..e0c36e992044e 100644 --- a/crates/turbopack-tests/tests/snapshot/typescript/jsconfig-baseurl/output/79fb1_turbopack-tests_tests_snapshot_typescript_jsconfig-baseurl_input_index_8f1e58.js +++ b/crates/turbopack-tests/tests/snapshot/typescript/jsconfig-baseurl/output/79fb1_turbopack-tests_tests_snapshot_typescript_jsconfig-baseurl_input_index_8f1e58.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/79fb1_turbopack-tests_tests_snapshot_typescript_jsconfig-baseurl_input_index_8f1e58.js", {}, - {"otherChunks":["output/79fb1_turbopack-tests_tests_snapshot_typescript_jsconfig-baseurl_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/typescript/jsconfig-baseurl/input/index.js (ecmascript)"],"chunkListPath":"output/a587c_tests_snapshot_typescript_jsconfig-baseurl_input_index.js_f5704b._.json"} + {"otherChunks":["output/79fb1_turbopack-tests_tests_snapshot_typescript_jsconfig-baseurl_input_index_b53fce.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/typescript/jsconfig-baseurl/input/index.js (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/typescript/jsconfig-baseurl/output/79fb1_turbopack-tests_tests_snapshot_typescript_jsconfig-baseurl_input_index_9edb6f.js b/crates/turbopack-tests/tests/snapshot/typescript/jsconfig-baseurl/output/79fb1_turbopack-tests_tests_snapshot_typescript_jsconfig-baseurl_input_index_9edb6f.js new file mode 100644 index 0000000000000..03bd54ce68390 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/typescript/jsconfig-baseurl/output/79fb1_turbopack-tests_tests_snapshot_typescript_jsconfig-baseurl_input_index_9edb6f.js @@ -0,0 +1,11 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/79fb1_turbopack-tests_tests_snapshot_typescript_jsconfig-baseurl_input_index_9edb6f.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/79fb1_turbopack-tests_tests_snapshot_typescript_jsconfig-baseurl_input_index_9edb6f.js", + "chunks": [ + "output/79fb1_turbopack-tests_tests_snapshot_typescript_jsconfig-baseurl_input_index_b53fce.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/typescript/jsconfig-baseurl/output/79fb1_turbopack-tests_tests_snapshot_typescript_jsconfig-baseurl_input_index_b53fce.js b/crates/turbopack-tests/tests/snapshot/typescript/jsconfig-baseurl/output/79fb1_turbopack-tests_tests_snapshot_typescript_jsconfig-baseurl_input_index_b53fce.js index 081e6f902f552..06d4062ecdf41 100644 --- a/crates/turbopack-tests/tests/snapshot/typescript/jsconfig-baseurl/output/79fb1_turbopack-tests_tests_snapshot_typescript_jsconfig-baseurl_input_index_b53fce.js +++ b/crates/turbopack-tests/tests/snapshot/typescript/jsconfig-baseurl/output/79fb1_turbopack-tests_tests_snapshot_typescript_jsconfig-baseurl_input_index_b53fce.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/79fb1_turbopack-tests_tests_snapshot_typescript_jsconfig-baseurl_input_index_b53fce.js", { -"[project]/crates/turbopack-tests/tests/snapshot/typescript/jsconfig-baseurl/input/prop.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/typescript/jsconfig-baseurl/input/prop.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { __turbopack_esm__({ "prop": ()=>prop @@ -8,7 +8,7 @@ __turbopack_esm__({ const prop = 1; })()), -"[project]/crates/turbopack-tests/tests/snapshot/typescript/jsconfig-baseurl/input/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/typescript/jsconfig-baseurl/input/index.js (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$typescript$2f$jsconfig$2d$baseurl$2f$input$2f$prop$2e$js__$28$ecmascript$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/typescript/jsconfig-baseurl/input/prop.js (ecmascript)"); var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$typescript$2f$jsconfig$2d$baseurl$2f$input$2f$prop$2e$js__$28$ecmascript$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/typescript/jsconfig-baseurl/input/prop.js (ecmascript)"); diff --git a/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-baseurl/output/a587c_tests_snapshot_typescript_tsconfig-baseurl_input_index.ts_0aa04e._.js b/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-baseurl/output/a587c_tests_snapshot_typescript_tsconfig-baseurl_input_index.ts_0aa04e._.js index f7b9e0e3f97dd..dce144db5edb4 100644 --- a/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-baseurl/output/a587c_tests_snapshot_typescript_tsconfig-baseurl_input_index.ts_0aa04e._.js +++ b/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-baseurl/output/a587c_tests_snapshot_typescript_tsconfig-baseurl_input_index.ts_0aa04e._.js @@ -1,7 +1,7 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ "output/a587c_tests_snapshot_typescript_tsconfig-baseurl_input_index.ts_0aa04e._.js", {}, - {"otherChunks":["output/a587c_tests_snapshot_typescript_tsconfig-baseurl_input_index.ts_b53fce._.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-baseurl/input/index.ts (ecmascript)"],"chunkListPath":"output/a587c_tests_snapshot_typescript_tsconfig-baseurl_input_index.ts_f5704b._.json"} + {"otherChunks":["output/a587c_tests_snapshot_typescript_tsconfig-baseurl_input_index.ts_b53fce._.js"],"runtimeModuleIds":["[project]/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-baseurl/input/index.ts (ecmascript)"]} ]); (() => { if (!Array.isArray(globalThis.TURBOPACK)) { @@ -15,6 +15,7 @@ if (!Array.isArray(globalThis.TURBOPACK)) { /** @typedef {import('../types').ChunkPath} ChunkPath */ /** @typedef {import('../types').ModuleId} ModuleId */ /** @typedef {import('../types').GetFirstModuleChunk} GetFirstModuleChunk */ +/** @typedef {import('../types').ChunkList} ChunkList */ /** @typedef {import('../types').Module} Module */ /** @typedef {import('../types').SourceInfo} SourceInfo */ @@ -327,7 +328,6 @@ function instantiateModule(id, source) { m: module, c: moduleCache, l: loadChunk.bind(null, { type: SourceTypeParent, parentId: id }), - k: registerChunkList, g: globalThis, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); @@ -1184,6 +1184,10 @@ function disposeChunkList(chunkListPath) { } } + // We must also dispose of the chunk list's chunk itself to ensure it may + // be reloaded properly in the future. + BACKEND.unloadChunk(chunkListPath); + return true; } @@ -1248,39 +1252,30 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) { /** * Subscribes to chunk list updates from the update server and applies them. * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths + * @param {ChunkList} chunkList */ -function registerChunkList(chunkListPath, chunkPaths) { +function registerChunkList(chunkList) { globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS.push([ - chunkListPath, - handleApply.bind(null, chunkListPath), + chunkList.path, + handleApply.bind(null, chunkList.path), ]); // Adding chunks to chunk lists and vice versa. - const chunks = new Set(chunkPaths); - chunkListChunksMap.set(chunkListPath, chunks); + const chunks = new Set(chunkList.chunks); + chunkListChunksMap.set(chunkList.path, chunks); for (const chunkPath of chunks) { let chunkChunkLists = chunkChunkListsMap.get(chunkPath); if (!chunkChunkLists) { - chunkChunkLists = new Set([chunkListPath]); + chunkChunkLists = new Set([chunkList.path]); chunkChunkListsMap.set(chunkPath, chunkChunkLists); } else { - chunkChunkLists.add(chunkListPath); + chunkChunkLists.add(chunkList.path); } } -} -/** - * Registers a chunk list and marks it as a runtime chunk list. This is called - * by the runtime of evaluated chunks. - * - * @param {ChunkPath} chunkListPath - * @param {ChunkPath[]} chunkPaths - */ -function registerChunkListAndMarkAsRuntime(chunkListPath, chunkPaths) { - registerChunkList(chunkListPath, chunkPaths); - markChunkListAsRuntime(chunkListPath); + if (chunkList.source === "entry") { + markChunkListAsRuntime(chunkList.path); + } } /** @@ -1308,6 +1303,19 @@ async function registerChunk([chunkPath, chunkModules, runtimeParams]) { BACKEND.registerChunk(chunkPath, runtimeParams); } +globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; + +const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || []; +for (const chunkList of chunkListsToRegister) { + registerChunkList(chunkList); +} +globalThis.TURBOPACK_CHUNK_LISTS = { + push: (chunkList) => { + registerChunkList(chunkList); + }, +}; + globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS = globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS || []; /** @typedef {import('../types/backend').RuntimeBackend} RuntimeBackend */ @@ -1328,11 +1336,6 @@ let BACKEND; return; } - registerChunkListAndMarkAsRuntime(params.chunkListPath, [ - chunkPath, - ...params.otherChunks, - ]); - const chunksToWaitFor = []; for (const otherChunkPath of params.otherChunks) { if (otherChunkPath.endsWith(".css")) { diff --git a/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-baseurl/output/a587c_tests_snapshot_typescript_tsconfig-baseurl_input_index.ts_9122ac._.js b/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-baseurl/output/a587c_tests_snapshot_typescript_tsconfig-baseurl_input_index.ts_9122ac._.js new file mode 100644 index 0000000000000..e88de2463ce33 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-baseurl/output/a587c_tests_snapshot_typescript_tsconfig-baseurl_input_index.ts_9122ac._.js @@ -0,0 +1,11 @@ +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ + "output/a587c_tests_snapshot_typescript_tsconfig-baseurl_input_index.ts_9122ac._.js", + {}, +]); +(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({ + "path": "output/a587c_tests_snapshot_typescript_tsconfig-baseurl_input_index.ts_9122ac._.js", + "chunks": [ + "output/a587c_tests_snapshot_typescript_tsconfig-baseurl_input_index.ts_b53fce._.js" + ], + "source": "entry" +}); \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-baseurl/output/a587c_tests_snapshot_typescript_tsconfig-baseurl_input_index.ts_b53fce._.js b/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-baseurl/output/a587c_tests_snapshot_typescript_tsconfig-baseurl_input_index.ts_b53fce._.js index 5ac932a4cea19..3ef0d8fa84862 100644 --- a/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-baseurl/output/a587c_tests_snapshot_typescript_tsconfig-baseurl_input_index.ts_b53fce._.js +++ b/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-baseurl/output/a587c_tests_snapshot_typescript_tsconfig-baseurl_input_index.ts_b53fce._.js @@ -1,6 +1,6 @@ (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/a587c_tests_snapshot_typescript_tsconfig-baseurl_input_index.ts_b53fce._.js", { -"[project]/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-baseurl/input/prop.ts (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-baseurl/input/prop.ts (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { __turbopack_esm__({ "prop": ()=>prop @@ -8,7 +8,7 @@ __turbopack_esm__({ const prop = 1; })()), -"[project]/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-baseurl/input/index.ts (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, k: __turbopack_register_chunk_list__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { +"[project]/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-baseurl/input/index.ts (ecmascript)": (({ r: __turbopack_require__, x: __turbopack_external_require__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, c: __turbopack_cache__, l: __turbopack_load__, j: __turbopack_cjs__, g: global, __dirname }) => (() => { var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$typescript$2f$tsconfig$2d$baseurl$2f$input$2f$prop$2e$ts__$28$ecmascript$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-baseurl/input/prop.ts (ecmascript)"); var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$typescript$2f$tsconfig$2d$baseurl$2f$input$2f$prop$2e$ts__$28$ecmascript$29$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-baseurl/input/prop.ts (ecmascript)");