Unverified Commit db34127f authored by Joseph's avatar Joseph Committed by GitHub

Merge branch 'canary' into fix/types-and-js-docs

parents 37e472a4 ff767a72
...@@ -18,7 +18,7 @@ Alternatively, any directives syntax supported by [`tracing_subscriber::filter:: ...@@ -18,7 +18,7 @@ Alternatively, any directives syntax supported by [`tracing_subscriber::filter::
> [!WARNING] > [!WARNING]
> A normal Next.js canary/stable release only includes the info level tracing. This is the tracing level intended for user-facing tracing. > A normal Next.js canary/stable release only includes the info level tracing. This is the tracing level intended for user-facing tracing.
> >
> For the more detailed tracing a custom Next.js build is required. See [Developing] for more information how to create one. > For the more detailed tracing a custom Next.js build is required. See [Developing] for more information on how to create one.
With this environment variable, Next.js will write a `.next-profiles/trace-turbopack` file with the tracing information in a binary format. With this environment variable, Next.js will write a `.next-profiles/trace-turbopack` file with the tracing information in a binary format.
...@@ -54,13 +54,13 @@ The trace viewer allows to switch between multiple different visualization modes ...@@ -54,13 +54,13 @@ The trace viewer allows to switch between multiple different visualization modes
- **... by value:** Spans are sorted and spans with the largest value are shown first. - **... by value:** Spans are sorted and spans with the largest value are shown first.
- **Bottom-up view:** Instead of showing the total value, the self value is shown. - **Bottom-up view:** Instead of showing the total value, the self value is shown.
And there different value modes: And there are different value modes:
- **Duration:** The CPU time of each span is shown. - **Duration:** The CPU time of each span is shown.
- **Allocated Memory:** How much memory was allocated during the span. - **Allocated Memory:** How much memory was allocated during the span.
- **Allocations:** How many allocations were made during the span. - **Allocations:** How many allocations were made during the span.
- **Deallocated Memory:** How much memory was deallocated during the span. - **Deallocated Memory:** How much memory was deallocated during the span.
- **Persistently allocated Memory:** How much memory was allocated but not deallocated during the span. It survives the span. - **Persistently Allocated Memory:** How much memory was allocated but not deallocated during the span. It survives the span.
[turbo-trace-viewer]: https://turbo-trace-viewer.vercel.app/ [turbo-trace-viewer]: https://turbo-trace-viewer.vercel.app/
[youtube-tutorial]: https://www.youtube.com/watch?v=PGO2szAye7A [youtube-tutorial]: https://www.youtube.com/watch?v=PGO2szAye7A
use std::{collections::BTreeMap, io::Write}; use std::{borrow::Cow, collections::BTreeMap, io::Write};
use anyhow::{Context, Result, bail}; use anyhow::{Context, Result, bail};
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
...@@ -21,11 +21,12 @@ use swc_core::{ ...@@ -21,11 +21,12 @@ use swc_core::{
}; };
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ use turbo_tasks::{
FxIndexMap, NonLocalValue, OperationVc, ResolvedVc, TryFlatJoinIterExt, Vc, trace::TraceRawVcs, FxIndexMap, NonLocalValue, OperationVc, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, Vc,
trace::TraceRawVcs,
}; };
use turbo_tasks_fs::{self, File, FileContent, FileSystemPath, rope::RopeBuilder}; use turbo_tasks_fs::{self, File, FileContent, FileSystemPath, rope::RopeBuilder};
use turbopack_core::{ use turbopack_core::{
asset::AssetContent, asset::{Asset, AssetContent},
chunk::{ chunk::{
ChunkItem, ChunkItemExt, ChunkableModule, ChunkingContext, EvaluatableAsset, ModuleId, ChunkItem, ChunkItemExt, ChunkableModule, ChunkingContext, EvaluatableAsset, ModuleId,
}, },
...@@ -34,10 +35,9 @@ use turbopack_core::{ ...@@ -34,10 +35,9 @@ use turbopack_core::{
ident::AssetIdent, ident::AssetIdent,
module::Module, module::Module,
module_graph::{ModuleGraph, ModuleGraphLayer, async_module_info::AsyncModulesInfo}, module_graph::{ModuleGraph, ModuleGraphLayer, async_module_info::AsyncModulesInfo},
output::OutputAsset, output::{OutputAsset, OutputAssetsReference},
reference_type::{EcmaScriptModulesReferenceSubType, ReferenceType}, reference_type::{EcmaScriptModulesReferenceSubType, ReferenceType},
resolve::ModulePart, resolve::ModulePart,
virtual_output::VirtualOutputAsset,
virtual_source::VirtualSource, virtual_source::VirtualSource,
}; };
use turbopack_ecmascript::{ use turbopack_ecmascript::{
...@@ -45,9 +45,6 @@ use turbopack_ecmascript::{ ...@@ -45,9 +45,6 @@ use turbopack_ecmascript::{
tree_shake::part::module::EcmascriptModulePartAsset, tree_shake::part::module::EcmascriptModulePartAsset,
}; };
/// Metadata for a server action: (layer, exported_name, filename)
type ActionMetadata = (ActionLayer, String, String);
#[turbo_tasks::value] #[turbo_tasks::value]
pub(crate) struct ServerActionsManifest { pub(crate) struct ServerActionsManifest {
pub loader: ResolvedVc<Box<dyn EvaluatableAsset>>, pub loader: ResolvedVc<Box<dyn EvaluatableAsset>>,
...@@ -79,15 +76,18 @@ pub(crate) async fn create_server_actions_manifest( ...@@ -79,15 +76,18 @@ pub(crate) async fn create_server_actions_manifest(
.context("loader module must be evaluatable")?; .context("loader module must be evaluatable")?;
let chunk_item = loader.as_chunk_item(module_graph, chunking_context); let chunk_item = loader.as_chunk_item(module_graph, chunking_context);
let manifest = build_manifest( let manifest = ResolvedVc::upcast(
node_root, ServerActionManifestAsset::new(
page_name, node_root,
runtime, page_name,
actions, runtime,
chunk_item, actions,
module_graph.async_module_info(), chunk_item,
) module_graph.async_module_info(),
.await?; )
.to_resolved()
.await?,
);
Ok(ServerActionsManifest { Ok(ServerActionsManifest {
loader: evaluable, loader: evaluable,
manifest, manifest,
...@@ -155,79 +155,133 @@ pub(crate) async fn build_server_actions_loader( ...@@ -155,79 +155,133 @@ pub(crate) async fn build_server_actions_loader(
/// Builds a manifest containing every action's hashed id, with an internal /// Builds a manifest containing every action's hashed id, with an internal
/// module id which exports a function using that hashed name. /// module id which exports a function using that hashed name.
async fn build_manifest( #[turbo_tasks::value]
struct ServerActionManifestAsset {
node_root: FileSystemPath, node_root: FileSystemPath,
page_name: RcStr, page_name: RcStr,
runtime: NextRuntime, runtime: NextRuntime,
actions: Vc<AllActions>, actions: ResolvedVc<AllActions>,
chunk_item: Vc<Box<dyn ChunkItem>>, chunk_item: ResolvedVc<Box<dyn ChunkItem>>,
async_module_info: Vc<AsyncModulesInfo>, async_module_info: ResolvedVc<AsyncModulesInfo>,
) -> Result<ResolvedVc<Box<dyn OutputAsset>>> { }
let manifest_path_prefix = &page_name;
let manifest_path = node_root.join(&format!(
"server/app{manifest_path_prefix}/server-reference-manifest.json",
))?;
let mut manifest = ServerReferenceManifest {
..Default::default()
};
let key = format!("app{page_name}"); #[turbo_tasks::value_impl]
impl ServerActionManifestAsset {
#[turbo_tasks::function]
pub fn new(
node_root: FileSystemPath,
page_name: RcStr,
runtime: NextRuntime,
actions: ResolvedVc<AllActions>,
chunk_item: ResolvedVc<Box<dyn ChunkItem>>,
async_module_info: ResolvedVc<AsyncModulesInfo>,
) -> Vc<Self> {
Self {
node_root,
page_name,
runtime,
actions,
chunk_item,
async_module_info,
}
.cell()
}
}
let actions_value = actions.await?; #[turbo_tasks::value_impl]
let loader_id = chunk_item.id().await?; impl OutputAsset for ServerActionManifestAsset {
let loader_id = match &loader_id { #[turbo_tasks::function]
ModuleId::Number(id) => ActionManifestModuleId::Number(*id), fn path(&self) -> Result<Vc<FileSystemPath>> {
ModuleId::String(id) => ActionManifestModuleId::String(id), let manifest_path_prefix = &self.page_name;
}; let manifest_path = self.node_root.join(&format!(
let mapping = match runtime { "server/app{manifest_path_prefix}/server-reference-manifest.json",
NextRuntime::Edge => &mut manifest.edge, ))?;
NextRuntime::NodeJs => &mut manifest.node, Ok(manifest_path.cell())
}; }
}
#[turbo_tasks::value_impl]
impl OutputAssetsReference for ServerActionManifestAsset {}
#[turbo_tasks::value_impl]
impl Asset for ServerActionManifestAsset {
#[turbo_tasks::function]
async fn content(&self) -> Result<Vc<AssetContent>> {
let mut manifest: ServerReferenceManifest = Default::default();
let key = format!("app{}", self.page_name);
// Collect all the action metadata including filenames and location let actions_value = self.actions.await?;
let mut action_metadata: Vec<(String, ActionMetadata)> = Vec::new(); let loader_id = self.chunk_item.id().await?;
for (hash_id, (layer, meta, module)) in actions_value.iter() { let loader_id = match &loader_id {
// Use source_path from the action comment if available (contains original .ts/.tsx path), ModuleId::Number(id) => ActionManifestModuleId::Number(*id),
// otherwise fall back to module.ident().path() (may be compiled .js path) ModuleId::String(id) => ActionManifestModuleId::String(id),
let filename = if !meta.source_path.is_empty() { };
meta.source_path.clone() let mapping = match self.runtime {
} else { NextRuntime::Edge => &mut manifest.edge,
module.ident().await?.path.to_string() NextRuntime::NodeJs => &mut manifest.node,
}; };
action_metadata.push((hash_id.clone(), (*layer, meta.name.clone(), filename))); struct ActionMetadata<'a> {
} exported_name: &'a str,
filename: Cow<'a, str>,
}
// Now create the manifest entries let action_metadata: Vec<(&str, ActionMetadata<'_>)> = actions_value
for (hash_id, (_layer, name, filename)) in &action_metadata { .iter()
let entry = mapping.entry(hash_id.as_str()).or_default(); .map(async |(hash_id, (_layer, meta, module))| {
entry.workers.insert( // Use source_path from the action comment if available (contains original .ts/.tsx
&key, // path), otherwise fall back to module.ident().path() (may be compiled .js
ActionManifestWorkerEntry { // path)
module_id: loader_id.clone(), let filename = if !meta.source_path.is_empty() {
is_async: async_module_info Cow::Borrowed(&*meta.source_path)
.is_async(chunk_item.module().to_resolved().await?) } else {
.await?, Cow::Owned(module.ident().await?.path.to_string())
exported_name: name.as_str(), };
filename: filename.as_str(),
Ok((
&**hash_id,
ActionMetadata {
exported_name: &meta.name,
filename,
},
))
})
.try_join()
.await?;
// Now create the manifest entries
for (
hash_id,
ActionMetadata {
exported_name,
filename,
}, },
); ) in &action_metadata
{
let entry = mapping.entry(hash_id).or_default();
entry.workers.insert(
&key,
ActionManifestWorkerEntry {
module_id: loader_id.clone(),
is_async: self
.async_module_info
.is_async(self.chunk_item.module().to_resolved().await?)
.await?,
exported_name,
filename: filename.as_ref(),
},
);
// Hoist the filename and exported_name to the entry level // Hoist the filename and exported_name to the entry level
entry.exported_name = name.as_str(); entry.exported_name = exported_name;
entry.filename = filename.as_str(); entry.filename = filename.as_ref();
} }
Ok(ResolvedVc::upcast( Ok(AssetContent::file(
VirtualOutputAsset::new( FileContent::Content(File::from(serde_json::to_string_pretty(&manifest)?)).cell(),
manifest_path, ))
AssetContent::file( }
FileContent::Content(File::from(serde_json::to_string_pretty(&manifest)?)).cell(),
),
)
.to_resolved()
.await?,
))
} }
/// The ActionBrowser layer's module is in the Client context, and we need to /// The ActionBrowser layer's module is in the Client context, and we need to
......
...@@ -25,7 +25,7 @@ The `run` command supports 4 flags: ...@@ -25,7 +25,7 @@ The `run` command supports 4 flags:
- `strategy` can be one of sequential, concurrent, or parallel. defines how - `strategy` can be one of sequential, concurrent, or parallel. defines how
work is structured work is structured
- `factor` defined how many pages should be built at once. defaults to num_cpus - `factor` defines how many pages should be built at once. defaults to num_cpus
- `limit` defines the highest number of pages to build. the pages are - `limit` defines the highest number of pages to build. the pages are
shuffled deterministically. defaults to 1 page shuffled deterministically. defaults to 1 page
- `pages` a comma separated list of routes to run. queues that precise set in - `pages` a comma separated list of routes to run. queues that precise set in
......
...@@ -34,7 +34,7 @@ The docs are written in [MDX](https://mdxjs.com/), a markdown format that suppor ...@@ -34,7 +34,7 @@ The docs are written in [MDX](https://mdxjs.com/), a markdown format that suppor
VSCode has a built-in markdown previewer that you can use to see your edits locally. To enable the previewer for MDX files, you'll need to add a configuration option to your user settings. VSCode has a built-in markdown previewer that you can use to see your edits locally. To enable the previewer for MDX files, you'll need to add a configuration option to your user settings.
Open the command palette (`⌘ + ⇧ + P` on Mac or `Ctrl + Shift + P` on Windows) and search from `Preferences: Open User Settings (JSON)`. Open the command palette (`⌘ + ⇧ + P` on Mac or `Ctrl + Shift + P` on Windows) and search for `Preferences: Open User Settings (JSON)`.
Then, add the following line to your `settings.json` file: Then, add the following line to your `settings.json` file:
...@@ -396,7 +396,7 @@ While we don't have a strict template for pages, there are page sections you'll ...@@ -396,7 +396,7 @@ While we don't have a strict template for pages, there are page sections you'll
- **Overview:** The first paragraph of a page should tell the user what the feature is and what it's used for. Followed by a minimum working example or its API reference. - **Overview:** The first paragraph of a page should tell the user what the feature is and what it's used for. Followed by a minimum working example or its API reference.
- **Convention:** If the feature has a convention, it should be explained here. - **Convention:** If the feature has a convention, it should be explained here.
- **Examples**: Show how the feature can be used with different use cases. - **Examples**: Show how the feature can be used with different use cases.
- **API Tables**: API Pages should have an overview table at the of the page with jump-to-section links (when possible). - **API Tables**: API Pages should have an overview table at the top of the page with jump-to-section links (when possible).
- **Next Steps (Related Links)**: Add links to related pages to guide the user's learning journey. - **Next Steps (Related Links)**: Add links to related pages to guide the user's learning journey.
Feel free to add these sections as needed. Feel free to add these sections as needed.
......
...@@ -15,5 +15,5 @@ ...@@ -15,5 +15,5 @@
"registry": "https://registry.npmjs.org/" "registry": "https://registry.npmjs.org/"
} }
}, },
"version": "16.3.0-canary.40" "version": "16.3.0-canary.41"
} }
\ No newline at end of file
{ {
"name": "create-next-app", "name": "create-next-app",
"version": "16.3.0-canary.40", "version": "16.3.0-canary.41",
"keywords": [ "keywords": [
"react", "react",
"next", "next",
......
{ {
"name": "eslint-config-next", "name": "eslint-config-next",
"version": "16.3.0-canary.40", "version": "16.3.0-canary.41",
"description": "ESLint configuration used by Next.js.", "description": "ESLint configuration used by Next.js.",
"license": "MIT", "license": "MIT",
"repository": { "repository": {
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
"dist" "dist"
], ],
"dependencies": { "dependencies": {
"@next/eslint-plugin-next": "16.3.0-canary.40", "@next/eslint-plugin-next": "16.3.0-canary.41",
"eslint-import-resolver-node": "^0.3.6", "eslint-import-resolver-node": "^0.3.6",
"eslint-import-resolver-typescript": "^3.5.2", "eslint-import-resolver-typescript": "^3.5.2",
"eslint-plugin-import": "^2.32.0", "eslint-plugin-import": "^2.32.0",
......
{ {
"name": "@next/eslint-plugin-internal", "name": "@next/eslint-plugin-internal",
"private": true, "private": true,
"version": "16.3.0-canary.40", "version": "16.3.0-canary.41",
"description": "ESLint plugin for working on Next.js.", "description": "ESLint plugin for working on Next.js.",
"exports": { "exports": {
".": "./src/eslint-plugin-internal.js" ".": "./src/eslint-plugin-internal.js"
......
{ {
"name": "@next/eslint-plugin-next", "name": "@next/eslint-plugin-next",
"version": "16.3.0-canary.40", "version": "16.3.0-canary.41",
"description": "ESLint plugin for Next.js.", "description": "ESLint plugin for Next.js.",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",
......
{ {
"name": "@next/font", "name": "@next/font",
"private": true, "private": true,
"version": "16.3.0-canary.40", "version": "16.3.0-canary.41",
"repository": { "repository": {
"url": "vercel/next.js", "url": "vercel/next.js",
"directory": "packages/font" "directory": "packages/font"
......
{ {
"name": "@next/bundle-analyzer", "name": "@next/bundle-analyzer",
"version": "16.3.0-canary.40", "version": "16.3.0-canary.41",
"main": "index.js", "main": "index.js",
"types": "index.d.ts", "types": "index.d.ts",
"license": "MIT", "license": "MIT",
......
{ {
"name": "@next/codemod", "name": "@next/codemod",
"version": "16.3.0-canary.40", "version": "16.3.0-canary.41",
"license": "MIT", "license": "MIT",
"repository": { "repository": {
"type": "git", "type": "git",
......
{ {
"name": "@next/env", "name": "@next/env",
"version": "16.3.0-canary.40", "version": "16.3.0-canary.41",
"keywords": [ "keywords": [
"react", "react",
"next", "next",
......
{ {
"name": "@next/mdx", "name": "@next/mdx",
"version": "16.3.0-canary.40", "version": "16.3.0-canary.41",
"main": "index.js", "main": "index.js",
"license": "MIT", "license": "MIT",
"repository": { "repository": {
......
{ {
"name": "@next/playwright", "name": "@next/playwright",
"version": "16.3.0-canary.40", "version": "16.3.0-canary.41",
"repository": { "repository": {
"url": "vercel/next.js", "url": "vercel/next.js",
"directory": "packages/next-playwright" "directory": "packages/next-playwright"
......
{ {
"name": "@next/plugin-storybook", "name": "@next/plugin-storybook",
"version": "16.3.0-canary.40", "version": "16.3.0-canary.41",
"repository": { "repository": {
"url": "vercel/next.js", "url": "vercel/next.js",
"directory": "packages/next-plugin-storybook" "directory": "packages/next-plugin-storybook"
......
{ {
"name": "@next/polyfill-module", "name": "@next/polyfill-module",
"version": "16.3.0-canary.40", "version": "16.3.0-canary.41",
"description": "A standard library polyfill for ES Modules supporting browsers (Edge 16+, Firefox 60+, Chrome 61+, Safari 10.1+)", "description": "A standard library polyfill for ES Modules supporting browsers (Edge 16+, Firefox 60+, Chrome 61+, Safari 10.1+)",
"main": "dist/polyfill-module.js", "main": "dist/polyfill-module.js",
"license": "MIT", "license": "MIT",
......
{ {
"name": "@next/polyfill-nomodule", "name": "@next/polyfill-nomodule",
"version": "16.3.0-canary.40", "version": "16.3.0-canary.41",
"description": "A polyfill for non-dead, nomodule browsers.", "description": "A polyfill for non-dead, nomodule browsers.",
"main": "dist/polyfill-nomodule.js", "main": "dist/polyfill-nomodule.js",
"license": "MIT", "license": "MIT",
......
{ {
"name": "@next/routing", "name": "@next/routing",
"version": "16.3.0-canary.40", "version": "16.3.0-canary.41",
"keywords": [ "keywords": [
"react", "react",
"next", "next",
......
{ {
"name": "next-rspack", "name": "next-rspack",
"version": "16.3.0-canary.40", "version": "16.3.0-canary.41",
"repository": { "repository": {
"url": "vercel/next.js", "url": "vercel/next.js",
"directory": "packages/next-rspack" "directory": "packages/next-rspack"
......
{ {
"name": "@next/swc", "name": "@next/swc",
"version": "16.3.0-canary.40", "version": "16.3.0-canary.41",
"private": true, "private": true,
"files": [ "files": [
"native/" "native/"
......
{ {
"name": "next", "name": "next",
"version": "16.3.0-canary.40", "version": "16.3.0-canary.41",
"description": "The React Framework", "description": "The React Framework",
"main": "./dist/server/next.js", "main": "./dist/server/next.js",
"license": "MIT", "license": "MIT",
...@@ -101,7 +101,7 @@ ...@@ -101,7 +101,7 @@
] ]
}, },
"dependencies": { "dependencies": {
"@next/env": "16.3.0-canary.40", "@next/env": "16.3.0-canary.41",
"@swc/helpers": "0.5.15", "@swc/helpers": "0.5.15",
"baseline-browser-mapping": "^2.9.19", "baseline-browser-mapping": "^2.9.19",
"caniuse-lite": "^1.0.30001579", "caniuse-lite": "^1.0.30001579",
...@@ -165,11 +165,11 @@ ...@@ -165,11 +165,11 @@
"@modelcontextprotocol/sdk": "1.18.1", "@modelcontextprotocol/sdk": "1.18.1",
"@mswjs/interceptors": "0.23.0", "@mswjs/interceptors": "0.23.0",
"@napi-rs/triples": "1.2.0", "@napi-rs/triples": "1.2.0",
"@next/font": "16.3.0-canary.40", "@next/font": "16.3.0-canary.41",
"@next/polyfill-module": "16.3.0-canary.40", "@next/polyfill-module": "16.3.0-canary.41",
"@next/polyfill-nomodule": "16.3.0-canary.40", "@next/polyfill-nomodule": "16.3.0-canary.41",
"@next/react-refresh-utils": "16.3.0-canary.40", "@next/react-refresh-utils": "16.3.0-canary.41",
"@next/swc": "16.3.0-canary.40", "@next/swc": "16.3.0-canary.41",
"@opentelemetry/api": "1.6.0", "@opentelemetry/api": "1.6.0",
"@playwright/test": "1.58.2", "@playwright/test": "1.58.2",
"@rspack/core": "1.6.7", "@rspack/core": "1.6.7",
......
...@@ -11,14 +11,14 @@ import { ...@@ -11,14 +11,14 @@ import {
getStaleAt, getStaleAt,
processRuntimePrefetchStream, processRuntimePrefetchStream,
writeDynamicRenderResponseIntoCache, writeDynamicRenderResponseIntoCache,
writeStaticStageResponseIntoCache, writePrerenderResponseIntoCache,
} from '../segment-cache/cache' } from '../segment-cache/cache'
import { FetchStrategy } from '../segment-cache/types' import { FetchStrategy } from '../segment-cache/types'
import { import {
UnknownDynamicStaleTime, UnknownDynamicStaleTime,
computeDynamicStaleAt, computeDynamicStaleAt,
} from '../segment-cache/bfcache' } from '../segment-cache/bfcache'
import { decodeStaticStage } from './fetch-server-response' import { decodeStageUntilBoundary } from './fetch-server-response'
import { discoverKnownRoute } from '../segment-cache/optimistic-routes' import { discoverKnownRoute } from '../segment-cache/optimistic-routes'
import type { NormalizedSearch } from '../segment-cache/cache-key' import type { NormalizedSearch } from '../segment-cache/cache-key'
...@@ -117,6 +117,10 @@ export function createInitialRouterState({ ...@@ -117,6 +117,10 @@ export function createInitialRouterState({
false // hasDynamicRewrite false // hasDynamicRewrite
) )
// TODO: Implement Shell extraction as part of Cached Navigations.
// Intentionally holding off on doing this until we decide how the Cached
// Navigations behavior should work in combination with App Shells.
// Write the initial seed data into the segment cache so subsequent // Write the initial seed data into the segment cache so subsequent
// navigations to the initial page can serve cached segments instantly. // navigations to the initial page can serve cached segments instantly.
if (initialSeedData !== null && initialStaleTime !== undefined) { if (initialSeedData !== null && initialStaleTime !== undefined) {
...@@ -126,17 +130,22 @@ export function createInitialRouterState({ ...@@ -126,17 +130,22 @@ export function createInitialRouterState({
) { ) {
// Partially static page — truncate the cloned Flight stream at the // Partially static page — truncate the cloned Flight stream at the
// static stage byte boundary, decode, and cache the static subset. // static stage byte boundary, decode, and cache the static subset.
decodeStaticStage<InitialRSCPayload>( // Promise.resolve wraps the Flight-deserialized thenable into a
initialFlightStreamForCache, // native Promise so we can chain `.then` on it safely.
initialStaticStageByteLength, Promise.resolve(initialStaticStageByteLength)
undefined .then(async (byteLength) => {
) const staticStageResponse =
.then(async (staticStageResponse) => { await decodeStageUntilBoundary<InitialRSCPayload>(
initialFlightStreamForCache,
byteLength,
undefined
)
const now = Date.now() const now = Date.now()
const staleAt = await getStaleAt(now, staticStageResponse.s) const staleAt = await getStaleAt(now, staticStageResponse.s)
writeStaticStageResponseIntoCache( writePrerenderResponseIntoCache(
now, now,
FetchStrategy.PPR,
staticStageResponse.f, staticStageResponse.f,
undefined, // no build ID mismatch check for initial HTML undefined, // no build ID mismatch check for initial HTML
staticStageResponse.h, staticStageResponse.h,
...@@ -160,8 +169,9 @@ export function createInitialRouterState({ ...@@ -160,8 +169,9 @@ export function createInitialRouterState({
getStaleAt(now, initialStaleTime) getStaleAt(now, initialStaleTime)
.then((staleAt) => { .then((staleAt) => {
writeStaticStageResponseIntoCache( writePrerenderResponseIntoCache(
now, now,
FetchStrategy.PPR,
initialFlightData, initialFlightData,
undefined, // buildId — not applicable for initial HTML undefined, // buildId — not applicable for initial HTML
initialHeadVaryParams, initialHeadVaryParams,
......
...@@ -357,7 +357,12 @@ export type RSCResponse<T> = { ...@@ -357,7 +357,12 @@ export type RSCResponse<T> = {
type FetchResponseCacheData = { type FetchResponseCacheData = {
isResponsePartial: boolean isResponsePartial: boolean
responseBodyClone?: ReadableStream<Uint8Array> // Separate clones of the response body for stage extraction. The static
// stage and shell stage are extracted from independent reads, so each
// needs its own ReadableStream. Both are derived from a chain of `tee()`
// calls in `processFetch`.
staticBodyClone?: ReadableStream<Uint8Array>
shellBodyClone?: ReadableStream<Uint8Array>
} }
/** /**
...@@ -389,9 +394,16 @@ export async function processFetch(response: Response): Promise<{ ...@@ -389,9 +394,16 @@ export async function processFetch(response: Response): Promise<{
let cacheData: FetchResponseCacheData let cacheData: FetchResponseCacheData
if (process.env.__NEXT_EXPERIMENTAL_CACHED_NAVIGATIONS) { if (process.env.__NEXT_EXPERIMENTAL_CACHED_NAVIGATIONS) {
const [stream1, stream2] = stream.tee() // Three readers needed: the main Flight decoder, the static-stage
// extractor, and the shell-stage extractor. Tee twice.
const [stream1, rest] = stream.tee()
const [staticBodyClone, shellBodyClone] = rest.tee()
responseStream = stream1 responseStream = stream1
cacheData = { isResponsePartial: isPartial, responseBodyClone: stream2 } cacheData = {
isResponsePartial: isPartial,
staticBodyClone,
shellBodyClone,
}
} else { } else {
responseStream = stream responseStream = stream
cacheData = { isResponsePartial: isPartial } cacheData = { isResponsePartial: isPartial }
...@@ -433,12 +445,12 @@ export async function resolveStaticStageData< ...@@ -433,12 +445,12 @@ export async function resolveStaticStageData<
flightResponse: T, flightResponse: T,
headers: RequestHeaders | undefined headers: RequestHeaders | undefined
): Promise<StaticStageData<T> | null> { ): Promise<StaticStageData<T> | null> {
const { isResponsePartial, responseBodyClone } = cacheData const { isResponsePartial, staticBodyClone } = cacheData
if (responseBodyClone) { if (staticBodyClone) {
if (!isResponsePartial) { if (!isResponsePartial) {
// Fully static — cache the entire decoded response as-is. // Fully static — cache the entire decoded response as-is.
responseBodyClone.cancel() staticBodyClone.cancel()
return { response: flightResponse, isResponsePartial: false } return { response: flightResponse, isResponsePartial: false }
} }
...@@ -446,9 +458,10 @@ export async function resolveStaticStageData< ...@@ -446,9 +458,10 @@ export async function resolveStaticStageData<
if (flightResponse.l !== undefined) { if (flightResponse.l !== undefined) {
// Partially static — truncate the body clone at the byte boundary and // Partially static — truncate the body clone at the byte boundary and
// decode it. // decode it.
const response = await decodeStaticStage<T>( const staticStageByteLength = await flightResponse.l
responseBodyClone, const response = await decodeStageUntilBoundary<T>(
flightResponse.l, staticBodyClone,
staticStageByteLength,
headers headers
) )
...@@ -456,30 +469,69 @@ export async function resolveStaticStageData< ...@@ -456,30 +469,69 @@ export async function resolveStaticStageData<
} }
// No caching — cancel the unused clone. // No caching — cancel the unused clone.
responseBodyClone.cancel() staticBodyClone.cancel()
} }
return null return null
} }
/**
* Resolves the shell stage of a prerender response, performing a separate
* Flight decode of the byte prefix when the shell differs from the main
* response. Returns null when no separate decode is needed:
*
* - `a === undefined`: server didn't emit shell stage info.
* - `a` resolves to `null`: the shell IS the main response — the caller can
* reuse the existing decoded `flightResponse` if it needs a shell payload.
*
* Returns the decoded shell payload when `a` resolves to a number, i.e.
* the shell is a strict prefix of the response and requires a separate
* decode at that byte boundary.
*/
export async function resolveShellStageData<
T extends NavigationFlightResponse | InitialRSCPayload,
>(
cacheData: FetchResponseCacheData,
flightResponse: T,
headers: RequestHeaders | undefined
): Promise<T | null> {
const { shellBodyClone } = cacheData
if (!shellBodyClone) {
return null
}
if (flightResponse.a === undefined) {
shellBodyClone.cancel()
return null
}
const shellByteLength = await flightResponse.a
if (shellByteLength === null) {
// Shell == main response — caller reuses the existing flightResponse.
shellBodyClone.cancel()
return null
}
return decodeStageUntilBoundary<T>(shellBodyClone, shellByteLength, headers)
}
/** /**
* Truncates and buffers a Flight stream clone at the given byte boundary and * Truncates and buffers a Flight stream clone at the given byte boundary and
* decodes the static stage prefix. Used by both the navigation path and the * decodes the prefix as a Flight payload. Used by the static-stage and
* initial HTML hydration path. * shell-stage extraction helpers.
*/ */
export async function decodeStaticStage<T>( export async function decodeStageUntilBoundary<T>(
responseBodyClone: ReadableStream<Uint8Array>, responseBodyClone: ReadableStream<Uint8Array>,
staticStageByteLengthPromise: Promise<number>, byteLength: number,
headers: RequestHeaders | undefined headers: RequestHeaders | undefined
): Promise<T> { ): Promise<T> {
const staticStageByteLength = await staticStageByteLengthPromise
// Buffer the truncated stream into a single chunk before passing it to // Buffer the truncated stream into a single chunk before passing it to
// Flight. This ensures all model data is available synchronously, which is // Flight. This ensures all model data is available synchronously, which is
// required for readVaryParams to synchronously read the thenable status. // required for readVaryParams to synchronously read the thenable status.
const { stream } = await createNonTaskyPrefetchResponseStream( const { stream } = await createNonTaskyPrefetchResponseStream(
responseBodyClone, responseBodyClone,
staticStageByteLength byteLength
) )
return createFromNextReadableStream<T>(stream, headers, { return createFromNextReadableStream<T>(stream, headers, {
......
...@@ -35,7 +35,7 @@ import { ...@@ -35,7 +35,7 @@ import {
markRouteEntryAsDynamicRewrite, markRouteEntryAsDynamicRewrite,
invalidateRouteCacheEntries, invalidateRouteCacheEntries,
getStaleAt, getStaleAt,
writeStaticStageResponseIntoCache, writePrerenderResponseIntoCache,
processRuntimePrefetchStream, processRuntimePrefetchStream,
writeDynamicRenderResponseIntoCache, writeDynamicRenderResponseIntoCache,
EntryStatus, EntryStatus,
...@@ -1737,6 +1737,9 @@ async function fetchMissingDynamicData( ...@@ -1737,6 +1737,9 @@ async function fetchMissingDynamicData(
await waitForNavigationLock() await waitForNavigationLock()
} }
// TODO: Implement Shell extraction as part of Cached Navigations.
// Intentionally holding off on doing this until we decide how the Cached
// Navigations behavior should work in combination with App Shells.
if (routeCacheEntry !== null && result.staticStageData !== null) { if (routeCacheEntry !== null && result.staticStageData !== null) {
const { response: staticStageResponse, isResponsePartial } = const { response: staticStageResponse, isResponsePartial } =
result.staticStageData result.staticStageData
...@@ -1747,8 +1750,9 @@ async function fetchMissingDynamicData( ...@@ -1747,8 +1750,9 @@ async function fetchMissingDynamicData(
result.responseHeaders.get(NEXT_NAV_DEPLOYMENT_ID_HEADER) ?? result.responseHeaders.get(NEXT_NAV_DEPLOYMENT_ID_HEADER) ??
staticStageResponse.b staticStageResponse.b
writeStaticStageResponseIntoCache( writePrerenderResponseIntoCache(
now, now,
FetchStrategy.PPR,
staticStageResponse.f, staticStageResponse.f,
buildId, buildId,
staticStageResponse.h, staticStageResponse.h,
......
...@@ -27,6 +27,7 @@ import { ...@@ -27,6 +27,7 @@ import {
import { import {
createFetch, createFetch,
createFromNextReadableStream, createFromNextReadableStream,
resolveShellStageData,
type RSCResponse, type RSCResponse,
type RequestHeaders, type RequestHeaders,
} from '../router-reducer/fetch-server-response' } from '../router-reducer/fetch-server-response'
...@@ -2251,16 +2252,109 @@ export async function fetchSegmentPrefetchesUsingDynamicRequest( ...@@ -2251,16 +2252,109 @@ export async function fetchSegmentPrefetchesUsingDynamicRequest(
response.cacheData, response.cacheData,
]) ])
const now = Date.now()
const staleAt = await getStaleAt(now, serverData.s, response)
const buildId =
response.headers.get(NEXT_NAV_DEPLOYMENT_ID_HEADER) ?? serverData.b
// Check if a reusable App Shell can be extracted from the main response.
let serverDataThatSatisfiesSpawnedEntries: NavigationFlightResponse
// The shell and full response have independent stale times. Track the
// staleAt that corresponds to whatever payload the spawned entries get
// filled with below.
let staleAtForSpawnedEntries = staleAt
if (!process.env.__NEXT_APP_SHELLS || cacheData === null) {
// NOTE: cacheData is always set when Cached Navigations is enabled, and
// therefore when App Shells is enabled. This null check can be removed
// when those flags fully land.
serverDataThatSatisfiesSpawnedEntries = serverData
} else {
const shellStageData = await resolveShellStageData(
cacheData,
serverData,
headers
)
if (shellStageData === null) {
// No App Shell can be extracted. This usually means the entire response
// _is_ the App Shell. The other possibility (for now, until the feature
// is fully stabilized) is that App Shells are not yet enabled. Either
// way, there's nothing extra for us to do: fulfill the pending entries
// using the response from the server.
serverDataThatSatisfiesSpawnedEntries = serverData
} else {
// Successfully extracted an App Shell that is a subset of the main
// response. Depending on the type of prefetch this is, we need to
// decide whether to fulfill the pending entries with the shell or with
// the entire response. In either scenario, we'll be inserting _both_
// versions of the response into the cache; the extra logic is only
// here so that we don't fulfill pending shell entries with something
// that's more concrete than what they expect.
// TODO: The only reason this matters is because during a navigation,
// if a segment is still pending, we render a promise that resolves to
// the eventual value of that segment. But that means we cannot
// eventually resolve that segment to something more concrete than what
// was already requested. Hence the extra logic here. A cleaner way to
// model this, though, is whenever we render a promise that resolves to
// the result of a pending entry, do one additional cache look-up right
// after the promise resolves, to ensure we never get a mismatching
// entry. Leaving this for a follow up.
const shellStaleAt = await getStaleAt(now, shellStageData.s)
if (fetchStrategy === FetchStrategy.RuntimeShell) {
// This is a Shell prefetch, so the pending entries must be fulfilled
// with the shell.
serverDataThatSatisfiesSpawnedEntries = shellStageData
staleAtForSpawnedEntries = shellStaleAt
// Separately, we'll also cache the entire response, by upserting it
// into the cache.
writePrerenderResponseIntoCache(
now,
FetchStrategy.PPR,
serverData.f,
buildId,
serverData.h,
staleAt,
dynamicRequestTree,
renderedSearch,
cacheData.isResponsePartial
)
} else {
// This is _not_ a Shell prefetch, so the pending entries should be
// fulfilled with the entire response.
serverDataThatSatisfiesSpawnedEntries = serverData
// Additionally, we might as well upsert the extracted Shell into the
// cache, too.
// `shellStageData` is only provided in cases where the shell is
// different from the main response. If they are equivalent, this
// branch is skipped. So it follows that any shell data reaches
// this path must be partial -- it does not represent the entire
// UI of the target page.
const isShellStagePartial = true
writePrerenderResponseIntoCache(
now,
FetchStrategy.RuntimeShell,
shellStageData.f,
buildId,
shellStageData.h,
shellStaleAt,
dynamicRequestTree,
renderedSearch,
isShellStagePartial
)
}
}
}
// Read head vary params synchronously. Individual segments carry their // Read head vary params synchronously. Individual segments carry their
// own thenables in CacheNodeSeedData. // own thenables in CacheNodeSeedData.
const headVaryParamsThenable = serverData.h const headVaryParamsThenable = serverDataThatSatisfiesSpawnedEntries.h
const headVaryParams = const headVaryParams =
headVaryParamsThenable !== null headVaryParamsThenable !== null
? readVaryParams(headVaryParamsThenable) ? readVaryParams(headVaryParamsThenable)
: null : null
const now = Date.now()
const staleAt = await getStaleAt(now, serverData.s, response)
// PPRRuntime and RuntimeShell prefetches are partial when the server // PPRRuntime and RuntimeShell prefetches are partial when the server
// marks the response as '~' (Partial). RuntimeShell additionally omits // marks the response as '~' (Partial). RuntimeShell additionally omits
// every dynamic suspense boundary below the App Shell, so its segments // every dynamic suspense boundary below the App Shell, so its segments
...@@ -2271,12 +2365,9 @@ export async function fetchSegmentPrefetchesUsingDynamicRequest( ...@@ -2271,12 +2365,9 @@ export async function fetchSegmentPrefetchesUsingDynamicRequest(
(fetchStrategy === FetchStrategy.PPRRuntime && (fetchStrategy === FetchStrategy.PPRRuntime &&
(cacheData?.isResponsePartial ?? false)) (cacheData?.isResponsePartial ?? false))
// Aside from writing the data into the cache, this function also returns const flightDatas = normalizeFlightData(
// the entries that were fulfilled, so we can streamingly update their sizes serverDataThatSatisfiesSpawnedEntries.f
// in the LRU as more data comes in. )
const buildId =
response.headers.get(NEXT_NAV_DEPLOYMENT_ID_HEADER) ?? serverData.b
const flightDatas = normalizeFlightData(serverData.f)
if (typeof flightDatas === 'string') { if (typeof flightDatas === 'string') {
rejectSegmentEntriesIfStillPending(spawnedEntries, Date.now() + 10 * 1000) rejectSegmentEntriesIfStillPending(spawnedEntries, Date.now() + 10 * 1000)
return null return null
...@@ -2289,6 +2380,9 @@ export async function fetchSegmentPrefetchesUsingDynamicRequest( ...@@ -2289,6 +2380,9 @@ export async function fetchSegmentPrefetchesUsingDynamicRequest(
// Not needed for prefetch responses; pass unknown to use the default. // Not needed for prefetch responses; pass unknown to use the default.
UnknownDynamicStaleTime UnknownDynamicStaleTime
) )
// Aside from writing the data into the cache, this function also returns
// the entries that were fulfilled, so we can streamingly update their sizes
// in the LRU as more data comes in.
fulfilledEntries = writeDynamicRenderResponseIntoCache( fulfilledEntries = writeDynamicRenderResponseIntoCache(
now, now,
fetchStrategy, fetchStrategy,
...@@ -2296,7 +2390,7 @@ export async function fetchSegmentPrefetchesUsingDynamicRequest( ...@@ -2296,7 +2390,7 @@ export async function fetchSegmentPrefetchesUsingDynamicRequest(
buildId, buildId,
isResponsePartial, isResponsePartial,
headVaryParams, headVaryParams,
staleAt, staleAtForSpawnedEntries,
navigationSeed, navigationSeed,
spawnedEntries spawnedEntries
) )
...@@ -3004,14 +3098,14 @@ export async function getStaleAt( ...@@ -3004,14 +3098,14 @@ export async function getStaleAt(
} }
/** /**
* Writes the static stage of a navigation response into the segment cache. * Writes a prerender response into the segment cache at the vary path
* When `isResponsePartial` is false, segments are written as non-partial with * determined by `fetchStrategy`. Default segments are skipped (by
* `FetchStrategy.Full` so no dynamic follow-up is needed. Default segments * `writeSeedDataIntoCache`) to avoid caching fallback content that would
* are skipped (by `writeSeedDataIntoCache`) to avoid caching fallback content * block refreshes from overwriting with dynamic data.
* that would block refreshes from overwriting with dynamic data.
*/ */
export function writeStaticStageResponseIntoCache( export function writePrerenderResponseIntoCache(
now: number, now: number,
fetchStrategy: FetchStrategy.PPR | FetchStrategy.RuntimeShell,
flightData: FlightData, flightData: FlightData,
buildId: string | undefined, buildId: string | undefined,
headVaryParamsThenable: VaryParamsThenable | null, headVaryParamsThenable: VaryParamsThenable | null,
...@@ -3020,10 +3114,6 @@ export function writeStaticStageResponseIntoCache( ...@@ -3020,10 +3114,6 @@ export function writeStaticStageResponseIntoCache(
renderedSearch: string, renderedSearch: string,
isResponsePartial: boolean isResponsePartial: boolean
): void { ): void {
const fetchStrategy = isResponsePartial
? FetchStrategy.PPR
: FetchStrategy.Full
const headVaryParams = const headVaryParams =
headVaryParamsThenable !== null headVaryParamsThenable !== null
? readVaryParams(headVaryParamsThenable) ? readVaryParams(headVaryParamsThenable)
......
...@@ -22,7 +22,7 @@ import { ...@@ -22,7 +22,7 @@ import {
deprecated_requestOptimisticRouteCacheEntry, deprecated_requestOptimisticRouteCacheEntry,
convertRootFlightRouterStateToRouteTree, convertRootFlightRouterStateToRouteTree,
getStaleAt, getStaleAt,
writeStaticStageResponseIntoCache, writePrerenderResponseIntoCache,
processRuntimePrefetchStream, processRuntimePrefetchStream,
writeDynamicRenderResponseIntoCache, writeDynamicRenderResponseIntoCache,
type RouteTree, type RouteTree,
...@@ -460,8 +460,13 @@ async function navigateToUnknownRoute( ...@@ -460,8 +460,13 @@ async function navigateToUnknownRoute(
responseHeaders.get(NEXT_NAV_DEPLOYMENT_ID_HEADER) ?? responseHeaders.get(NEXT_NAV_DEPLOYMENT_ID_HEADER) ??
staticStageResponse.b staticStageResponse.b
writeStaticStageResponseIntoCache( // TODO: Implement Shell extraction as part of Cached Navigations.
// Intentionally holding off on doing this until we decide how the
// Cached Navigations behavior should work in combination with App
// Shells.
writePrerenderResponseIntoCache(
now, now,
FetchStrategy.PPR,
staticStageResponse.f, staticStageResponse.f,
buildId, buildId,
staticStageResponse.h, staticStageResponse.h,
......
...@@ -790,8 +790,24 @@ function pingRootRouteTree( ...@@ -790,8 +790,24 @@ function pingRootRouteTree(
return PrefetchTaskExitStatus.InProgress return PrefetchTaskExitStatus.InProgress
} }
if (tree.prefetchHints & PrefetchHint.SubtreeHasRuntimePrefetch) { // We may need to do a runtime prefetch for one or more segments.
// The route has segments that require a runtime prefetch. // Before checking, we can do some fast checks to bail out of this
// branch early.
if (
// Do any segments have runtime prefetching configured? This is
// sent by the server as part of the prefetch hints.
tree.prefetchHints & PrefetchHint.SubtreeHasRuntimePrefetch ||
// Are we in the Shell prefetching phase? The Shell phase is allowed
// to perform runtime prefetches even without an explicit opt-in
// because the Shell for a given route is reusable across all given
// params, by definition. So it does not lead to an explosion in
// prefetching costs.
// TODO: In the future, the server could emit a hint to tell us
// *not* to prefetch via a runtime request, via build-time
// heuristics, like if no `cookies()` call was detected. We'll leave
// this optimization for later.
task.phase === PrefetchPhase.Shell
) {
const runtimeStrategy = const runtimeStrategy =
task.phase === PrefetchPhase.Shell task.phase === PrefetchPhase.Shell
? FetchStrategy.RuntimeShell ? FetchStrategy.RuntimeShell
...@@ -1073,7 +1089,22 @@ function pingNewPartOfCacheComponentsTree( ...@@ -1073,7 +1089,22 @@ function pingNewPartOfCacheComponentsTree(
// shared layouts.) Segments in here default to being prefetched statically. // shared layouts.) Segments in here default to being prefetched statically.
// However, if the server instructs us to, we may switch to a runtime // However, if the server instructs us to, we may switch to a runtime
// prefetch instead. Traverse the tree and check at each segment. // prefetch instead. Traverse the tree and check at each segment.
if (tree.prefetchHints & PrefetchHint.HasRuntimePrefetch) { //
// In the Shell phase, every new segment is treated as a runtime-prefetch
// boundary regardless of `HasRuntimePrefetch`, because the Shell is
// reusable across all params by definition (see the matching comment in
// the main scheduler loop).
//
// TODO: If we're reasonably confident that the shell does not depend on
// session data (cookies), we should attempt to prefetch the shell
// statically, instead of via a runtime prefetch. This is an optimization to skip a
// runtime shell request. For fully static pages, it doesn't matter since
// server will respond to even a runtime request with a static response. For
// a partially static page, we can send down a hint from the server.
if (
tree.prefetchHints & PrefetchHint.HasRuntimePrefetch ||
task.phase === PrefetchPhase.Shell
) {
if (task.spawnedRuntimePrefetches === null) { if (task.spawnedRuntimePrefetches === null) {
task.spawnedRuntimePrefetches = new Set([tree.requestKey]) task.spawnedRuntimePrefetches = new Set([tree.requestKey])
} else { } else {
......
...@@ -69,7 +69,7 @@ export function ComponentStyles() { ...@@ -69,7 +69,7 @@ export function ComponentStyles() {
// ... // ...
${COMPONENT_NAME_STYLES} ${COMPONENT_NAME_STYLES}
`} `}
</style </style>
) )
} }
``` ```
......
...@@ -3593,7 +3593,7 @@ async function renderToStream( ...@@ -3593,7 +3593,7 @@ async function renderToStream(
) { ) {
if (process.env.__NEXT_USE_NODE_STREAMS) { if (process.env.__NEXT_USE_NODE_STREAMS) {
// MARK: nodeStreams dev CacheComponents RSC // MARK: nodeStreams dev CacheComponents RSC
let debugChannel: DebugChannelPair | undefined let debugChannelClientStream: ReplayableNodeStream | undefined
// eslint-disable-next-line @typescript-eslint/no-shadow // eslint-disable-next-line @typescript-eslint/no-shadow
const getPayload = async (requestStore: RequestStore) => { const getPayload = async (requestStore: RequestStore) => {
...@@ -3652,11 +3652,11 @@ async function renderToStream( ...@@ -3652,11 +3652,11 @@ async function renderToStream(
let validationDebugChannelClient: AnyStream | undefined = undefined let validationDebugChannelClient: AnyStream | undefined = undefined
if (returnedDebugChannel) { if (returnedDebugChannel) {
const [t1, t2] = teeStream( debugChannelClientStream = new ReplayableNodeStream(
returnedDebugChannel.clientSide.readable returnedDebugChannel.clientSide.readable
) )
returnedDebugChannel.clientSide.readable = t1 validationDebugChannelClient =
validationDebugChannelClient = t2 debugChannelClientStream.createReplayStream()
} }
consoleAsyncStorage.run( consoleAsyncStorage.run(
...@@ -3676,14 +3676,14 @@ async function renderToStream( ...@@ -3676,14 +3676,14 @@ async function renderToStream(
reactServerResult = new ReactServerResult(serverStream) reactServerResult = new ReactServerResult(serverStream)
requestStore = finalRequestStore requestStore = finalRequestStore
debugChannel = returnedDebugChannel
} else { } else {
logValidationSkipped(ctx) logValidationSkipped(ctx)
// We're either bypassing caches or we can't restart the render. // We're either bypassing caches or we can't restart the render.
// Do a dynamic render, but with (basic) environment labels. // Do a dynamic render, but with (basic) environment labels.
debugChannel = setReactDebugChannel && createNodeDebugChannel() const debugChannel =
setReactDebugChannel && createNodeDebugChannel()
const serverStream = await stagedRenderWithoutCachesInDevNode( const serverStream = await stagedRenderWithoutCachesInDevNode(
ctx, ctx,
...@@ -3696,17 +3696,19 @@ async function renderToStream( ...@@ -3696,17 +3696,19 @@ async function renderToStream(
} }
) )
reactServerResult = new ReactServerResult(serverStream) reactServerResult = new ReactServerResult(serverStream)
}
if (debugChannel && setReactDebugChannel) { if (debugChannel) {
const [readableSsr, readableBrowser] = teeStream( debugChannelClientStream = new ReplayableNodeStream(
debugChannel.clientSide.readable debugChannel.clientSide.readable
) )
}
}
reactDebugStream = readableSsr if (debugChannelClientStream && setReactDebugChannel) {
reactDebugStream = debugChannelClientStream.createReplayStream()
setReactDebugChannel( setReactDebugChannel(
{ readable: readableBrowser }, { readable: debugChannelClientStream.createReplayStream() },
htmlRequestId, htmlRequestId,
requestId requestId
) )
......
...@@ -3,17 +3,27 @@ ...@@ -3,17 +3,27 @@
* Loaded by debug-channel-server.ts when __NEXT_USE_NODE_STREAMS is enabled. * Loaded by debug-channel-server.ts when __NEXT_USE_NODE_STREAMS is enabled.
*/ */
import { PassThrough, Writable } from 'node:stream' import { PassThrough, Writable, type Readable } from 'node:stream'
import type { DebugChannelPair } from './debug-channel-server.web' import type { DebugChannelServer } from './debug-channel-server.web'
export { createWebDebugChannel } from './debug-channel-server.web' export { createWebDebugChannel } from './debug-channel-server.web'
/**
* Node variant: identical to `DebugChannelPair` except the client-side readable
* is narrowed to a Node `Readable`, so node call sites can consume it (e.g.
* `new ReplayableNodeStream(...)`) without casting `AnyStream` down.
*/
export type NodeDebugChannelPair = {
serverSide: DebugChannelServer
clientSide: { readable: Readable }
}
/** /**
* Creates a debug channel using Node.js streams. * Creates a debug channel using Node.js streams.
* Use with renderToNodeFlightStream (React's renderToPipeableStream), * Use with renderToNodeFlightStream (React's renderToPipeableStream),
* which expects debugChannel to be a Node.js stream with a .write() method. * which expects debugChannel to be a Node.js stream with a .write() method.
*/ */
export function createNodeDebugChannel(): DebugChannelPair { export function createNodeDebugChannel(): NodeDebugChannelPair {
// The readable side is a PassThrough that the client reads from. The // The readable side is a PassThrough that the client reads from. The
// server-side write target is a separate, write-only Writable that forwards // server-side write target is a separate, write-only Writable that forwards
// into it rather than the PassThrough itself: React's renderToPipeableStream // into it rather than the PassThrough itself: React's renderToPipeableStream
...@@ -23,12 +33,11 @@ export function createNodeDebugChannel(): DebugChannelPair { ...@@ -23,12 +33,11 @@ export function createNodeDebugChannel(): DebugChannelPair {
// The forwarding must use `passthrough.write()` / `passthrough.end()`, not // The forwarding must use `passthrough.write()` / `passthrough.end()`, not
// `passthrough.push()` / `passthrough.push(null)`. A PassThrough is a Duplex; // `passthrough.push()` / `passthrough.push(null)`. A PassThrough is a Duplex;
// pushing `null` ends only its readable half and leaves the writable half // pushing `null` ends only its readable half and leaves the writable half
// open (`writableEnded` stays false). The readable is later consumed via // open (`writableEnded` stays false). If the readable is consumed via
// `Readable.toWeb()` (in `connectReactDebugChannel`, and inside `teeStream`), // `Readable.toWeb()`, that web stream never closes while the PassThrough's
// and `Readable.toWeb()` never closes the resulting web stream while the // writable half is still open — so the debug channel would never close on the
// PassThrough's writable half is still open — so the debug channel never // client. Ending it via `passthrough.end()` closes both halves and the close
// closes on the client. Ending it via `passthrough.end()` closes both halves // propagates.
// and the close propagates.
const passthrough = new PassThrough() const passthrough = new PassThrough()
const writable = new Writable({ const writable = new Writable({
......
...@@ -8,11 +8,13 @@ export type { ...@@ -8,11 +8,13 @@ export type {
DebugChannelPair, DebugChannelPair,
DebugChannelServer, DebugChannelServer,
} from './debug-channel-server.web' } from './debug-channel-server.web'
export type { NodeDebugChannelPair } from './debug-channel-server.node'
import type { DebugChannelPair } from './debug-channel-server.web' import type { DebugChannelPair } from './debug-channel-server.web'
import type { NodeDebugChannelPair } from './debug-channel-server.node'
type DebugChannelMod = { type DebugChannelMod = {
createWebDebugChannel: typeof import('./debug-channel-server.web').createWebDebugChannel createWebDebugChannel: typeof import('./debug-channel-server.web').createWebDebugChannel
createNodeDebugChannel: typeof import('./debug-channel-server.web').createNodeDebugChannel createNodeDebugChannel: typeof import('./debug-channel-server.node').createNodeDebugChannel
} }
let _m: DebugChannelMod let _m: DebugChannelMod
...@@ -32,7 +34,7 @@ export function createWebDebugChannel(): DebugChannelPair | undefined { ...@@ -32,7 +34,7 @@ export function createWebDebugChannel(): DebugChannelPair | undefined {
return _m.createWebDebugChannel() return _m.createWebDebugChannel()
} }
export function createNodeDebugChannel(): DebugChannelPair | undefined { export function createNodeDebugChannel(): NodeDebugChannelPair | undefined {
if (process.env.NODE_ENV === 'production') { if (process.env.NODE_ENV === 'production') {
return undefined return undefined
} }
......
...@@ -53,6 +53,6 @@ export function createWebDebugChannel(): DebugChannelPair { ...@@ -53,6 +53,6 @@ export function createWebDebugChannel(): DebugChannelPair {
* Use with renderToNodeFlightStream (React's renderToPipeableStream), * Use with renderToNodeFlightStream (React's renderToPipeableStream),
* which expects debugChannel to be a Node.js stream with a .write() method. * which expects debugChannel to be a Node.js stream with a .write() method.
*/ */
export function createNodeDebugChannel(): DebugChannelPair { export function createNodeDebugChannel(): never {
throw new Error('not implemented') throw new Error('not implemented')
} }
...@@ -30,13 +30,17 @@ import { ...@@ -30,13 +30,17 @@ import {
} from '../stream-utils/node-web-streams-helper' } from '../stream-utils/node-web-streams-helper'
import { indexOfUint8Array } from '../stream-utils/uint8array-helpers' import { indexOfUint8Array } from '../stream-utils/uint8array-helpers'
import { ENCODED_TAGS } from '../stream-utils/encoded-tags' import { ENCODED_TAGS } from '../stream-utils/encoded-tags'
import { createNodeBufferedTransformStream } from '../stream-utils/node-buffered-transform-stream'
import { MISSING_ROOT_TAGS_ERROR } from '../../shared/lib/errors/constants' import { MISSING_ROOT_TAGS_ERROR } from '../../shared/lib/errors/constants'
import { import {
htmlEscapeAttributeString, htmlEscapeAttributeString,
htmlEscapeJsonString, htmlEscapeJsonString,
} from '../../shared/lib/htmlescape' } from '../../shared/lib/htmlescape'
import { createInlinedDataReadableStream } from './use-flight-response' import { createInlinedDataReadableStream } from './use-flight-response'
import type { AnyStream as AnyStreamType } from './app-render-prerender-utils' import {
ReplayableNodeStream,
type AnyStream as AnyStreamType,
} from './app-render-prerender-utils'
import { DetachedPromise } from '../../lib/detached-promise' import { DetachedPromise } from '../../lib/detached-promise'
import { getTracer } from '../lib/trace/tracer' import { getTracer } from '../lib/trace/tracer'
import { AppRenderSpan } from '../lib/trace/constants' import { AppRenderSpan } from '../lib/trace/constants'
...@@ -116,52 +120,6 @@ function webToReadable( ...@@ -116,52 +120,6 @@ function webToReadable(
return Readable.fromWeb(stream as WebReadableStream) return Readable.fromWeb(stream as WebReadableStream)
} }
// ---------------------------------------------------------------------------
// Buffered transform – Node.js Transform that coalesces chunks written in the
// same microtask into a single Uint8Array before pushing downstream.
// ---------------------------------------------------------------------------
function createBufferedTransformStream(): Transform {
let bufferedChunks: Array<Uint8Array> = []
let bufferByteLength = 0
let flushScheduled = false
function flushBuffered(stream: Transform): void {
if (bufferedChunks.length === 0) return
const merged = new Uint8Array(bufferByteLength)
let copiedBytes = 0
for (let i = 0; i < bufferedChunks.length; i++) {
const bufferedChunk = bufferedChunks[i]
merged.set(bufferedChunk, copiedBytes)
copiedBytes += bufferedChunk.byteLength
}
bufferedChunks.length = 0
bufferByteLength = 0
stream.push(merged)
}
return new Transform({
transform(chunk, _encoding, callback) {
bufferedChunks.push(chunk)
bufferByteLength += chunk.byteLength
if (!flushScheduled) {
flushScheduled = true
queueMicrotask(() => {
flushScheduled = false
flushBuffered(this)
})
}
callback()
},
flush(callback) {
flushBuffered(this)
callback()
},
})
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Flight data injection – Node.js Transform that passes HTML chunks through // Flight data injection – Node.js Transform that passes HTML chunks through
// while pulling from a separate data stream and interleaving its chunks. // while pulling from a separate data stream and interleaving its chunks.
...@@ -725,7 +683,7 @@ export async function continueFizzStream( ...@@ -725,7 +683,7 @@ export async function continueFizzStream(
// 1. Buffer – coalesces chunks written in the same microtask into one Uint8Array // 1. Buffer – coalesces chunks written in the same microtask into one Uint8Array
// 2. Flight data injection – interleaves RSC data chunks with the HTML stream // 2. Flight data injection – interleaves RSC data chunks with the HTML stream
// 3. Head insertion – inserts server-generated HTML before </head> // 3. Head insertion – inserts server-generated HTML before </head>
const buffered = createBufferedTransformStream() const buffered = createNodeBufferedTransformStream()
webToReadable(renderStream).pipe(buffered) webToReadable(renderStream).pipe(buffered)
let source: Readable = buffered let source: Readable = buffered
...@@ -836,7 +794,7 @@ export async function continueDynamicHTMLResumeNode( ...@@ -836,7 +794,7 @@ export async function continueDynamicHTMLResumeNode(
): Promise<AnyStream> { ): Promise<AnyStream> {
await waitAtLeastOneReactRenderTask() await waitAtLeastOneReactRenderTask()
const buffered = createBufferedTransformStream() const buffered = createNodeBufferedTransformStream()
webToReadable(renderStream).pipe(buffered) webToReadable(renderStream).pipe(buffered)
let source: Readable = buffered let source: Readable = buffered
...@@ -1098,7 +1056,12 @@ export function getServerPrerender(ComponentMod: { ...@@ -1098,7 +1056,12 @@ export function getServerPrerender(ComponentMod: {
export const getClientPrerender: typeof import('react-dom/static').prerender = export const getClientPrerender: typeof import('react-dom/static').prerender =
prerender prerender
// Node counterpart of the web `teeStream`. Like the web version it assumes the
// stream type matching its build — here a Node `Readable` — and fans out
// through `ReplayableNodeStream`. Need three or more consumers from one source?
// Use `ReplayableNodeStream` directly (N `createReplayStream()` calls) to avoid
// nesting tees.
export function teeStream(stream: AnyStream): [AnyStream, AnyStream] { export function teeStream(stream: AnyStream): [AnyStream, AnyStream] {
const [s1, s2] = nodeReadableToWebReadableStream(stream).tee() const replayable = new ReplayableNodeStream(stream as Readable)
return [webToReadable(s1), webToReadable(s2)] return [replayable.createReplayStream(), replayable.createReplayStream()]
} }
import type { Readable } from 'node:stream' import type { Readable } from 'node:stream'
import { createBufferedTransformStream } from '../stream-utils/node-web-streams-helper' import { createBufferedTransformStream } from '../stream-utils/node-web-streams-helper'
import { createNodeBufferedTransformStream } from '../stream-utils/node-buffered-transform-stream'
import { import {
HMR_MESSAGE_SENT_TO_BROWSER, HMR_MESSAGE_SENT_TO_BROWSER,
type HmrMessageSentToBrowser, type HmrMessageSentToBrowser,
} from './hot-reloader-types' } from './hot-reloader-types'
import type { AnyStream } from '../app-render/stream-ops' import type { AnyStream } from '../app-render/stream-ops'
function toWebReadableStream(stream: AnyStream): ReadableStream<Uint8Array> { // Chunks are sent to the browser in batches to reduce overhead, flushing
if (stream instanceof ReadableStream) { // synchronously once this many bytes have accumulated.
return stream const MAX_DEBUG_CHANNEL_BATCH_BYTES = 128 * 1024
}
const { Readable: ReadableClass } =
require('node:stream') as typeof import('node:stream')
return ReadableClass.toWeb(stream as Readable) as ReadableStream<Uint8Array>
}
export interface ReactDebugChannelForBrowser { export interface ReactDebugChannelForBrowser {
readonly readable: AnyStream readonly readable: AnyStream
...@@ -24,19 +20,23 @@ const reactDebugChannelsByHtmlRequestId = new Map< ...@@ -24,19 +20,23 @@ const reactDebugChannelsByHtmlRequestId = new Map<
ReactDebugChannelForBrowser ReactDebugChannelForBrowser
>() >()
/**
* Reads the React debug channel and forwards its chunks to the browser through
* the websocket. Branches on the stream type so that Node streams stay
* node-native — batched with a Node `Transform` and consumed via events.
*/
export function connectReactDebugChannel( export function connectReactDebugChannel(
requestId: string, requestId: string,
debugChannel: ReactDebugChannelForBrowser, debugChannel: ReactDebugChannelForBrowser,
sendToClient: (message: HmrMessageSentToBrowser) => void sendToClient: (message: HmrMessageSentToBrowser) => void
) { ) {
const reader = toWebReadableStream(debugChannel.readable) let finished = false
.pipeThrough(
// We're sending the chunks in batches to reduce overhead in the browser.
createBufferedTransformStream({ maxBufferByteLength: 128 * 1024 })
)
.getReader()
const stop = () => { const stop = () => {
if (finished) {
return
}
finished = true
sendToClient({ sendToClient({
type: HMR_MESSAGE_SENT_TO_BROWSER.REACT_DEBUG_CHUNK, type: HMR_MESSAGE_SENT_TO_BROWSER.REACT_DEBUG_CHUNK,
requestId, requestId,
...@@ -45,25 +45,55 @@ export function connectReactDebugChannel( ...@@ -45,25 +45,55 @@ export function connectReactDebugChannel(
} }
const onError = (err: unknown) => { const onError = (err: unknown) => {
console.error(new Error('React debug channel stream error', { cause: err })) if (!finished) {
console.error(
new Error('React debug channel stream error', { cause: err })
)
}
stop() stop()
} }
const progress = (entry: ReadableStreamReadResult<Uint8Array>) => { const sendChunk = (chunk: Uint8Array) => {
if (entry.done) { sendToClient({
stop() type: HMR_MESSAGE_SENT_TO_BROWSER.REACT_DEBUG_CHUNK,
} else { requestId,
sendToClient({ chunk,
type: HMR_MESSAGE_SENT_TO_BROWSER.REACT_DEBUG_CHUNK, })
requestId,
chunk: entry.value,
})
reader.read().then(progress, onError)
}
} }
reader.read().then(progress, onError) const { readable } = debugChannel
if (readable instanceof ReadableStream) {
const reader = readable
.pipeThrough(
createBufferedTransformStream({
maxBufferByteLength: MAX_DEBUG_CHANNEL_BATCH_BYTES,
})
)
.getReader()
const progress = (entry: ReadableStreamReadResult<Uint8Array>) => {
if (entry.done) {
stop()
} else {
sendChunk(entry.value)
reader.read().then(progress, onError)
}
}
reader.read().then(progress, onError)
} else {
const source = readable as Readable
// `pipe` does not forward source errors to the destination, so handle them
// on the source directly.
source.on('error', onError)
const batched = source.pipe(
createNodeBufferedTransformStream(MAX_DEBUG_CHANNEL_BATCH_BYTES)
)
batched.on('data', sendChunk)
batched.on('end', stop)
batched.on('error', onError)
}
} }
export function connectReactDebugChannelForHtmlRequest( export function connectReactDebugChannelForHtmlRequest(
......
import { Transform } from 'node:stream'
/**
* Node.js `Transform` that coalesces chunks written in the same microtask into
* a single `Uint8Array` before pushing downstream, flushing synchronously once
* the buffer reaches `maxBufferByteLength` (default: never, i.e.
* microtask-only).
*
* This lives in its own module, separate from `stream-ops.node`, so that
* consumers outside the render layer (e.g. the dev server's React debug
* channel) can batch Node streams without pulling in `stream-ops.node`'s
* `react-dom/server` and `react-dom/static` imports. Those resolve to the
* vendored React only inside aliased app bundles; loaded from the unaliased
* dev-server runtime they resolve to the app's installed React, which on React
* 18 has no `react-dom/static` export.
*/
export function createNodeBufferedTransformStream(
maxBufferByteLength: number = Infinity
): Transform {
let bufferedChunks: Array<Uint8Array> = []
let bufferByteLength = 0
let flushScheduled = false
function flushBuffered(stream: Transform): void {
if (bufferedChunks.length === 0) return
const merged = new Uint8Array(bufferByteLength)
let copiedBytes = 0
for (let i = 0; i < bufferedChunks.length; i++) {
const bufferedChunk = bufferedChunks[i]
merged.set(bufferedChunk, copiedBytes)
copiedBytes += bufferedChunk.byteLength
}
bufferedChunks.length = 0
bufferByteLength = 0
stream.push(merged)
}
return new Transform({
transform(chunk, _encoding, callback) {
bufferedChunks.push(chunk)
bufferByteLength += chunk.byteLength
if (bufferByteLength >= maxBufferByteLength) {
flushBuffered(this)
} else if (!flushScheduled) {
flushScheduled = true
queueMicrotask(() => {
flushScheduled = false
flushBuffered(this)
})
}
callback()
},
flush(callback) {
flushBuffered(this)
callback()
},
})
}
...@@ -75,13 +75,17 @@ const clientBoundary = { ...@@ -75,13 +75,17 @@ const clientBoundary = {
const maybeServerAction = const maybeServerAction =
propName === 'action' || /.+Action$/.test(propName) propName === 'action' || /.+Action$/.test(propName)
// There's a special case for the error file that the `reset` prop // There's a special case for error files where the
// is allowed to be a function: // framework-injected `reset` and `unstable_retry` props are
// allowed to be functions. Next.js provides these props, the
// user doesn't pass them, so they don't need to be
// serializable.
// https://github.com/vercel/next.js/issues/46573 // https://github.com/vercel/next.js/issues/46573
const isErrorReset = const isErrorBoundaryFunctionProp =
(isErrorFile || isGlobalErrorFile) && propName === 'reset' (isErrorFile || isGlobalErrorFile) &&
(propName === 'reset' || propName === 'unstable_retry')
if (!maybeServerAction && !isErrorReset) { if (!maybeServerAction && !isErrorBoundaryFunctionProp) {
diagnostics.push({ diagnostics.push({
file: source, file: source,
category: ts.DiagnosticCategory.Warning, category: ts.DiagnosticCategory.Warning,
......
{ {
"name": "@next/react-refresh-utils", "name": "@next/react-refresh-utils",
"version": "16.3.0-canary.40", "version": "16.3.0-canary.41",
"description": "An experimental package providing utilities for React Refresh.", "description": "An experimental package providing utilities for React Refresh.",
"repository": { "repository": {
"url": "vercel/next.js", "url": "vercel/next.js",
......
{ {
"name": "@next/third-parties", "name": "@next/third-parties",
"version": "16.3.0-canary.40", "version": "16.3.0-canary.41",
"repository": { "repository": {
"url": "vercel/next.js", "url": "vercel/next.js",
"directory": "packages/third-parties" "directory": "packages/third-parties"
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
"third-party-capital": "1.0.20" "third-party-capital": "1.0.20"
}, },
"devDependencies": { "devDependencies": {
"next": "16.3.0-canary.40", "next": "16.3.0-canary.41",
"outdent": "0.8.0", "outdent": "0.8.0",
"prettier": "2.5.1", "prettier": "2.5.1",
"typescript": "6.0.2" "typescript": "6.0.2"
......
...@@ -986,7 +986,7 @@ importers: ...@@ -986,7 +986,7 @@ importers:
packages/eslint-config-next: packages/eslint-config-next:
dependencies: dependencies:
'@next/eslint-plugin-next': '@next/eslint-plugin-next':
specifier: 16.3.0-canary.40 specifier: 16.3.0-canary.41
version: link:../eslint-plugin-next version: link:../eslint-plugin-next
eslint: eslint:
specifier: '>=9.0.0' specifier: '>=9.0.0'
...@@ -1063,7 +1063,7 @@ importers: ...@@ -1063,7 +1063,7 @@ importers:
packages/next: packages/next:
dependencies: dependencies:
'@next/env': '@next/env':
specifier: 16.3.0-canary.40 specifier: 16.3.0-canary.41
version: link:../next-env version: link:../next-env
'@swc/helpers': '@swc/helpers':
specifier: 0.5.15 specifier: 0.5.15
...@@ -1184,19 +1184,19 @@ importers: ...@@ -1184,19 +1184,19 @@ importers:
specifier: 1.2.0 specifier: 1.2.0
version: 1.2.0 version: 1.2.0
'@next/font': '@next/font':
specifier: 16.3.0-canary.40 specifier: 16.3.0-canary.41
version: link:../font version: link:../font
'@next/polyfill-module': '@next/polyfill-module':
specifier: 16.3.0-canary.40 specifier: 16.3.0-canary.41
version: link:../next-polyfill-module version: link:../next-polyfill-module
'@next/polyfill-nomodule': '@next/polyfill-nomodule':
specifier: 16.3.0-canary.40 specifier: 16.3.0-canary.41
version: link:../next-polyfill-nomodule version: link:../next-polyfill-nomodule
'@next/react-refresh-utils': '@next/react-refresh-utils':
specifier: 16.3.0-canary.40 specifier: 16.3.0-canary.41
version: link:../react-refresh-utils version: link:../react-refresh-utils
'@next/swc': '@next/swc':
specifier: 16.3.0-canary.40 specifier: 16.3.0-canary.41
version: link:../next-swc version: link:../next-swc
'@opentelemetry/api': '@opentelemetry/api':
specifier: 1.6.0 specifier: 1.6.0
...@@ -1930,7 +1930,7 @@ importers: ...@@ -1930,7 +1930,7 @@ importers:
version: 1.0.20 version: 1.0.20
devDependencies: devDependencies:
next: next:
specifier: 16.3.0-canary.40 specifier: 16.3.0-canary.41
version: link:../next version: link:../next
outdent: outdent:
specifier: 0.8.0 specifier: 0.8.0
...@@ -16,8 +16,8 @@ The plugin only applies to VSCode so manual testing in VSCode is required. ...@@ -16,8 +16,8 @@ The plugin only applies to VSCode so manual testing in VSCode is required.
`app/client.tsx#ClientComponent` has props that can and can't be serialized. `app/client.tsx#ClientComponent` has props that can and can't be serialized.
Ensure the current comments still describe the observed behavior. Ensure the current comments still describe the observed behavior.
`app/error.tsx#Error` and `app/global-error.tsx#GlobalError` have a `reset` prop `app/error.tsx#Error` and `app/global-error.tsx#GlobalError` have `reset` and
that should be excluded from the serialization check. `unstable_retry` props that should be excluded from the serialization check.
### Client Boundary ### Client Boundary
......
...@@ -5,10 +5,13 @@ import { useEffect } from 'react' ...@@ -5,10 +5,13 @@ import { useEffect } from 'react'
export default function Error({ export default function Error({
error, error,
reset, reset,
//^^^ fine because it's the special reset prop in an error file unstable_retry,
//^^^ `reset` and `unstable_retry` are fine because they are the special
// framework-injected function props in an error file
}: { }: {
error: Error & { digest?: string } error: Error & { digest?: string }
reset: () => void reset: () => void
unstable_retry: () => void
}) { }) {
useEffect(() => { useEffect(() => {
console.error(error) console.error(error)
...@@ -18,6 +21,7 @@ export default function Error({ ...@@ -18,6 +21,7 @@ export default function Error({
<div> <div>
<h2>Something went wrong!</h2> <h2>Something went wrong!</h2>
<button onClick={() => reset()}>Try again</button> <button onClick={() => reset()}>Try again</button>
<button onClick={() => unstable_retry()}>Retry</button>
</div> </div>
) )
} }
...@@ -3,16 +3,20 @@ ...@@ -3,16 +3,20 @@
export default function GlobalError({ export default function GlobalError({
error, error,
reset, reset,
//^^^ fine because it's the special reset prop in a global-error file unstable_retry,
//^^^ `reset` and `unstable_retry` are fine because they are the special
// framework-injected function props in a global-error file
}: { }: {
error: Error & { digest?: string } error: Error & { digest?: string }
reset: () => void reset: () => void
unstable_retry: () => void
}) { }) {
return ( return (
<html> <html>
<body> <body>
<h2>Something went wrong!</h2> <h2>Something went wrong!</h2>
<button onClick={() => reset()}>Try again</button> <button onClick={() => reset()}>Try again</button>
<button onClick={() => unstable_retry()}>Retry</button>
</body> </body>
</html> </html>
) )
......
'use client'
// Error boundaries receive `error`, `reset`, and `unstable_retry` from the
// framework. `reset` and `unstable_retry` are functions, but they are injected
// by Next.js rather than passed by the user, so the client-entry serialization
// rule must not flag them. `_notExempt` is an ordinary function prop and
// must still be flagged, proving the exemption stays scoped to error-boundary
// props.
export default function Error({
error,
reset,
unstable_retry,
_notExempt,
}: {
error: Error & { digest?: string }
reset: () => void
unstable_retry: () => void
_notExempt: () => void
}) {
return (
<div>
<h2>Something went wrong!</h2>
<p>{error.message}</p>
<button onClick={() => reset()}>Reset</button>
<button onClick={() => unstable_retry()}>Try again</button>
<button onClick={() => _notExempt()}>Nope</button>
</div>
)
}
'use client'
// `global-error.tsx` receives the same framework-injected props as `error.tsx`.
// Its function props (`reset`, `unstable_retry`) are provided by Next.js and
// must not be flagged as non-serializable. `_notExempt` is an ordinary function
// prop and must still be flagged, proving the exemption stays scoped to known
// error-boundary props.
export default function GlobalError({
error,
reset,
unstable_retry,
_notExempt,
}: {
error: Error & { digest?: string }
reset: () => void
unstable_retry: () => void
_notExempt: () => void
}) {
return (
<html>
<body>
<h2>Something went wrong!</h2>
<p>{error.message}</p>
<button onClick={() => reset()}>Reset</button>
<button onClick={() => unstable_retry()}>Try again</button>
<button onClick={() => _notExempt()}>Nope</button>
</body>
</html>
)
}
...@@ -2,7 +2,7 @@ import type { PluginLanguageService } from '../test-utils' ...@@ -2,7 +2,7 @@ import type { PluginLanguageService } from '../test-utils'
import ts from 'typescript' import ts from 'typescript'
import { relative, resolve } from 'node:path' import { relative, resolve } from 'node:path'
import { getPluginLanguageService } from '../test-utils' import { getPluginLanguageService, NEXT_TS_ERRORS } from '../test-utils'
type PartialDiagnostic = Pick< type PartialDiagnostic = Pick<
ts.Diagnostic, ts.Diagnostic,
...@@ -118,4 +118,46 @@ describe('typescript-plugin - client-boundary', () => { ...@@ -118,4 +118,46 @@ describe('typescript-plugin - client-boundary', () => {
} }
`) `)
}) })
it('should not flag framework-injected function props in error files', () => {
const tsFile = resolve(__dirname, 'app/error.tsx')
const flaggedProps = languageService
.getSemanticDiagnostics(tsFile)
.filter(
(diagnostic) =>
diagnostic.code === NEXT_TS_ERRORS.INVALID_CLIENT_ENTRY_PROP
)
.map((diagnostic) => String(diagnostic.messageText))
// `reset` and `unstable_retry` are injected by Next.js into error
// boundaries, so they must not be flagged as non-serializable props.
expect(flaggedProps.some((m) => m.includes('"reset"'))).toBe(false)
expect(flaggedProps.some((m) => m.includes('"unstable_retry"'))).toBe(false)
// The exemption stays scoped to known error-boundary props: an ordinary
// function prop in an error file is still flagged.
expect(flaggedProps.some((m) => m.includes('"_notExempt"'))).toBe(true)
expect(flaggedProps).toHaveLength(1)
})
it('should not flag framework-injected function props in global-error files', () => {
const tsFile = resolve(__dirname, 'app/global-error.tsx')
const flaggedProps = languageService
.getSemanticDiagnostics(tsFile)
.filter(
(diagnostic) =>
diagnostic.code === NEXT_TS_ERRORS.INVALID_CLIENT_ENTRY_PROP
)
.map((diagnostic) => String(diagnostic.messageText))
// `reset` and `unstable_retry` are injected by Next.js into global-error
// boundaries, so they must not be flagged as non-serializable props.
expect(flaggedProps.some((m) => m.includes('"reset"'))).toBe(false)
expect(flaggedProps.some((m) => m.includes('"unstable_retry"'))).toBe(false)
// The exemption stays scoped to known error-boundary props: an ordinary
// function prop in a global-error file is still flagged.
expect(flaggedProps.some((m) => m.includes('"_notExempt"'))).toBe(true)
expect(flaggedProps).toHaveLength(1)
})
}) })
...@@ -33,6 +33,26 @@ export default function Page() { ...@@ -33,6 +33,26 @@ export default function Page() {
</Link> </Link>
</li> </li>
</ul> </ul>
<h2>Static posts</h2>
<p>
These posts are fully prerendered at build time. The same shell concept
applies: navigating to a post whose URL was never prefetched should
still render an instant shell while the per-URL content loads.
</p>
<ul>
<li>
<LinkAccordion href="/static-posts/1">Static post 1</LinkAccordion>
</li>
<li>
<LinkAccordion href="/static-posts/2">Static post 2</LinkAccordion>
</li>
<li>
<Link href="/static-posts/124" prefetch={false}>
Unprefetched static post
</Link>
</li>
</ul>
</main> </main>
) )
} }
import { Suspense } from 'react'
type Params = { id: string }
// This page is fully static: no cookies, no `connection()`, no other dynamic
// data. All params are statically known via `generateStaticParams`, so the
// page is prerendered for every URL at build time. With default prefetch
// behavior, the client requests the full prerender; the response carries a
// shell byte offset (`a`) and we extract the shell prefix to cache at the
// Fallback vary path.
export async function generateStaticParams() {
return [{ id: '1' }, { id: '2' }, { id: '124' }, { id: '125' }]
}
export default function Page({ params }: { params: Promise<Params> }) {
return (
<main>
<Suspense fallback={<p id="static-shell">App shell for static posts</p>}>
<ParamsDependent params={params} />
</Suspense>
</main>
)
}
async function ParamsDependent({ params }: { params: Promise<Params> }) {
const { id } = await params
return <p id="static-content">{`Static post ${id}`}</p>
}
...@@ -132,4 +132,57 @@ describe('App Shell prefetching', () => { ...@@ -132,4 +132,57 @@ describe('App Shell prefetching', () => {
.click() .click()
}, [{ includes: 'Post 2' }]) }, [{ includes: 'Post 2' }])
}) })
it('extracts the App Shell from a fully-static prerender response', async () => {
// The /static-posts/[id] route is fully static: all params are known via
// `generateStaticParams` and the page accesses no other dynamic data, so
// each URL is prerendered at build time. When the client prefetches one
// URL, it receives the full prerender; the client extracts the shell
// prefix (using the byte offset in the response) and caches it at the
// Fallback vary path, so that navigations to OTHER URLs in the same
// route still get an instant shell before the per-URL content arrives.
let page: Playwright.Page
const browser = await next.browser('/', {
beforePageLoad(p: Playwright.Page) {
page = p
},
})
const act = createRouterAct(page)
// Reveal the LinkAccordion for /static-posts/1. Two prefetch responses
// fire: one for the per-segment static prefetch of /static-posts/1
// (which contains the resolved page content + the shell above the
// params boundary), and one for the runtime shell prefetch (which the
// server may return either as a truncated shell or as the full
// prerender that the client extracts a shell prefix from). Both
// responses contain the "App shell for static posts" substring.
await act(async () => {
await browser
.elementByCss('input[data-link-accordion="/static-posts/1"]')
.click()
}, [
{ includes: 'App shell for static posts' },
{ includes: 'App shell for static posts' },
])
// Click the link to /static-posts/124 — a different param than what
// was prefetched, rendered with prefetch={false}. The cached App
// Shell should render immediately, before the per-URL navigation
// response arrives.
await act(async () => {
await browser.elementByCss('a[href="/static-posts/124"]').click()
// While the navigation response is blocked (we're still in the
// `act` block), the cached App Shell should already be visible.
expect(await browser.elementById('static-shell').text()).toEqual(
'App shell for static posts'
)
})
// After the outer act unblocks the navigation, the per-URL content
// streams in.
expect(await browser.elementById('static-content').text()).toEqual(
'Static post 124'
)
})
}) })
...@@ -11,13 +11,13 @@ describe('New Link Behavior with material-ui', () => { ...@@ -11,13 +11,13 @@ describe('New Link Behavior with material-ui', () => {
'next.config.js': new FileRef(path.join(appDir, 'next.config.js')), 'next.config.js': new FileRef(path.join(appDir, 'next.config.js')),
}, },
dependencies: { dependencies: {
'@emotion/cache': 'latest', '@emotion/cache': '11.10.5',
'@emotion/react': 'latest', '@emotion/react': '11.10.6',
'@emotion/server': 'latest', '@emotion/server': '11.10.0',
'@emotion/styled': 'latest', '@emotion/styled': '11.10.6',
'@mui/icons-material': 'latest', '@mui/icons-material': '5.11.16',
'@mui/material': 'latest', '@mui/material': '5.11.16',
'prop-types': 'latest', 'prop-types': '15.8.1',
}, },
}) })
......
...@@ -14,7 +14,7 @@ It supports having multiple key families, which are stored in separate files, bu ...@@ -14,7 +14,7 @@ It supports having multiple key families, which are stored in separate files, bu
There is a single `CURRENT` file which stores the latest committed sequence number. There is a single `CURRENT` file which stores the latest committed sequence number.
All other files have a sequence number as file name, e. g. `0000123.sst`. All files are immutable once there sequence number is <= the committed sequence number. But they might be deleted when they are superseeded by other committed files. All other files have a sequence number as file name, e. g. `0000123.sst`. All files are immutable once their sequence number is <= the committed sequence number. But they might be deleted when they are superseded by other committed files.
There are four different file types: There are four different file types:
...@@ -102,7 +102,7 @@ Each block stores a 4-byte CRC32 checksum (big-endian) computed on the **on-disk ...@@ -102,7 +102,7 @@ Each block stores a 4-byte CRC32 checksum (big-endian) computed on the **on-disk
- 8 bytes hash - 8 bytes hash
- 2 bytes block index - 2 bytes block index
An Index block contains `n` 8 bytes hashes, which specify `n - 1` hash ranges (eq hash goes into the prev range, except for the first key). Between these `n` hashes there are `n - 1` 2 byte block indicies that point to the block that contains the hash range. An Index block contains `n` 8 bytes hashes, which specify `n - 1` hash ranges (eq hash goes into the prev range, except for the first key). Between these `n` hashes there are `n - 1` 2 byte block indices that point to the block that contains the hash range.
The hashes are sorted. The hashes are sorted.
...@@ -222,7 +222,7 @@ For compaction we compute the "coverage" of the SST files. The coverage is the a ...@@ -222,7 +222,7 @@ For compaction we compute the "coverage" of the SST files. The coverage is the a
For a single SST file we can compute `(max_hash - min_hash) / u64::MAX` as the coverage of the SST file. We sum up all these coverages to get the total coverage. For a single SST file we can compute `(max_hash - min_hash) / u64::MAX` as the coverage of the SST file. We sum up all these coverages to get the total coverage.
Compaction chooses a few SST files and runs the merge step of merge sort on tham to create a few new SST files with sorted ranges. Compaction chooses a few SST files and runs the merge step of merge sort on them to create a few new SST files with sorted ranges.
Example: Example:
...@@ -305,7 +305,7 @@ DEL 17: (2, 3, 4, 5, 6, 7, 8, 9) ...@@ -305,7 +305,7 @@ DEL 17: (2, 3, 4, 5, 6, 7, 8, 9)
CURRENT: 17 CURRENT: 17
``` ```
Configuration options for compations are: Configuration options for compactions are:
- max number of SST files that are merged at once - max number of SST files that are merged at once
- coverage when compaction is triggered (otherwise calling compact is a noop) - coverage when compaction is triggered (otherwise calling compact is a noop)
......
...@@ -11,6 +11,7 @@ use turbopack_core::{ ...@@ -11,6 +11,7 @@ use turbopack_core::{
ChunkingConfig, ChunkingConfigs, ChunkingContext, ContentHashing, CrossOrigin, ChunkingConfig, ChunkingConfigs, ChunkingContext, ContentHashing, CrossOrigin,
EntryChunkGroupResult, EvaluatableAsset, EvaluatableAssets, MinifyType, EntryChunkGroupResult, EvaluatableAsset, EvaluatableAssets, MinifyType,
SourceMapSourceType, SourceMapsType, UnusedReferences, UrlBehavior, SourceMapSourceType, SourceMapsType, UnusedReferences, UrlBehavior,
WorkerConfigurationOptions,
availability_info::AvailabilityInfo, availability_info::AvailabilityInfo,
chunk_group::{MakeChunkGroupResult, make_chunk_group}, chunk_group::{MakeChunkGroupResult, make_chunk_group},
chunk_id_strategy::ModuleIdStrategy, chunk_id_strategy::ModuleIdStrategy,
...@@ -475,14 +476,6 @@ impl BrowserChunkingContext { ...@@ -475,14 +476,6 @@ impl BrowserChunkingContext {
Vc::cell(self.chunk_base_path.clone()) Vc::cell(self.chunk_base_path.clone())
} }
/// Returns the worker base-path override. When `Some`, takes precedence
/// over `chunk_base_path` for the entrypoint URL and the module chunks
/// loaded inside the worker.
#[turbo_tasks::function]
pub fn worker_asset_prefix(&self) -> Vc<Option<RcStr>> {
Vc::cell(self.worker_asset_prefix.clone())
}
/// Returns the asset suffix path. /// Returns the asset suffix path.
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn asset_suffix(&self) -> Vc<AssetSuffix> { pub fn asset_suffix(&self) -> Vc<AssetSuffix> {
...@@ -1014,15 +1007,19 @@ impl ChunkingContext for BrowserChunkingContext { ...@@ -1014,15 +1007,19 @@ impl ChunkingContext for BrowserChunkingContext {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
fn worker_forwarded_globals(&self) -> Vc<Vec<RcStr>> { fn worker_configuration_options(&self) -> Vc<WorkerConfigurationOptions> {
Vc::cell(self.worker_forwarded_globals.clone()) WorkerConfigurationOptions {
asset_prefix: self.worker_asset_prefix.clone(),
forwarded_globals: self.worker_forwarded_globals.clone(),
}
.cell()
} }
#[turbo_tasks::function] #[turbo_tasks::function]
async fn worker_entrypoint(self: Vc<Self>) -> Result<Vc<Box<dyn OutputAsset>>> { async fn worker_entrypoint(self: Vc<Self>) -> Result<Vc<Box<dyn OutputAsset>>> {
let chunking_context: Vc<Box<dyn ChunkingContext>> = Vc::upcast(self); let chunking_context: Vc<Box<dyn ChunkingContext>> = Vc::upcast(self);
let resolved = chunking_context.to_resolved().await?; let resolved = chunking_context.to_resolved().await?;
let forwarded_globals = chunking_context.worker_forwarded_globals(); let forwarded_globals = Vc::cell(self.await?.worker_forwarded_globals.clone());
let entrypoint = EcmascriptBrowserWorkerEntrypoint::new(*resolved, forwarded_globals); let entrypoint = EcmascriptBrowserWorkerEntrypoint::new(*resolved, forwarded_globals);
Ok(Vc::upcast(entrypoint)) Ok(Vc::upcast(entrypoint))
} }
......
...@@ -174,19 +174,25 @@ impl EcmascriptBrowserEvaluateChunk { ...@@ -174,19 +174,25 @@ impl EcmascriptBrowserEvaluateChunk {
let asset_context = turbopack::get_runtime_asset_context(environment); let asset_context = turbopack::get_runtime_asset_context(environment);
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
// is per-page. To keep the shared `runtime.js` stable, always include the machinery.
let has_async_modules = if matches!(runtime_type, RuntimeType::Production) {
!this.module_graph.async_module_info().await?.is_empty()
} else {
true
};
match runtime_type { match runtime_type {
RuntimeType::Production | RuntimeType::Development => { RuntimeType::Production | RuntimeType::Development => {
let runtime_code = turbopack_ecmascript_runtime::get_browser_runtime_code( let runtime_code = turbopack_ecmascript_runtime::get_browser_runtime_code(
asset_context, asset_context,
this.chunking_context.chunk_base_path(), this.chunking_context.chunk_base_path(),
this.chunking_context.worker_asset_prefix(),
this.chunking_context.asset_suffix(), this.chunking_context.asset_suffix(),
this.chunking_context.worker_forwarded_globals(),
runtime_type, runtime_type,
output_root_to_root_path, output_root_to_root_path,
source_maps, source_maps,
this.chunking_context.chunk_loading_global(), this.chunking_context.chunk_loading_global(),
this.chunking_context.cross_origin(), this.chunking_context.cross_origin(),
has_async_modules,
); );
code.push_code(&*runtime_code.await?); code.push_code(&*runtime_code.await?);
} }
......
...@@ -279,6 +279,19 @@ pub enum SourceMapSourceType { ...@@ -279,6 +279,19 @@ pub enum SourceMapSourceType {
#[turbo_tasks::value(transparent, cell = "keyed")] #[turbo_tasks::value(transparent, cell = "keyed")]
pub struct UnusedReferences(FxHashSet<ResolvedVc<Box<dyn ModuleReference>>>); pub struct UnusedReferences(FxHashSet<ResolvedVc<Box<dyn ModuleReference>>>);
#[turbo_tasks::value(shared)]
#[derive(Debug, Clone, Default)]
pub struct WorkerConfigurationOptions {
/// The worker base-path override. When `Some`, takes precedence over
/// `chunk_base_path` for the worker entrypoint URL and the module chunks
/// loaded inside the worker.
pub asset_prefix: Option<RcStr>,
/// The list of global variable names to forward to workers. These globals
/// are read from `globalThis` at worker creation time and passed to the
/// worker via URL params.
pub forwarded_globals: Vec<RcStr>,
}
/// A context for the chunking that influences the way chunks are created /// A context for the chunking that influences the way chunks are created
#[turbo_tasks::value_trait] #[turbo_tasks::value_trait]
pub trait ChunkingContext { pub trait ChunkingContext {
...@@ -481,12 +494,11 @@ pub trait ChunkingContext { ...@@ -481,12 +494,11 @@ pub trait ChunkingContext {
#[turbo_tasks::function] #[turbo_tasks::function]
fn debug_ids_enabled(self: Vc<Self>) -> Vc<bool>; fn debug_ids_enabled(self: Vc<Self>) -> Vc<bool>;
/// Returns the list of global variable names to forward to workers. /// Returns the worker-related configuration: the base-path override and the
/// These globals are read from globalThis at worker creation time and passed /// list of globals to forward to workers.
/// to the worker via URL params.
#[turbo_tasks::function] #[turbo_tasks::function]
fn worker_forwarded_globals(self: Vc<Self>) -> Vc<Vec<RcStr>> { fn worker_configuration_options(self: Vc<Self>) -> Vc<WorkerConfigurationOptions> {
Vc::cell(vec![]) WorkerConfigurationOptions::default().cell()
} }
/// Returns the worker entrypoint for this chunking context. /// Returns the worker entrypoint for this chunking context.
......
...@@ -30,6 +30,7 @@ pub use crate::chunk::{ ...@@ -30,6 +30,7 @@ pub use crate::chunk::{
AssetSuffix, ChunkGroupResult, ChunkGroupType, ChunkingConfig, ChunkingConfigs, AssetSuffix, ChunkGroupResult, ChunkGroupType, ChunkingConfig, ChunkingConfigs,
ChunkingContext, ChunkingContextExt, EntryChunkGroupResult, MangleType, MinifyType, ChunkingContext, ChunkingContextExt, EntryChunkGroupResult, MangleType, MinifyType,
SourceMapSourceType, SourceMapsType, UnusedReferences, UrlBehavior, SourceMapSourceType, SourceMapsType, UnusedReferences, UrlBehavior,
WorkerConfigurationOptions,
}, },
data::{ChunkData, ChunkDataOption, ChunksData}, data::{ChunkData, ChunkDataOption, ChunksData},
evaluate::{EvaluatableAsset, EvaluatableAssetExt, EvaluatableAssets}, evaluate::{EvaluatableAsset, EvaluatableAssetExt, EvaluatableAssets},
......
...@@ -20,21 +20,8 @@ declare var TURBOPACK_NEXT_CHUNK_URLS: ChunkUrl[] | undefined ...@@ -20,21 +20,8 @@ declare var TURBOPACK_NEXT_CHUNK_URLS: ChunkUrl[] | undefined
// Injected by rust code // Injected by rust code
declare var CHUNK_BASE_PATH: string declare var CHUNK_BASE_PATH: string
/**
* Custom base path for Web Worker URLs (the entrypoint and the module
* chunks loaded inside the worker). Mirrors webpack's
* `output.workerPublicPath`. `null` means "use CHUNK_BASE_PATH"; an empty
* string is a literal empty prefix (not a fallback).
*
* The worker's bootstrap rejects module chunks whose origin differs from
* the worker's own origin, so the override has to apply to both — using
* `WORKER_BASE_PATH` only for the entrypoint would leave the worker unable
* to load any chunks when `CHUNK_BASE_PATH` is on a different origin.
*/
declare var WORKER_BASE_PATH: string | null
declare var ASSET_SUFFIX: string declare var ASSET_SUFFIX: string
declare var CROSS_ORIGIN: 'anonymous' | 'use-credentials' | null declare var CROSS_ORIGIN: 'anonymous' | 'use-credentials' | null
declare var WORKER_FORWARDED_GLOBALS: string[]
interface TurbopackBrowserBaseContext<M> extends TurbopackBaseContext<M> { interface TurbopackBrowserBaseContext<M> extends TurbopackBaseContext<M> {
R: ResolvePathFromModule R: ResolvePathFromModule
...@@ -315,62 +302,6 @@ function exportUrl( ...@@ -315,62 +302,6 @@ function exportUrl(
} }
browserContextPrototype.q = exportUrl browserContextPrototype.q = exportUrl
/**
* Creates a worker by instantiating the given WorkerConstructor with the
* appropriate URL and options.
*
* The entrypoint is a pre-compiled worker runtime file. The params configure
* which module chunks to load and which module to run as the entry point.
*
* The params are a JSON array of the following structure:
* `[TURBOPACK_NEXT_CHUNK_URLS, ASSET_SUFFIX, ...WORKER_FORWARDED_GLOBALS values]`
*
* @param WorkerConstructor The Worker or SharedWorker constructor
* @param entrypoint URL path to the worker entrypoint chunk
* @param moduleChunks list of module chunk paths to load
* @param workerOptions options to pass to the Worker constructor (optional)
*/
function createWorker(
WorkerConstructor: { new (url: URL, options?: object): Worker },
entrypoint: ChunkPath,
moduleChunks: ChunkPath[],
workerOptions?: object
): Worker {
const isSharedWorker = WorkerConstructor.name === 'SharedWorker'
// `WORKER_BASE_PATH` overrides `CHUNK_BASE_PATH` for the entrypoint and the
// module chunks loaded inside the worker, keeping them same-origin to each
// other when `CHUNK_BASE_PATH` (= `assetPrefix`) is a cross-origin CDN.
// `null` falls back; an empty string is treated as a literal empty prefix.
const workerBasePath = WORKER_BASE_PATH ?? CHUNK_BASE_PATH
const chunkUrls = moduleChunks
.map((chunk) => getChunkRelativeUrl(chunk, workerBasePath))
.reverse()
const params: unknown[] = [chunkUrls, ASSET_SUFFIX]
for (const globalName of WORKER_FORWARDED_GLOBALS) {
params.push((globalThis as Record<string, unknown>)[globalName])
}
const url = new URL(
getChunkRelativeUrl(entrypoint, workerBasePath),
location.origin
)
const paramsJson = JSON.stringify(params)
if (isSharedWorker) {
url.searchParams.set('params', paramsJson)
} else {
url.hash = '#params=' + encodeURIComponent(paramsJson)
}
// Remove type: "module" from options since our worker entrypoint is not a module
const options = workerOptions
? { ...workerOptions, type: undefined }
: undefined
return new WorkerConstructor(url, options)
}
browserContextPrototype.b = createWorker
/** /**
* Instantiates a runtime module. * Instantiates a runtime module.
*/ */
...@@ -393,6 +324,15 @@ function getChunkRelativeUrl( ...@@ -393,6 +324,15 @@ function getChunkRelativeUrl(
.join('/')}${ASSET_SUFFIX}` as ChunkUrl .join('/')}${ASSET_SUFFIX}` as ChunkUrl
} }
// Shared runtime primitives consumed by the bundled `createWorker` helper,
// exposed as `__turbopack_chunk_base_path__` and `__turbopack_chunk_asset_suffix__`.
browserContextPrototype.b = CHUNK_BASE_PATH as ChunkBasePath
browserContextPrototype.X = ASSET_SUFFIX as AssetSuffix
// Shared runtime primitive: build a chunk's URL. Used by the bundled worker
// helper and the WASM helper, exposed as `__turbopack_chunk_relative_url__`.
browserContextPrototype.h = getChunkRelativeUrl
/** /**
* Return the ChunkPath from a ChunkScript. * Return the ChunkPath from a ChunkScript.
*/ */
......
...@@ -27,7 +27,6 @@ nodeContextPrototype.M = moduleFactories ...@@ -27,7 +27,6 @@ nodeContextPrototype.M = moduleFactories
// Cast moduleCache to ModuleWithDirection for production mode // Cast moduleCache to ModuleWithDirection for production mode
nodeContextPrototype.c = moduleCache as ModuleCache<ModuleWithDirection> nodeContextPrototype.c = moduleCache as ModuleCache<ModuleWithDirection>
nodeContextPrototype.R = resolvePathFromModule nodeContextPrototype.R = resolvePathFromModule
nodeContextPrototype.b = createWorker
nodeContextPrototype.C = clearChunkCache nodeContextPrototype.C = clearChunkCache
function instantiateModule( function instantiateModule(
......
...@@ -36,7 +36,6 @@ nodeDevContextPrototype.q = exportUrl ...@@ -36,7 +36,6 @@ nodeDevContextPrototype.q = exportUrl
nodeDevContextPrototype.M = moduleFactories nodeDevContextPrototype.M = moduleFactories
nodeDevContextPrototype.c = devModuleCache nodeDevContextPrototype.c = devModuleCache
nodeDevContextPrototype.R = resolvePathFromModule nodeDevContextPrototype.R = resolvePathFromModule
nodeDevContextPrototype.b = createWorker
nodeDevContextPrototype.C = clearChunkCache nodeDevContextPrototype.C = clearChunkCache
/** /**
......
...@@ -164,38 +164,6 @@ function loadWebAssemblyModule( ...@@ -164,38 +164,6 @@ function loadWebAssemblyModule(
} }
contextPrototype.u = loadWebAssemblyModule contextPrototype.u = loadWebAssemblyModule
/**
* Creates a Node.js worker thread by instantiating the given WorkerConstructor
* with the appropriate path and options, including forwarded globals.
*
* @param WorkerConstructor The Worker constructor from worker_threads
* @param workerPath Path to the worker entry chunk
* @param workerOptions options to pass to the Worker constructor (optional)
*/
function createWorker(
WorkerConstructor: { new (path: string, options?: object): unknown },
workerPath: string,
workerOptions?: { workerData?: unknown; [key: string]: unknown }
): unknown {
// Build the forwarded globals object
const forwardedGlobals: Record<string, unknown> = {}
for (const name of WORKER_FORWARDED_GLOBALS) {
forwardedGlobals[name] = (globalThis as Record<string, unknown>)[name]
}
// Merge workerData with forwarded globals
const existingWorkerData = workerOptions?.workerData || {}
const options = {
...workerOptions,
workerData: {
...(typeof existingWorkerData === 'object' ? existingWorkerData : {}),
__turbopack_globals__: forwardedGlobals,
},
}
return new WorkerConstructor(workerPath, options)
}
const regexJsUrl = /\.js(?:\?[^#]*)?(?:#.*)?$/ const regexJsUrl = /\.js(?:\?[^#]*)?(?:#.*)?$/
/** /**
* Checks if a given path/URL ends with .js, optionally followed by ?query or #fragment. * Checks if a given path/URL ends with .js, optionally followed by ?query or #fragment.
......
/// <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')
const enum QueueStatus {
Unknown = -1,
Unresolved = 0,
Resolved = 1,
}
type AsyncQueueFn = (() => void) & { queueCount: number }
type AsyncQueue = AsyncQueueFn[] & {
status: QueueStatus
}
type Dep = Exports | AsyncModulePromise | Promise<Exports>
type AsyncModuleExt = {
[turbopackQueues]: (fn: (queue: AsyncQueue) => void) => void
[turbopackExports]: Exports
[turbopackError]?: any
}
type AsyncModulePromise<T = Exports> = Promise<T> & AsyncModuleExt
function isPromise<T = any>(maybePromise: any): maybePromise is Promise<T> {
return (
maybePromise != null &&
typeof maybePromise === 'object' &&
'then' in maybePromise &&
typeof maybePromise.then === 'function'
)
}
function isAsyncModuleExt<T extends {}>(obj: T): obj is AsyncModuleExt & T {
return turbopackQueues in obj
}
function createPromise<T>() {
let resolve: (value: T | PromiseLike<T>) => void
let reject: (reason?: any) => void
const promise = new Promise<T>((res, rej) => {
reject = rej
resolve = res
})
return {
promise,
resolve: resolve!,
reject: reject!,
}
}
function resolveQueue(queue?: AsyncQueue) {
if (queue && queue.status !== QueueStatus.Resolved) {
queue.status = QueueStatus.Resolved
queue.forEach((fn) => fn.queueCount--)
queue.forEach((fn) => (fn.queueCount-- ? fn.queueCount++ : fn()))
}
}
function wrapDeps(deps: Dep[]): AsyncModuleExt[] {
return deps.map((dep): AsyncModuleExt => {
if (dep !== null && typeof dep === 'object') {
if (isAsyncModuleExt(dep)) return dep
if (isPromise(dep)) {
const queue: AsyncQueue = Object.assign([], {
status: QueueStatus.Unresolved,
})
const obj: AsyncModuleExt = {
[turbopackExports]: {},
[turbopackQueues]: (fn: (queue: AsyncQueue) => void) => fn(queue),
}
dep.then(
(res) => {
obj[turbopackExports] = res
resolveQueue(queue)
},
(err) => {
obj[turbopackError] = err
resolveQueue(queue)
}
)
return obj
}
}
return {
[turbopackExports]: dep,
[turbopackQueues]: () => {},
}
})
}
function asyncModule(
this: TurbopackBaseContext<Module>,
body: (
handleAsyncDependencies: (
deps: Dep[]
) => Exports[] | Promise<() => Exports[]>,
asyncResult: (err?: any) => void
) => void,
hasAwait: boolean
) {
const module = this.m
const queue: AsyncQueue | undefined = hasAwait
? Object.assign([], { status: QueueStatus.Unknown })
: undefined
const depQueues: Set<AsyncQueue> = new Set()
const { resolve, reject, promise: rawPromise } = createPromise<Exports>()
const promise: AsyncModulePromise = Object.assign(rawPromise, {
[turbopackExports]: module.exports,
[turbopackQueues]: (fn) => {
queue && fn(queue)
depQueues.forEach(fn)
promise['catch'](() => {})
},
} satisfies AsyncModuleExt)
const attributes: PropertyDescriptor = {
get(): any {
return promise
},
set(v: any) {
// Calling `esmExport` leads to this.
if (v !== promise) {
promise[turbopackExports] = v
}
},
}
Object.defineProperty(module, 'exports', attributes)
Object.defineProperty(module, 'namespaceObject', attributes)
function handleAsyncDependencies(deps: Dep[]) {
const currentDeps = wrapDeps(deps)
const getResult = () =>
currentDeps.map((d) => {
if (d[turbopackError]) throw d[turbopackError]
return d[turbopackExports]
})
const { promise, resolve } = createPromise<() => Exports[]>()
const fn: AsyncQueueFn = Object.assign(() => resolve(getResult), {
queueCount: 0,
})
function fnQueue(q: AsyncQueue) {
if (q !== queue && !depQueues.has(q)) {
depQueues.add(q)
if (q && q.status === QueueStatus.Unresolved) {
fn.queueCount++
q.push(fn)
}
}
}
currentDeps.map((dep) => dep[turbopackQueues](fnQueue))
return fn.queueCount ? promise : getResult()
}
function asyncResult(err?: any) {
if (err) {
reject((promise[turbopackError] = err))
} else {
resolve(promise[turbopackExports])
}
resolveQueue(queue)
}
body(handleAsyncDependencies, asyncResult)
if (queue && queue.status === QueueStatus.Unknown) {
queue.status = QueueStatus.Unresolved
}
}
contextPrototype.a = asyncModule
...@@ -21,6 +21,14 @@ type ChunkScript = CurrentScript & { readonly brand: unique symbol } ...@@ -21,6 +21,14 @@ type ChunkScript = CurrentScript & { readonly brand: unique symbol }
* CHUNK_SUFFIX), e.g. `/_next/static/chunks/21a106126841c540.js?dpl=1123123` * CHUNK_SUFFIX), e.g. `/_next/static/chunks/21a106126841c540.js?dpl=1123123`
*/ */
type ChunkUrl = string & { readonly brand: unique symbol } type ChunkUrl = string & { readonly brand: unique symbol }
/**
* Stores CHUNK_BASE_PATH.
*/
type ChunkBasePath = string & { readonly brand: unique symbol }
/**
* Stores ASSET_SUFFIX.
*/
type AssetSuffix = string & { readonly brand: unique symbol }
/** The dependency specifier when importing externals */ /** The dependency specifier when importing externals */
type DependencySpecifier = string type DependencySpecifier = string
/** This is a string in development and a number in production (both arbitrary, implementation defined) */ /** This is a string in development and a number in production (both arbitrary, implementation defined) */
...@@ -41,6 +49,11 @@ type ChunkData = ...@@ -41,6 +49,11 @@ type ChunkData =
moduleChunks: ChunkPath[] moduleChunks: ChunkPath[]
} }
type GetChunkRelativeURL = (
chunkPath: ChunkPath | ChunkListPath,
basePath?: string
) => ChunkUrl
type CommonJsRequire = (moduleId: ModuleId) => Exports type CommonJsRequire = (moduleId: ModuleId) => Exports
type RuntimeRequire = (request: string) => Exports type RuntimeRequire = (request: string) => Exports
type ModuleContextFactory = (map: ModuleContextMap) => ModuleContext type ModuleContextFactory = (map: ModuleContextMap) => ModuleContext
...@@ -98,11 +111,6 @@ type AsyncModule = ( ...@@ -98,11 +111,6 @@ type AsyncModule = (
type ResolveAbsolutePath = (modulePath?: string) => string type ResolveAbsolutePath = (modulePath?: string) => string
type ResolveFileUrl = (modulePath?: string) => string type ResolveFileUrl = (modulePath?: string) => string
type GetWorkerURL = (
entrypoint: ChunkPath,
moduleChunks: ChunkPath[],
shared: boolean
) => URL
type ExternalRequire = ( type ExternalRequire = (
id: DependencySpecifier, id: DependencySpecifier,
...@@ -146,12 +154,14 @@ interface TurbopackBaseContext<M> { ...@@ -146,12 +154,14 @@ interface TurbopackBaseContext<M> {
M: ModuleFactories M: ModuleFactories
l: LoadChunk l: LoadChunk
L: LoadChunkByUrl L: LoadChunkByUrl
h: GetChunkRelativeURL
w: LoadWebAssembly w: LoadWebAssembly
u: LoadWebAssemblyModule u: LoadWebAssemblyModule
P: ResolveAbsolutePath P: ResolveAbsolutePath
F: ResolveFileUrl F: ResolveFileUrl
U: RelativeURL U: RelativeURL
b: GetWorkerURL b: ChunkBasePath
X: AssetSuffix
x: ExternalRequire x: ExternalRequire
y: ExternalImport y: ExternalImport
z: CommonJsRequire z: CommonJsRequire
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
/* eslint-disable @typescript-eslint/no-unused-vars */ /* eslint-disable @typescript-eslint/no-unused-vars */
/// <reference path="./runtime-types.d.ts" /> /// <reference path="./runtime-types.d.ts" />
/// <reference path="./async-module.ts" />
type EsmNamespaceObject = Record<string, any> type EsmNamespaceObject = Record<string, any>
...@@ -508,35 +509,6 @@ function getChunkPath(chunkData: ChunkData): ChunkPath { ...@@ -508,35 +509,6 @@ function getChunkPath(chunkData: ChunkData): ChunkPath {
return typeof chunkData === 'string' ? chunkData : chunkData.path return typeof chunkData === 'string' ? chunkData : chunkData.path
} }
function isPromise<T = any>(maybePromise: any): maybePromise is Promise<T> {
return (
maybePromise != null &&
typeof maybePromise === 'object' &&
'then' in maybePromise &&
typeof maybePromise.then === 'function'
)
}
function isAsyncModuleExt<T extends {}>(obj: T): obj is AsyncModuleExt & T {
return turbopackQueues in obj
}
function createPromise<T>() {
let resolve: (value: T | PromiseLike<T>) => void
let reject: (reason?: any) => void
const promise = new Promise<T>((res, rej) => {
reject = rej
resolve = res
})
return {
promise,
resolve: resolve!,
reject: reject!,
}
}
// Load the CompressedmoduleFactories of a chunk into the `moduleFactories` Map. // Load the CompressedmoduleFactories of a chunk into the `moduleFactories` Map.
// The CompressedModuleFactories format is // The CompressedModuleFactories format is
// - 1 or more module ids // - 1 or more module ids
...@@ -597,169 +569,6 @@ function installCompressedModuleFactories( ...@@ -597,169 +569,6 @@ function installCompressedModuleFactories(
} }
} }
// 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')
const enum QueueStatus {
Unknown = -1,
Unresolved = 0,
Resolved = 1,
}
type AsyncQueueFn = (() => void) & { queueCount: number }
type AsyncQueue = AsyncQueueFn[] & {
status: QueueStatus
}
function resolveQueue(queue?: AsyncQueue) {
if (queue && queue.status !== QueueStatus.Resolved) {
queue.status = QueueStatus.Resolved
queue.forEach((fn) => fn.queueCount--)
queue.forEach((fn) => (fn.queueCount-- ? fn.queueCount++ : fn()))
}
}
type Dep = Exports | AsyncModulePromise | Promise<Exports>
type AsyncModuleExt = {
[turbopackQueues]: (fn: (queue: AsyncQueue) => void) => void
[turbopackExports]: Exports
[turbopackError]?: any
}
type AsyncModulePromise<T = Exports> = Promise<T> & AsyncModuleExt
function wrapDeps(deps: Dep[]): AsyncModuleExt[] {
return deps.map((dep): AsyncModuleExt => {
if (dep !== null && typeof dep === 'object') {
if (isAsyncModuleExt(dep)) return dep
if (isPromise(dep)) {
const queue: AsyncQueue = Object.assign([], {
status: QueueStatus.Unresolved,
})
const obj: AsyncModuleExt = {
[turbopackExports]: {},
[turbopackQueues]: (fn: (queue: AsyncQueue) => void) => fn(queue),
}
dep.then(
(res) => {
obj[turbopackExports] = res
resolveQueue(queue)
},
(err) => {
obj[turbopackError] = err
resolveQueue(queue)
}
)
return obj
}
}
return {
[turbopackExports]: dep,
[turbopackQueues]: () => {},
}
})
}
function asyncModule(
this: TurbopackBaseContext<Module>,
body: (
handleAsyncDependencies: (
deps: Dep[]
) => Exports[] | Promise<() => Exports[]>,
asyncResult: (err?: any) => void
) => void,
hasAwait: boolean
) {
const module = this.m
const queue: AsyncQueue | undefined = hasAwait
? Object.assign([], { status: QueueStatus.Unknown })
: undefined
const depQueues: Set<AsyncQueue> = new Set()
const { resolve, reject, promise: rawPromise } = createPromise<Exports>()
const promise: AsyncModulePromise = Object.assign(rawPromise, {
[turbopackExports]: module.exports,
[turbopackQueues]: (fn) => {
queue && fn(queue)
depQueues.forEach(fn)
promise['catch'](() => {})
},
} satisfies AsyncModuleExt)
const attributes: PropertyDescriptor = {
get(): any {
return promise
},
set(v: any) {
// Calling `esmExport` leads to this.
if (v !== promise) {
promise[turbopackExports] = v
}
},
}
Object.defineProperty(module, 'exports', attributes)
Object.defineProperty(module, 'namespaceObject', attributes)
function handleAsyncDependencies(deps: Dep[]) {
const currentDeps = wrapDeps(deps)
const getResult = () =>
currentDeps.map((d) => {
if (d[turbopackError]) throw d[turbopackError]
return d[turbopackExports]
})
const { promise, resolve } = createPromise<() => Exports[]>()
const fn: AsyncQueueFn = Object.assign(() => resolve(getResult), {
queueCount: 0,
})
function fnQueue(q: AsyncQueue) {
if (q !== queue && !depQueues.has(q)) {
depQueues.add(q)
if (q && q.status === QueueStatus.Unresolved) {
fn.queueCount++
q.push(fn)
}
}
}
currentDeps.map((dep) => dep[turbopackQueues](fnQueue))
return fn.queueCount ? promise : getResult()
}
function asyncResult(err?: any) {
if (err) {
reject((promise[turbopackError] = err))
} else {
resolve(promise[turbopackExports])
}
resolveQueue(queue)
}
body(handleAsyncDependencies, asyncResult)
if (queue && queue.status === QueueStatus.Unknown) {
queue.status = QueueStatus.Unresolved
}
}
contextPrototype.a = asyncModule
/** /**
* A pseudo "fake" URL object to resolve to its relative path. * A pseudo "fake" URL object to resolve to its relative path.
* *
......
...@@ -19,14 +19,13 @@ use crate::{RuntimeType, embed_js::embed_static_code}; ...@@ -19,14 +19,13 @@ use crate::{RuntimeType, embed_js::embed_static_code};
pub async fn get_browser_runtime_code( pub async fn get_browser_runtime_code(
asset_context: ResolvedVc<Box<dyn AssetContext>>, asset_context: ResolvedVc<Box<dyn AssetContext>>,
chunk_base_path: Vc<Option<RcStr>>, chunk_base_path: Vc<Option<RcStr>>,
worker_asset_prefix: Vc<Option<RcStr>>,
asset_suffix: Vc<AssetSuffix>, asset_suffix: Vc<AssetSuffix>,
worker_forwarded_globals: Vc<Vec<RcStr>>,
runtime_type: RuntimeType, runtime_type: RuntimeType,
output_root_to_root_path: RcStr, output_root_to_root_path: RcStr,
generate_source_map: bool, generate_source_map: bool,
chunk_loading_global: Vc<RcStr>, chunk_loading_global: Vc<RcStr>,
cross_origin: Vc<CrossOrigin>, cross_origin: Vc<CrossOrigin>,
has_async_modules: bool,
) -> Result<Vc<Code>> { ) -> Result<Vc<Code>> {
let asset_context = *asset_context; let asset_context = *asset_context;
let environment = asset_context.compile_time_info().environment(); let environment = asset_context.compile_time_info().environment();
...@@ -87,14 +86,6 @@ pub async fn get_browser_runtime_code( ...@@ -87,14 +86,6 @@ pub async fn get_browser_runtime_code(
let relative_root_path = output_root_to_root_path; let relative_root_path = output_root_to_root_path;
let chunk_base_path = chunk_base_path.await?; let chunk_base_path = chunk_base_path.await?;
let chunk_base_path = chunk_base_path.as_ref().map_or_else(|| "", |f| f.as_str()); let chunk_base_path = chunk_base_path.as_ref().map_or_else(|| "", |f| f.as_str());
// `null` (no override) and `Some("")` (empty-string prefix) are distinct
// states, so inject as a JS literal — `null` for None and a quoted string
// for Some — instead of collapsing both to "".
let worker_asset_prefix = worker_asset_prefix.await?;
let worker_asset_prefix_js: String = worker_asset_prefix.as_ref().map_or_else(
|| "null".to_string(),
|f| format!("{}", StringifyJs(f.as_str())),
);
let asset_suffix = asset_suffix.await?; let asset_suffix = asset_suffix.await?;
let chunk_loading_global = chunk_loading_global.await?; let chunk_loading_global = chunk_loading_global.await?;
let cross_origin = *cross_origin.await?; let cross_origin = *cross_origin.await?;
...@@ -118,13 +109,11 @@ pub async fn get_browser_runtime_code( ...@@ -118,13 +109,11 @@ pub async fn get_browser_runtime_code(
}} }}
var CHUNK_BASE_PATH = {}; var CHUNK_BASE_PATH = {};
var WORKER_BASE_PATH = {};
var RELATIVE_ROOT_PATH = {}; var RELATIVE_ROOT_PATH = {};
var RUNTIME_PUBLIC_PATH = {}; var RUNTIME_PUBLIC_PATH = {};
"#, "#,
StringifyJs(&chunk_loading_global), StringifyJs(&chunk_loading_global),
StringifyJs(chunk_base_path), StringifyJs(chunk_base_path),
worker_asset_prefix_js,
StringifyJs(relative_root_path.as_str()), StringifyJs(relative_root_path.as_str()),
StringifyJs(chunk_base_path), StringifyJs(chunk_base_path),
)?; )?;
...@@ -178,17 +167,18 @@ pub async fn get_browser_runtime_code( ...@@ -178,17 +167,18 @@ pub async fn get_browser_runtime_code(
StringifyJs(&cross_origin) StringifyJs(&cross_origin)
)?; )?;
// Output the list of global variable names to forward to workers
let worker_forwarded_globals = worker_forwarded_globals.await?;
writedoc!(
code,
r#"
var WORKER_FORWARDED_GLOBALS = {};
"#,
StringifyJs(&*worker_forwarded_globals)
)?;
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 has_async_modules {
code.push_code(
&*embed_static_code(
asset_context,
rcstr!("shared/runtime/async-module.ts"),
generate_source_map,
)
.await?,
);
}
for runtime_code in runtime_base_code { for runtime_code in runtime_base_code {
code.push_code( code.push_code(
&*embed_static_code(asset_context, runtime_code.into(), generate_source_map).await?, &*embed_static_code(asset_context, runtime_code.into(), generate_source_map).await?,
......
...@@ -13,6 +13,7 @@ use crate::{RuntimeType, embed_js::embed_static_code}; ...@@ -13,6 +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,
generate_source_map: bool, generate_source_map: bool,
) -> Result<Vc<Code>> { ) -> Result<Vc<Code>> {
let asset_context = *asset_context; let asset_context = *asset_context;
...@@ -47,6 +48,17 @@ pub async fn get_nodejs_runtime_code( ...@@ -47,6 +48,17 @@ 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 has_async_modules {
code.push_code(
&*embed_static_code(
asset_context,
rcstr!("shared/runtime/async-module.ts"),
generate_source_map,
)
.await?,
);
}
code.push_code(&*shared_base_external_utils_code.await?); code.push_code(&*shared_base_external_utils_code.await?);
code.push_code(&*shared_node_external_utils_code.await?); code.push_code(&*shared_node_external_utils_code.await?);
code.push_code(&*shared_node_wasm_utils_code.await?); code.push_code(&*shared_node_wasm_utils_code.await?);
......
// Embedded worker-runtime helper. This file is bundled as a regular module and
// `__turbopack_require__`d by the generated web-worker loader code.
//
// The chunk-URL builder, the chunk base path and the asset suffix are read from
// the shared `__turbopack_chunk_relative_url__` / `__turbopack_chunk_base_path__`
// / `__turbopack_chunk_asset_suffix__` runtime primitives. The worker base-path
// override and forwarded-global names are baked into this module at build time by
// `turbopack-ecmascript` replacing the `_TURBOPACK_WORKER_BASE_PATH_` /
// `_TURBOPACK_WORKER_FORWARDED_GLOBALS_` free variables, and the forwarded-global
// values are read from `globalThis`.
declare const __turbopack_chunk_relative_url__: (
chunkPath: string,
basePath?: string
) => string
declare const __turbopack_chunk_base_path__: string
declare const __turbopack_chunk_asset_suffix__: string
declare const _TURBOPACK_WORKER_FORWARDED_GLOBALS_: string[]
declare const _TURBOPACK_WORKER_BASE_PATH_: string | null
type WorkerConstructor = new (url: URL, options?: object) => Worker
/**
* Creates a web worker by instantiating the given WorkerConstructor with the
* appropriate URL and options.
*
* The entrypoint is a pre-compiled worker runtime file. The params configure
* which module chunks to load and which module to run as the entry point.
*
* The params are a JSON array of the following structure:
* `[TURBOPACK_NEXT_CHUNK_URLS, ASSET_SUFFIX, ...workerForwardedGlobals values]`
*
* @param WorkerConstructor The Worker or SharedWorker constructor
* @param entrypoint path to the worker entrypoint chunk
* @param moduleChunks list of module chunk paths to load
* @param workerOptions options to pass to the Worker constructor (optional)
*/
function createWorker(
WorkerConstructor: WorkerConstructor,
entrypoint: string,
moduleChunks: string[],
workerOptions?: object
): Worker {
const isSharedWorker = WorkerConstructor.name === 'SharedWorker'
// `WORKER_BASE_PATH` overrides `CHUNK_BASE_PATH` for the entrypoint and the
// module chunks loaded inside the worker, keeping them same-origin to each
// other when `CHUNK_BASE_PATH` (= `assetPrefix`) is a cross-origin CDN.
// `null` falls back; an empty string is treated as a literal empty prefix.
const workerBasePath =
_TURBOPACK_WORKER_BASE_PATH_ ?? __turbopack_chunk_base_path__
const chunkUrls = moduleChunks
.map((chunk) => __turbopack_chunk_relative_url__(chunk, workerBasePath))
.reverse()
const params: unknown[] = [chunkUrls, __turbopack_chunk_asset_suffix__]
const globals = _TURBOPACK_WORKER_FORWARDED_GLOBALS_
for (let i = 0; i < globals.length; i++) {
params.push((globalThis as Record<string, unknown>)[globals[i]])
}
const url = new URL(
__turbopack_chunk_relative_url__(entrypoint, workerBasePath),
location.origin
)
const paramsJson = JSON.stringify(params)
if (isSharedWorker) {
url.searchParams.set('params', paramsJson)
} else {
url.hash = '#params=' + encodeURIComponent(paramsJson)
}
// Remove type: "module" from options since our worker entrypoint is not a module
const options = workerOptions
? { ...workerOptions, type: undefined }
: undefined
return new WorkerConstructor(url, options)
}
/**
* Returns a function, that when called with the constructor + any options calls `createWorker()`.
* The worker configuration (`entrypoint`, `moduleChunks`) is passed by `turbopack-ecmascript`
* at build time, leaving the runtime caller to only supply the constructor and options.
*/
export default function generateCreateWorker(
entrypoint: string,
moduleChunks: string[]
) {
return (
WorkerConstructor: { new (url: URL, options?: object): Worker },
workerOptions?: object
) => createWorker(WorkerConstructor, entrypoint, moduleChunks, workerOptions)
}
// Embedded worker-runtime helper. This file is bundled as a regular module and
// `__turbopack_require__`d by the generated Node.js worker-thread loader code.
//
// The list of globals to forward is baked into this module at build time by
// `turbopack-ecmascript` replacing the `_TURBOPACK_WORKER_FORWARDED_GLOBALS_`
// free variable; the forwarded-global values are read from `globalThis`.
declare const _TURBOPACK_WORKER_FORWARDED_GLOBALS_: string[]
/**
* Creates a Node.js worker thread by instantiating the given WorkerConstructor
* with the appropriate path and options, including forwarded globals.
*
* @param WorkerConstructor The Worker constructor from worker_threads
* @param workerPath Path to the worker entry chunk
* @param workerOptions options to pass to the Worker constructor (optional)
*/
function createWorker(
WorkerConstructor: { new (path: string, options?: object): unknown },
workerPath: string,
workerOptions?: { workerData?: unknown; [key: string]: unknown }
): unknown {
// Build the forwarded globals object
const forwardedGlobals: Record<string, unknown> = {}
const globals = _TURBOPACK_WORKER_FORWARDED_GLOBALS_
for (let i = 0; i < globals.length; i++) {
forwardedGlobals[globals[i]] = (globalThis as Record<string, unknown>)[
globals[i]
]
}
// Merge workerData with forwarded globals
const existingWorkerData = workerOptions?.workerData || {}
const options = {
...workerOptions,
workerData: {
...(typeof existingWorkerData === 'object' ? existingWorkerData : {}),
__turbopack_globals__: forwardedGlobals,
},
}
return new WorkerConstructor(workerPath, options)
}
/**
* Returns a function, that when called with the constructor + any options calls `createWorker()`.
* The worker configuration (`entrypoint`) is passed by `turbopack-ecmascript` at build time,
* leaving the runtime caller to only supply the constructor and options.
*/
export default function generateCreateWorker(entrypoint: string) {
return (
WorkerConstructor: { new (path: string, options?: object): unknown },
workerOptions?: { workerData?: unknown; [key: string]: unknown }
) => createWorker(WorkerConstructor, entrypoint, workerOptions)
}
...@@ -39,7 +39,7 @@ use crate::{ ...@@ -39,7 +39,7 @@ use crate::{
member::MemberReplacement, member::MemberReplacement,
require_context::RequireContextAssetReferenceCodeGen, require_context::RequireContextAssetReferenceCodeGen,
unreachable::Unreachable, unreachable::Unreachable,
worker::WorkerAssetReferenceCodeGen, worker::{WorkerAssetReferenceCodeGen, WorkerGlobalsReplacementCodeGen},
}, },
}; };
...@@ -203,6 +203,7 @@ pub enum CodeGen { ...@@ -203,6 +203,7 @@ pub enum CodeGen {
UrlAssetReferenceCodeGen(UrlAssetReferenceCodeGen), UrlAssetReferenceCodeGen(UrlAssetReferenceCodeGen),
WorkerAssetReferenceCodeGen(WorkerAssetReferenceCodeGen), WorkerAssetReferenceCodeGen(WorkerAssetReferenceCodeGen),
ModuleHotReferenceCodeGen(ModuleHotReferenceCodeGen), ModuleHotReferenceCodeGen(ModuleHotReferenceCodeGen),
WorkerGlobalsReplacementCodeGen(WorkerGlobalsReplacementCodeGen),
} }
impl CodeGen { impl CodeGen {
...@@ -239,6 +240,7 @@ impl CodeGen { ...@@ -239,6 +240,7 @@ impl CodeGen {
Self::ModuleHotReferenceCodeGen(v) => { Self::ModuleHotReferenceCodeGen(v) => {
v.code_generation(ctx, scope_hoisting_context).await v.code_generation(ctx, scope_hoisting_context).await
} }
Self::WorkerGlobalsReplacementCodeGen(v) => v.code_generation(ctx).await,
} }
} }
} }
......
use turbo_tasks::Vc;
use turbo_tasks_fs::{FileSystem, embed_directory};
#[turbo_tasks::function]
pub fn embed_fs() -> Vc<Box<dyn FileSystem>> {
embed_directory!("turbopack-ecmascript", "$CARGO_MANIFEST_DIR/js/src")
}
...@@ -13,6 +13,7 @@ pub mod async_chunk; ...@@ -13,6 +13,7 @@ pub mod async_chunk;
pub mod bytes_source_transform; pub mod bytes_source_transform;
pub mod chunk; pub mod chunk;
pub mod code_gen; pub mod code_gen;
pub mod embed_js;
mod errors; mod errors;
pub mod json_source_transform; pub mod json_source_transform;
pub mod magic_identifier; pub mod magic_identifier;
......
...@@ -91,7 +91,7 @@ use turbopack_core::{ ...@@ -91,7 +91,7 @@ use turbopack_core::{
use turbopack_resolve::{ecmascript::cjs_resolve_source, typescript::tsconfig}; use turbopack_resolve::{ecmascript::cjs_resolve_source, typescript::tsconfig};
use turbopack_swc_utils::emitter::IssueEmitter; use turbopack_swc_utils::emitter::IssueEmitter;
use unreachable::Unreachable; use unreachable::Unreachable;
use worker::WorkerAssetReference; use worker::{WorkerAssetReference, WorkerGlobalPlaceholder, WorkerGlobalsReplacementCodeGen};
pub use crate::references::esm::export::{FollowExportsResult, follow_reexports}; pub use crate::references::esm::export::{FollowExportsResult, follow_reexports};
use crate::{ use crate::{
...@@ -1165,6 +1165,23 @@ async fn analyze_ecmascript_module_internal( ...@@ -1165,6 +1165,23 @@ async fn analyze_ecmascript_module_internal(
"unexpected Effect::FreeVar in tracing mode" "unexpected Effect::FreeVar in tracing mode"
); );
// Worker runtime helpers reference these as free vars; replace each
// with the value baked from the chunking context's worker config.
let worker_placeholder = match &*var {
"_TURBOPACK_WORKER_FORWARDED_GLOBALS_" => {
Some(WorkerGlobalPlaceholder::ForwardedGlobals)
}
"_TURBOPACK_WORKER_BASE_PATH_" => Some(WorkerGlobalPlaceholder::BasePath),
_ => None,
};
if let Some(placeholder) = worker_placeholder {
analysis.add_code_gen(WorkerGlobalsReplacementCodeGen::new(
placeholder,
ast_path.into(),
));
continue;
}
if options.enable_exports_info_inlining && var == "__webpack_exports_info__" { if options.enable_exports_info_inlining && var == "__webpack_exports_info__" {
if analysis_state.first_webpack_exports_info { if analysis_state.first_webpack_exports_info {
analysis_state.first_webpack_exports_info = false; analysis_state.first_webpack_exports_info = false;
......
use anyhow::Result; use anyhow::Result;
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
use swc_core::{ use swc_core::{
common::util::take::Take, common::{DUMMY_SP, util::take::Take},
ecma::ast::{CallExpr, Callee, Expr, ExprOrSpread, Lit}, ecma::ast::{ArrayLit, CallExpr, Callee, Expr, ExprOrSpread, Lit, Null},
quote_expr, quote_expr,
}; };
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
...@@ -400,3 +400,61 @@ impl WorkerAssetReferenceCodeGen { ...@@ -400,3 +400,61 @@ impl WorkerAssetReferenceCodeGen {
Ok(CodeGeneration::visitors(vec![visitor])) Ok(CodeGeneration::visitors(vec![visitor]))
} }
} }
#[derive(
PartialEq, Eq, TraceRawVcs, ValueDebugFormat, NonLocalValue, Debug, Hash, Encode, Decode,
)]
pub enum WorkerGlobalPlaceholder {
/// `const _TURBOPACK_WORKER_FORWARDED_GLOBALS_ = []`
ForwardedGlobals,
/// `const _TURBOPACK_WORKER_BASE_PATH_ = '_TURBOPACK_WORKER_BASE_PATH_REPLACE_'`
BasePath,
}
#[derive(
PartialEq, Eq, TraceRawVcs, ValueDebugFormat, NonLocalValue, Debug, Hash, Encode, Decode,
)]
pub struct WorkerGlobalsReplacementCodeGen {
/// Which placeholder this codegen replaces (determines the injected value).
placeholder: WorkerGlobalPlaceholder,
path: AstPath,
}
impl WorkerGlobalsReplacementCodeGen {
pub fn new(placeholder: WorkerGlobalPlaceholder, path: AstPath) -> Self {
WorkerGlobalsReplacementCodeGen { placeholder, path }
}
pub async fn code_generation(
&self,
chunking_context: Vc<Box<dyn ChunkingContext>>,
) -> Result<CodeGeneration> {
let options = chunking_context.worker_configuration_options().await?;
let value: Expr = match self.placeholder {
WorkerGlobalPlaceholder::ForwardedGlobals => Expr::Array(ArrayLit {
span: DUMMY_SP,
elems: options
.forwarded_globals
.iter()
.map(|global| Some(Expr::Lit(Lit::Str(global.as_str().into())).into()))
.collect(),
}),
WorkerGlobalPlaceholder::BasePath => match &options.asset_prefix {
Some(asset_prefix) => Expr::Lit(Lit::Str(asset_prefix.as_str().into())),
None => Expr::Lit(Lit::Null(Null { span: DUMMY_SP })),
},
};
let visitor = create_visitor!(self.path, visit_mut_expr, |expr: &mut Expr| {
*expr = value.clone();
});
Ok(CodeGeneration::visitors(vec![visitor]))
}
}
impl From<WorkerGlobalsReplacementCodeGen> for CodeGen {
fn from(val: WorkerGlobalsReplacementCodeGen) -> Self {
CodeGen::WorkerGlobalsReplacementCodeGen(val)
}
}
...@@ -84,7 +84,6 @@ pub const TURBOPACK_RESOLVE_ABSOLUTE_PATH: &TurbopackRuntimeFunctionShortcut = m ...@@ -84,7 +84,6 @@ pub const TURBOPACK_RESOLVE_ABSOLUTE_PATH: &TurbopackRuntimeFunctionShortcut = m
pub const TURBOPACK_RESOLVE_FILE_URL: &TurbopackRuntimeFunctionShortcut = make_shortcut!("F"); pub const TURBOPACK_RESOLVE_FILE_URL: &TurbopackRuntimeFunctionShortcut = make_shortcut!("F");
pub const TURBOPACK_RELATIVE_URL: &TurbopackRuntimeFunctionShortcut = make_shortcut!("U"); pub const TURBOPACK_RELATIVE_URL: &TurbopackRuntimeFunctionShortcut = make_shortcut!("U");
pub const TURBOPACK_RESOLVE_MODULE_ID_PATH: &TurbopackRuntimeFunctionShortcut = make_shortcut!("R"); pub const TURBOPACK_RESOLVE_MODULE_ID_PATH: &TurbopackRuntimeFunctionShortcut = make_shortcut!("R");
pub const TURBOPACK_CREATE_WORKER: &TurbopackRuntimeFunctionShortcut = make_shortcut!("b");
pub const TURBOPACK_ASYNC_MODULE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("a"); pub const TURBOPACK_ASYNC_MODULE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("a");
pub const TURBOPACK_EXTERNAL_REQUIRE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("x"); pub const TURBOPACK_EXTERNAL_REQUIRE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("x");
pub const TURBOPACK_EXTERNAL_IMPORT: &TurbopackRuntimeFunctionShortcut = make_shortcut!("y"); pub const TURBOPACK_EXTERNAL_IMPORT: &TurbopackRuntimeFunctionShortcut = make_shortcut!("y");
...@@ -94,10 +93,13 @@ pub const TURBOPACK_REQUIRE_REAL: &TurbopackRuntimeFunctionShortcut = make_short ...@@ -94,10 +93,13 @@ pub const TURBOPACK_REQUIRE_REAL: &TurbopackRuntimeFunctionShortcut = make_short
pub const TURBOPACK_WASM: &TurbopackRuntimeFunctionShortcut = make_shortcut!("w"); pub const TURBOPACK_WASM: &TurbopackRuntimeFunctionShortcut = make_shortcut!("w");
pub const TURBOPACK_WASM_MODULE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("u"); pub const TURBOPACK_WASM_MODULE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("u");
pub const TURBOPACK_GLOBAL: &TurbopackRuntimeFunctionShortcut = make_shortcut!("g"); pub const TURBOPACK_GLOBAL: &TurbopackRuntimeFunctionShortcut = make_shortcut!("g");
pub const TURBOPACK_CHUNK_RELATIVE_URL: &TurbopackRuntimeFunctionShortcut = make_shortcut!("h");
pub const TURBOPACK_CHUNK_BASE_PATH: &TurbopackRuntimeFunctionShortcut = make_shortcut!("b");
pub const TURBOPACK_ASSET_SUFFIX: &TurbopackRuntimeFunctionShortcut = make_shortcut!("X");
/// Adding an entry to this list will automatically ensure that `__turbopack_XXX__` can be called /// Adding an entry to this list will automatically ensure that `__turbopack_XXX__` can be called
/// from user code (by inserting a replacement into free_var_references) /// from user code (by inserting a replacement into free_var_references)
pub const TURBOPACK_RUNTIME_FUNCTION_SHORTCUTS: [(&str, &TurbopackRuntimeFunctionShortcut); 24] = [ pub const TURBOPACK_RUNTIME_FUNCTION_SHORTCUTS: [(&str, &TurbopackRuntimeFunctionShortcut); 26] = [
("__turbopack_require__", TURBOPACK_REQUIRE), ("__turbopack_require__", TURBOPACK_REQUIRE),
("__turbopack_module_context__", TURBOPACK_MODULE_CONTEXT), ("__turbopack_module_context__", TURBOPACK_MODULE_CONTEXT),
("__turbopack_import__", TURBOPACK_IMPORT), ("__turbopack_import__", TURBOPACK_IMPORT),
...@@ -119,7 +121,6 @@ pub const TURBOPACK_RUNTIME_FUNCTION_SHORTCUTS: [(&str, &TurbopackRuntimeFunctio ...@@ -119,7 +121,6 @@ pub const TURBOPACK_RUNTIME_FUNCTION_SHORTCUTS: [(&str, &TurbopackRuntimeFunctio
"__turbopack_resolve_module_id_path__", "__turbopack_resolve_module_id_path__",
TURBOPACK_RESOLVE_MODULE_ID_PATH, TURBOPACK_RESOLVE_MODULE_ID_PATH,
), ),
("__turbopack_create_worker__", TURBOPACK_CREATE_WORKER),
("__turbopack_external_require__", TURBOPACK_EXTERNAL_REQUIRE), ("__turbopack_external_require__", TURBOPACK_EXTERNAL_REQUIRE),
("__turbopack_external_import__", TURBOPACK_EXTERNAL_IMPORT), ("__turbopack_external_import__", TURBOPACK_EXTERNAL_IMPORT),
("__turbopack_refresh__", TURBOPACK_REFRESH), ("__turbopack_refresh__", TURBOPACK_REFRESH),
...@@ -131,4 +132,10 @@ pub const TURBOPACK_RUNTIME_FUNCTION_SHORTCUTS: [(&str, &TurbopackRuntimeFunctio ...@@ -131,4 +132,10 @@ pub const TURBOPACK_RUNTIME_FUNCTION_SHORTCUTS: [(&str, &TurbopackRuntimeFunctio
), ),
("__turbopack_wasm__", TURBOPACK_WASM), ("__turbopack_wasm__", TURBOPACK_WASM),
("__turbopack_wasm_module__", TURBOPACK_WASM_MODULE), ("__turbopack_wasm_module__", TURBOPACK_WASM_MODULE),
(
"__turbopack_chunk_relative_url__",
TURBOPACK_CHUNK_RELATIVE_URL,
),
("__turbopack_chunk_base_path__", TURBOPACK_CHUNK_BASE_PATH),
("__turbopack_chunk_asset_suffix__", TURBOPACK_ASSET_SUFFIX),
]; ];
...@@ -2,19 +2,22 @@ use anyhow::{Result, bail}; ...@@ -2,19 +2,22 @@ use anyhow::{Result, bail};
use indoc::formatdoc; use indoc::formatdoc;
use turbo_rcstr::rcstr; use turbo_rcstr::rcstr;
use turbo_tasks::{ResolvedVc, TryJoinIterExt, ValueToString, Vc}; use turbo_tasks::{ResolvedVc, TryJoinIterExt, ValueToString, Vc};
use turbo_tasks_fs::FileSystem;
use turbopack_core::{ use turbopack_core::{
chunk::{ chunk::{
AsyncModuleInfo, ChunkData, ChunkGroupType, ChunkableModule, ChunkingContext, AsyncModuleInfo, ChunkData, ChunkGroupType, ChunkableModule, ChunkingContext,
ChunkingContextExt, ChunkingType, ChunksData, EvaluatableAsset, ChunkingContextExt, ChunkingType, ChunksData, EvaluatableAsset, ModuleChunkItemIdExt,
availability_info::AvailabilityInfo, ModuleId, availability_info::AvailabilityInfo,
}, },
context::AssetContext, context::AssetContext,
file_source::FileSource,
ident::AssetIdent, ident::AssetIdent,
module::{Module, ModuleSideEffects}, module::{Module, ModuleSideEffects},
module_graph::{ModuleGraph, chunk_group_info::ChunkGroup}, module_graph::{ModuleGraph, chunk_group_info::ChunkGroup},
output::{OutputAsset, OutputAssets, OutputAssetsWithReferenced}, output::{OutputAsset, OutputAssets, OutputAssetsWithReferenced},
reference::{ModuleReference, ModuleReferences}, reference::{ModuleReference, ModuleReferences, SingleChunkableModuleReference},
resolve::ModuleResolveResult, reference_type::{EcmaScriptModulesReferenceSubType, ReferenceType},
resolve::{ExportUsage, ModulePart, ModuleResolveResult},
}; };
use super::worker_type::WorkerType; use super::worker_type::WorkerType;
...@@ -23,8 +26,9 @@ use crate::{ ...@@ -23,8 +26,9 @@ use crate::{
EcmascriptChunkItemContent, EcmascriptChunkItemOptions, EcmascriptChunkPlaceable, EcmascriptChunkItemContent, EcmascriptChunkItemOptions, EcmascriptChunkPlaceable,
EcmascriptExports, data::EcmascriptChunkData, ecmascript_chunk_item, EcmascriptExports, data::EcmascriptChunkData, ecmascript_chunk_item,
}, },
runtime_functions::{TURBOPACK_CREATE_WORKER, TURBOPACK_EXPORT_VALUE}, embed_js::embed_fs,
utils::StringifyJs, runtime_functions::{TURBOPACK_EXPORT_VALUE, TURBOPACK_REQUIRE},
utils::{StringifyJs, StringifyModuleId},
}; };
/// The WorkerLoaderModule is a module that creates a separate root chunk group for the given module /// The WorkerLoaderModule is a module that creates a separate root chunk group for the given module
...@@ -130,6 +134,28 @@ impl WorkerLoaderModule { ...@@ -130,6 +134,28 @@ impl WorkerLoaderModule {
)) ))
} }
/// `createWorker` is stored in a module; for each worker we need to
/// load, we require this module and then use it.
#[turbo_tasks::function]
async fn create_worker_module(self: Vc<Self>) -> Result<Vc<Box<dyn Module>>> {
let this = self.await?;
let helper = match this.worker_type {
WorkerType::WebWorker | WorkerType::SharedWebWorker => {
rcstr!("worker/browser/createWorker.ts")
}
WorkerType::NodeWorkerThread => rcstr!("worker/node/createWorker.ts"),
};
Ok(this
.asset_context
.process(
Vc::upcast(FileSource::new(embed_fs().root().await?.join(&helper)?)),
ReferenceType::EcmaScriptModules(EcmaScriptModulesReferenceSubType::ImportPart(
ModulePart::Export(rcstr!("default")),
)),
)
.module())
}
/// Returns output assets including the worker entrypoint for web workers. /// Returns output assets including the worker entrypoint for web workers.
#[turbo_tasks::function] #[turbo_tasks::function]
async fn chunk_group_with_type( async fn chunk_group_with_type(
...@@ -171,11 +197,22 @@ impl Module for WorkerLoaderModule { ...@@ -171,11 +197,22 @@ impl Module for WorkerLoaderModule {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn references(self: Vc<Self>) -> Result<Vc<ModuleReferences>> { async fn references(self: Vc<Self>) -> Result<Vc<ModuleReferences>> {
let this = self.await?; let this = self.await?;
Ok(Vc::cell(vec![ResolvedVc::upcast( Ok(Vc::cell(vec![
WorkerModuleReference::new(*ResolvedVc::upcast(this.inner), this.worker_type) ResolvedVc::upcast(
WorkerModuleReference::new(*ResolvedVc::upcast(this.inner), this.worker_type)
.to_resolved()
.await?,
),
ResolvedVc::upcast(
SingleChunkableModuleReference::new(
self.create_worker_module(),
rcstr!("createWorker"),
ExportUsage::named(rcstr!("default")),
)
.to_resolved() .to_resolved()
.await?, .await?,
)])) ),
]))
} }
#[turbo_tasks::function] #[turbo_tasks::function]
...@@ -226,14 +263,14 @@ impl EcmascriptChunkPlaceable for WorkerLoaderModule { ...@@ -226,14 +263,14 @@ impl EcmascriptChunkPlaceable for WorkerLoaderModule {
// otherwise we will induce a turbo tasks cycle. But we only need an // otherwise we will induce a turbo tasks cycle. But we only need an
// approximate solution. We'll use the same estimate for both web // approximate solution. We'll use the same estimate for both web
// and Node.js workers. // and Node.js workers.
let fake_id = ModuleId::String(rcstr!("a_fake_module"));
return Ok(EcmascriptChunkItemContent { return Ok(EcmascriptChunkItemContent {
inner_code: formatdoc! { inner_code: formatdoc! {
r#" r#"
{TURBOPACK_EXPORT_VALUE}(function(Ctor, opts) {{ {TURBOPACK_EXPORT_VALUE}({TURBOPACK_REQUIRE}({workers_module})["default"](__dirname + "/" + {worker_path:#}));
return {TURBOPACK_CREATE_WORKER}(Ctor, __dirname + "/" + {worker_path:#}, opts);
}});
"#, "#,
worker_path = StringifyJs(&"a_fake_path_for_size_estimation"), worker_path = StringifyJs(&"a_fake_path_for_size_estimation"),
workers_module = StringifyModuleId(&fake_id),
} }
.into(), .into(),
options, options,
...@@ -242,6 +279,11 @@ impl EcmascriptChunkPlaceable for WorkerLoaderModule { ...@@ -242,6 +279,11 @@ impl EcmascriptChunkPlaceable for WorkerLoaderModule {
.cell()); .cell());
} }
let create_worker_id = self
.create_worker_module()
.chunk_item_id(chunking_context)
.await?;
let code = match this.worker_type { let code = match this.worker_type {
WorkerType::WebWorker | WorkerType::SharedWebWorker => { WorkerType::WebWorker | WorkerType::SharedWebWorker => {
// For web workers, generate code that exports a function to create the worker. // For web workers, generate code that exports a function to create the worker.
...@@ -266,12 +308,11 @@ impl EcmascriptChunkPlaceable for WorkerLoaderModule { ...@@ -266,12 +308,11 @@ impl EcmascriptChunkPlaceable for WorkerLoaderModule {
formatdoc! { formatdoc! {
r#" r#"
{TURBOPACK_EXPORT_VALUE}(function(Ctor, opts) {{ {TURBOPACK_EXPORT_VALUE}({TURBOPACK_REQUIRE}({workers_module})["default"]({entrypoint}, {chunks}));
return {TURBOPACK_CREATE_WORKER}(Ctor, {entrypoint}, {chunks}, opts);
}});
"#, "#,
entrypoint = StringifyJs(&entrypoint_path), entrypoint = StringifyJs(&entrypoint_path),
chunks = StringifyJs(&chunks_data), chunks = StringifyJs(&chunks_data),
workers_module = StringifyModuleId(&create_worker_id),
} }
} }
WorkerType::NodeWorkerThread => { WorkerType::NodeWorkerThread => {
...@@ -299,11 +340,10 @@ impl EcmascriptChunkPlaceable for WorkerLoaderModule { ...@@ -299,11 +340,10 @@ impl EcmascriptChunkPlaceable for WorkerLoaderModule {
// directory // directory
formatdoc! { formatdoc! {
r#" r#"
{TURBOPACK_EXPORT_VALUE}(function(Ctor, opts) {{ {TURBOPACK_EXPORT_VALUE}({TURBOPACK_REQUIRE}({workers_module})["default"](__dirname + "/" + {worker_path:#}));
return {TURBOPACK_CREATE_WORKER}(Ctor, __dirname + "/" + {worker_path:#}, opts);
}});
"#, "#,
worker_path = StringifyJs(entry_path.file_name()), worker_path = StringifyJs(entry_path.file_name()),
workers_module = StringifyModuleId(&create_worker_id),
} }
} }
}; };
......
...@@ -12,7 +12,7 @@ use turbopack_core::{ ...@@ -12,7 +12,7 @@ use turbopack_core::{
AssetSuffix, Chunk, ChunkGroupResult, ChunkItem, ChunkType, ChunkableModule, AssetSuffix, Chunk, ChunkGroupResult, ChunkItem, ChunkType, ChunkableModule,
ChunkingConfig, ChunkingConfigs, ChunkingContext, ContentHashing, EntryChunkGroupResult, ChunkingConfig, ChunkingConfigs, ChunkingContext, ContentHashing, EntryChunkGroupResult,
EvaluatableAsset, MinifyType, SourceMapSourceType, SourceMapsType, UnusedReferences, EvaluatableAsset, MinifyType, SourceMapSourceType, SourceMapsType, UnusedReferences,
UrlBehavior, UrlBehavior, WorkerConfigurationOptions,
availability_info::AvailabilityInfo, availability_info::AvailabilityInfo,
chunk_group::{MakeChunkGroupResult, make_chunk_group}, chunk_group::{MakeChunkGroupResult, make_chunk_group},
chunk_id_strategy::ModuleIdStrategy, chunk_id_strategy::ModuleIdStrategy,
...@@ -714,7 +714,11 @@ impl ChunkingContext for NodeJsChunkingContext { ...@@ -714,7 +714,11 @@ impl ChunkingContext for NodeJsChunkingContext {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
fn worker_forwarded_globals(&self) -> Vc<Vec<RcStr>> { fn worker_configuration_options(&self) -> Vc<WorkerConfigurationOptions> {
Vc::cell(self.worker_forwarded_globals.clone()) WorkerConfigurationOptions {
asset_prefix: None,
forwarded_globals: self.worker_forwarded_globals.clone(),
}
.cell()
} }
} }
...@@ -16,6 +16,7 @@ use turbopack_core::{ ...@@ -16,6 +16,7 @@ use turbopack_core::{
source_map::{GenerateSourceMap, SourceMapAsset}, source_map::{GenerateSourceMap, SourceMapAsset},
}; };
use turbopack_ecmascript::{chunk::EcmascriptChunkPlaceable, utils::StringifyJs}; use turbopack_ecmascript::{chunk::EcmascriptChunkPlaceable, utils::StringifyJs};
use turbopack_ecmascript_runtime::RuntimeType;
use super::runtime::EcmascriptBuildNodeRuntimeChunk; use super::runtime::EcmascriptBuildNodeRuntimeChunk;
use crate::NodeJsChunkingContext; use crate::NodeJsChunkingContext;
...@@ -149,8 +150,21 @@ impl EcmascriptBuildNodeEntryChunk { ...@@ -149,8 +150,21 @@ impl EcmascriptBuildNodeEntryChunk {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
fn runtime_chunk(&self) -> Vc<EcmascriptBuildNodeRuntimeChunk> { async fn runtime_chunk(&self) -> Result<Vc<EcmascriptBuildNodeRuntimeChunk>> {
EcmascriptBuildNodeRuntimeChunk::new(*self.chunking_context) // Detect async modules from the whole-app graph in production. In development, the graph
// is per-page. To keep the shared `runtime.js` stable, always include the machinery.
let has_async_modules = if matches!(
*self.chunking_context.runtime_type().await?,
RuntimeType::Production
) {
!self.module_graph.async_module_info().await?.is_empty()
} else {
true
};
Ok(EcmascriptBuildNodeRuntimeChunk::new(
*self.chunking_context,
has_async_modules,
))
} }
#[turbo_tasks::function] #[turbo_tasks::function]
......
...@@ -24,14 +24,22 @@ use crate::NodeJsChunkingContext; ...@@ -24,14 +24,22 @@ 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,
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl EcmascriptBuildNodeRuntimeChunk { impl EcmascriptBuildNodeRuntimeChunk {
/// Creates a new [`Vc<EcmascriptBuildNodeRuntimeChunk>`]. /// Creates a new [`Vc<EcmascriptBuildNodeRuntimeChunk>`].
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn new(chunking_context: ResolvedVc<NodeJsChunkingContext>) -> Vc<Self> { pub fn new(
EcmascriptBuildNodeRuntimeChunk { chunking_context }.cell() chunking_context: ResolvedVc<NodeJsChunkingContext>,
has_async_modules: bool,
) -> Vc<Self> {
EcmascriptBuildNodeRuntimeChunk {
chunking_context,
has_async_modules,
}
.cell()
} }
#[turbo_tasks::function] #[turbo_tasks::function]
...@@ -56,10 +64,9 @@ impl EcmascriptBuildNodeRuntimeChunk { ...@@ -56,10 +64,9 @@ impl EcmascriptBuildNodeRuntimeChunk {
let asset_prefix = asset_prefix.as_deref().unwrap_or("/"); let asset_prefix = asset_prefix.as_deref().unwrap_or("/");
// Get the list of global variable names to forward to workers // Get the list of global variable names to forward to workers
let worker_forwarded_globals = let worker_config = Vc::upcast::<Box<dyn ChunkingContext>>(*this.chunking_context)
Vc::upcast::<Box<dyn ChunkingContext>>(*this.chunking_context) .worker_configuration_options()
.worker_forwarded_globals() .await?;
.await?;
writedoc!( writedoc!(
code, code,
...@@ -67,16 +74,14 @@ impl EcmascriptBuildNodeRuntimeChunk { ...@@ -67,16 +74,14 @@ impl EcmascriptBuildNodeRuntimeChunk {
var RUNTIME_PUBLIC_PATH = {}; var RUNTIME_PUBLIC_PATH = {};
var RELATIVE_ROOT_PATH = {}; var RELATIVE_ROOT_PATH = {};
var ASSET_PREFIX = {}; var ASSET_PREFIX = {};
var WORKER_FORWARDED_GLOBALS = {};
"#, "#,
StringifyJs(runtime_public_path), StringifyJs(runtime_public_path),
StringifyJs(output_root_to_root_path.as_str()), StringifyJs(output_root_to_root_path.as_str()),
StringifyJs(asset_prefix), StringifyJs(asset_prefix),
StringifyJs(&*worker_forwarded_globals),
)?; )?;
// Add preamble to read forwarded globals from workerData (for worker_threads) // Add preamble to read forwarded globals from workerData (for worker_threads)
if !worker_forwarded_globals.is_empty() { if !worker_config.forwarded_globals.is_empty() {
writedoc!( writedoc!(
code, code,
r#" r#"
...@@ -105,6 +110,7 @@ impl EcmascriptBuildNodeRuntimeChunk { ...@@ -105,6 +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,
generate_source_map, generate_source_map,
); );
code.push_code(&*runtime_code.await?); code.push_code(&*runtime_code.await?);
...@@ -113,6 +119,7 @@ impl EcmascriptBuildNodeRuntimeChunk { ...@@ -113,6 +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,
generate_source_map, generate_source_map,
); );
code.push_code(&*runtime_code.await?); code.push_code(&*runtime_code.await?);
......
# swc-ast-explorer # swc-ast-explorer
A small binary to print out SWC Abtract Syntax Trees. A small binary to print out SWC Abstract Syntax Trees.
```shell ```shell
echo "console.log('hello')" | cargo run -p swc-ast-explorer echo "console.log('hello')" | cargo run -p swc-ast-explorer
......
# turbopack-tests # turbopack-tests
An extracted create to perform snapshot tests on turbopack. An extracted crate to perform snapshot tests on turbopack.
## Testing ## Testing
......
var RUNTIME_PUBLIC_PATH = "output/[turbopack]_runtime.js"; var RUNTIME_PUBLIC_PATH = "output/[turbopack]_runtime.js";
var RELATIVE_ROOT_PATH = "../../../../../../.."; var RELATIVE_ROOT_PATH = "../../../../../../..";
var ASSET_PREFIX = "/"; var ASSET_PREFIX = "/";
var WORKER_FORWARDED_GLOBALS = [];
// Dummy runtime // Dummy runtime
\ No newline at end of file
var RUNTIME_PUBLIC_PATH = "output/[turbopack]_runtime.js"; var RUNTIME_PUBLIC_PATH = "output/[turbopack]_runtime.js";
var RELATIVE_ROOT_PATH = "../../../../../../.."; var RELATIVE_ROOT_PATH = "../../../../../../..";
var ASSET_PREFIX = "/"; var ASSET_PREFIX = "/";
var WORKER_FORWARDED_GLOBALS = [];
// Dummy runtime // Dummy runtime
\ No newline at end of file
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push(["output/0_9x_turbopack-tests_tests_snapshot_imports_ignore-comments_input_vercel_mjs_0r_m8l7._.js", (globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push(["output/0_9x_turbopack-tests_tests_snapshot_imports_ignore-comments_input_vercel_mjs_1n6almu._.js",
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/ignore-comments/input/vercel.mjs [test] (ecmascript, async loader)", ((__turbopack_context__) => { "[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/ignore-comments/input/vercel.mjs [test] (ecmascript, async loader)", ((__turbopack_context__) => {
__turbopack_context__.v((parentImport) => { __turbopack_context__.v((parentImport) => {
......
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push([ (globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push([
"output/0rv8_turbopack-tests_tests_snapshot_imports_ignore-comments_input_index_1aqs1qt.js", "output/0rv8_turbopack-tests_tests_snapshot_imports_ignore-comments_input_index_1dyvkws.js",
{"otherChunks":["output/0_9x_turbopack-tests_tests_snapshot_imports_ignore-comments_input_vercel_mjs_0r_m8l7._.js","output/1do3_crates_turbopack-tests_tests_snapshot_imports_ignore-comments_input_09jmqs-._.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/ignore-comments/input/index.js [test] (ecmascript)"]} {"otherChunks":["output/0_9x_turbopack-tests_tests_snapshot_imports_ignore-comments_input_vercel_mjs_1n6almu._.js","output/[root-of-the-server]__08ofy-h._.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/ignore-comments/input/index.js [test] (ecmascript)"]}
]); ]);
// Dummy runtime // Dummy runtime
\ No newline at end of file
{
"version": 3,
"sources": [],
"sections": [
{"offset": {"line": 3, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/ignore-comments/input/vercel.cjs"],"sourcesContent":["module.exports = 'turbopack'\n"],"names":["module","exports"],"mappings":"AAAAA,OAAOC,OAAO,GAAG"}},
{"offset": {"line": 20, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/ignore-comments/input/index.js"],"sourcesContent":["import('./vercel.mjs').then(console.log)\nimport(/* webpackIgnore: false */ './vercel.mjs').then(console.log)\nconsole.log(require('./vercel.cjs'))\nnew Worker(\n /* turbopackIgnore: false */ new URL('./vercel.cjs', import.meta.url)\n)\n\n// turbopack shouldn't attempt to bundle these, and they should be preserved in the output\nimport(/* webpackIgnore: true */ './ignore.mjs')\nimport(/* turbopackIgnore: true */ './ignore.mjs')\n\n// this should work for cjs requires too\nrequire(/* webpackIgnore: true */ './ignore.cjs')\nrequire(/* turbopackIgnore: true */ './ignore.cjs')\n\nnew Worker(\n /* turbopackIgnore: true */ new URL('./ignore-worker.cjs', import.meta.url)\n)\nnew Worker(\n /* webpackIgnore: true */ new URL('./ignore-worker.cjs', import.meta.url)\n)\n\nexport function foo(plugin) {\n return require(/* turbopackIgnore: true */ plugin)\n}\n"],"names":["then","console","log","Worker","require","foo","plugin"],"mappings":";;;;;;;;;AAAA,gKAAuBA,IAAI,CAACC,QAAQC,GAAG;AACvC,gKAAkDF,IAAI,CAACC,QAAQC,GAAG;AAClED,QAAQC,GAAG;AACX,iKAAIC;AAIJ,0FAA0F;AAC1F,MAAM,CAAC,uBAAuB,GAAG;AACjC,MAAM,CAAC,yBAAyB,GAAG;AAEnC,wCAAwC;AACxCC,QAAQ,uBAAuB,GAAG;AAClCA,QAAQ,yBAAyB,GAAG;AAEpC,IAAID;AAGJ,IAAIA;AAIG,SAASE,IAAIC,MAAM;IACxB,OAAOF,QAAQ,yBAAyB,GAAGE;AAC7C"}}]
}
\ No newline at end of file
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push(["output/1do3_crates_turbopack-tests_tests_snapshot_imports_ignore-comments_input_09jmqs-._.js", (globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push(["output/[root-of-the-server]__08ofy-h._.js",
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/ignore-comments/input/vercel.cjs [test] (ecmascript)", ((__turbopack_context__, module, exports) => { "[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/ignore-comments/input/vercel.cjs [test] (ecmascript)", ((__turbopack_context__, module, exports) => {
module.exports = 'turbopack'; module.exports = 'turbopack';
...@@ -6,11 +6,74 @@ module.exports = 'turbopack'; ...@@ -6,11 +6,74 @@ module.exports = 'turbopack';
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/ignore-comments/input/vercel.cjs (static in ecmascript)", ((__turbopack_context__) => { "[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/ignore-comments/input/vercel.cjs (static in ecmascript)", ((__turbopack_context__) => {
__turbopack_context__.q("/static/vercel.0kkt412gy5vj6.cjs");}), __turbopack_context__.q("/static/vercel.0kkt412gy5vj6.cjs");}),
"[turbopack-ecmascript]/worker/browser/createWorker.ts [test] (ecmascript)", ((__turbopack_context__) => {
"use strict";
// Embedded worker-runtime helper. This file is bundled as a regular module and
// `__turbopack_require__`d by the generated web-worker loader code.
//
// The chunk-URL builder, the chunk base path and the asset suffix are read from
// the shared `__turbopack_chunk_relative_url__` / `__turbopack_chunk_base_path__`
// / `__turbopack_chunk_asset_suffix__` runtime primitives. The worker base-path
// override and forwarded-global names are baked into this module at build time by
// `turbopack-ecmascript` replacing the `_TURBOPACK_WORKER_BASE_PATH_` /
// `_TURBOPACK_WORKER_FORWARDED_GLOBALS_` free variables, and the forwarded-global
// values are read from `globalThis`.
__turbopack_context__.s([
"default",
()=>generateCreateWorker
]);
/**
* Creates a web worker by instantiating the given WorkerConstructor with the
* appropriate URL and options.
*
* The entrypoint is a pre-compiled worker runtime file. The params configure
* which module chunks to load and which module to run as the entry point.
*
* The params are a JSON array of the following structure:
* `[TURBOPACK_NEXT_CHUNK_URLS, ASSET_SUFFIX, ...workerForwardedGlobals values]`
*
* @param WorkerConstructor The Worker or SharedWorker constructor
* @param entrypoint path to the worker entrypoint chunk
* @param moduleChunks list of module chunk paths to load
* @param workerOptions options to pass to the Worker constructor (optional)
*/ function createWorker(WorkerConstructor, entrypoint, moduleChunks, workerOptions) {
const isSharedWorker = WorkerConstructor.name === 'SharedWorker';
// `WORKER_BASE_PATH` overrides `CHUNK_BASE_PATH` for the entrypoint and the
// module chunks loaded inside the worker, keeping them same-origin to each
// other when `CHUNK_BASE_PATH` (= `assetPrefix`) is a cross-origin CDN.
// `null` falls back; an empty string is treated as a literal empty prefix.
const workerBasePath = null ?? /*TURBOPACK member replacement*/ __turbopack_context__.b;
const chunkUrls = moduleChunks.map((chunk)=>/*TURBOPACK member replacement*/ __turbopack_context__.h(chunk, workerBasePath)).reverse();
const params = [
chunkUrls,
/*TURBOPACK member replacement*/ __turbopack_context__.X
];
const globals = [];
for(let i = 0; i < globals.length; i++){
params.push(globalThis[globals[i]]);
}
const url = new URL(/*TURBOPACK member replacement*/ __turbopack_context__.h(entrypoint, workerBasePath), location.origin);
const paramsJson = JSON.stringify(params);
if (isSharedWorker) {
url.searchParams.set('params', paramsJson);
} else {
url.hash = '#params=' + encodeURIComponent(paramsJson);
}
// Remove type: "module" from options since our worker entrypoint is not a module
const options = workerOptions ? {
...workerOptions,
type: undefined
} : undefined;
return new WorkerConstructor(url, options);
}
function generateCreateWorker(entrypoint, moduleChunks) {
return (WorkerConstructor, workerOptions)=>createWorker(WorkerConstructor, entrypoint, moduleChunks, workerOptions);
}
}),
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/ignore-comments/input/vercel.cjs [test] (ecmascript, worker loader)", ((__turbopack_context__) => { "[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/ignore-comments/input/vercel.cjs [test] (ecmascript, worker loader)", ((__turbopack_context__) => {
__turbopack_context__.v(function(Ctor, opts) { __turbopack_context__.v(__turbopack_context__.r("[turbopack-ecmascript]/worker/browser/createWorker.ts [test] (ecmascript)")["default"]("output/0uxq_crates_turbopack-tests_tests_snapshot_imports_ignore-comments_output_0uy0mni._.js", ["output/0_9x_turbopack-tests_tests_snapshot_imports_ignore-comments_input_vercel_cjs_0j-fab5._.js","output/0rv8_turbopack-tests_tests_snapshot_imports_ignore-comments_input_vercel_cjs_02p77ng._.js"]));
return __turbopack_context__.b(Ctor, "output/0uxq_crates_turbopack-tests_tests_snapshot_imports_ignore-comments_output_0uy0mni._.js", ["output/0_9x_turbopack-tests_tests_snapshot_imports_ignore-comments_input_vercel_cjs_0j-fab5._.js","output/0rv8_turbopack-tests_tests_snapshot_imports_ignore-comments_input_vercel_cjs_02p77ng._.js"], opts);
});
}), }),
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/ignore-comments/input/ignore-worker.cjs (static in ecmascript)", ((__turbopack_context__) => { "[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/ignore-comments/input/ignore-worker.cjs (static in ecmascript)", ((__turbopack_context__) => {
...@@ -45,4 +108,4 @@ function foo(plugin) { ...@@ -45,4 +108,4 @@ function foo(plugin) {
}), }),
]); ]);
//# sourceMappingURL=1do3_crates_turbopack-tests_tests_snapshot_imports_ignore-comments_input_09jmqs-._.js.map //# sourceMappingURL=%5Broot-of-the-server%5D__08ofy-h._.js.map
\ No newline at end of file \ No newline at end of file
{
"version": 3,
"sources": [],
"sections": [
{"offset": {"line": 3, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/ignore-comments/input/vercel.cjs"],"sourcesContent":["module.exports = 'turbopack'\n"],"names":["module","exports"],"mappings":"AAAAA,OAAOC,OAAO,GAAG"}},
{"offset": {"line": 11, "column": 0}, "map": {"version":3,"sources":["turbopack:///[turbopack-ecmascript]/worker/browser/createWorker.ts"],"sourcesContent":["// Embedded worker-runtime helper. This file is bundled as a regular module and\n// `__turbopack_require__`d by the generated web-worker loader code.\n//\n// The chunk-URL builder, the chunk base path and the asset suffix are read from\n// the shared `__turbopack_chunk_relative_url__` / `__turbopack_chunk_base_path__`\n// / `__turbopack_chunk_asset_suffix__` runtime primitives. The worker base-path\n// override and forwarded-global names are baked into this module at build time by\n// `turbopack-ecmascript` replacing the `_TURBOPACK_WORKER_BASE_PATH_` /\n// `_TURBOPACK_WORKER_FORWARDED_GLOBALS_` free variables, and the forwarded-global\n// values are read from `globalThis`.\ndeclare const __turbopack_chunk_relative_url__: (\n chunkPath: string,\n basePath?: string\n) => string\n\ndeclare const __turbopack_chunk_base_path__: string\ndeclare const __turbopack_chunk_asset_suffix__: string\n\ndeclare const _TURBOPACK_WORKER_FORWARDED_GLOBALS_: string[]\ndeclare const _TURBOPACK_WORKER_BASE_PATH_: string | null\n\ntype WorkerConstructor = new (url: URL, options?: object) => Worker\n\n/**\n * Creates a web worker by instantiating the given WorkerConstructor with the\n * appropriate URL and options.\n *\n * The entrypoint is a pre-compiled worker runtime file. The params configure\n * which module chunks to load and which module to run as the entry point.\n *\n * The params are a JSON array of the following structure:\n * `[TURBOPACK_NEXT_CHUNK_URLS, ASSET_SUFFIX, ...workerForwardedGlobals values]`\n *\n * @param WorkerConstructor The Worker or SharedWorker constructor\n * @param entrypoint path to the worker entrypoint chunk\n * @param moduleChunks list of module chunk paths to load\n * @param workerOptions options to pass to the Worker constructor (optional)\n */\nfunction createWorker(\n WorkerConstructor: WorkerConstructor,\n entrypoint: string,\n moduleChunks: string[],\n workerOptions?: object\n): Worker {\n const isSharedWorker = WorkerConstructor.name === 'SharedWorker'\n\n // `WORKER_BASE_PATH` overrides `CHUNK_BASE_PATH` for the entrypoint and the\n // module chunks loaded inside the worker, keeping them same-origin to each\n // other when `CHUNK_BASE_PATH` (= `assetPrefix`) is a cross-origin CDN.\n // `null` falls back; an empty string is treated as a literal empty prefix.\n const workerBasePath =\n _TURBOPACK_WORKER_BASE_PATH_ ?? __turbopack_chunk_base_path__\n\n const chunkUrls = moduleChunks\n .map((chunk) => __turbopack_chunk_relative_url__(chunk, workerBasePath))\n .reverse()\n const params: unknown[] = [chunkUrls, __turbopack_chunk_asset_suffix__]\n const globals = _TURBOPACK_WORKER_FORWARDED_GLOBALS_\n for (let i = 0; i < globals.length; i++) {\n params.push((globalThis as Record<string, unknown>)[globals[i]])\n }\n\n const url = new URL(\n __turbopack_chunk_relative_url__(entrypoint, workerBasePath),\n location.origin\n )\n const paramsJson = JSON.stringify(params)\n if (isSharedWorker) {\n url.searchParams.set('params', paramsJson)\n } else {\n url.hash = '#params=' + encodeURIComponent(paramsJson)\n }\n\n // Remove type: \"module\" from options since our worker entrypoint is not a module\n const options = workerOptions\n ? { ...workerOptions, type: undefined }\n : undefined\n return new WorkerConstructor(url, options)\n}\n\n/**\n * Returns a function, that when called with the constructor + any options calls `createWorker()`.\n * The worker configuration (`entrypoint`, `moduleChunks`) is passed by `turbopack-ecmascript`\n * at build time, leaving the runtime caller to only supply the constructor and options.\n */\nexport default function generateCreateWorker(\n entrypoint: string,\n moduleChunks: string[]\n) {\n return (\n WorkerConstructor: { new (url: URL, options?: object): Worker },\n workerOptions?: object\n ) => createWorker(WorkerConstructor, entrypoint, moduleChunks, workerOptions)\n}\n"],"names":["createWorker","WorkerConstructor","entrypoint","moduleChunks","workerOptions","isSharedWorker","name","workerBasePath","chunkUrls","map","chunk","reverse","params","globals","i","length","push","globalThis","url","URL","location","origin","paramsJson","JSON","stringify","searchParams","set","hash","encodeURIComponent","options","type","undefined","generateCreateWorker"],"mappings":"AAAA,+EAA+E;AAC/E,oEAAoE;AACpE,EAAE;AACF,gFAAgF;AAChF,kFAAkF;AAClF,gFAAgF;AAChF,kFAAkF;AAClF,wEAAwE;AACxE,kFAAkF;AAClF,qCAAqC;;;;;AAcrC;;;;;;;;;;;;;;CAcC,GACD,SAASA,aACPC,iBAAoC,EACpCC,UAAkB,EAClBC,YAAsB,EACtBC,aAAsB;IAEtB,MAAMC,iBAAiBJ,kBAAkBK,IAAI,KAAK;IAElD,4EAA4E;IAC5E,2EAA2E;IAC3E,wEAAwE;IACxE,2EAA2E;IAC3E,MAAMC,iBACJ;IAEF,MAAMC,YAAYL,aACfM,GAAG,CAAC,CAACC,QAAU,yDAAiCA,OAAOH,iBACvDI,OAAO;IACV,MAAMC,SAAoB;QAACJ;;KAA4C;IACvE,MAAMK;IACN,IAAK,IAAIC,IAAI,GAAGA,IAAID,QAAQE,MAAM,EAAED,IAAK;QACvCF,OAAOI,IAAI,CAAC,AAACC,UAAsC,CAACJ,OAAO,CAACC,EAAE,CAAC;IACjE;IAEA,MAAMI,MAAM,IAAIC,IACd,yDAAiCjB,YAAYK,iBAC7Ca,SAASC,MAAM;IAEjB,MAAMC,aAAaC,KAAKC,SAAS,CAACZ;IAClC,IAAIP,gBAAgB;QAClBa,IAAIO,YAAY,CAACC,GAAG,CAAC,UAAUJ;IACjC,OAAO;QACLJ,IAAIS,IAAI,GAAG,aAAaC,mBAAmBN;IAC7C;IAEA,iFAAiF;IACjF,MAAMO,UAAUzB,gBACZ;QAAE,GAAGA,aAAa;QAAE0B,MAAMC;IAAU,IACpCA;IACJ,OAAO,IAAI9B,kBAAkBiB,KAAKW;AACpC;AAOe,SAASG,qBACtB9B,UAAkB,EAClBC,YAAsB;IAEtB,OAAO,CACLF,mBACAG,gBACGJ,aAAaC,mBAAmBC,YAAYC,cAAcC;AACjE"}},
{"offset": {"line": 83, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/ignore-comments/input/index.js"],"sourcesContent":["import('./vercel.mjs').then(console.log)\nimport(/* webpackIgnore: false */ './vercel.mjs').then(console.log)\nconsole.log(require('./vercel.cjs'))\nnew Worker(\n /* turbopackIgnore: false */ new URL('./vercel.cjs', import.meta.url)\n)\n\n// turbopack shouldn't attempt to bundle these, and they should be preserved in the output\nimport(/* webpackIgnore: true */ './ignore.mjs')\nimport(/* turbopackIgnore: true */ './ignore.mjs')\n\n// this should work for cjs requires too\nrequire(/* webpackIgnore: true */ './ignore.cjs')\nrequire(/* turbopackIgnore: true */ './ignore.cjs')\n\nnew Worker(\n /* turbopackIgnore: true */ new URL('./ignore-worker.cjs', import.meta.url)\n)\nnew Worker(\n /* webpackIgnore: true */ new URL('./ignore-worker.cjs', import.meta.url)\n)\n\nexport function foo(plugin) {\n return require(/* turbopackIgnore: true */ plugin)\n}\n"],"names":["then","console","log","Worker","require","foo","plugin"],"mappings":";;;;;;;;;AAAA,gKAAuBA,IAAI,CAACC,QAAQC,GAAG;AACvC,gKAAkDF,IAAI,CAACC,QAAQC,GAAG;AAClED,QAAQC,GAAG;AACX,iKAAIC;AAIJ,0FAA0F;AAC1F,MAAM,CAAC,uBAAuB,GAAG;AACjC,MAAM,CAAC,yBAAyB,GAAG;AAEnC,wCAAwC;AACxCC,QAAQ,uBAAuB,GAAG;AAClCA,QAAQ,yBAAyB,GAAG;AAEpC,IAAID;AAGJ,IAAIA;AAIG,SAASE,IAAIC,MAAM;IACxB,OAAOF,QAAQ,yBAAyB,GAAGE;AAC7C"}}]
}
\ No newline at end of file
var RUNTIME_PUBLIC_PATH = "output/[turbopack]_runtime.js"; var RUNTIME_PUBLIC_PATH = "output/[turbopack]_runtime.js";
var RELATIVE_ROOT_PATH = "../../../../../../.."; var RELATIVE_ROOT_PATH = "../../../../../../..";
var ASSET_PREFIX = "/"; var ASSET_PREFIX = "/";
var WORKER_FORWARDED_GLOBALS = [];
/** /**
* This file contains runtime types and functions that are shared between all * This file contains runtime types and functions that are shared between all
* TurboPack ECMAScript runtimes. * TurboPack ECMAScript runtimes.
* *
* It will be prepended to the runtime code of each runtime. * It will be prepended to the runtime code of each runtime.
*/ /* eslint-disable @typescript-eslint/no-unused-vars */ /// <reference path="./runtime-types.d.ts" /> */ /* eslint-disable @typescript-eslint/no-unused-vars */ /// <reference path="./runtime-types.d.ts" />
/// <reference path="./async-module.ts" />
/** /**
* Describes why a module was instantiated. * Describes why a module was instantiated.
* Shared between browser and Node.js runtimes. * Shared between browser and Node.js runtimes.
...@@ -343,25 +343,6 @@ contextPrototype.f = moduleContext; ...@@ -343,25 +343,6 @@ contextPrototype.f = moduleContext;
*/ function getChunkPath(chunkData) { */ function getChunkPath(chunkData) {
return typeof chunkData === 'string' ? chunkData : chunkData.path; return typeof chunkData === 'string' ? chunkData : chunkData.path;
} }
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
};
}
// Load the CompressedmoduleFactories of a chunk into the `moduleFactories` Map. // Load the CompressedmoduleFactories of a chunk into the `moduleFactories` Map.
// The CompressedModuleFactories format is // The CompressedModuleFactories format is
// - 1 or more module ids // - 1 or more module ids
...@@ -411,110 +392,6 @@ function installCompressedModuleFactories(chunkModules, offset, moduleFactories, ...@@ -411,110 +392,6 @@ function installCompressedModuleFactories(chunkModules, offset, moduleFactories,
i = end + 1; // end is pointing at the last factory advance to the next id or the end of the array. i = end + 1; // end is pointing at the last factory advance to the next id or the end of the array.
} }
} }
// 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 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;
/** /**
* A pseudo "fake" URL object to resolve to its relative path. * A pseudo "fake" URL object to resolve to its relative path.
* *
...@@ -788,30 +665,6 @@ function loadWebAssemblyModule(chunkPath, _edgeModule) { ...@@ -788,30 +665,6 @@ function loadWebAssemblyModule(chunkPath, _edgeModule) {
return compileWebAssemblyFromPath(resolved); return compileWebAssemblyFromPath(resolved);
} }
contextPrototype.u = loadWebAssemblyModule; contextPrototype.u = loadWebAssemblyModule;
/**
* Creates a Node.js worker thread by instantiating the given WorkerConstructor
* with the appropriate path and options, including forwarded globals.
*
* @param WorkerConstructor The Worker constructor from worker_threads
* @param workerPath Path to the worker entry chunk
* @param workerOptions options to pass to the Worker constructor (optional)
*/ function createWorker(WorkerConstructor, workerPath, workerOptions) {
// Build the forwarded globals object
const forwardedGlobals = {};
for (const name of WORKER_FORWARDED_GLOBALS){
forwardedGlobals[name] = globalThis[name];
}
// Merge workerData with forwarded globals
const existingWorkerData = workerOptions?.workerData || {};
const options = {
...workerOptions,
workerData: {
...typeof existingWorkerData === 'object' ? existingWorkerData : {},
__turbopack_globals__: forwardedGlobals
}
};
return new WorkerConstructor(workerPath, options);
}
const regexJsUrl = /\.js(?:\?[^#]*)?(?:#.*)?$/; const regexJsUrl = /\.js(?:\?[^#]*)?(?:#.*)?$/;
/** /**
* Checks if a given path/URL ends with .js, optionally followed by ?query or #fragment. * Checks if a given path/URL ends with .js, optionally followed by ?query or #fragment.
...@@ -831,7 +684,6 @@ nodeContextPrototype.M = moduleFactories; ...@@ -831,7 +684,6 @@ nodeContextPrototype.M = moduleFactories;
// Cast moduleCache to ModuleWithDirection for production mode // Cast moduleCache to ModuleWithDirection for production mode
nodeContextPrototype.c = moduleCache; nodeContextPrototype.c = moduleCache;
nodeContextPrototype.R = resolvePathFromModule; nodeContextPrototype.R = resolvePathFromModule;
nodeContextPrototype.b = createWorker;
nodeContextPrototype.C = clearChunkCache; nodeContextPrototype.C = clearChunkCache;
function instantiateModule(id, sourceType, sourceData) { function instantiateModule(id, sourceType, sourceData) {
const moduleFactory = moduleFactories.get(id); const moduleFactory = moduleFactories.get(id);
......
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