Unverified Commit 607ba02a authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Reland "feat(turbopack): support basic next/dynamic" (#56934)

This reverts commit 8a51ebcb.

### What?

see #56389 


Closes WEB-1791
parent 975c9994
......@@ -3448,6 +3448,7 @@ dependencies = [
"either",
"fxhash",
"hex",
"next-transform-dynamic",
"next-transform-font",
"once_cell",
"pathdiff",
......
......@@ -15,7 +15,6 @@ either = "1"
fxhash = "0.2.1"
hex = "0.4.3"
once_cell = { workspace = true }
next-transform-font = {workspace = true}
pathdiff = "0.2.0"
regex = "1.5"
rustc-hash = "1"
......@@ -24,6 +23,9 @@ serde_json = "1"
sha1 = "0.10.1"
tracing = { version = "0.1.37" }
next-transform-dynamic = { workspace = true }
next-transform-font = { workspace = true }
turbopack-binding = { workspace = true, features = [
"__swc_core",
"__swc_core_next_core",
......
......@@ -37,6 +37,7 @@ use std::{cell::RefCell, path::PathBuf, rc::Rc, sync::Arc};
use auto_cjs::contains_cjs;
use either::Either;
use fxhash::FxHashSet;
use next_transform_dynamic::{next_dynamic, NextDynamicMode};
use next_transform_font::next_font_loaders;
use serde::Deserialize;
use turbopack_binding::swc::{
......@@ -58,7 +59,6 @@ mod auto_cjs;
pub mod cjs_optimizer;
pub mod disallow_re_export_all_in_page;
pub mod named_import_transform;
pub mod next_dynamic;
pub mod next_ssg;
pub mod optimize_barrel;
pub mod optimize_server_react;
......@@ -226,7 +226,7 @@ where
!opts.disable_next_ssg
),
amp_attributes::amp_attributes(),
next_dynamic::next_dynamic(
next_dynamic(
opts.is_development,
opts.is_server,
match &opts.server_components {
......@@ -238,6 +238,7 @@ where
},
_ => false,
},
NextDynamicMode::Webpack,
file.name.clone(),
opts.pages_dir.clone()
),
......
This diff is collapsed.
......@@ -2,13 +2,13 @@ use std::path::PathBuf;
use next_swc::{
disallow_re_export_all_in_page::disallow_re_export_all_in_page,
next_dynamic::next_dynamic,
next_ssg::next_ssg,
react_server_components::server_components,
server_actions::{
server_actions, {self},
},
};
use next_transform_dynamic::{next_dynamic, NextDynamicMode};
use next_transform_font::{next_font_loaders, Config as FontLoaderConfig};
use turbopack_binding::swc::{
core::{
......@@ -56,6 +56,7 @@ fn next_dynamic_errors(input: PathBuf) {
true,
false,
false,
NextDynamicMode::Webpack,
FileName::Real(PathBuf::from("/some-project/src/some-file.js")),
Some("/some-project/src".into()),
)
......
......@@ -4,7 +4,6 @@ use next_swc::{
amp_attributes::amp_attributes,
cjs_optimizer::cjs_optimizer,
named_import_transform::named_import_transform,
next_dynamic::next_dynamic,
next_ssg::next_ssg,
optimize_barrel::optimize_barrel,
optimize_server_react::optimize_server_react,
......@@ -15,6 +14,7 @@ use next_swc::{
},
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 serde::de::DeserializeOwned;
use turbopack_binding::swc::{
......@@ -64,6 +64,7 @@ fn next_dynamic_fixture(input: PathBuf) {
true,
false,
false,
NextDynamicMode::Webpack,
FileName::Real(PathBuf::from("/some-project/src/some-file.js")),
Some("/some-project/src".into()),
)
......@@ -79,6 +80,7 @@ fn next_dynamic_fixture(input: PathBuf) {
false,
false,
false,
NextDynamicMode::Webpack,
FileName::Real(PathBuf::from("/some-project/src/some-file.js")),
Some("/some-project/src".into()),
)
......@@ -94,6 +96,7 @@ fn next_dynamic_fixture(input: PathBuf) {
false,
true,
false,
NextDynamicMode::Webpack,
FileName::Real(PathBuf::from("/some-project/src/some-file.js")),
Some("/some-project/src".into()),
)
......@@ -116,6 +119,7 @@ fn app_dir_next_dynamic_fixture(input: PathBuf) {
true,
false,
true,
NextDynamicMode::Webpack,
FileName::Real(PathBuf::from("/some-project/src/some-file.js")),
Some("/some-project/src".into()),
)
......@@ -131,6 +135,7 @@ fn app_dir_next_dynamic_fixture(input: PathBuf) {
false,
false,
true,
NextDynamicMode::Webpack,
FileName::Real(PathBuf::from("/some-project/src/some-file.js")),
Some("/some-project/src".into()),
)
......@@ -146,6 +151,7 @@ fn app_dir_next_dynamic_fixture(input: PathBuf) {
false,
true,
true,
NextDynamicMode::Webpack,
FileName::Real(PathBuf::from("/some-project/src/some-file.js")),
Some("/some-project/src".into()),
)
......
use std::collections::HashMap;
use anyhow::{bail, Context, Result};
use next_core::{
app_structure::{
......@@ -21,7 +23,8 @@ use next_core::{
next_edge::route_regex::get_named_middleware_regex,
next_manifests::{
AppBuildManifest, AppPathsManifest, BuildManifest, ClientReferenceManifest,
EdgeFunctionDefinition, MiddlewareMatcher, MiddlewaresManifestV2, PagesManifest, Regions,
EdgeFunctionDefinition, LoadableManifest, MiddlewareMatcher, MiddlewaresManifestV2,
PagesManifest, Regions,
},
next_server::{
get_server_module_options_context, get_server_resolve_options_context,
......@@ -39,7 +42,7 @@ use turbopack_binding::{
turbopack::{
core::{
asset::{Asset, AssetContent},
chunk::{ChunkingContext, EvaluatableAssets},
chunk::{availability_info::AvailabilityInfo, ChunkingContext, EvaluatableAssets},
file_source::FileSource,
module::Module,
output::{OutputAsset, OutputAssets},
......@@ -53,6 +56,10 @@ use turbopack_binding::{
};
use crate::{
dynamic_imports::{
collect_chunk_group, collect_evaluated_chunk_group, collect_next_dynamic_imports,
DynamicImportedChunks,
},
project::Project,
route::{Endpoint, Route, Routes, WrittenEndpoint},
server_actions::create_server_actions_manifest,
......@@ -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() {
NextRuntime::Edge => {
// create edge chunks
......@@ -714,7 +782,7 @@ impl AppEndpoint {
let files = chunking_context.evaluated_chunk_group(
app_entry.rsc_entry.ident(),
Vc::cell(evaluatable_assets),
Vc::cell(evaluatable_assets.clone()),
);
server_assets.extend(files.await?.iter().copied());
......@@ -801,6 +869,24 @@ impl AppEndpoint {
)?;
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 {
files,
server_assets: Vc::cell(server_assets),
......@@ -857,6 +943,25 @@ impl AppEndpoint {
)?;
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 {
rsc_chunk,
server_assets: Vc::cell(server_assets),
......
This diff is collapsed.
......@@ -3,6 +3,7 @@
#![feature(async_fn_in_trait)]
mod app;
mod dynamic_imports;
mod entrypoints;
mod middleware;
mod pages;
......
use std::collections::HashMap;
use anyhow::{bail, Context, Result};
use indexmap::IndexMap;
use next_core::{
......@@ -11,8 +13,8 @@ use next_core::{
next_dynamic::NextDynamicTransition,
next_edge::route_regex::get_named_middleware_regex,
next_manifests::{
BuildManifest, EdgeFunctionDefinition, MiddlewareMatcher, MiddlewaresManifestV2,
PagesManifest,
BuildManifest, EdgeFunctionDefinition, LoadableManifest, MiddlewareMatcher,
MiddlewaresManifestV2, PagesManifest,
},
next_pages::create_page_ssr_entry_module,
next_server::{
......@@ -37,7 +39,7 @@ use turbopack_binding::{
build::BuildChunkingContext,
core::{
asset::AssetContent,
chunk::{ChunkingContext, EvaluatableAssets},
chunk::{availability_info::AvailabilityInfo, ChunkingContext, EvaluatableAssets},
context::AssetContext,
file_source::FileSource,
issue::{IssueSeverity, OptionIssueSource},
......@@ -63,6 +65,10 @@ use turbopack_binding::{
};
use crate::{
dynamic_imports::{
collect_chunk_group, collect_evaluated_chunk_group, collect_next_dynamic_imports,
DynamicImportedChunks,
},
project::Project,
route::{Endpoint, Route, Routes, WrittenEndpoint},
server_paths::all_server_paths,
......@@ -609,9 +615,21 @@ impl PageEndpoint {
evaluatable_assets.push(evaluatable);
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 {
let ssr_module = create_page_ssr_entry_module(
this.pathname,
......@@ -633,8 +651,15 @@ impl PageEndpoint {
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 {
entry: ssr_entry_chunk,
dynamic_import_entries,
}
.cell())
}
......@@ -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]
async fn build_manifest(
self: Vc<Self>,
......@@ -796,18 +893,27 @@ impl PageEndpoint {
};
let page_output = match *ssr_chunk.await? {
SsrChunk::NodeJs { entry } => {
SsrChunk::NodeJs {
entry,
dynamic_import_entries,
} => {
let pages_manifest = self.pages_manifest(entry);
server_assets.push(pages_manifest);
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 {
entry_chunk: entry,
server_assets: Vc::cell(server_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 files_value = files.await?;
if let Some(&file) = files_value.first() {
......@@ -867,6 +973,9 @@ impl PageEndpoint {
));
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 {
files,
server_assets: Vc::cell(server_assets),
......@@ -1001,6 +1110,12 @@ impl PageEndpointOutput {
#[turbo_tasks::value]
pub enum SsrChunk {
NodeJs { entry: Vc<Box<dyn OutputAsset>> },
Edge { files: Vc<OutputAssets> },
NodeJs {
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 {
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)]
#[serde(rename_all = "camelCase")]
pub struct ServerReferenceManifest {
......
......@@ -65,12 +65,7 @@ impl CustomTransformer for NextJsDynamic {
},
self.is_server,
self.is_server_components,
NextDynamicMode::Turbopack {
dynamic_transition_name: match self.mode {
NextMode::Development => "next-client-chunks".to_string(),
NextMode::Build => "next-dynamic".to_string(),
},
},
NextDynamicMode::Webpack,
FileName::Real(ctx.file_path_str.into()),
self.pages_dir.clone(),
));
......
......@@ -19,6 +19,11 @@ use swc_core::{
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(
is_development: bool,
is_server: bool,
......@@ -89,6 +94,7 @@ enum NextDynamicPatcherState {
Webpack,
/// In Turbo mode, contains a list of modules that need to be imported with
/// the given transition under a particular ident.
#[allow(unused)]
Turbopack {
dynamic_transition_name: String,
imports: Vec<TurbopackImport>,
......
......@@ -78,6 +78,7 @@ import {
PAGES_MANIFEST,
PHASE_DEVELOPMENT_SERVER,
SERVER_REFERENCE_MANIFEST,
REACT_LOADABLE_MANIFEST,
} from '../../../shared/lib/constants'
import { getMiddlewareRouteMatcher } from '../../../shared/lib/router/utils/middleware-route-matcher'
......@@ -117,6 +118,7 @@ import { normalizeMetadataRoute } from '../../../lib/metadata/get-metadata-route
import { clearModuleContext } from '../render-server'
import type { ActionManifest } from '../../../build/webpack/plugins/flight-client-entry-plugin'
import { denormalizePagePath } from '../../../shared/lib/page-path/denormalize-page-path'
import type { LoadableManifest } from '../../load-components'
const wsServer = new ws.Server({ noServer: true })
......@@ -503,6 +505,7 @@ async function startWatcher(opts: SetupOpts) {
ws,
Map<string, AsyncIterator<any>>
>()
const loadbleManifests = new Map<string, LoadableManifest>()
const clients = new Set<ws>()
async function loadMiddlewareManifest(
......@@ -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>()
async function changeSubscription(
......@@ -697,6 +710,14 @@ async function startWatcher(opts: SetupOpts) {
return manifest
}
function mergeLoadableManifests(manifests: Iterable<LoadableManifest>) {
const manifest: LoadableManifest = {}
for (const m of manifests) {
Object.assign(manifest, m)
}
return manifest
}
async function writeFileAtomic(
filePath: string,
content: string
......@@ -866,13 +887,14 @@ async function startWatcher(opts: SetupOpts) {
)
}
async function writeOtherManifests(): Promise<void> {
const loadableManifestPath = path.join(
distDir,
'react-loadable-manifest.json'
)
async function writeLoadableManifest(): Promise<void> {
const loadableManifest = mergeLoadableManifests(loadbleManifests.values())
const loadableManifestPath = path.join(distDir, REACT_LOADABLE_MANIFEST)
deleteCache(loadableManifestPath)
await writeFileAtomic(loadableManifestPath, JSON.stringify({}, null, 2))
await writeFileAtomic(
loadableManifestPath,
JSON.stringify(loadableManifest, null, 2)
)
}
async function subscribeToHmrEvents(id: string, client: ws) {
......@@ -1052,8 +1074,8 @@ async function startWatcher(opts: SetupOpts) {
await writeAppPathsManifest()
await writeMiddlewareManifest()
await writeActionManifest()
await writeOtherManifests()
await writeFontManifest()
await writeLoadableManifest()
const turbopackHotReloader: NextJsHotReloaderInterface = {
turbopackProject: project,
......@@ -1220,7 +1242,7 @@ async function startWatcher(opts: SetupOpts) {
await writeFallbackBuildManifest()
await writePagesManifest()
await writeMiddlewareManifest()
await writeOtherManifests()
await writeLoadableManifest()
return
}
......@@ -1329,12 +1351,13 @@ async function startWatcher(opts: SetupOpts) {
} else {
middlewareManifests.delete(page)
}
await loadLoadableManifest(page, 'pages')
await writeBuildManifest(opts.fsChecker.rewrites)
await writeFallbackBuildManifest()
await writePagesManifest()
await writeMiddlewareManifest()
await writeOtherManifests()
await writeLoadableManifest()
processIssues(page, page, writtenEndpoint)
......@@ -1358,10 +1381,11 @@ async function startWatcher(opts: SetupOpts) {
} else {
middlewareManifests.delete(page)
}
await loadLoadableManifest(page, 'pages')
await writePagesManifest()
await writeMiddlewareManifest()
await writeOtherManifests()
await writeLoadableManifest()
processIssues(page, page, writtenEndpoint)
......@@ -1395,7 +1419,7 @@ async function startWatcher(opts: SetupOpts) {
await writeAppPathsManifest()
await writeMiddlewareManifest()
await writeActionManifest()
await writeOtherManifests()
await writeLoadableManifest()
processIssues(page, page, writtenEndpoint, true)
......@@ -1420,7 +1444,7 @@ async function startWatcher(opts: SetupOpts) {
await writeAppPathsManifest()
await writeMiddlewareManifest()
await writeMiddlewareManifest()
await writeOtherManifests()
await writeLoadableManifest()
processIssues(page, page, writtenEndpoint, true)
......
......@@ -33,6 +33,17 @@ export type 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> = {
Component: NextComponentType
pageConfig: PageConfig
......
......@@ -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 correct filename', async () => {
const $ = await get$(basePath + '/dynamic/chunkfilename')
expect($('body').text()).toMatch(/test chunkfilename/)
expect($('html').html()).toMatch(/hello-world\.js/)
})
it('should render the component on client side', async () => {
let browser
try {
browser = await webdriver(
next.url,
basePath + '/dynamic/chunkfilename'
)
await check(
() => browser.elementByCss('body').text(),
/test chunkfilename/
)
} finally {
if (browser) {
await browser.close()
it('should render the component on client side', async () => {
let browser
try {
browser = await webdriver(
next.url,
basePath + '/dynamic/chunkfilename'
)
await check(
() => browser.elementByCss('body').text(),
/test chunkfilename/
)
} finally {
if (browser) {
await browser.close()
}
}
}
})
})
})
}
)
describe('custom loading', () => {
it('should render custom loading on the server side when `ssr:false` and `loading` is provided', async () => {
......@@ -289,45 +292,49 @@ describe.each([
})
})
describe('Multiple modules', () => {
it('should only include the rendered module script tag', async () => {
const $ = await get$(basePath + '/dynamic/multiple-modules')
const html = $('html').html()
expect(html).toMatch(/hello1\.js/)
expect(html).not.toMatch(/hello2\.js/)
})
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'
)
// TODO: Make this test work with Turbopack. Currently the test relies on `chunkFileName` which is not supported by Turbopack.
;(process.env.TURBOPACK ? describe.skip : describe)(
'Multiple modules',
() => {
it('should only include the rendered module script tag', async () => {
const $ = await get$(basePath + '/dynamic/multiple-modules')
const html = $('html').html()
expect(html).toMatch(/hello1\.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 () => {
const $ = await get$(basePath + '/dynamic/multiple-modules')
const html = $('html').html()
try {
expect(html.match(/chunks[\\/]hello1\.js/g).length).toBe(1)
expect(html).not.toMatch(/hello2\.js/)
} catch (err) {
console.error(html)
throw err
}
})
})
it('should only render one bundle if component is used multiple times', async () => {
const $ = await get$(basePath + '/dynamic/multiple-modules')
const html = $('html').html()
try {
expect(html.match(/chunks[\\/]hello1\.js/g).length).toBe(1)
expect(html).not.toMatch(/hello2\.js/)
} catch (err) {
console.error(html)
throw err
}
})
}
)
})
}
)
......
......@@ -46,31 +46,38 @@ function runTests() {
})
}
describe('next/dynamic', () => {
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))
runTests(true)
})
;(process.env.TURBOPACK ? describe.skip : describe)('production mode', () => {
beforeAll(async () => {
await runNextCommand(['build', appDir])
app = nextServer({
dir: appDir,
dev: false,
quiet: true,
// This test is not needed for Turbopack as it relies on an experimental webpack feature.
;(process.env.TURBOPACK ? describe.skip : describe)(
'next/dynamic lazy compilation',
() => {
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))
server = await startApp(app)
appPort = server.address().port
runTests(true)
})
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