Unverified Commit 4c66441e authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

feat(turbopack): lazy-load federation exposes

Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent a1a83a55
......@@ -10,7 +10,8 @@ use turbopack_core::{
context::AssetContext,
module::Module,
module_graph::{
GraphEntries,
GraphEntries, ModuleGraph, SingleModuleGraph,
binding_usage_info::compute_binding_usage_info,
chunk_group_info::{ChunkGroupEntry, ChunkGroupKey, EntryHeuristics},
},
output::OutputAssets,
......@@ -78,21 +79,57 @@ impl ModuleFederationEndpoint {
.clone()
.unwrap_or_else(|| format!("{name}.js").into());
let module = self.entry_module().to_resolved().await?;
let module_graph = this.project.module_graph(*module);
let is_production = this.project.next_mode().await?.is_production();
let graphs = vec![SingleModuleGraph::new_with_entry(
ChunkGroupEntry::Entry {
modules: vec![module],
heuristics: EntryHeuristics::high_priority(),
},
false,
is_production,
)];
let (module_graph, binding_usage_info) = if is_production {
let graph_without_usage = ModuleGraph::from_graphs(graphs.clone(), None);
let binding_usage_info = compute_binding_usage_info(graph_without_usage, true);
let resolved_binding_usage_info = binding_usage_info.resolve().await?;
(
ModuleGraph::from_graphs(graphs, Some(binding_usage_info)).connect(),
Some(resolved_binding_usage_info),
)
} else {
(ModuleGraph::from_graphs(graphs, None).connect(), None)
};
let client_chunking_context = this.project.client_chunking_context().to_resolved().await?;
let client_chunking_context =
ResolvedVc::try_downcast_type::<BrowserChunkingContext>(client_chunking_context)
.context("expected a browser chunking context")?;
let federation_chunking_context = client_chunking_context
let federation_chunk_root = this
.project
.node_root()
.owned()
.await?
.join("static/chunks/mf")?;
let output_root_to_root_path = format!(
"../../../{}",
client_chunking_context.output_root_to_root_path().await?
)
.into();
let mut federation_chunking_context = client_chunking_context
.await?
.clone_builder()
.asset_suffix(AssetSuffix::None.resolved_cell())
.output_root(federation_chunk_root.clone(), output_root_to_root_path)
.chunk_root_path(federation_chunk_root.clone())
.asset_root_path(federation_chunk_root.join("media")?)
.shared_runtime(false)
.shared_runtime_chunk(false)
.chunk_loading_global(format!("TURBOPACK_{name}").into())
.single_chunk()
.await?
.build();
.chunk_loading_global(format!("TURBOPACK_{name}").into());
if let Some(binding_usage_info) = binding_usage_info {
federation_chunking_context = federation_chunking_context
.export_usage(Some(binding_usage_info))
.unused_references(binding_usage_info.unused_references().to_resolved().await?);
}
let federation_chunking_context = federation_chunking_context.build();
let EntryChunkGroupResult { asset, .. } = *federation_chunking_context
.entry_chunk_group(
this.project
......
......@@ -3,7 +3,7 @@ const nextConfig = {
experimental: {
turbopackModuleFederation: {
name: 'nextRemote',
filename: 'nextRemote.js',
filename: 'nested/nextRemote.js',
exposes: {
'./message': './lib/message.js',
},
......
import { symlink } from 'fs/promises'
import type { ChildProcess } from 'child_process'
import type { Server } from 'http'
import { symlink, writeFile } from 'fs/promises'
import { join } from 'path'
import execa from 'execa'
import type { ChildProcess } from 'child_process'
import { isNextDeploy, nextTestSetup } from 'e2e-utils'
import {
fetchViaHTTP,
......@@ -10,8 +11,52 @@ import {
launchApp,
nextStart,
retry,
startStaticServer,
} from 'next-test-utils'
const webpack = require('next/dist/compiled/webpack/webpack')
.webpack as typeof import('webpack')
async function buildWebpackHost(
context: string,
outputPath: string,
remoteUrl: string
) {
await new Promise<void>((resolve, reject) => {
webpack(
{
mode: 'development',
context,
entry: './index.js',
output: {
path: outputPath,
publicPath: 'auto',
uniqueName: 'webpack-host',
},
plugins: [
new webpack.container.ModuleFederationPlugin({
name: 'webpackHost',
remotes: {
nextRemote: `nextRemote@${remoteUrl}`,
},
}),
],
},
(error, stats) => {
if (error) return reject(error)
if (stats?.hasErrors()) {
return reject(new Error(stats.toString({ errors: true })))
}
resolve()
}
)
})
await writeFile(
join(outputPath, 'index.html'),
'<p id="webpack-message">loading</p><script src="/main.js"></script>'
)
}
const isTurbopack = Boolean(process.env.IS_TURBOPACK_TEST)
// This test launches a second local Next.js server, which deployed fixtures cannot reach.
// Cache Components runs in an additional experimental mode that does not yet expose
......@@ -29,10 +74,13 @@ describeTurbopack('turbopack module federation between Next.js apps', () => {
skipDeployment: true,
})
let remoteServer: ChildProcess
let remotePort: number
let remoteOrigin: string
let webpackHostServer: Server
let webpackHostOrigin: string
beforeAll(async () => {
remotePort = await findPort()
const remotePort = await findPort()
remoteOrigin = `http://localhost:${remotePort}`
const remoteDir = join(next.testDir, 'remote')
await symlink(
join(next.testDir, 'node_modules'),
......@@ -63,26 +111,34 @@ describeTurbopack('turbopack module federation between Next.js apps', () => {
if (response.status !== 200) {
throw new Error(`Remote server returned status ${response.status}`)
}
const federationResponse = await fetchViaHTTP(
remotePort,
'/_next/static/nextRemote.js'
process.env.MF_REMOTE_URL = `${remoteOrigin}/_next/static/nested/nextRemote.js`
const webpackHostPort = await findPort()
webpackHostOrigin = `http://localhost:${webpackHostPort}`
const webpackHostOutput = join(next.testDir, 'webpack-host-dist')
await buildWebpackHost(
join(next.testDir, 'webpack-host'),
webpackHostOutput,
process.env.MF_REMOTE_URL
)
webpackHostServer = await startStaticServer(
webpackHostOutput,
undefined,
webpackHostPort
)
if (federationResponse.status !== 200) {
throw new Error(
`Federation endpoint returned status ${federationResponse.status}`
)
}
process.env.MF_REMOTE_URL = `http://localhost:${remotePort}/_next/static/nextRemote.js`
await next.start()
})
afterAll(async () => {
await new Promise<void>((resolve, reject) => {
webpackHostServer.close((error) => (error ? reject(error) : resolve()))
})
await killApp(remoteServer)
delete process.env.MF_REMOTE_URL
})
it('loads a tree-shaken module exposed by another Next.js app', async () => {
it('loads a module exposed by another Next.js app', async () => {
const browser = await next.browser('/')
await retry(async () => {
expect(await browser.elementByCss('#remote-message').text()).toBe(
......@@ -90,15 +146,33 @@ describeTurbopack('turbopack module federation between Next.js apps', () => {
)
}, 15_000)
const entry = await fetchViaHTTP(
remotePort,
'/_next/static/nextRemote.js'
).then((response) => response.text())
// The exposed module lives in its own async chunk, fetched from the producer's origin
// rather than from the host that loaded the remote entry.
const remoteChunks = await browser.eval(
`performance.getEntriesByType('resource').map((entry) => entry.name).filter((name) => name.startsWith(${JSON.stringify(remoteOrigin + '/_next/static/chunks/mf/')}) && !name.includes('/nested/nextRemote.js'))`
)
expect(remoteChunks).not.toEqual([])
if (!isNextDev) {
expect(entry).not.toContain(
const sources = await Promise.all(
[process.env.MF_REMOTE_URL!, ...(remoteChunks as string[])].map((url) =>
fetch(url).then((response) => response.text())
)
)
const federationOutput = sources.join('\n')
expect(federationOutput).not.toContain(
'MODULE_FEDERATION_UNUSED_EXPORT_SHOULD_BE_REMOVED'
)
expect(entry).not.toContain('remote/lib/message.js')
expect(federationOutput).not.toContain('remote/lib/message.js')
}
})
it('exposes the module to a webpack host', async () => {
const browser = await next.browser('/', { baseUrl: webpackHostOrigin })
await retry(async () => {
expect(await browser.elementByCss('#webpack-message').text()).toBe(
'hello from Next.js'
)
}, 15_000)
})
})
import('nextRemote/message').then((module) => {
document.querySelector('#webpack-message').textContent = module.message
})
......@@ -255,6 +255,26 @@ impl BrowserChunkingContextBuilder {
self
}
pub fn output_root(
mut self,
output_root: FileSystemPath,
output_root_to_root_path: RcStr,
) -> Self {
self.chunking_context.output_root = output_root;
self.chunking_context.output_root_to_root_path = output_root_to_root_path;
self
}
pub fn chunk_root_path(mut self, chunk_root_path: FileSystemPath) -> Self {
self.chunking_context.chunk_root_path = chunk_root_path;
self
}
pub fn asset_root_path(mut self, asset_root_path: FileSystemPath) -> Self {
self.chunking_context.asset_root_path = asset_root_path;
self
}
pub fn hash_salt(mut self, salt: ResolvedVc<RcStr>) -> Self {
self.chunking_context.hash_salt = salt;
self
......@@ -1127,10 +1147,7 @@ impl ChunkingContext for BrowserChunkingContext {
extra_referenced_assets: Vc<OutputAssets>,
availability_info: AvailabilityInfo,
) -> Result<Vc<EntryChunkGroupResult>> {
if !self.await?.single_chunk {
bail!("Browser chunking context only supports entry chunk groups in single-chunk mode");
}
let this = self.await?;
if !extra_chunks.await?.is_empty() {
bail!("single-chunk entry does not support extra chunks");
}
......@@ -1159,11 +1176,22 @@ impl ChunkingContext for BrowserChunkingContext {
let chunks = chunks.await?;
let ecmascript_chunk = chunks
let mut ecmascript_chunks = chunks
.iter()
.filter_map(|chunk| ResolvedVc::try_downcast_type::<EcmascriptChunk>(*chunk))
.collect::<Vec<_>>();
let other_chunks = chunks
.iter()
.find_map(|chunk| ResolvedVc::try_downcast_type::<EcmascriptChunk>(*chunk));
.filter_map(|chunk| {
if ResolvedVc::try_downcast_type::<EcmascriptChunk>(*chunk).is_some() {
None
} else {
ResolvedVc::try_sidecast::<Box<dyn OutputAsset>>(*chunk)
}
})
.collect::<Vec<_>>();
if chunks.len() != 1 || ecmascript_chunk.is_none() {
if this.single_chunk && (chunks.len() != 1 || ecmascript_chunks.len() != 1) {
SingleChunkProducedMultipleChunksIssue {
path: path.clone(),
chunk_count: chunks.len(),
......@@ -1172,10 +1200,8 @@ impl ChunkingContext for BrowserChunkingContext {
.emit();
}
// use a stub if chunks == 0, we already emitted an issue
let ecmascript_chunk = match ecmascript_chunk {
Some(ecmascript_chunk) => ecmascript_chunk,
None => {
if ecmascript_chunks.is_empty() {
ecmascript_chunks.push(
EcmascriptChunk::new(
Vc::upcast(*self),
EcmascriptChunkContent {
......@@ -1186,9 +1212,9 @@ impl ChunkingContext for BrowserChunkingContext {
Vec::new(),
)
.to_resolved()
.await?
}
};
.await?,
);
}
let evaluatable_assets = chunk_group
.entries()
......@@ -1198,13 +1224,15 @@ impl ChunkingContext for BrowserChunkingContext {
})
.collect::<Result<Vec<_>>>()?;
let referenced_output_assets =
OutputAssets::concat(vec![extra_referenced_assets, Vc::cell(other_chunks)]);
let asset = ResolvedVc::upcast(
EcmascriptBrowserSingleEntryChunk::new(
*self,
path,
*ecmascript_chunk,
ecmascript_chunks.into_iter().map(|chunk| *chunk).collect(),
Vc::cell(evaluatable_assets),
extra_referenced_assets,
referenced_output_assets,
Vc::cell(references),
*module_graph,
)
......
use anyhow::Result;
use std::io::Write;
use anyhow::{Context, Result};
use indoc::writedoc;
use turbo_tasks::{ResolvedVc, ValueToString, Vc};
use turbo_tasks_fs::{File, FileContent, FileSystemPath};
use turbopack_core::{
asset::{Asset, AssetContent},
chunk::{Chunk, ChunkingContext, EvaluatableAssets},
code_builder::{Code, CodeBuilder},
environment::ChunkLoading,
module_graph::ModuleGraph,
output::{
OutputAsset, OutputAssets, OutputAssetsReference, OutputAssetsReferences,
......@@ -12,25 +16,24 @@ use turbopack_core::{
},
source_map::{GenerateSourceMap, SourceMapAsset},
};
use turbopack_ecmascript::chunk::EcmascriptChunk;
use turbopack_ecmascript::{chunk::EcmascriptChunk, utils::StringifyJs};
use crate::{
BrowserChunkingContext,
ecmascript::{chunk::EcmascriptBrowserChunk, evaluate::chunk::EcmascriptBrowserEvaluateChunk},
};
/// A self-contained chunk that inlines the entire module together with
/// the Turbopack browser runtime and the evaluation of the entry modules.
/// A browser entry asset that concatenates the synchronous ECMAScript chunks,
/// the Turbopack browser runtime, and evaluation of the entry modules.
///
/// Unlike the regular evaluate chunk, this emits everything into one file at a
/// fixed path and never loads additional chunks at runtime.
/// Async chunks remain separate and are referenced from this fixed-path entry.
#[turbo_tasks::value(shared)]
#[derive(ValueToString)]
#[value_to_string("Ecmascript Browser Single Entry Chunk")]
pub struct EcmascriptBrowserSingleEntryChunk {
chunking_context: ResolvedVc<BrowserChunkingContext>,
path: FileSystemPath,
chunk: ResolvedVc<EcmascriptChunk>,
chunks: Vec<ResolvedVc<EcmascriptChunk>>,
evaluatable_assets: ResolvedVc<EvaluatableAssets>,
referenced_output_assets: ResolvedVc<OutputAssets>,
references: ResolvedVc<OutputAssetsReferences>,
......@@ -43,7 +46,7 @@ impl EcmascriptBrowserSingleEntryChunk {
pub fn new(
chunking_context: ResolvedVc<BrowserChunkingContext>,
path: FileSystemPath,
chunk: ResolvedVc<EcmascriptChunk>,
chunks: Vec<ResolvedVc<EcmascriptChunk>>,
evaluatable_assets: ResolvedVc<EvaluatableAssets>,
referenced_output_assets: ResolvedVc<OutputAssets>,
references: ResolvedVc<OutputAssetsReferences>,
......@@ -52,7 +55,7 @@ impl EcmascriptBrowserSingleEntryChunk {
EcmascriptBrowserSingleEntryChunk {
chunking_context,
path,
chunk,
chunks,
evaluatable_assets,
referenced_output_assets,
references,
......@@ -74,14 +77,40 @@ impl EcmascriptBrowserSingleEntryChunk {
*this.chunking_context.debug_ids_enabled().await?,
);
let module_chunk = EcmascriptBrowserChunk::new(*this.chunking_context, *this.chunk);
code.push_code(&*module_chunk.own_content().code().await?);
if !matches!(
*this.chunking_context.chunk_loading().await?,
ChunkLoading::Edge | ChunkLoading::SingleChunk
) {
let output_root = this.chunking_context.output_root().owned().await?;
let mut chunk_base = this
.path
.parent()
.get_relative_path_to(&output_root)
.context("browser entry and chunk output must use the same file system")?;
if chunk_base.is_empty() {
chunk_base = "./".into();
} else if !chunk_base.ends_with('/') {
chunk_base = format!("{chunk_base}/").into();
}
writedoc!(
code,
r#"
var TURBOPACK_CHUNK_BASE_PATH = new URL({}, document.currentScript.src).href;
"#,
StringifyJs(&chunk_base)
)?;
}
for chunk in &this.chunks {
let module_chunk = EcmascriptBrowserChunk::new(*this.chunking_context, **chunk);
code.push_code(&*module_chunk.own_content().code().await?);
}
// Keep independently-generated programs from parsing as one call expression.
code += ";\n";
let evaluate_chunk = EcmascriptBrowserEvaluateChunk::new(
*this.chunking_context,
this.chunk.ident(),
this.chunks[0].ident(),
OutputAssets::empty(),
*this.evaluatable_assets,
*this.module_graph,
......
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