Unverified Commit a4b5ee75 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

[backport] Turbopack: don't strip async-module runtime from shared runtime chunks (#96653)

Backports [#96599](https://github.com/vercel/next.js/pull/96599)
("Turbopack: don't strip async-module runtime from shared runtime
chunks") to `next-16-3`.


<!-- NEXT_JS_LLM -->
parent a1047285
...@@ -1270,6 +1270,11 @@ impl Project { ...@@ -1270,6 +1270,11 @@ impl Project {
next_mode.runtime_type(), next_mode.runtime_type(),
) )
.source_maps(*self.next_config().server_source_maps().await?) .source_maps(*self.next_config().server_source_maps().await?)
// This context is shared by every node-side transform that needs to evaluate JS at
// build time (postcss configs, webpack loaders, next/font/google, ...). Each of those
// builds its own module graph but they all emit the same `[turbopack]_runtime.js`, so
// no single graph can decide which optional runtime features to drop.
.shared_runtime_chunk(true)
.build(), .build(),
); );
...@@ -1623,6 +1628,7 @@ impl Project { ...@@ -1623,6 +1628,7 @@ impl Project {
.next_config() .next_config()
.turbo_nested_async_chunking(self.next_mode(), true), .turbo_nested_async_chunking(self.next_mode(), true),
shared_runtime: self.next_config().turbo_shared_runtime(self.next_mode()), shared_runtime: self.next_config().turbo_shared_runtime(self.next_mode()),
per_page_module_graph: self.per_page_module_graph(),
debug_ids: self.next_config().turbopack_debug_ids(), debug_ids: self.next_config().turbopack_debug_ids(),
worker_asset_prefix: self.next_config().turbopack_worker_asset_prefix(), worker_asset_prefix: self.next_config().turbopack_worker_asset_prefix(),
should_use_absolute_url_references: self.next_config().inline_css(), should_use_absolute_url_references: self.next_config().inline_css(),
...@@ -1721,6 +1727,7 @@ impl Project { ...@@ -1721,6 +1727,7 @@ impl Project {
css_url_suffix, css_url_suffix,
hash_salt: self.next_config().output_hash_salt().to_resolved().await?, hash_salt: self.next_config().output_hash_salt().to_resolved().await?,
style_groups_algorithm: self.next_config().css_chunking().owned().await?, style_groups_algorithm: self.next_config().css_chunking().owned().await?,
per_page_module_graph: self.per_page_module_graph(),
}; };
Ok(if client_assets { Ok(if client_assets {
get_server_chunking_context_with_client_assets(options) get_server_chunking_context_with_client_assets(options)
......
...@@ -488,6 +488,7 @@ pub struct ClientChunkingContextOptions { ...@@ -488,6 +488,7 @@ pub struct ClientChunkingContextOptions {
pub scope_hoisting: Vc<bool>, pub scope_hoisting: Vc<bool>,
pub nested_async_chunking: Vc<bool>, pub nested_async_chunking: Vc<bool>,
pub shared_runtime: Vc<bool>, pub shared_runtime: Vc<bool>,
pub per_page_module_graph: Vc<bool>,
pub debug_ids: Vc<bool>, pub debug_ids: Vc<bool>,
pub worker_asset_prefix: Vc<Option<RcStr>>, pub worker_asset_prefix: Vc<Option<RcStr>>,
pub should_use_absolute_url_references: Vc<bool>, pub should_use_absolute_url_references: Vc<bool>,
...@@ -536,6 +537,7 @@ pub async fn get_client_chunking_context( ...@@ -536,6 +537,7 @@ pub async fn get_client_chunking_context(
scope_hoisting, scope_hoisting,
nested_async_chunking, nested_async_chunking,
shared_runtime, shared_runtime,
per_page_module_graph,
debug_ids, debug_ids,
worker_asset_prefix, worker_asset_prefix,
should_use_absolute_url_references, should_use_absolute_url_references,
...@@ -605,6 +607,10 @@ pub async fn get_client_chunking_context( ...@@ -605,6 +607,10 @@ pub async fn get_client_chunking_context(
builder = builder.chunk_loading_global(g.clone()); builder = builder.chunk_loading_global(g.clone());
} }
// Per-page graphs each see only one page, so none of them can decide what the shared runtime
// chunk may leave out.
builder = builder.shared_runtime_chunk(*per_page_module_graph.await?);
if next_mode.is_development() { if next_mode.is_development() {
builder = builder builder = builder
.hot_module_replacement() .hot_module_replacement()
......
...@@ -1053,6 +1053,7 @@ pub struct ServerChunkingContextOptions { ...@@ -1053,6 +1053,7 @@ pub struct ServerChunkingContextOptions {
pub css_url_suffix: Vc<Option<RcStr>>, pub css_url_suffix: Vc<Option<RcStr>>,
pub hash_salt: ResolvedVc<RcStr>, pub hash_salt: ResolvedVc<RcStr>,
pub style_groups_algorithm: StyleGroupsAlgorithm, pub style_groups_algorithm: StyleGroupsAlgorithm,
pub per_page_module_graph: Vc<bool>,
} }
/// Like `get_server_chunking_context` but all assets are emitted as client assets (so `/_next`) /// Like `get_server_chunking_context` but all assets are emitted as client assets (so `/_next`)
...@@ -1081,6 +1082,7 @@ pub async fn get_server_chunking_context_with_client_assets( ...@@ -1081,6 +1082,7 @@ pub async fn get_server_chunking_context_with_client_assets(
css_url_suffix, css_url_suffix,
hash_salt, hash_salt,
style_groups_algorithm, style_groups_algorithm,
per_page_module_graph,
} = options; } = options;
let css_url_suffix = css_url_suffix.to_resolved().await?; let css_url_suffix = css_url_suffix.to_resolved().await?;
...@@ -1127,6 +1129,9 @@ pub async fn get_server_chunking_context_with_client_assets( ...@@ -1127,6 +1129,9 @@ pub async fn get_server_chunking_context_with_client_assets(
.debug_ids(*debug_ids.await?) .debug_ids(*debug_ids.await?)
.hash_salt(hash_salt) .hash_salt(hash_salt)
.nested_async_availability(*nested_async_chunking.await?) .nested_async_availability(*nested_async_chunking.await?)
// Per-page graphs each see only one page, so none of them can decide what the shared runtime
// chunk may leave out.
.shared_runtime_chunk(*per_page_module_graph.await?)
.worker_forwarded_globals(worker_forwarded_globals()); .worker_forwarded_globals(worker_forwarded_globals());
builder = builder.source_map_source_type(if next_mode.is_development() { builder = builder.source_map_source_type(if next_mode.is_development() {
...@@ -1185,6 +1190,7 @@ pub async fn get_server_chunking_context( ...@@ -1185,6 +1190,7 @@ pub async fn get_server_chunking_context(
css_url_suffix, css_url_suffix,
hash_salt, hash_salt,
style_groups_algorithm, style_groups_algorithm,
per_page_module_graph,
} = options; } = options;
let css_url_suffix = css_url_suffix.to_resolved().await?; let css_url_suffix = css_url_suffix.to_resolved().await?;
let next_mode = mode.await?; let next_mode = mode.await?;
...@@ -1234,6 +1240,9 @@ pub async fn get_server_chunking_context( ...@@ -1234,6 +1240,9 @@ pub async fn get_server_chunking_context(
.debug_ids(*debug_ids.await?) .debug_ids(*debug_ids.await?)
.hash_salt(hash_salt) .hash_salt(hash_salt)
.nested_async_availability(*nested_async_chunking.await?) .nested_async_availability(*nested_async_chunking.await?)
// Per-page graphs each see only one page, so none of them can decide what the shared runtime
// chunk may leave out.
.shared_runtime_chunk(*per_page_module_graph.await?)
.worker_forwarded_globals(worker_forwarded_globals()); .worker_forwarded_globals(worker_forwarded_globals());
if next_mode.is_development() { if next_mode.is_development() {
......
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
// @ts-expect-error -- resolved through the turbopack loader rule in next.config.js
import { value } from './data.sync-txt'
export default function Page() {
return <p>hello world {value}</p>
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
turbopack: {
rules: {
'*.sync-txt': { loaders: ['./sync-loader.js'], as: '*.js' },
},
},
}
module.exports = nextConfig
/**
* A minimal, fully synchronous loader. Its module graph contains no async
* modules, so on its own it asks for a runtime without the top-level-await
* machinery.
*/
module.exports = function syncLoader(source) {
return source
}
import { nextTestSetup } from 'e2e-utils'
// The chunks Turbopack builds for build-time Node.js evaluation (webpack
// loaders, postcss configs, next/font/google, ...) all share a single
// `[turbopack]_runtime.js` at a fixed path, but each transform builds its own
// module graph. A graph without async modules must therefore not strip the
// async-module (top-level await) machinery from that runtime: chunks belonging
// to another graph may call `__turbopack_context__.a(...)`, and which variant
// wins the write depends on emission order.
//
// This fixture uses a single fully synchronous loader, so its graph contains no
// async modules. That alone used to be enough to emit a runtime without `.a`,
// which broke any build where a postcss config or another loader needed it.
// `.next/build/chunks` is Turbopack-only, so this is skipped for webpack.
;(process.env.IS_TURBOPACK_TEST ? describe : describe.skip)(
'turbopack-shared-runtime-async-module',
() => {
const { next } = nextTestSetup({
files: __dirname,
})
it('keeps the async-module runtime in the shared build-time runtime chunk', async () => {
const runtime = await next.readFile(
'.next/build/chunks/[turbopack]_runtime.js'
)
// `asyncModule` is what gets installed as `__turbopack_context__.a`.
expect(runtime).toContain('contextPrototype.a = asyncModule')
})
it('renders a page transformed by the loader', async () => {
const $ = await next.render$('/')
expect($('p').text()).toContain('from-loader')
})
}
)
...@@ -174,6 +174,18 @@ impl BrowserChunkingContextBuilder { ...@@ -174,6 +174,18 @@ impl BrowserChunkingContextBuilder {
self self
} }
/// Marks this context as being shared by multiple independent module graphs (e.g. per-page
/// graphs), each of which only sees part of what is written to `chunk_root_path`.
///
/// The runtime chunk is emitted to a fixed path, so every graph sharing this context writes
/// the same file. Optional runtime features must therefore not be decided from a single
/// graph: one graph would omit a helper that another graph's chunks call, and which variant
/// lands on disk depends on emission order.
pub fn shared_runtime_chunk(mut self, shared_runtime_chunk: bool) -> Self {
self.chunking_context.shared_runtime_chunk = shared_runtime_chunk;
self
}
pub fn should_use_absolute_url_references( pub fn should_use_absolute_url_references(
mut self, mut self,
should_use_absolute_url_references: bool, should_use_absolute_url_references: bool,
...@@ -350,6 +362,9 @@ pub struct BrowserChunkingContext { ...@@ -350,6 +362,9 @@ pub struct BrowserChunkingContext {
/// entrypoint's chunk group bootstrap params via /// entrypoint's chunk group bootstrap params via
/// `ChunkGroupResult.chunk_group_bootstrap_params`. /// `ChunkGroupResult.chunk_group_bootstrap_params`.
shared_runtime: bool, shared_runtime: bool,
/// Whether the runtime chunk is shared with other module graphs using this context.
/// See [`BrowserChunkingContextBuilder::shared_runtime_chunk`].
shared_runtime_chunk: bool,
/// The environment chunks will be evaluated in. /// The environment chunks will be evaluated in.
environment: ResolvedVc<Environment>, environment: ResolvedVc<Environment>,
/// The kind of runtime to include in the output. /// The kind of runtime to include in the output.
...@@ -429,6 +444,7 @@ impl BrowserChunkingContext { ...@@ -429,6 +444,7 @@ impl BrowserChunkingContext {
enable_dynamic_chunk_content_loading: false, enable_dynamic_chunk_content_loading: false,
debug_ids: false, debug_ids: false,
shared_runtime: false, shared_runtime: false,
shared_runtime_chunk: false,
environment, environment,
runtime_type, runtime_type,
minify_type: MinifyType::NoMinify, minify_type: MinifyType::NoMinify,
...@@ -472,17 +488,20 @@ impl BrowserChunkingContext { ...@@ -472,17 +488,20 @@ impl BrowserChunkingContext {
/// The shared browser runtime chunk for this chunking context. /// The shared browser runtime chunk for this chunking context.
/// ///
/// Returns the same asset every time: [`EcmascriptBrowserRuntimeChunk::new`] is a /// Returns the same asset every time: [`EcmascriptBrowserRuntimeChunk::new`] is a
/// `#[turbo_tasks::function]` memoized on `(chunking_context, has_async_modules)`. /// `#[turbo_tasks::function]` memoized on `(chunking_context, include_async_module_runtime)`.
pub(crate) async fn generate_runtime_chunk( pub(crate) async fn generate_runtime_chunk(
self: Vc<Self>, self: Vc<Self>,
module_graph: Vc<ModuleGraph>, module_graph: Vc<ModuleGraph>,
) -> Result<Vc<EcmascriptBrowserRuntimeChunk>> { ) -> Result<Vc<EcmascriptBrowserRuntimeChunk>> {
// Detect async modules from the whole-app graph in production. In development, the graph // Only omit the machinery when this graph sees every chunk that shares the runtime. With
// is per-page. To keep the shared `runtime.js` stable, always include the machinery. // per-page graphs it doesn't, and this asset is emitted to a fixed path, so a graph
let runtime_type = self.await?.runtime_type; // without async modules would otherwise strip a helper another page's chunks call.
let has_async_modules = matches!(runtime_type, RuntimeType::Development) let include_async_module_runtime =
|| !module_graph.async_module_info().await?.is_empty(); self.await?.shared_runtime_chunk || !module_graph.async_module_info().await?.is_empty();
Ok(EcmascriptBrowserRuntimeChunk::new(self, has_async_modules)) Ok(EcmascriptBrowserRuntimeChunk::new(
self,
include_async_module_runtime,
))
} }
fn generate_chunk_list_register_chunk( fn generate_chunk_list_register_chunk(
...@@ -552,6 +571,14 @@ impl BrowserChunkingContext { ...@@ -552,6 +571,14 @@ impl BrowserChunkingContext {
Vc::cell(self.shared_runtime) Vc::cell(self.shared_runtime)
} }
/// Whether the runtime chunk is shared with other module graphs using this context, meaning no
/// single graph may decide which optional runtime features to omit.
/// See [`BrowserChunkingContextBuilder::shared_runtime_chunk`].
#[turbo_tasks::function]
pub fn shared_runtime_chunk(&self) -> Vc<bool> {
Vc::cell(self.shared_runtime_chunk)
}
/// Returns the asset base path. /// Returns the asset base path.
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn chunk_base_path(&self) -> Vc<Option<RcStr>> { pub fn chunk_base_path(&self) -> Vc<Option<RcStr>> {
......
...@@ -191,7 +191,7 @@ impl EcmascriptBrowserEvaluateChunk { ...@@ -191,7 +191,7 @@ impl EcmascriptBrowserEvaluateChunk {
let runtime_type = *this.chunking_context.runtime_type().await?; let runtime_type = *this.chunking_context.runtime_type().await?;
// Detect async modules from the whole-app graph in production. In development the graph // Detect async modules from the whole-app graph in production. In development the graph
// is per-page, so always include the machinery. // is per-page, so always include the machinery.
let has_async_modules = if matches!(runtime_type, RuntimeType::Production) { let include_async_module_runtime = if matches!(runtime_type, RuntimeType::Production) {
!this.module_graph.async_module_info().await?.is_empty() !this.module_graph.async_module_info().await?.is_empty()
} else { } else {
true true
...@@ -208,7 +208,7 @@ impl EcmascriptBrowserEvaluateChunk { ...@@ -208,7 +208,7 @@ impl EcmascriptBrowserEvaluateChunk {
this.chunking_context.chunk_loading_global(), this.chunking_context.chunk_loading_global(),
this.chunking_context.cross_origin(), this.chunking_context.cross_origin(),
this.chunking_context.chunk_load_retry(), this.chunking_context.chunk_load_retry(),
has_async_modules, include_async_module_runtime,
this.chunking_context.chunk_loading(), this.chunking_context.chunk_loading(),
*this.chunking_context.generate_component_chunks().await?, *this.chunking_context.generate_component_chunks().await?,
); );
......
...@@ -26,7 +26,7 @@ use crate::BrowserChunkingContext; ...@@ -26,7 +26,7 @@ use crate::BrowserChunkingContext;
#[value_to_string("Ecmascript Browser Runtime Chunk")] #[value_to_string("Ecmascript Browser Runtime Chunk")]
pub(crate) struct EcmascriptBrowserRuntimeChunk { pub(crate) struct EcmascriptBrowserRuntimeChunk {
chunking_context: ResolvedVc<BrowserChunkingContext>, chunking_context: ResolvedVc<BrowserChunkingContext>,
has_async_modules: bool, include_async_module_runtime: bool,
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -34,11 +34,11 @@ impl EcmascriptBrowserRuntimeChunk { ...@@ -34,11 +34,11 @@ impl EcmascriptBrowserRuntimeChunk {
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn new( pub fn new(
chunking_context: ResolvedVc<BrowserChunkingContext>, chunking_context: ResolvedVc<BrowserChunkingContext>,
has_async_modules: bool, include_async_module_runtime: bool,
) -> Vc<Self> { ) -> Vc<Self> {
EcmascriptBrowserRuntimeChunk { EcmascriptBrowserRuntimeChunk {
chunking_context, chunking_context,
has_async_modules, include_async_module_runtime,
} }
.cell() .cell()
} }
...@@ -70,7 +70,7 @@ impl EcmascriptBrowserRuntimeChunk { ...@@ -70,7 +70,7 @@ impl EcmascriptBrowserRuntimeChunk {
chunking_context.chunk_loading_global(), chunking_context.chunk_loading_global(),
chunking_context.cross_origin(), chunking_context.cross_origin(),
chunking_context.chunk_load_retry(), chunking_context.chunk_load_retry(),
this.has_async_modules, this.include_async_module_runtime,
chunking_context.chunk_loading(), chunking_context.chunk_loading(),
*chunking_context.generate_component_chunks().await?, *chunking_context.generate_component_chunks().await?,
); );
......
...@@ -245,6 +245,9 @@ async fn build_internal( ...@@ -245,6 +245,9 @@ async fn build_internal(
.await?, .await?,
runtime_type, runtime_type,
) )
// Shared by every build-time JS evaluation, each with its own module graph but all
// emitting the same runtime chunk.
.shared_runtime_chunk(true)
.build(), .build(),
), ),
load_env(root_path.clone()), load_env(root_path.clone()),
......
...@@ -295,6 +295,9 @@ async fn source( ...@@ -295,6 +295,9 @@ async fn source(
node_build_environment().to_resolved().await?, node_build_environment().to_resolved().await?,
RuntimeType::Development, RuntimeType::Development,
) )
// Shared by every build-time JS evaluation, each with its own module graph but all emitting
// the same runtime chunk.
.shared_runtime_chunk(true)
.build(); .build();
let node_backend = child_process_backend(); let node_backend = child_process_backend();
......
...@@ -26,7 +26,7 @@ pub async fn get_browser_runtime_code( ...@@ -26,7 +26,7 @@ pub async fn get_browser_runtime_code(
chunk_loading_global: Vc<RcStr>, chunk_loading_global: Vc<RcStr>,
cross_origin: Vc<CrossOrigin>, cross_origin: Vc<CrossOrigin>,
chunk_load_retry: Vc<ChunkLoadRetry>, chunk_load_retry: Vc<ChunkLoadRetry>,
has_async_modules: bool, include_async_module_runtime: bool,
chunk_loading: Vc<ChunkLoading>, chunk_loading: Vc<ChunkLoading>,
support_component_chunks: bool, support_component_chunks: bool,
) -> Result<Vc<Code>> { ) -> Result<Vc<Code>> {
...@@ -192,8 +192,7 @@ pub async fn get_browser_runtime_code( ...@@ -192,8 +192,7 @@ pub async fn get_browser_runtime_code(
)?; )?;
code.push_code(&*shared_runtime_utils_code.await?); code.push_code(&*shared_runtime_utils_code.await?);
// Only include the async-module (top-level await) machinery when the app uses it. if include_async_module_runtime {
if has_async_modules {
code.push_code( code.push_code(
&*embed_static_code( &*embed_static_code(
asset_context, asset_context,
......
...@@ -13,7 +13,7 @@ use crate::{RuntimeType, embed_js::embed_static_code}; ...@@ -13,7 +13,7 @@ use crate::{RuntimeType, embed_js::embed_static_code};
pub async fn get_nodejs_runtime_code( pub async fn get_nodejs_runtime_code(
asset_context: ResolvedVc<Box<dyn AssetContext>>, asset_context: ResolvedVc<Box<dyn AssetContext>>,
runtime_type: RuntimeType, runtime_type: RuntimeType,
has_async_modules: bool, include_async_module_runtime: bool,
generate_source_map: bool, generate_source_map: bool,
) -> Result<Vc<Code>> { ) -> Result<Vc<Code>> {
let asset_context = *asset_context; let asset_context = *asset_context;
...@@ -42,8 +42,7 @@ pub async fn get_nodejs_runtime_code( ...@@ -42,8 +42,7 @@ pub async fn get_nodejs_runtime_code(
let mut code = CodeBuilder::default(); let mut code = CodeBuilder::default();
code.push_code(&*shared_runtime_utils_code.await?); code.push_code(&*shared_runtime_utils_code.await?);
// Only include the async-module (top-level await) machinery when the app uses it. if include_async_module_runtime {
if has_async_modules {
code.push_code( code.push_code(
&*embed_static_code( &*embed_static_code(
asset_context, asset_context,
......
...@@ -166,6 +166,18 @@ impl NodeJsChunkingContextBuilder { ...@@ -166,6 +166,18 @@ impl NodeJsChunkingContextBuilder {
self self
} }
/// Marks this context as being shared by multiple independent module graphs, each of which
/// only sees part of what is written to `chunk_root_path`.
///
/// The runtime chunk is emitted to a fixed path (`[turbopack]_runtime.js`), so every graph
/// sharing this context writes the same file. Optional runtime features must therefore not be
/// decided from a single graph: one graph would omit a helper that another graph's chunks
/// call, and which variant lands on disk depends on emission order.
pub fn shared_runtime_chunk(mut self, shared_runtime_chunk: bool) -> Self {
self.chunking_context.shared_runtime_chunk = shared_runtime_chunk;
self
}
/// Builds the chunking context. /// Builds the chunking context.
pub fn build(self) -> Vc<NodeJsChunkingContext> { pub fn build(self) -> Vc<NodeJsChunkingContext> {
NodeJsChunkingContext::cell(self.chunking_context) NodeJsChunkingContext::cell(self.chunking_context)
...@@ -238,6 +250,9 @@ pub struct NodeJsChunkingContext { ...@@ -238,6 +250,9 @@ pub struct NodeJsChunkingContext {
asset_content_hashing: ContentHashing, asset_content_hashing: ContentHashing,
/// Salt mixed into chunk and asset content hashes. Empty string means no salt. /// Salt mixed into chunk and asset content hashes. Empty string means no salt.
hash_salt: ResolvedVc<RcStr>, hash_salt: ResolvedVc<RcStr>,
/// Whether the runtime chunk is shared with other module graphs using this context.
/// See [`NodeJsChunkingContextBuilder::shared_runtime_chunk`].
shared_runtime_chunk: bool,
} }
impl NodeJsChunkingContext { impl NodeJsChunkingContext {
...@@ -283,6 +298,7 @@ impl NodeJsChunkingContext { ...@@ -283,6 +298,7 @@ impl NodeJsChunkingContext {
worker_forwarded_globals: vec![], worker_forwarded_globals: vec![],
asset_content_hashing: ContentHashing::Direct { length: 13 }, asset_content_hashing: ContentHashing::Direct { length: 13 },
hash_salt: ResolvedVc::cell(RcStr::default()), hash_salt: ResolvedVc::cell(RcStr::default()),
shared_runtime_chunk: false,
}, },
} }
} }
...@@ -339,6 +355,14 @@ impl NodeJsChunkingContext { ...@@ -339,6 +355,14 @@ impl NodeJsChunkingContext {
*self, path, chunks, *self, path, chunks,
))) )))
} }
/// Whether the runtime chunk is shared with other module graphs using this context, meaning no
/// single graph may decide which optional runtime features to omit.
/// See [`NodeJsChunkingContextBuilder::shared_runtime_chunk`].
#[turbo_tasks::function]
pub fn shared_runtime_chunk(&self) -> Vc<bool> {
Vc::cell(self.shared_runtime_chunk)
}
} }
impl NodeJsChunkingContext { impl NodeJsChunkingContext {
......
...@@ -17,7 +17,6 @@ use turbopack_core::{ ...@@ -17,7 +17,6 @@ use turbopack_core::{
version::VersionedContent, version::VersionedContent,
}; };
use turbopack_ecmascript::{chunk::EcmascriptChunkPlaceable, utils::StringifyJs}; use turbopack_ecmascript::{chunk::EcmascriptChunkPlaceable, utils::StringifyJs};
use turbopack_ecmascript_runtime::RuntimeType;
use super::{ use super::{
chunk_list_content::EcmascriptBuildNodeChunkListContent, chunk_list_content::EcmascriptBuildNodeChunkListContent,
...@@ -155,19 +154,16 @@ impl EcmascriptBuildNodeEntryChunk { ...@@ -155,19 +154,16 @@ impl EcmascriptBuildNodeEntryChunk {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn runtime_chunk(&self) -> Result<Vc<EcmascriptBuildNodeRuntimeChunk>> { async fn runtime_chunk(&self) -> Result<Vc<EcmascriptBuildNodeRuntimeChunk>> {
// Detect async modules from the whole-app graph in production. In development, the graph // Only omit the machinery when this graph sees every chunk that shares the runtime. When
// is per-page. To keep the shared `runtime.js` stable, always include the machinery. // the runtime chunk is shared (per-page graphs, or contexts like the node execution
let has_async_modules = if matches!( // context that several independent per-transform graphs write to), a graph without async
*self.chunking_context.runtime_type().await?, // modules would strip a helper that another graph's chunks call, and which variant lands
RuntimeType::Production // on disk depends on emission order.
) { let include_async_module_runtime = *self.chunking_context.shared_runtime_chunk().await?
!self.module_graph.async_module_info().await?.is_empty() || !self.module_graph.async_module_info().await?.is_empty();
} else {
true
};
Ok(EcmascriptBuildNodeRuntimeChunk::new( Ok(EcmascriptBuildNodeRuntimeChunk::new(
*self.chunking_context, *self.chunking_context,
has_async_modules, include_async_module_runtime,
)) ))
} }
......
...@@ -24,7 +24,7 @@ use crate::NodeJsChunkingContext; ...@@ -24,7 +24,7 @@ use crate::NodeJsChunkingContext;
#[value_to_string("Ecmascript Build Node Runtime Chunk")] #[value_to_string("Ecmascript Build Node Runtime Chunk")]
pub(crate) struct EcmascriptBuildNodeRuntimeChunk { pub(crate) struct EcmascriptBuildNodeRuntimeChunk {
chunking_context: ResolvedVc<NodeJsChunkingContext>, chunking_context: ResolvedVc<NodeJsChunkingContext>,
has_async_modules: bool, include_async_module_runtime: bool,
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -33,11 +33,11 @@ impl EcmascriptBuildNodeRuntimeChunk { ...@@ -33,11 +33,11 @@ impl EcmascriptBuildNodeRuntimeChunk {
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn new( pub fn new(
chunking_context: ResolvedVc<NodeJsChunkingContext>, chunking_context: ResolvedVc<NodeJsChunkingContext>,
has_async_modules: bool, include_async_module_runtime: bool,
) -> Vc<Self> { ) -> Vc<Self> {
EcmascriptBuildNodeRuntimeChunk { EcmascriptBuildNodeRuntimeChunk {
chunking_context, chunking_context,
has_async_modules, include_async_module_runtime,
} }
.cell() .cell()
} }
...@@ -110,7 +110,7 @@ impl EcmascriptBuildNodeRuntimeChunk { ...@@ -110,7 +110,7 @@ impl EcmascriptBuildNodeRuntimeChunk {
let runtime_code = turbopack_ecmascript_runtime::get_nodejs_runtime_code( let runtime_code = turbopack_ecmascript_runtime::get_nodejs_runtime_code(
asset_context, asset_context,
RuntimeType::Development, RuntimeType::Development,
this.has_async_modules, this.include_async_module_runtime,
generate_source_map, generate_source_map,
); );
code.push_code(&*runtime_code.await?); code.push_code(&*runtime_code.await?);
...@@ -119,7 +119,7 @@ impl EcmascriptBuildNodeRuntimeChunk { ...@@ -119,7 +119,7 @@ impl EcmascriptBuildNodeRuntimeChunk {
let runtime_code = turbopack_ecmascript_runtime::get_nodejs_runtime_code( let runtime_code = turbopack_ecmascript_runtime::get_nodejs_runtime_code(
asset_context, asset_context,
RuntimeType::Production, RuntimeType::Production,
this.has_async_modules, this.include_async_module_runtime,
generate_source_map, generate_source_map,
); );
code.push_code(&*runtime_code.await?); code.push_code(&*runtime_code.await?);
......
...@@ -526,138 +526,6 @@ function applyModuleFactoryName(factory) { ...@@ -526,138 +526,6 @@ function applyModuleFactoryName(factory) {
value: 'module evaluation' value: 'module evaluation'
}); });
} }
/// <reference path="./runtime-types.d.ts" />
/// <reference path="./runtime-utils.ts" />
/**
* Top-level-await / async-module machinery. This is only included in the runtime
* when the module graph actually contains an async module (a module with
* top-level await, or one that transitively depends on one). When no async
* module is present, the chunk items never reference `__turbopack_context__.a`,
* so this whole file can be omitted.
*
* everything below is adapted from webpack
* https://github.com/webpack/webpack/blob/6be4065ade1e252c1d8dcba4af0f43e32af1bdc1/lib/runtime/AsyncModuleRuntimeModule.js#L13
*/ const turbopackQueues = Symbol('turbopack queues');
const turbopackExports = Symbol('turbopack exports');
const turbopackError = Symbol('turbopack error');
function isPromise(maybePromise) {
return maybePromise != null && typeof maybePromise === 'object' && 'then' in maybePromise && typeof maybePromise.then === 'function';
}
function isAsyncModuleExt(obj) {
return turbopackQueues in obj;
}
function createPromise() {
let resolve;
let reject;
const promise = new Promise((res, rej)=>{
reject = rej;
resolve = res;
});
return {
promise,
resolve: resolve,
reject: reject
};
}
function resolveQueue(queue) {
if (queue && queue.status !== 1) {
queue.status = 1;
queue.forEach((fn)=>fn.queueCount--);
queue.forEach((fn)=>fn.queueCount-- ? fn.queueCount++ : fn());
}
}
function wrapDeps(deps) {
return deps.map((dep)=>{
if (dep !== null && typeof dep === 'object') {
if (isAsyncModuleExt(dep)) return dep;
if (isPromise(dep)) {
const queue = Object.assign([], {
status: 0
});
const obj = {
[turbopackExports]: {},
[turbopackQueues]: (fn)=>fn(queue)
};
dep.then((res)=>{
obj[turbopackExports] = res;
resolveQueue(queue);
}, (err)=>{
obj[turbopackError] = err;
resolveQueue(queue);
});
return obj;
}
}
return {
[turbopackExports]: dep,
[turbopackQueues]: ()=>{}
};
});
}
function asyncModule(body, hasAwait) {
const module = this.m;
const queue = hasAwait ? Object.assign([], {
status: -1
}) : undefined;
const depQueues = new Set();
const { resolve, reject, promise: rawPromise } = createPromise();
const promise = Object.assign(rawPromise, {
[turbopackExports]: module.exports,
[turbopackQueues]: (fn)=>{
queue && fn(queue);
depQueues.forEach(fn);
promise['catch'](()=>{});
}
});
const attributes = {
get () {
return promise;
},
set (v) {
// Calling `esmExport` leads to this.
if (v !== promise) {
promise[turbopackExports] = v;
}
}
};
Object.defineProperty(module, 'exports', attributes);
Object.defineProperty(module, 'namespaceObject', attributes);
function handleAsyncDependencies(deps) {
const currentDeps = wrapDeps(deps);
const getResult = ()=>currentDeps.map((d)=>{
if (d[turbopackError]) throw d[turbopackError];
return d[turbopackExports];
});
const { promise, resolve } = createPromise();
const fn = Object.assign(()=>resolve(getResult), {
queueCount: 0
});
function fnQueue(q) {
if (q !== queue && !depQueues.has(q)) {
depQueues.add(q);
if (q && q.status === 0) {
fn.queueCount++;
q.push(fn);
}
}
}
currentDeps.map((dep)=>dep[turbopackQueues](fnQueue));
return fn.queueCount ? promise : getResult();
}
function asyncResult(err) {
if (err) {
reject(promise[turbopackError] = err);
} else {
resolve(promise[turbopackExports]);
}
resolveQueue(queue);
}
body(handleAsyncDependencies, asyncResult);
if (queue && queue.status === -1) {
queue.status = 0;
}
}
contextPrototype.a = asyncModule;
/// <reference path="../shared/runtime/runtime-utils.ts" /> /// <reference path="../shared/runtime/runtime-utils.ts" />
/// A 'base' utilities to support runtime can have externals. /// A 'base' utilities to support runtime can have externals.
/// Currently this is for node.js / edge runtime both. /// Currently this is for node.js / edge runtime both.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment