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::{ ...@@ -10,7 +10,8 @@ use turbopack_core::{
context::AssetContext, context::AssetContext,
module::Module, module::Module,
module_graph::{ module_graph::{
GraphEntries, GraphEntries, ModuleGraph, SingleModuleGraph,
binding_usage_info::compute_binding_usage_info,
chunk_group_info::{ChunkGroupEntry, ChunkGroupKey, EntryHeuristics}, chunk_group_info::{ChunkGroupEntry, ChunkGroupKey, EntryHeuristics},
}, },
output::OutputAssets, output::OutputAssets,
...@@ -78,21 +79,57 @@ impl ModuleFederationEndpoint { ...@@ -78,21 +79,57 @@ impl ModuleFederationEndpoint {
.clone() .clone()
.unwrap_or_else(|| format!("{name}.js").into()); .unwrap_or_else(|| format!("{name}.js").into());
let module = self.entry_module().to_resolved().await?; 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 = this.project.client_chunking_context().to_resolved().await?;
let client_chunking_context = let client_chunking_context =
ResolvedVc::try_downcast_type::<BrowserChunkingContext>(client_chunking_context) ResolvedVc::try_downcast_type::<BrowserChunkingContext>(client_chunking_context)
.context("expected a browser 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? .await?
.clone_builder() .clone_builder()
.asset_suffix(AssetSuffix::None.resolved_cell()) .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(false)
.shared_runtime_chunk(false) .shared_runtime_chunk(false)
.chunk_loading_global(format!("TURBOPACK_{name}").into()) .chunk_loading_global(format!("TURBOPACK_{name}").into());
.single_chunk() if let Some(binding_usage_info) = binding_usage_info {
.await? federation_chunking_context = federation_chunking_context
.build(); .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 let EntryChunkGroupResult { asset, .. } = *federation_chunking_context
.entry_chunk_group( .entry_chunk_group(
this.project this.project
......
...@@ -3,7 +3,7 @@ const nextConfig = { ...@@ -3,7 +3,7 @@ const nextConfig = {
experimental: { experimental: {
turbopackModuleFederation: { turbopackModuleFederation: {
name: 'nextRemote', name: 'nextRemote',
filename: 'nextRemote.js', filename: 'nested/nextRemote.js',
exposes: { exposes: {
'./message': './lib/message.js', './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 { join } from 'path'
import execa from 'execa' import execa from 'execa'
import type { ChildProcess } from 'child_process'
import { isNextDeploy, nextTestSetup } from 'e2e-utils' import { isNextDeploy, nextTestSetup } from 'e2e-utils'
import { import {
fetchViaHTTP, fetchViaHTTP,
...@@ -10,8 +11,52 @@ import { ...@@ -10,8 +11,52 @@ import {
launchApp, launchApp,
nextStart, nextStart,
retry, retry,
startStaticServer,
} from 'next-test-utils' } 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) const isTurbopack = Boolean(process.env.IS_TURBOPACK_TEST)
// This test launches a second local Next.js server, which deployed fixtures cannot reach. // 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 // 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', () => { ...@@ -29,10 +74,13 @@ describeTurbopack('turbopack module federation between Next.js apps', () => {
skipDeployment: true, skipDeployment: true,
}) })
let remoteServer: ChildProcess let remoteServer: ChildProcess
let remotePort: number let remoteOrigin: string
let webpackHostServer: Server
let webpackHostOrigin: string
beforeAll(async () => { beforeAll(async () => {
remotePort = await findPort() const remotePort = await findPort()
remoteOrigin = `http://localhost:${remotePort}`
const remoteDir = join(next.testDir, 'remote') const remoteDir = join(next.testDir, 'remote')
await symlink( await symlink(
join(next.testDir, 'node_modules'), join(next.testDir, 'node_modules'),
...@@ -63,26 +111,34 @@ describeTurbopack('turbopack module federation between Next.js apps', () => { ...@@ -63,26 +111,34 @@ describeTurbopack('turbopack module federation between Next.js apps', () => {
if (response.status !== 200) { if (response.status !== 200) {
throw new Error(`Remote server returned status ${response.status}`) throw new Error(`Remote server returned status ${response.status}`)
} }
const federationResponse = await fetchViaHTTP( process.env.MF_REMOTE_URL = `${remoteOrigin}/_next/static/nested/nextRemote.js`
remotePort,
'/_next/static/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
) )
if (federationResponse.status !== 200) { webpackHostServer = await startStaticServer(
throw new Error( webpackHostOutput,
`Federation endpoint returned status ${federationResponse.status}` undefined,
webpackHostPort
) )
}
process.env.MF_REMOTE_URL = `http://localhost:${remotePort}/_next/static/nextRemote.js`
await next.start() await next.start()
}) })
afterAll(async () => { afterAll(async () => {
await new Promise<void>((resolve, reject) => {
webpackHostServer.close((error) => (error ? reject(error) : resolve()))
})
await killApp(remoteServer) await killApp(remoteServer)
delete process.env.MF_REMOTE_URL 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('/') const browser = await next.browser('/')
await retry(async () => { await retry(async () => {
expect(await browser.elementByCss('#remote-message').text()).toBe( expect(await browser.elementByCss('#remote-message').text()).toBe(
...@@ -90,15 +146,33 @@ describeTurbopack('turbopack module federation between Next.js apps', () => { ...@@ -90,15 +146,33 @@ describeTurbopack('turbopack module federation between Next.js apps', () => {
) )
}, 15_000) }, 15_000)
const entry = await fetchViaHTTP( // The exposed module lives in its own async chunk, fetched from the producer's origin
remotePort, // rather than from the host that loaded the remote entry.
'/_next/static/nextRemote.js' const remoteChunks = await browser.eval(
).then((response) => response.text()) `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) { 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' '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 { ...@@ -255,6 +255,26 @@ impl BrowserChunkingContextBuilder {
self 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 { pub fn hash_salt(mut self, salt: ResolvedVc<RcStr>) -> Self {
self.chunking_context.hash_salt = salt; self.chunking_context.hash_salt = salt;
self self
...@@ -1127,10 +1147,7 @@ impl ChunkingContext for BrowserChunkingContext { ...@@ -1127,10 +1147,7 @@ impl ChunkingContext for BrowserChunkingContext {
extra_referenced_assets: Vc<OutputAssets>, extra_referenced_assets: Vc<OutputAssets>,
availability_info: AvailabilityInfo, availability_info: AvailabilityInfo,
) -> Result<Vc<EntryChunkGroupResult>> { ) -> Result<Vc<EntryChunkGroupResult>> {
if !self.await?.single_chunk { let this = self.await?;
bail!("Browser chunking context only supports entry chunk groups in single-chunk mode");
}
if !extra_chunks.await?.is_empty() { if !extra_chunks.await?.is_empty() {
bail!("single-chunk entry does not support extra chunks"); bail!("single-chunk entry does not support extra chunks");
} }
...@@ -1159,11 +1176,22 @@ impl ChunkingContext for BrowserChunkingContext { ...@@ -1159,11 +1176,22 @@ impl ChunkingContext for BrowserChunkingContext {
let chunks = chunks.await?; 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() .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 { SingleChunkProducedMultipleChunksIssue {
path: path.clone(), path: path.clone(),
chunk_count: chunks.len(), chunk_count: chunks.len(),
...@@ -1172,10 +1200,8 @@ impl ChunkingContext for BrowserChunkingContext { ...@@ -1172,10 +1200,8 @@ impl ChunkingContext for BrowserChunkingContext {
.emit(); .emit();
} }
// use a stub if chunks == 0, we already emitted an issue if ecmascript_chunks.is_empty() {
let ecmascript_chunk = match ecmascript_chunk { ecmascript_chunks.push(
Some(ecmascript_chunk) => ecmascript_chunk,
None => {
EcmascriptChunk::new( EcmascriptChunk::new(
Vc::upcast(*self), Vc::upcast(*self),
EcmascriptChunkContent { EcmascriptChunkContent {
...@@ -1186,9 +1212,9 @@ impl ChunkingContext for BrowserChunkingContext { ...@@ -1186,9 +1212,9 @@ impl ChunkingContext for BrowserChunkingContext {
Vec::new(), Vec::new(),
) )
.to_resolved() .to_resolved()
.await? .await?,
);
} }
};
let evaluatable_assets = chunk_group let evaluatable_assets = chunk_group
.entries() .entries()
...@@ -1198,13 +1224,15 @@ impl ChunkingContext for BrowserChunkingContext { ...@@ -1198,13 +1224,15 @@ impl ChunkingContext for BrowserChunkingContext {
}) })
.collect::<Result<Vec<_>>>()?; .collect::<Result<Vec<_>>>()?;
let referenced_output_assets =
OutputAssets::concat(vec![extra_referenced_assets, Vc::cell(other_chunks)]);
let asset = ResolvedVc::upcast( let asset = ResolvedVc::upcast(
EcmascriptBrowserSingleEntryChunk::new( EcmascriptBrowserSingleEntryChunk::new(
*self, *self,
path, path,
*ecmascript_chunk, ecmascript_chunks.into_iter().map(|chunk| *chunk).collect(),
Vc::cell(evaluatable_assets), Vc::cell(evaluatable_assets),
extra_referenced_assets, referenced_output_assets,
Vc::cell(references), Vc::cell(references),
*module_graph, *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::{ResolvedVc, ValueToString, Vc};
use turbo_tasks_fs::{File, FileContent, FileSystemPath}; use turbo_tasks_fs::{File, FileContent, FileSystemPath};
use turbopack_core::{ use turbopack_core::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
chunk::{Chunk, ChunkingContext, EvaluatableAssets}, chunk::{Chunk, ChunkingContext, EvaluatableAssets},
code_builder::{Code, CodeBuilder}, code_builder::{Code, CodeBuilder},
environment::ChunkLoading,
module_graph::ModuleGraph, module_graph::ModuleGraph,
output::{ output::{
OutputAsset, OutputAssets, OutputAssetsReference, OutputAssetsReferences, OutputAsset, OutputAssets, OutputAssetsReference, OutputAssetsReferences,
...@@ -12,25 +16,24 @@ use turbopack_core::{ ...@@ -12,25 +16,24 @@ use turbopack_core::{
}, },
source_map::{GenerateSourceMap, SourceMapAsset}, source_map::{GenerateSourceMap, SourceMapAsset},
}; };
use turbopack_ecmascript::chunk::EcmascriptChunk; use turbopack_ecmascript::{chunk::EcmascriptChunk, utils::StringifyJs};
use crate::{ use crate::{
BrowserChunkingContext, BrowserChunkingContext,
ecmascript::{chunk::EcmascriptBrowserChunk, evaluate::chunk::EcmascriptBrowserEvaluateChunk}, ecmascript::{chunk::EcmascriptBrowserChunk, evaluate::chunk::EcmascriptBrowserEvaluateChunk},
}; };
/// A self-contained chunk that inlines the entire module together with /// A browser entry asset that concatenates the synchronous ECMAScript chunks,
/// the Turbopack browser runtime and the evaluation of the entry modules. /// the Turbopack browser runtime, and evaluation of the entry modules.
/// ///
/// Unlike the regular evaluate chunk, this emits everything into one file at a /// Async chunks remain separate and are referenced from this fixed-path entry.
/// fixed path and never loads additional chunks at runtime.
#[turbo_tasks::value(shared)] #[turbo_tasks::value(shared)]
#[derive(ValueToString)] #[derive(ValueToString)]
#[value_to_string("Ecmascript Browser Single Entry Chunk")] #[value_to_string("Ecmascript Browser Single Entry Chunk")]
pub struct EcmascriptBrowserSingleEntryChunk { pub struct EcmascriptBrowserSingleEntryChunk {
chunking_context: ResolvedVc<BrowserChunkingContext>, chunking_context: ResolvedVc<BrowserChunkingContext>,
path: FileSystemPath, path: FileSystemPath,
chunk: ResolvedVc<EcmascriptChunk>, chunks: Vec<ResolvedVc<EcmascriptChunk>>,
evaluatable_assets: ResolvedVc<EvaluatableAssets>, evaluatable_assets: ResolvedVc<EvaluatableAssets>,
referenced_output_assets: ResolvedVc<OutputAssets>, referenced_output_assets: ResolvedVc<OutputAssets>,
references: ResolvedVc<OutputAssetsReferences>, references: ResolvedVc<OutputAssetsReferences>,
...@@ -43,7 +46,7 @@ impl EcmascriptBrowserSingleEntryChunk { ...@@ -43,7 +46,7 @@ impl EcmascriptBrowserSingleEntryChunk {
pub fn new( pub fn new(
chunking_context: ResolvedVc<BrowserChunkingContext>, chunking_context: ResolvedVc<BrowserChunkingContext>,
path: FileSystemPath, path: FileSystemPath,
chunk: ResolvedVc<EcmascriptChunk>, chunks: Vec<ResolvedVc<EcmascriptChunk>>,
evaluatable_assets: ResolvedVc<EvaluatableAssets>, evaluatable_assets: ResolvedVc<EvaluatableAssets>,
referenced_output_assets: ResolvedVc<OutputAssets>, referenced_output_assets: ResolvedVc<OutputAssets>,
references: ResolvedVc<OutputAssetsReferences>, references: ResolvedVc<OutputAssetsReferences>,
...@@ -52,7 +55,7 @@ impl EcmascriptBrowserSingleEntryChunk { ...@@ -52,7 +55,7 @@ impl EcmascriptBrowserSingleEntryChunk {
EcmascriptBrowserSingleEntryChunk { EcmascriptBrowserSingleEntryChunk {
chunking_context, chunking_context,
path, path,
chunk, chunks,
evaluatable_assets, evaluatable_assets,
referenced_output_assets, referenced_output_assets,
references, references,
...@@ -74,14 +77,40 @@ impl EcmascriptBrowserSingleEntryChunk { ...@@ -74,14 +77,40 @@ impl EcmascriptBrowserSingleEntryChunk {
*this.chunking_context.debug_ids_enabled().await?, *this.chunking_context.debug_ids_enabled().await?,
); );
let module_chunk = EcmascriptBrowserChunk::new(*this.chunking_context, *this.chunk); 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?); code.push_code(&*module_chunk.own_content().code().await?);
}
// Keep independently-generated programs from parsing as one call expression. // Keep independently-generated programs from parsing as one call expression.
code += ";\n"; code += ";\n";
let evaluate_chunk = EcmascriptBrowserEvaluateChunk::new( let evaluate_chunk = EcmascriptBrowserEvaluateChunk::new(
*this.chunking_context, *this.chunking_context,
this.chunk.ident(), this.chunks[0].ident(),
OutputAssets::empty(), OutputAssets::empty(),
*this.evaluatable_assets, *this.evaluatable_assets,
*this.module_graph, *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