Unverified Commit 5b52e777 authored by OJ Kwon's avatar OJ Kwon Committed by GitHub

feat(turbopack): support basic next/dynamic (#56389)

Closes WEB-1702

This PR implements initial support for the `next/dynamic` in Turbopack,
more specifically resolving some hydration errors and other components
boot up cases.

Previously, turbopack had partial next/dynamic support via its own mode
(https://github.com/vercel/next.js/pull/56389/files#diff-e1af4f79cb88a73f819a25443d15ed4b1ffabcbb879256caa59b751fad46d7c4L68),
which does a transform against `next/dynamic` wrapped import to embed
dynamically resolvable chunk ids like
(https://github.com/vercel/next.js/blob/ad42b610c25b72561ad367b82b1c7383fd2a5dd2/packages/next-swc/crates/next-transform-dynamic/tests/fixture/wrapped-import/output-turbo-dev-server.js).

However, since next.js relies on static path to the chunks to the
dynamic import and passing those ids in between client-server to ensure
component load (and avoid hydration errors), it doesn't work out of the
box. This PR changes turbopack's behavior to closely mimic what current
next.js's webpack plugin does, by

1. Traverse the module graph, find out `dynamic(import())`
2. Generate chunks for those imports, creates a partial LoadableManifest
per each imports
3. Merge partial manifest into a single `react-loadable-manifest.json`
4. For the id, use static (Webpack mode) instead of dynamic so we can
embed it in `react-loadable-manifest` as well as next.js can use it to
pass it between server-client context.

I left a small comment to the implementation
(https://github.com/vercel/next.js/pull/56389/files#diff-bf12ed2c69d0bc89a06884779da4ae44967eb8becada031dea12bedef28e2622R155)
for the lifecycle of this feature in case to fix further.

This makes to pass most of the basic next-dynamic related integration
tests, except if the import have webpack specific features like
https://github.com/vercel/next.js/blob/ad42b610c25b72561ad367b82b1c7383fd2a5dd2/test/development/basic/next-dynamic/pages/dynamic/multiple-modules.js#L5.

---------
Co-authored-by: 's avatarTim Neutkens <tim@timneutkens.nl>
parent 8f2fd2e7
...@@ -3440,6 +3440,7 @@ dependencies = [ ...@@ -3440,6 +3440,7 @@ dependencies = [
"either", "either",
"fxhash", "fxhash",
"hex", "hex",
"next-transform-dynamic",
"next-transform-font", "next-transform-font",
"once_cell", "once_cell",
"pathdiff", "pathdiff",
......
...@@ -15,7 +15,6 @@ either = "1" ...@@ -15,7 +15,6 @@ either = "1"
fxhash = "0.2.1" fxhash = "0.2.1"
hex = "0.4.3" hex = "0.4.3"
once_cell = { workspace = true } once_cell = { workspace = true }
next-transform-font = {workspace = true}
pathdiff = "0.2.0" pathdiff = "0.2.0"
regex = "1.5" regex = "1.5"
rustc-hash = "1" rustc-hash = "1"
...@@ -24,6 +23,9 @@ serde_json = "1" ...@@ -24,6 +23,9 @@ serde_json = "1"
sha1 = "0.10.1" sha1 = "0.10.1"
tracing = { version = "0.1.37" } tracing = { version = "0.1.37" }
next-transform-dynamic = { workspace = true }
next-transform-font = { workspace = true }
turbopack-binding = { workspace = true, features = [ turbopack-binding = { workspace = true, features = [
"__swc_core", "__swc_core",
"__swc_core_next_core", "__swc_core_next_core",
......
...@@ -37,6 +37,7 @@ use std::{cell::RefCell, path::PathBuf, rc::Rc, sync::Arc}; ...@@ -37,6 +37,7 @@ use std::{cell::RefCell, path::PathBuf, rc::Rc, sync::Arc};
use auto_cjs::contains_cjs; use auto_cjs::contains_cjs;
use either::Either; use either::Either;
use fxhash::FxHashSet; use fxhash::FxHashSet;
use next_transform_dynamic::{next_dynamic, NextDynamicMode};
use next_transform_font::next_font_loaders; use next_transform_font::next_font_loaders;
use serde::Deserialize; use serde::Deserialize;
use turbopack_binding::swc::{ use turbopack_binding::swc::{
...@@ -58,7 +59,6 @@ mod auto_cjs; ...@@ -58,7 +59,6 @@ mod auto_cjs;
pub mod cjs_optimizer; pub mod cjs_optimizer;
pub mod disallow_re_export_all_in_page; pub mod disallow_re_export_all_in_page;
pub mod named_import_transform; pub mod named_import_transform;
pub mod next_dynamic;
pub mod next_ssg; pub mod next_ssg;
pub mod optimize_barrel; pub mod optimize_barrel;
pub mod optimize_server_react; pub mod optimize_server_react;
...@@ -226,7 +226,7 @@ where ...@@ -226,7 +226,7 @@ where
!opts.disable_next_ssg !opts.disable_next_ssg
), ),
amp_attributes::amp_attributes(), amp_attributes::amp_attributes(),
next_dynamic::next_dynamic( next_dynamic(
opts.is_development, opts.is_development,
opts.is_server, opts.is_server,
match &opts.server_components { match &opts.server_components {
...@@ -238,6 +238,7 @@ where ...@@ -238,6 +238,7 @@ where
}, },
_ => false, _ => false,
}, },
NextDynamicMode::Webpack,
file.name.clone(), file.name.clone(),
opts.pages_dir.clone() opts.pages_dir.clone()
), ),
......
This diff is collapsed.
...@@ -2,13 +2,13 @@ use std::path::PathBuf; ...@@ -2,13 +2,13 @@ use std::path::PathBuf;
use next_swc::{ use next_swc::{
disallow_re_export_all_in_page::disallow_re_export_all_in_page, disallow_re_export_all_in_page::disallow_re_export_all_in_page,
next_dynamic::next_dynamic,
next_ssg::next_ssg, next_ssg::next_ssg,
react_server_components::server_components, react_server_components::server_components,
server_actions::{ server_actions::{
server_actions, {self}, server_actions, {self},
}, },
}; };
use next_transform_dynamic::{next_dynamic, NextDynamicMode};
use next_transform_font::{next_font_loaders, Config as FontLoaderConfig}; use next_transform_font::{next_font_loaders, Config as FontLoaderConfig};
use turbopack_binding::swc::{ use turbopack_binding::swc::{
core::{ core::{
...@@ -56,6 +56,7 @@ fn next_dynamic_errors(input: PathBuf) { ...@@ -56,6 +56,7 @@ fn next_dynamic_errors(input: PathBuf) {
true, true,
false, false,
false, false,
NextDynamicMode::Webpack,
FileName::Real(PathBuf::from("/some-project/src/some-file.js")), FileName::Real(PathBuf::from("/some-project/src/some-file.js")),
Some("/some-project/src".into()), Some("/some-project/src".into()),
) )
......
...@@ -4,7 +4,6 @@ use next_swc::{ ...@@ -4,7 +4,6 @@ use next_swc::{
amp_attributes::amp_attributes, amp_attributes::amp_attributes,
cjs_optimizer::cjs_optimizer, cjs_optimizer::cjs_optimizer,
named_import_transform::named_import_transform, named_import_transform::named_import_transform,
next_dynamic::next_dynamic,
next_ssg::next_ssg, next_ssg::next_ssg,
optimize_barrel::optimize_barrel, optimize_barrel::optimize_barrel,
optimize_server_react::optimize_server_react, optimize_server_react::optimize_server_react,
...@@ -15,6 +14,7 @@ use next_swc::{ ...@@ -15,6 +14,7 @@ use next_swc::{
}, },
shake_exports::{shake_exports, Config as ShakeExportsConfig}, shake_exports::{shake_exports, Config as ShakeExportsConfig},
}; };
use next_transform_dynamic::{next_dynamic, NextDynamicMode};
use next_transform_font::{next_font_loaders, Config as FontLoaderConfig}; use next_transform_font::{next_font_loaders, Config as FontLoaderConfig};
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use turbopack_binding::swc::{ use turbopack_binding::swc::{
...@@ -64,6 +64,7 @@ fn next_dynamic_fixture(input: PathBuf) { ...@@ -64,6 +64,7 @@ fn next_dynamic_fixture(input: PathBuf) {
true, true,
false, false,
false, false,
NextDynamicMode::Webpack,
FileName::Real(PathBuf::from("/some-project/src/some-file.js")), FileName::Real(PathBuf::from("/some-project/src/some-file.js")),
Some("/some-project/src".into()), Some("/some-project/src".into()),
) )
...@@ -79,6 +80,7 @@ fn next_dynamic_fixture(input: PathBuf) { ...@@ -79,6 +80,7 @@ fn next_dynamic_fixture(input: PathBuf) {
false, false,
false, false,
false, false,
NextDynamicMode::Webpack,
FileName::Real(PathBuf::from("/some-project/src/some-file.js")), FileName::Real(PathBuf::from("/some-project/src/some-file.js")),
Some("/some-project/src".into()), Some("/some-project/src".into()),
) )
...@@ -94,6 +96,7 @@ fn next_dynamic_fixture(input: PathBuf) { ...@@ -94,6 +96,7 @@ fn next_dynamic_fixture(input: PathBuf) {
false, false,
true, true,
false, false,
NextDynamicMode::Webpack,
FileName::Real(PathBuf::from("/some-project/src/some-file.js")), FileName::Real(PathBuf::from("/some-project/src/some-file.js")),
Some("/some-project/src".into()), Some("/some-project/src".into()),
) )
...@@ -116,6 +119,7 @@ fn app_dir_next_dynamic_fixture(input: PathBuf) { ...@@ -116,6 +119,7 @@ fn app_dir_next_dynamic_fixture(input: PathBuf) {
true, true,
false, false,
true, true,
NextDynamicMode::Webpack,
FileName::Real(PathBuf::from("/some-project/src/some-file.js")), FileName::Real(PathBuf::from("/some-project/src/some-file.js")),
Some("/some-project/src".into()), Some("/some-project/src".into()),
) )
...@@ -131,6 +135,7 @@ fn app_dir_next_dynamic_fixture(input: PathBuf) { ...@@ -131,6 +135,7 @@ fn app_dir_next_dynamic_fixture(input: PathBuf) {
false, false,
false, false,
true, true,
NextDynamicMode::Webpack,
FileName::Real(PathBuf::from("/some-project/src/some-file.js")), FileName::Real(PathBuf::from("/some-project/src/some-file.js")),
Some("/some-project/src".into()), Some("/some-project/src".into()),
) )
...@@ -146,6 +151,7 @@ fn app_dir_next_dynamic_fixture(input: PathBuf) { ...@@ -146,6 +151,7 @@ fn app_dir_next_dynamic_fixture(input: PathBuf) {
false, false,
true, true,
true, true,
NextDynamicMode::Webpack,
FileName::Real(PathBuf::from("/some-project/src/some-file.js")), FileName::Real(PathBuf::from("/some-project/src/some-file.js")),
Some("/some-project/src".into()), Some("/some-project/src".into()),
) )
......
use std::collections::HashMap;
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
use next_core::{ use next_core::{
app_structure::{ app_structure::{
...@@ -21,7 +23,8 @@ use next_core::{ ...@@ -21,7 +23,8 @@ use next_core::{
next_edge::route_regex::get_named_middleware_regex, next_edge::route_regex::get_named_middleware_regex,
next_manifests::{ next_manifests::{
AppBuildManifest, AppPathsManifest, BuildManifest, ClientReferenceManifest, AppBuildManifest, AppPathsManifest, BuildManifest, ClientReferenceManifest,
EdgeFunctionDefinition, MiddlewareMatcher, MiddlewaresManifestV2, PagesManifest, Regions, EdgeFunctionDefinition, LoadableManifest, MiddlewareMatcher, MiddlewaresManifestV2,
PagesManifest, Regions,
}, },
next_server::{ next_server::{
get_server_module_options_context, get_server_resolve_options_context, get_server_module_options_context, get_server_resolve_options_context,
...@@ -39,7 +42,7 @@ use turbopack_binding::{ ...@@ -39,7 +42,7 @@ use turbopack_binding::{
turbopack::{ turbopack::{
core::{ core::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
chunk::{ChunkingContext, EvaluatableAssets}, chunk::{availability_info::AvailabilityInfo, ChunkingContext, EvaluatableAssets},
file_source::FileSource, file_source::FileSource,
module::Module, module::Module,
output::{OutputAsset, OutputAssets}, output::{OutputAsset, OutputAssets},
...@@ -53,6 +56,10 @@ use turbopack_binding::{ ...@@ -53,6 +56,10 @@ use turbopack_binding::{
}; };
use crate::{ use crate::{
dynamic_imports::{
collect_chunk_group, collect_evaluated_chunk_group, collect_next_dynamic_imports,
DynamicImportedChunks,
},
project::Project, project::Project,
route::{Endpoint, Route, Routes, WrittenEndpoint}, route::{Endpoint, Route, Routes, WrittenEndpoint},
server_actions::create_server_actions_manifest, server_actions::create_server_actions_manifest,
...@@ -679,6 +686,67 @@ impl AppEndpoint { ...@@ -679,6 +686,67 @@ impl AppEndpoint {
))) )))
} }
async fn create_react_loadable_manifest(
dynamic_import_entries: Vc<DynamicImportedChunks>,
ty: &'static str,
node_root: Vc<FileSystemPath>,
pathname: &str,
) -> Result<Vc<OutputAssets>> {
let dynamic_import_entries = &*dynamic_import_entries.await?;
let mut output = vec![];
let mut loadable_manifest: HashMap<String, LoadableManifest> = Default::default();
for (origin, dynamic_imports) in dynamic_import_entries.into_iter() {
let origin_path = &*origin.ident().path().await?;
for (import, chunk_output) in dynamic_imports {
let chunk_output = chunk_output.await?;
output.extend(chunk_output.iter().copied());
let id = format!("{} -> {}", origin_path, import);
let server_path = node_root.join("server".to_string());
let server_path_value = server_path.await?;
let files = chunk_output
.iter()
.map(move |&file| {
let server_path_value = server_path_value.clone();
async move {
Ok(server_path_value
.get_path_to(&*file.ident().path().await?)
.map(|path| path.to_string()))
}
})
.try_flat_join()
.await?;
let manifest_item = LoadableManifest {
id: id.clone(),
files,
};
loadable_manifest.insert(id, manifest_item);
}
}
let loadable_path_prefix = get_asset_prefix_from_pathname(pathname);
let loadable_manifest = Vc::upcast(VirtualOutputAsset::new(
node_root.join(format!(
"server/app{loadable_path_prefix}/{ty}/react-loadable-manifest.json",
)),
AssetContent::file(
FileContent::Content(File::from(serde_json::to_string_pretty(
&loadable_manifest,
)?))
.cell(),
),
));
output.push(loadable_manifest);
Ok(Vc::cell(output))
}
let endpoint_output = match app_entry.config.await?.runtime.unwrap_or_default() { let endpoint_output = match app_entry.config.await?.runtime.unwrap_or_default() {
NextRuntime::Edge => { NextRuntime::Edge => {
// create edge chunks // create edge chunks
...@@ -714,7 +782,7 @@ impl AppEndpoint { ...@@ -714,7 +782,7 @@ impl AppEndpoint {
let files = chunking_context.evaluated_chunk_group( let files = chunking_context.evaluated_chunk_group(
app_entry.rsc_entry.ident(), app_entry.rsc_entry.ident(),
Vc::cell(evaluatable_assets), Vc::cell(evaluatable_assets.clone()),
); );
server_assets.extend(files.await?.iter().copied()); server_assets.extend(files.await?.iter().copied());
...@@ -801,6 +869,24 @@ impl AppEndpoint { ...@@ -801,6 +869,24 @@ impl AppEndpoint {
)?; )?;
server_assets.push(app_paths_manifest_output); server_assets.push(app_paths_manifest_output);
// create react-loadable-manifest for next/dynamic
let dynamic_import_modules =
collect_next_dynamic_imports(app_entry.rsc_entry).await?;
let dynamic_import_entries = collect_evaluated_chunk_group(
chunking_context,
dynamic_import_modules,
Vc::cell(evaluatable_assets),
)
.await?;
let loadable_manifest_output = create_react_loadable_manifest(
dynamic_import_entries,
ty,
node_root,
&app_entry.pathname,
)
.await?;
server_assets.extend(loadable_manifest_output.await?.iter().copied());
AppEndpointOutput::Edge { AppEndpointOutput::Edge {
files, files,
server_assets: Vc::cell(server_assets), server_assets: Vc::cell(server_assets),
...@@ -857,6 +943,25 @@ impl AppEndpoint { ...@@ -857,6 +943,25 @@ impl AppEndpoint {
)?; )?;
server_assets.push(app_paths_manifest_output); server_assets.push(app_paths_manifest_output);
// create react-loadable-manifest for next/dynamic
let availability_info = Value::new(AvailabilityInfo::Root);
let dynamic_import_modules =
collect_next_dynamic_imports(app_entry.rsc_entry).await?;
let dynamic_import_entries = collect_chunk_group(
this.app_project.project().rsc_chunking_context(),
dynamic_import_modules,
availability_info,
)
.await?;
let loadable_manifest_output = create_react_loadable_manifest(
dynamic_import_entries,
ty,
node_root,
&app_entry.pathname,
)
.await?;
server_assets.extend(loadable_manifest_output.await?.iter().copied());
AppEndpointOutput::NodeJs { AppEndpointOutput::NodeJs {
rsc_chunk, rsc_chunk,
server_assets: Vc::cell(server_assets), server_assets: Vc::cell(server_assets),
......
This diff is collapsed.
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#![feature(async_fn_in_trait)] #![feature(async_fn_in_trait)]
mod app; mod app;
mod dynamic_imports;
mod entrypoints; mod entrypoints;
mod middleware; mod middleware;
mod pages; mod pages;
......
use std::collections::HashMap;
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
use indexmap::IndexMap; use indexmap::IndexMap;
use next_core::{ use next_core::{
...@@ -11,8 +13,8 @@ use next_core::{ ...@@ -11,8 +13,8 @@ use next_core::{
next_dynamic::NextDynamicTransition, next_dynamic::NextDynamicTransition,
next_edge::route_regex::get_named_middleware_regex, next_edge::route_regex::get_named_middleware_regex,
next_manifests::{ next_manifests::{
BuildManifest, EdgeFunctionDefinition, MiddlewareMatcher, MiddlewaresManifestV2, BuildManifest, EdgeFunctionDefinition, LoadableManifest, MiddlewareMatcher,
PagesManifest, MiddlewaresManifestV2, PagesManifest,
}, },
next_pages::create_page_ssr_entry_module, next_pages::create_page_ssr_entry_module,
next_server::{ next_server::{
...@@ -37,7 +39,7 @@ use turbopack_binding::{ ...@@ -37,7 +39,7 @@ use turbopack_binding::{
build::BuildChunkingContext, build::BuildChunkingContext,
core::{ core::{
asset::AssetContent, asset::AssetContent,
chunk::{ChunkingContext, EvaluatableAssets}, chunk::{availability_info::AvailabilityInfo, ChunkingContext, EvaluatableAssets},
context::AssetContext, context::AssetContext,
file_source::FileSource, file_source::FileSource,
issue::{IssueSeverity, OptionIssueSource}, issue::{IssueSeverity, OptionIssueSource},
...@@ -63,6 +65,10 @@ use turbopack_binding::{ ...@@ -63,6 +65,10 @@ use turbopack_binding::{
}; };
use crate::{ use crate::{
dynamic_imports::{
collect_chunk_group, collect_evaluated_chunk_group, collect_next_dynamic_imports,
DynamicImportedChunks,
},
project::Project, project::Project,
route::{Endpoint, Route, Routes, WrittenEndpoint}, route::{Endpoint, Route, Routes, WrittenEndpoint},
server_paths::all_server_paths, server_paths::all_server_paths,
...@@ -609,9 +615,21 @@ impl PageEndpoint { ...@@ -609,9 +615,21 @@ impl PageEndpoint {
evaluatable_assets.push(evaluatable); evaluatable_assets.push(evaluatable);
let edge_files = edge_chunking_context let edge_files = edge_chunking_context
.evaluated_chunk_group(ssr_module.ident(), Vc::cell(evaluatable_assets)); .evaluated_chunk_group(ssr_module.ident(), Vc::cell(evaluatable_assets.clone()));
let dynamic_import_modules = collect_next_dynamic_imports(ssr_module).await?;
let dynamic_import_entries = collect_evaluated_chunk_group(
edge_chunking_context,
dynamic_import_modules,
Vc::cell(evaluatable_assets.clone()),
)
.await?;
Ok(SsrChunk::Edge { files: edge_files }.cell()) Ok(SsrChunk::Edge {
files: edge_files,
dynamic_import_entries,
}
.cell())
} else { } else {
let ssr_module = create_page_ssr_entry_module( let ssr_module = create_page_ssr_entry_module(
this.pathname, this.pathname,
...@@ -633,8 +651,15 @@ impl PageEndpoint { ...@@ -633,8 +651,15 @@ impl PageEndpoint {
runtime_entries, runtime_entries,
); );
let availability_info = Value::new(AvailabilityInfo::Root);
let dynamic_import_modules = collect_next_dynamic_imports(ssr_module).await?;
let dynamic_import_entries =
collect_chunk_group(chunking_context, dynamic_import_modules, availability_info)
.await?;
Ok(SsrChunk::NodeJs { Ok(SsrChunk::NodeJs {
entry: ssr_entry_chunk, entry: ssr_entry_chunk,
dynamic_import_entries,
} }
.cell()) .cell())
} }
...@@ -728,6 +753,78 @@ impl PageEndpoint { ...@@ -728,6 +753,78 @@ impl PageEndpoint {
))) )))
} }
#[turbo_tasks::function]
async fn react_loadable_manifest(
self: Vc<Self>,
dynamic_import_entries: Vc<DynamicImportedChunks>,
) -> Result<Vc<OutputAssets>> {
let this = self.await?;
let node_root = this.pages_project.project().node_root();
let pages_dir = this.pages_project.pages_dir().await?;
let dynamic_import_entries = &*dynamic_import_entries.await?;
let mut output = vec![];
let mut loadable_manifest: HashMap<String, LoadableManifest> = Default::default();
for (origin, dynamic_imports) in dynamic_import_entries.into_iter() {
let origin_path = &*origin.ident().path().await?;
for (import, chunk_output) in dynamic_imports {
let chunk_output = chunk_output.await?;
output.extend(chunk_output.iter().copied());
// https://github.com/vercel/next.js/blob/b7c85b87787283d8fb86f705f67bdfabb6b654bb/packages/next-swc/crates/next-transform-dynamic/src/lib.rs#L230
// For the pages dir, next_dynamic transform puts relative paths to the pages
// dir for the origin import.
let id = format!(
"{} -> {}",
pages_dir
.get_path_to(origin_path)
.map_or_else(|| origin_path.to_string(), |path| path.to_string()),
import
);
let server_path = node_root.join("server".to_string());
let server_path_value = server_path.await?;
let files = chunk_output
.iter()
.map(move |file| {
let server_path_value = server_path_value.clone();
async move {
Ok(server_path_value
.get_path_to(&*file.ident().path().await?)
.map(|path| path.to_string()))
}
})
.try_flat_join()
.await?;
let manifest_item = LoadableManifest {
id: id.clone(),
files,
};
loadable_manifest.insert(id, manifest_item);
}
}
let loadable_path_prefix = get_asset_prefix_from_pathname(&this.pathname.await?);
let loadable_manifest = Vc::upcast(VirtualOutputAsset::new(
node_root.join(format!(
"server/pages{loadable_path_prefix}/react-loadable-manifest.json"
)),
AssetContent::file(
FileContent::Content(File::from(serde_json::to_string_pretty(
&loadable_manifest,
)?))
.cell(),
),
));
output.push(loadable_manifest);
Ok(Vc::cell(output))
}
#[turbo_tasks::function] #[turbo_tasks::function]
async fn build_manifest( async fn build_manifest(
self: Vc<Self>, self: Vc<Self>,
...@@ -796,18 +893,27 @@ impl PageEndpoint { ...@@ -796,18 +893,27 @@ impl PageEndpoint {
}; };
let page_output = match *ssr_chunk.await? { let page_output = match *ssr_chunk.await? {
SsrChunk::NodeJs { entry } => { SsrChunk::NodeJs {
entry,
dynamic_import_entries,
} => {
let pages_manifest = self.pages_manifest(entry); let pages_manifest = self.pages_manifest(entry);
server_assets.push(pages_manifest); server_assets.push(pages_manifest);
server_assets.push(entry); server_assets.push(entry);
let loadable_manifest_output = self.react_loadable_manifest(dynamic_import_entries);
server_assets.extend(loadable_manifest_output.await?.iter().copied());
PageEndpointOutput::NodeJs { PageEndpointOutput::NodeJs {
entry_chunk: entry, entry_chunk: entry,
server_assets: Vc::cell(server_assets), server_assets: Vc::cell(server_assets),
client_assets: Vc::cell(client_assets), client_assets: Vc::cell(client_assets),
} }
} }
SsrChunk::Edge { files } => { SsrChunk::Edge {
files,
dynamic_import_entries,
} => {
let node_root = this.pages_project.project().node_root(); let node_root = this.pages_project.project().node_root();
let files_value = files.await?; let files_value = files.await?;
if let Some(&file) = files_value.first() { if let Some(&file) = files_value.first() {
...@@ -867,6 +973,9 @@ impl PageEndpoint { ...@@ -867,6 +973,9 @@ impl PageEndpoint {
)); ));
server_assets.push(middleware_manifest_v2); server_assets.push(middleware_manifest_v2);
let loadable_manifest_output = self.react_loadable_manifest(dynamic_import_entries);
server_assets.extend(loadable_manifest_output.await?.iter().copied());
PageEndpointOutput::Edge { PageEndpointOutput::Edge {
files, files,
server_assets: Vc::cell(server_assets), server_assets: Vc::cell(server_assets),
...@@ -1001,6 +1110,12 @@ impl PageEndpointOutput { ...@@ -1001,6 +1110,12 @@ impl PageEndpointOutput {
#[turbo_tasks::value] #[turbo_tasks::value]
pub enum SsrChunk { pub enum SsrChunk {
NodeJs { entry: Vc<Box<dyn OutputAsset>> }, NodeJs {
Edge { files: Vc<OutputAssets> }, entry: Vc<Box<dyn OutputAsset>>,
dynamic_import_entries: Vc<DynamicImportedChunks>,
},
Edge {
files: Vc<OutputAssets>,
dynamic_import_entries: Vc<DynamicImportedChunks>,
},
} }
...@@ -145,6 +145,17 @@ pub struct AppPathsManifest { ...@@ -145,6 +145,17 @@ pub struct AppPathsManifest {
pub node_server_app_paths: PagesManifest, pub node_server_app_paths: PagesManifest,
} }
// A struct represent a single entry in react-loadable-manifest.json.
// The manifest is in a format of:
// { [`${origin} -> ${imported}`]: { id: `${origin} -> ${imported}`, files:
// string[] } }
#[derive(Serialize, Default, Debug)]
#[serde(rename_all = "camelCase")]
pub struct LoadableManifest {
pub id: String,
pub files: Vec<String>,
}
#[derive(Serialize, Default, Debug)] #[derive(Serialize, Default, Debug)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct ServerReferenceManifest { pub struct ServerReferenceManifest {
......
...@@ -65,12 +65,7 @@ impl CustomTransformer for NextJsDynamic { ...@@ -65,12 +65,7 @@ impl CustomTransformer for NextJsDynamic {
}, },
self.is_server, self.is_server,
self.is_server_components, self.is_server_components,
NextDynamicMode::Turbopack { NextDynamicMode::Webpack,
dynamic_transition_name: match self.mode {
NextMode::Development => "next-client-chunks".to_string(),
NextMode::Build => "next-dynamic".to_string(),
},
},
FileName::Real(ctx.file_path_str.into()), FileName::Real(ctx.file_path_str.into()),
self.pages_dir.clone(), self.pages_dir.clone(),
)); ));
......
...@@ -19,6 +19,11 @@ use swc_core::{ ...@@ -19,6 +19,11 @@ use swc_core::{
quote, quote,
}; };
/// Creates a SWC visitor to transform `next/dynamic` calls to have the
/// corresponding `loadableGenerated` property.
///
/// [NOTE] We do not use `NextDynamicMode::Turbopack` yet. It isn't compatible
/// with current loadable manifest, which causes hydration errors.
pub fn next_dynamic( pub fn next_dynamic(
is_development: bool, is_development: bool,
is_server: bool, is_server: bool,
...@@ -89,6 +94,7 @@ enum NextDynamicPatcherState { ...@@ -89,6 +94,7 @@ enum NextDynamicPatcherState {
Webpack, Webpack,
/// In Turbo mode, contains a list of modules that need to be imported with /// In Turbo mode, contains a list of modules that need to be imported with
/// the given transition under a particular ident. /// the given transition under a particular ident.
#[allow(unused)]
Turbopack { Turbopack {
dynamic_transition_name: String, dynamic_transition_name: String,
imports: Vec<TurbopackImport>, imports: Vec<TurbopackImport>,
......
...@@ -78,6 +78,7 @@ import { ...@@ -78,6 +78,7 @@ import {
PAGES_MANIFEST, PAGES_MANIFEST,
PHASE_DEVELOPMENT_SERVER, PHASE_DEVELOPMENT_SERVER,
SERVER_REFERENCE_MANIFEST, SERVER_REFERENCE_MANIFEST,
REACT_LOADABLE_MANIFEST,
} from '../../../shared/lib/constants' } from '../../../shared/lib/constants'
import { getMiddlewareRouteMatcher } from '../../../shared/lib/router/utils/middleware-route-matcher' import { getMiddlewareRouteMatcher } from '../../../shared/lib/router/utils/middleware-route-matcher'
...@@ -117,6 +118,7 @@ import { normalizeMetadataRoute } from '../../../lib/metadata/get-metadata-route ...@@ -117,6 +118,7 @@ import { normalizeMetadataRoute } from '../../../lib/metadata/get-metadata-route
import { clearModuleContext } from '../render-server' import { clearModuleContext } from '../render-server'
import type { ActionManifest } from '../../../build/webpack/plugins/flight-client-entry-plugin' import type { ActionManifest } from '../../../build/webpack/plugins/flight-client-entry-plugin'
import { denormalizePagePath } from '../../../shared/lib/page-path/denormalize-page-path' import { denormalizePagePath } from '../../../shared/lib/page-path/denormalize-page-path'
import type { LoadableManifest } from '../../load-components'
const wsServer = new ws.Server({ noServer: true }) const wsServer = new ws.Server({ noServer: true })
...@@ -503,6 +505,7 @@ async function startWatcher(opts: SetupOpts) { ...@@ -503,6 +505,7 @@ async function startWatcher(opts: SetupOpts) {
ws, ws,
Map<string, AsyncIterator<any>> Map<string, AsyncIterator<any>>
>() >()
const loadbleManifests = new Map<string, LoadableManifest>()
const clients = new Set<ws>() const clients = new Set<ws>()
async function loadMiddlewareManifest( async function loadMiddlewareManifest(
...@@ -560,6 +563,16 @@ async function startWatcher(opts: SetupOpts) { ...@@ -560,6 +563,16 @@ async function startWatcher(opts: SetupOpts) {
) )
} }
async function loadLoadableManifest(
pageName: string,
type: 'app' | 'pages' = 'pages'
): Promise<void> {
loadbleManifests.set(
pageName,
await loadPartialManifest(REACT_LOADABLE_MANIFEST, pageName, type)
)
}
const buildingReported = new Set<string>() const buildingReported = new Set<string>()
async function changeSubscription( async function changeSubscription(
...@@ -697,6 +710,14 @@ async function startWatcher(opts: SetupOpts) { ...@@ -697,6 +710,14 @@ async function startWatcher(opts: SetupOpts) {
return manifest return manifest
} }
function mergeLoadableManifests(manifests: Iterable<LoadableManifest>) {
const manifest: LoadableManifest = {}
for (const m of manifests) {
Object.assign(manifest, m)
}
return manifest
}
async function writeFileAtomic( async function writeFileAtomic(
filePath: string, filePath: string,
content: string content: string
...@@ -866,13 +887,14 @@ async function startWatcher(opts: SetupOpts) { ...@@ -866,13 +887,14 @@ async function startWatcher(opts: SetupOpts) {
) )
} }
async function writeOtherManifests(): Promise<void> { async function writeLoadableManifest(): Promise<void> {
const loadableManifestPath = path.join( const loadableManifest = mergeLoadableManifests(loadbleManifests.values())
distDir, const loadableManifestPath = path.join(distDir, REACT_LOADABLE_MANIFEST)
'react-loadable-manifest.json'
)
deleteCache(loadableManifestPath) deleteCache(loadableManifestPath)
await writeFileAtomic(loadableManifestPath, JSON.stringify({}, null, 2)) await writeFileAtomic(
loadableManifestPath,
JSON.stringify(loadableManifest, null, 2)
)
} }
async function subscribeToHmrEvents(id: string, client: ws) { async function subscribeToHmrEvents(id: string, client: ws) {
...@@ -1052,8 +1074,8 @@ async function startWatcher(opts: SetupOpts) { ...@@ -1052,8 +1074,8 @@ async function startWatcher(opts: SetupOpts) {
await writeAppPathsManifest() await writeAppPathsManifest()
await writeMiddlewareManifest() await writeMiddlewareManifest()
await writeActionManifest() await writeActionManifest()
await writeOtherManifests()
await writeFontManifest() await writeFontManifest()
await writeLoadableManifest()
const turbopackHotReloader: NextJsHotReloaderInterface = { const turbopackHotReloader: NextJsHotReloaderInterface = {
turbopackProject: project, turbopackProject: project,
...@@ -1220,7 +1242,7 @@ async function startWatcher(opts: SetupOpts) { ...@@ -1220,7 +1242,7 @@ async function startWatcher(opts: SetupOpts) {
await writeFallbackBuildManifest() await writeFallbackBuildManifest()
await writePagesManifest() await writePagesManifest()
await writeMiddlewareManifest() await writeMiddlewareManifest()
await writeOtherManifests() await writeLoadableManifest()
return return
} }
...@@ -1329,12 +1351,13 @@ async function startWatcher(opts: SetupOpts) { ...@@ -1329,12 +1351,13 @@ async function startWatcher(opts: SetupOpts) {
} else { } else {
middlewareManifests.delete(page) middlewareManifests.delete(page)
} }
await loadLoadableManifest(page, 'pages')
await writeBuildManifest(opts.fsChecker.rewrites) await writeBuildManifest(opts.fsChecker.rewrites)
await writeFallbackBuildManifest() await writeFallbackBuildManifest()
await writePagesManifest() await writePagesManifest()
await writeMiddlewareManifest() await writeMiddlewareManifest()
await writeOtherManifests() await writeLoadableManifest()
processIssues(page, page, writtenEndpoint) processIssues(page, page, writtenEndpoint)
...@@ -1358,10 +1381,11 @@ async function startWatcher(opts: SetupOpts) { ...@@ -1358,10 +1381,11 @@ async function startWatcher(opts: SetupOpts) {
} else { } else {
middlewareManifests.delete(page) middlewareManifests.delete(page)
} }
await loadLoadableManifest(page, 'pages')
await writePagesManifest() await writePagesManifest()
await writeMiddlewareManifest() await writeMiddlewareManifest()
await writeOtherManifests() await writeLoadableManifest()
processIssues(page, page, writtenEndpoint) processIssues(page, page, writtenEndpoint)
...@@ -1395,7 +1419,7 @@ async function startWatcher(opts: SetupOpts) { ...@@ -1395,7 +1419,7 @@ async function startWatcher(opts: SetupOpts) {
await writeAppPathsManifest() await writeAppPathsManifest()
await writeMiddlewareManifest() await writeMiddlewareManifest()
await writeActionManifest() await writeActionManifest()
await writeOtherManifests() await writeLoadableManifest()
processIssues(page, page, writtenEndpoint, true) processIssues(page, page, writtenEndpoint, true)
...@@ -1420,7 +1444,7 @@ async function startWatcher(opts: SetupOpts) { ...@@ -1420,7 +1444,7 @@ async function startWatcher(opts: SetupOpts) {
await writeAppPathsManifest() await writeAppPathsManifest()
await writeMiddlewareManifest() await writeMiddlewareManifest()
await writeMiddlewareManifest() await writeMiddlewareManifest()
await writeOtherManifests() await writeLoadableManifest()
processIssues(page, page, writtenEndpoint, true) processIssues(page, page, writtenEndpoint, true)
......
...@@ -33,6 +33,17 @@ export type ManifestItem = { ...@@ -33,6 +33,17 @@ export type ManifestItem = {
export type ReactLoadableManifest = { [moduleId: string]: ManifestItem } export type ReactLoadableManifest = { [moduleId: string]: ManifestItem }
/**
* A manifest entry type for the react-loadable-manifest.json.
*
* The whole manifest.json is a type of `Record<pathName, LoadableManifest>`
* where pathName is a string-based key points to the path of the page contains
* each dynamic imports.
*/
export interface LoadableManifest {
[k: string]: { id: string | number; files: string[] }
}
export type LoadComponentsReturnType<NextModule = any> = { export type LoadComponentsReturnType<NextModule = any> = {
Component: NextComponentType Component: NextComponentType
pageConfig: PageConfig pageConfig: PageConfig
......
...@@ -237,32 +237,35 @@ describe.each([ ...@@ -237,32 +237,35 @@ describe.each([
}) })
} }
}) })
// Turbopack doesn't have this feature.
;(process.env.TURBOPACK ? describe.skip : describe)(
'custom chunkfilename',
() => {
it('should render the correct filename', async () => {
const $ = await get$(basePath + '/dynamic/chunkfilename')
expect($('body').text()).toMatch(/test chunkfilename/)
expect($('html').html()).toMatch(/hello-world\.js/)
})
describe('custom chunkfilename', () => { it('should render the component on client side', async () => {
it('should render the correct filename', async () => { let browser
const $ = await get$(basePath + '/dynamic/chunkfilename') try {
expect($('body').text()).toMatch(/test chunkfilename/) browser = await webdriver(
expect($('html').html()).toMatch(/hello-world\.js/) next.url,
}) basePath + '/dynamic/chunkfilename'
)
it('should render the component on client side', async () => { await check(
let browser () => browser.elementByCss('body').text(),
try { /test chunkfilename/
browser = await webdriver( )
next.url, } finally {
basePath + '/dynamic/chunkfilename' if (browser) {
) await browser.close()
await check( }
() => browser.elementByCss('body').text(),
/test chunkfilename/
)
} finally {
if (browser) {
await browser.close()
} }
} })
}) }
}) )
describe('custom loading', () => { describe('custom loading', () => {
it('should render custom loading on the server side when `ssr:false` and `loading` is provided', async () => { it('should render custom loading on the server side when `ssr:false` and `loading` is provided', async () => {
...@@ -289,45 +292,49 @@ describe.each([ ...@@ -289,45 +292,49 @@ describe.each([
}) })
}) })
describe('Multiple modules', () => { // TODO: Make this test work with Turbopack. Currently the test relies on `chunkFileName` which is not supported by Turbopack.
it('should only include the rendered module script tag', async () => { ;(process.env.TURBOPACK ? describe.skip : describe)(
const $ = await get$(basePath + '/dynamic/multiple-modules') 'Multiple modules',
const html = $('html').html() () => {
expect(html).toMatch(/hello1\.js/) it('should only include the rendered module script tag', async () => {
expect(html).not.toMatch(/hello2\.js/) const $ = await get$(basePath + '/dynamic/multiple-modules')
}) const html = $('html').html()
it('should only load the rendered module in the browser', async () => {
let browser
try {
browser = await webdriver(
next.url,
basePath + '/dynamic/multiple-modules'
)
const html = await browser.eval(
'document.documentElement.innerHTML'
)
expect(html).toMatch(/hello1\.js/) expect(html).toMatch(/hello1\.js/)
expect(html).not.toMatch(/hello2\.js/) expect(html).not.toMatch(/hello2\.js/)
} finally { })
if (browser) {
await browser.close() it('should only load the rendered module in the browser', async () => {
let browser
try {
browser = await webdriver(
next.url,
basePath + '/dynamic/multiple-modules'
)
const html = await browser.eval(
'document.documentElement.innerHTML'
)
expect(html).toMatch(/hello1\.js/)
expect(html).not.toMatch(/hello2\.js/)
} finally {
if (browser) {
await browser.close()
}
} }
} })
})
it('should only render one bundle if component is used multiple times', async () => { it('should only render one bundle if component is used multiple times', async () => {
const $ = await get$(basePath + '/dynamic/multiple-modules') const $ = await get$(basePath + '/dynamic/multiple-modules')
const html = $('html').html() const html = $('html').html()
try { try {
expect(html.match(/chunks[\\/]hello1\.js/g).length).toBe(1) expect(html.match(/chunks[\\/]hello1\.js/g).length).toBe(1)
expect(html).not.toMatch(/hello2\.js/) expect(html).not.toMatch(/hello2\.js/)
} catch (err) { } catch (err) {
console.error(html) console.error(html)
throw err throw err
} }
}) })
}) }
)
}) })
} }
) )
......
...@@ -46,31 +46,38 @@ function runTests() { ...@@ -46,31 +46,38 @@ function runTests() {
}) })
} }
describe('next/dynamic', () => { // This test is not needed for Turbopack as it relies on an experimental webpack feature.
describe('dev mode', () => { ;(process.env.TURBOPACK ? describe.skip : describe)(
beforeAll(async () => { 'next/dynamic lazy compilation',
appPort = await findPort() () => {
app = await launchApp(appDir, appPort) describe('dev mode', () => {
}) beforeAll(async () => {
afterAll(() => killApp(app)) appPort = await findPort()
app = await launchApp(appDir, appPort)
runTests(true)
})
;(process.env.TURBOPACK ? describe.skip : describe)('production mode', () => {
beforeAll(async () => {
await runNextCommand(['build', appDir])
app = nextServer({
dir: appDir,
dev: false,
quiet: true,
}) })
afterAll(() => killApp(app))
server = await startApp(app) runTests(true)
appPort = server.address().port
}) })
afterAll(() => stopApp(server)) ;(process.env.TURBOPACK ? describe.skip : describe)(
'production mode',
() => {
beforeAll(async () => {
await runNextCommand(['build', appDir])
runTests() app = nextServer({
}) dir: appDir,
}) dev: false,
quiet: true,
})
server = await startApp(app)
appPort = server.address().port
})
afterAll(() => stopApp(server))
runTests()
}
)
}
)
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