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

Prototype single-graph Turbopack build analysis

Co-authored-by: 's avatarWill Binns-Smith <755844+wbinnssmith@users.noreply.github.com>
parent 5b106049
......@@ -342,6 +342,9 @@ pub struct ProjectOptions {
/// Whether to write the route hashes manifest.
pub write_routes_hashes_manifest: bool,
/// Keep source mappings in the build graph for bundle analysis.
pub analyze: bool,
/// The version of Node.js that is available/currently running.
pub current_node_js_version: RcStr,
......@@ -931,7 +934,12 @@ impl ProjectContainer {
nodejs: ResolvedVc::cell(options.define_env.nodejs.iter().cloned().collect()),
}
.cell();
next_config = NextConfig::from_string(Vc::cell(options.next_config.clone()));
let config = NextConfig::from_string(Vc::cell(options.next_config.clone()));
next_config = if options.analyze {
config.with_build_analyze()
} else {
config
};
root_path_str = options.root_path.clone();
project_path = options.project_path.clone();
watch = options.watch;
......
......@@ -185,6 +185,7 @@ fn main() {
.into(),
no_mangling: false,
write_routes_hashes_manifest: false,
analyze: false,
current_node_js_version: rcstr!("18.0.0"),
debug_build_paths: None,
deferred_entries: None,
......
......@@ -130,6 +130,10 @@ pub struct NextConfig {
output: Option<OutputType>,
turbopack: Option<TurbopackConfig>,
production_browser_source_maps: bool,
/// Compute mappings for build analysis without publishing them. Set by the
/// build's NAPI option rather than a user next.config.js field.
#[serde(skip)]
build_analyze: bool,
#[bincode(with = "turbo_bincode::serde_self_describing")]
output_file_tracing_includes: Option<serde_json::Value>,
#[bincode(with = "turbo_bincode::serde_self_describing")]
......@@ -198,6 +202,18 @@ impl NextConfig {
new.experimental.turbopack_input_source_maps = Some(false);
new.cell()
}
#[turbo_tasks::function]
pub fn with_build_analyze(&self) -> Vc<Self> {
let mut new = self.clone();
new.build_analyze = true;
new.cell()
}
#[turbo_tasks::function]
pub fn build_analyze(&self) -> Vc<bool> {
Vc::cell(self.build_analyze)
}
}
#[derive(
......@@ -2590,6 +2606,7 @@ impl NextConfig {
Ok(match (source_maps, input_source_maps) {
(true, true) => SourceMapsType::Full,
(true, false) => SourceMapsType::Partial,
(false, _) if self.build_analyze => SourceMapsType::Internal,
(false, _) => SourceMapsType::None,
}
.cell())
......@@ -2609,6 +2626,7 @@ impl NextConfig {
Ok(match (source_maps, input_source_maps) {
(true, true) => SourceMapsType::Full,
(true, false) => SourceMapsType::Partial,
(false, _) if self.build_analyze => SourceMapsType::Internal,
(false, _) => SourceMapsType::None,
}
.cell())
......
......@@ -166,7 +166,13 @@ async fn get_analyze_data_operation(
app_dir_only: bool,
) -> Result<Vc<OutputAssets>> {
let project = container.project();
let project = project.with_next_config(project.next_config().with_analyze_config());
// The production build already computed in-memory mappings in its own graph.
// Standalone analysis still uses its map-enabled variant of the project.
let project = if *project.next_config().build_analyze().await? {
project
} else {
project.with_next_config(project.next_config().with_analyze_config())
};
let analyze_output_root = project
.node_root()
......
......@@ -209,6 +209,9 @@ pub struct NapiProjectOptions {
/// Whether to write the route hashes manifest.
pub write_routes_hashes_manifest: bool,
/// Keep source mappings in memory for a build with bundle analysis.
pub analyze: Option<bool>,
/// The version of Node.js that is available/currently running.
pub current_node_js_version: RcStr,
......@@ -312,6 +315,7 @@ impl NapiProjectOptions {
browserslist_query,
no_mangling,
write_routes_hashes_manifest,
analyze,
current_node_js_version,
debug_build_paths,
deferred_entries,
......@@ -341,6 +345,7 @@ impl NapiProjectOptions {
browserslist_query,
no_mangling,
write_routes_hashes_manifest,
analyze: analyze.unwrap_or(false),
current_node_js_version,
debug_build_paths: debug_build_paths.map(|p| DebugBuildPaths {
app: p.app,
......
......@@ -432,6 +432,8 @@ export interface NapiProjectOptions {
noMangling: boolean
/** Whether to write the route hashes manifest. */
writeRoutesHashesManifest: boolean
/** Keep source mappings in memory for a build with bundle analysis. */
analyze?: boolean
/** The version of Node.js that is available/currently running. */
currentNodeJsVersion: RcStr
/**
......
......@@ -99,6 +99,7 @@ export async function turbopackBuild(telemetry: Telemetry): Promise<{
noMangling,
writeRoutesHashesManifest:
!!process.env.NEXT_TURBOPACK_WRITE_ROUTES_HASHES_MANIFEST,
analyze: NextBuildContext.analyze,
currentNodeJsVersion,
isPersistentCachingEnabled: persistentCaching,
deferredEntries: config.experimental.deferredEntries,
......
'use client'
export function Demo() {
return <button onClick={() => alert('clicked')}>Demo</button>
}
import './styles.css'
export default function RootLayout({
children,
}: {
......
import ClientEntry from './client-entry'
import { Demo } from './demo'
import './page.css'
export default function Page() {
return (
<div className="analyze-page">
Hello World
Hello World <Demo />
<ClientEntry />
</div>
)
......
button {
color: rebeccapurple;
}
......@@ -8,7 +8,7 @@ import { validateGraphDump } from '../../lib/analyze-graph-schema'
import { shouldUseTurbopack } from 'next-test-utils'
import path from 'node:path'
import type { ChildProcess } from 'node:child_process'
import { existsSync, readFileSync, writeFileSync } from 'node:fs'
import { existsSync, readFileSync, readdirSync, writeFileSync } from 'node:fs'
type RouteEntry = {
route_entry_id: string
......@@ -547,6 +547,78 @@ describe('next analyze', () => {
})
expect(replay.stderr).not.toContain('Analyzing a production build')
}, 120_000)
it('builds and analyzes the same production assets without publishing browser maps', async () => {
const staticDir = path.join(next.testDir, '.next/static')
const analyzeDir = path.join(next.testDir, '.next/diagnostics/analyze/data')
const build = await next.build()
expect(build.exitCode).toBe(0)
const assets = () =>
readdirSync(staticDir, { recursive: true, encoding: 'utf8' })
.filter((file) => /\.(js|css)$/.test(file))
.sort()
const normalAssets = assets()
expect(normalAssets.some((file) => file.endsWith('.js'))).toBe(true)
expect(normalAssets.some((file) => file.endsWith('.css'))).toBe(true)
const normalContents = normalAssets.map((file) =>
readFileSync(path.join(staticDir, file))
)
const result = await next.build({ args: ['--experimental-analyze'] })
expect(result.exitCode).toBe(0)
expect(assets()).toEqual(normalAssets)
for (const [index, file] of normalAssets.entries()) {
const content = readFileSync(path.join(staticDir, file))
expect(content).toEqual(normalContents[index])
expect(content.toString()).not.toContain('sourceMappingURL=')
}
expect(
readdirSync(staticDir, { recursive: true, encoding: 'utf8' }).some((f) =>
f.endsWith('.map')
)
).toBe(false)
expect(
existsSync(path.join(next.testDir, '.next/server/app/page.js'))
).toBe(true)
expect(
readFileSync(path.join(analyzeDir, 'analyze.data'), 'utf8')
).toContain('"path":"demo.tsx"')
expect(existsSync(path.join(analyzeDir, 'modules.data'))).toBe(true)
}, 180_000)
it('preserves published maps when production browser maps are enabled', async () => {
const configPath = path.join(next.testDir, 'next.config.js')
const originalConfig = readFileSync(configPath, 'utf8')
try {
await next.patchFile(
'next.config.js',
`${originalConfig}\nmodule.exports.productionBrowserSourceMaps = true\n`
)
expect((await next.build()).exitCode).toBe(0)
const staticDir = path.join(next.testDir, '.next/static')
const files = () =>
readdirSync(staticDir, { recursive: true, encoding: 'utf8' })
.filter((file) => /\.(js|css)(\.map)?$/.test(file))
.sort()
const normalFiles = files()
expect(normalFiles.some((file) => file.endsWith('.map'))).toBe(true)
const normalContents = normalFiles.map((file) =>
readFileSync(path.join(staticDir, file))
)
expect(
(await next.build({ args: ['--experimental-analyze'] })).exitCode
).toBe(0)
expect(files()).toEqual(normalFiles)
for (const [index, file] of normalFiles.entries()) {
expect(readFileSync(path.join(staticDir, file))).toEqual(
normalContents[index]
)
}
} finally {
await next.patchFile('next.config.js', originalConfig)
}
}, 180_000)
;['-o', '--output'].forEach((flag) => {
describe(`with ${flag} flag`, () => {
it('writes output to .next/diagnostics/analyze path', async () => {
......
/** @type {import('next').NextConfig} */
const nextConfig = {}
const nextConfig = { generateBuildId: async () => 'fixed-analyze-id' }
module.exports = nextConfig
......@@ -776,16 +776,29 @@ impl ChunkingContext for BrowserChunkingContext {
fn reference_chunk_source_maps(&self, _chunk: Vc<Box<dyn OutputAsset>>) -> Vc<bool> {
Vc::cell(match self.source_maps_type {
SourceMapsType::Full => true,
SourceMapsType::Partial => true,
SourceMapsType::Partial | SourceMapsType::Internal => true,
SourceMapsType::None => false,
})
}
#[turbo_tasks::function]
fn publish_chunk_source_maps(&self, _chunk: Vc<Box<dyn OutputAsset>>) -> Vc<bool> {
Vc::cell(matches!(
self.source_maps_type,
SourceMapsType::Full | SourceMapsType::Partial
))
}
#[turbo_tasks::function]
fn emit_pure_import_annotations(&self) -> Vc<bool> {
Vc::cell(!matches!(self.source_maps_type, SourceMapsType::Internal))
}
#[turbo_tasks::function]
fn reference_module_source_maps(&self, _module: Vc<Box<dyn Module>>) -> Vc<bool> {
Vc::cell(match self.source_maps_type {
SourceMapsType::Full => true,
SourceMapsType::Partial => true,
SourceMapsType::Partial | SourceMapsType::Internal => true,
SourceMapsType::None => false,
})
}
......
......@@ -123,7 +123,7 @@ impl OutputAssetsReference for EcmascriptBrowserChunk {
let chunk_references = this.chunk.references().await?;
let include_source_map = *this
.chunking_context
.reference_chunk_source_maps(Vc::upcast(self))
.publish_chunk_source_maps(Vc::upcast(self))
.await?
&& *self.own_content().has_source_map().await?;
let ref_assets = chunk_references.assets.await?;
......
......@@ -155,7 +155,13 @@ impl VersionedContent for EcmascriptBrowserChunkContent {
Ok(AssetContent::file(
FileContent::Content(File::from(
self.code()
.to_rope_with_magic_comments(|| *this.source_map)
.to_rope_with_magic_comments(
*this
.chunking_context
.publish_chunk_source_maps(*ResolvedVc::upcast(this.chunk))
.await?,
|| *this.source_map,
)
.await?,
))
.cell(),
......
......@@ -279,7 +279,7 @@ impl OutputAssetsReference for EcmascriptBrowserEvaluateChunk {
let include_source_map = *this
.chunking_context
.reference_chunk_source_maps(Vc::upcast(self))
.publish_chunk_source_maps(Vc::upcast(self))
.await?;
if include_source_map {
......@@ -313,10 +313,17 @@ impl OutputAsset for EcmascriptBrowserEvaluateChunk {
impl Asset for EcmascriptBrowserEvaluateChunk {
#[turbo_tasks::function]
async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> {
let this = self.await?;
Ok(AssetContent::file(
FileContent::Content(File::from(
self.code()
.to_rope_with_magic_comments(|| self.source_map())
.to_rope_with_magic_comments(
*this
.chunking_context
.publish_chunk_source_maps(Vc::upcast(self))
.await?,
|| self.source_map(),
)
.await?,
))
.cell(),
......
......@@ -123,7 +123,7 @@ impl OutputAssetsReference for EcmascriptBrowserRuntimeChunk {
if *this
.chunking_context
.reference_chunk_source_maps(Vc::upcast(self))
.publish_chunk_source_maps(Vc::upcast(self))
.await?
{
references.push(ResolvedVc::upcast(self.source_map().to_resolved().await?))
......@@ -154,10 +154,17 @@ impl OutputAsset for EcmascriptBrowserRuntimeChunk {
impl Asset for EcmascriptBrowserRuntimeChunk {
#[turbo_tasks::function]
async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> {
let this = self.await?;
Ok(AssetContent::file(
FileContent::Content(File::from(
self.code()
.to_rope_with_magic_comments(|| self.source_map())
.to_rope_with_magic_comments(
*this
.chunking_context
.publish_chunk_source_maps(Vc::upcast(self))
.await?,
|| self.source_map(),
)
.await?,
))
.cell(),
......
......@@ -117,7 +117,7 @@ impl OutputAssetsReference for EcmascriptBrowserSingleEntryChunk {
if *this
.chunking_context
.reference_chunk_source_maps(Vc::upcast(self))
.publish_chunk_source_maps(Vc::upcast(self))
.await?
{
assets.push(ResolvedVc::upcast(self.source_map().to_resolved().await?));
......@@ -144,10 +144,17 @@ impl OutputAsset for EcmascriptBrowserSingleEntryChunk {
impl Asset for EcmascriptBrowserSingleEntryChunk {
#[turbo_tasks::function]
async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> {
let this = self.await?;
Ok(AssetContent::file(
FileContent::Content(File::from(
self.code()
.to_rope_with_magic_comments(|| self.source_map())
.to_rope_with_magic_comments(
*this
.chunking_context
.publish_chunk_source_maps(Vc::upcast(self))
.await?,
|| self.source_map(),
)
.await?,
))
.cell(),
......
......@@ -126,10 +126,17 @@ impl OutputAsset for EcmascriptBrowserWorkerEntrypoint {
impl Asset for EcmascriptBrowserWorkerEntrypoint {
#[turbo_tasks::function]
async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> {
let this = self.await?;
Ok(AssetContent::file(
FileContent::Content(File::from(
self.code()
.to_rope_with_magic_comments(|| self.source_map())
.to_rope_with_magic_comments(
*this
.chunking_context
.publish_chunk_source_maps(Vc::upcast(self))
.await?,
|| self.source_map(),
)
.await?,
))
.cell(),
......
......@@ -62,6 +62,8 @@ pub enum SourceMapsType {
Full,
/// Ignores existing input source maps, but writes source maps for output files.
Partial,
/// Computes mappings for analysis without writing source maps for output files.
Internal,
/// Ignores the existence of source maps and does not write source maps for output files.
None,
}
......@@ -375,6 +377,17 @@ pub trait ChunkingContext {
#[turbo_tasks::function]
fn reference_chunk_source_maps(self: Vc<Self>, chunk: Vc<Box<dyn OutputAsset>>) -> Vc<bool>;
/// Whether to publish a chunk's source map and reference it from the output.
/// Internal mappings may still be computed for analysis when this is false.
#[turbo_tasks::function]
fn publish_chunk_source_maps(self: Vc<Self>, chunk: Vc<Box<dyn OutputAsset>>) -> Vc<bool>;
/// Keep generated PURE markers in the emitted code. Without source maps,
/// the SWC printer drops synthesized PURE spans; internal mappings must
/// preserve that output behavior even while retaining source attribution.
#[turbo_tasks::function]
fn emit_pure_import_annotations(self: Vc<Self>) -> Vc<bool>;
/// Include Source Maps for modules
#[turbo_tasks::function]
fn reference_module_source_maps(self: Vc<Self>, module: Vc<Box<dyn Module>>) -> Vc<bool>;
......
......@@ -117,14 +117,16 @@ impl Code {
PersistedCode(self).resolved_cell()
}
// Formats the code with the source map and debug id comments as
// Formats the code with the published source map and debug id comments.
// Mappings may be kept only for analysis without referencing a map on disk.
pub async fn to_rope_with_magic_comments(
self: Vc<Self>,
publish_source_map: bool,
source_map_path_fn: impl FnOnce() -> Vc<SourceMapAsset>,
) -> Result<Rope> {
let code = self.await?;
Ok(
if code.has_source_map() || code.should_generate_debug_id() {
if (publish_source_map && code.has_source_map()) || code.should_generate_debug_id() {
let mut rope_builder = RopeBuilder::default();
let debug_id = self.debug_id().await?;
// hand minified version of
......@@ -157,7 +159,7 @@ impl Code {
write!(rope_builder, "\n//# debugId={}", debug_id)?;
}
if code.has_source_map() {
if publish_source_map && code.has_source_map() {
let source_map_path = source_map_path_fn().path().await?;
write!(
rope_builder,
......
......@@ -143,7 +143,13 @@ impl CssChunk {
async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> {
let code = self.code().await?;
let rope = if code.has_source_map() {
let rope = if code.has_source_map()
&& *self
.await?
.chunking_context
.publish_chunk_source_maps(Vc::upcast(self))
.await?
{
use std::io::Write;
let mut rope_builder = RopeBuilder::default();
rope_builder.concat(code.source_code());
......@@ -279,7 +285,7 @@ impl OutputAssetsReference for CssChunk {
.await?;
let source_map = if *this
.chunking_context
.reference_chunk_source_maps(Vc::upcast(self))
.publish_chunk_source_maps(Vc::upcast(self))
.await?
{
Some(ResolvedVc::upcast(
......
......@@ -93,7 +93,7 @@ impl OutputAssetsReference for SingleItemCssChunk {
let mut references = Vec::new();
if *this
.chunking_context
.reference_chunk_source_maps(Vc::upcast(self))
.publish_chunk_source_maps(Vc::upcast(self))
.await?
{
references.push(ResolvedVc::upcast(
......@@ -141,7 +141,13 @@ impl Asset for SingleItemCssChunk {
async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> {
let code = self.code().await?;
let rope = if code.has_source_map() {
let rope = if code.has_source_map()
&& *self
.await?
.chunking_context
.publish_chunk_source_maps(Vc::upcast(self))
.await?
{
use std::io::Write;
let mut rope_builder = RopeBuilder::default();
rope_builder.concat(code.source_code());
......
......@@ -1009,7 +1009,11 @@ impl EsmAssetReference {
)
}
};
if this.is_pure_import {
// SWC drops synthesized PURE spans when there are no maps.
// Internal mappings must not change emitted build bytes.
if this.is_pure_import
&& *chunking_context.emit_pure_import_annotations().await?
{
call_expr.set_span(PURE_SP);
}
result.push(CodeGenerationHoistedStmt::new(
......
......@@ -133,7 +133,7 @@ impl OutputAssetsReference for SingleFileEcmascriptOutput {
let include_source_map = *this
.chunking_context
.reference_chunk_source_maps(Vc::upcast(self))
.publish_chunk_source_maps(Vc::upcast(self))
.await?;
let references = if include_source_map {
......
......@@ -490,16 +490,29 @@ impl ChunkingContext for NodeJsChunkingContext {
fn reference_chunk_source_maps(&self, _chunk: Vc<Box<dyn OutputAsset>>) -> Vc<bool> {
Vc::cell(match self.source_maps_type {
SourceMapsType::Full => true,
SourceMapsType::Partial => true,
SourceMapsType::Partial | SourceMapsType::Internal => true,
SourceMapsType::None => false,
})
}
#[turbo_tasks::function]
fn publish_chunk_source_maps(&self, _chunk: Vc<Box<dyn OutputAsset>>) -> Vc<bool> {
Vc::cell(matches!(
self.source_maps_type,
SourceMapsType::Full | SourceMapsType::Partial
))
}
#[turbo_tasks::function]
fn emit_pure_import_annotations(&self) -> Vc<bool> {
Vc::cell(!matches!(self.source_maps_type, SourceMapsType::Internal))
}
#[turbo_tasks::function]
fn reference_module_source_maps(&self, _module: Vc<Box<dyn Module>>) -> Vc<bool> {
Vc::cell(match self.source_maps_type {
SourceMapsType::Full => true,
SourceMapsType::Partial => true,
SourceMapsType::Partial | SourceMapsType::Internal => true,
SourceMapsType::None => false,
})
}
......
......@@ -87,7 +87,7 @@ impl OutputAssetsReference for EcmascriptBuildNodeChunk {
let chunk_references = this.chunk.references().await?;
let include_source_map = *this
.chunking_context
.reference_chunk_source_maps(Vc::upcast(self))
.publish_chunk_source_maps(Vc::upcast(self))
.await?;
let ref_assets = chunk_references.assets.await?;
let mut assets =
......
......@@ -115,7 +115,13 @@ impl VersionedContent for EcmascriptNodeChunkContent {
Ok(AssetContent::file(
FileContent::Content(File::from(
self.code()
.to_rope_with_magic_comments(|| *this.source_map)
.to_rope_with_magic_comments(
*this
.chunking_context
.publish_chunk_source_maps(*ResolvedVc::upcast(this.chunk))
.await?,
|| *this.source_map,
)
.await?,
))
.cell(),
......
......@@ -188,7 +188,7 @@ impl OutputAssetsReference for EcmascriptBuildNodeEntryChunk {
if *this
.chunking_context
.reference_chunk_source_maps(Vc::upcast(self))
.publish_chunk_source_maps(Vc::upcast(self))
.await?
{
assets.push(ResolvedVc::upcast(self.source_map().to_resolved().await?))
......
......@@ -165,7 +165,7 @@ impl OutputAssetsReference for EcmascriptBuildNodeRuntimeChunk {
if *this
.chunking_context
.reference_chunk_source_maps(Vc::upcast(self))
.publish_chunk_source_maps(Vc::upcast(self))
.await?
{
references.push(ResolvedVc::upcast(self.source_map().to_resolved().await?))
......@@ -194,10 +194,17 @@ impl OutputAsset for EcmascriptBuildNodeRuntimeChunk {
impl Asset for EcmascriptBuildNodeRuntimeChunk {
#[turbo_tasks::function]
async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> {
let this = self.await?;
Ok(AssetContent::file(
FileContent::Content(File::from(
self.code()
.to_rope_with_magic_comments(|| self.source_map())
.to_rope_with_magic_comments(
*this
.chunking_context
.publish_chunk_source_maps(Vc::upcast(self))
.await?,
|| self.source_map(),
)
.await?,
))
.cell(),
......
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