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 { ...@@ -342,6 +342,9 @@ pub struct ProjectOptions {
/// Whether to write the route hashes manifest. /// Whether to write the route hashes manifest.
pub write_routes_hashes_manifest: bool, 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. /// The version of Node.js that is available/currently running.
pub current_node_js_version: RcStr, pub current_node_js_version: RcStr,
...@@ -931,7 +934,12 @@ impl ProjectContainer { ...@@ -931,7 +934,12 @@ impl ProjectContainer {
nodejs: ResolvedVc::cell(options.define_env.nodejs.iter().cloned().collect()), nodejs: ResolvedVc::cell(options.define_env.nodejs.iter().cloned().collect()),
} }
.cell(); .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(); root_path_str = options.root_path.clone();
project_path = options.project_path.clone(); project_path = options.project_path.clone();
watch = options.watch; watch = options.watch;
......
...@@ -185,6 +185,7 @@ fn main() { ...@@ -185,6 +185,7 @@ fn main() {
.into(), .into(),
no_mangling: false, no_mangling: false,
write_routes_hashes_manifest: false, write_routes_hashes_manifest: false,
analyze: false,
current_node_js_version: rcstr!("18.0.0"), current_node_js_version: rcstr!("18.0.0"),
debug_build_paths: None, debug_build_paths: None,
deferred_entries: None, deferred_entries: None,
......
...@@ -130,6 +130,10 @@ pub struct NextConfig { ...@@ -130,6 +130,10 @@ pub struct NextConfig {
output: Option<OutputType>, output: Option<OutputType>,
turbopack: Option<TurbopackConfig>, turbopack: Option<TurbopackConfig>,
production_browser_source_maps: bool, 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")] #[bincode(with = "turbo_bincode::serde_self_describing")]
output_file_tracing_includes: Option<serde_json::Value>, output_file_tracing_includes: Option<serde_json::Value>,
#[bincode(with = "turbo_bincode::serde_self_describing")] #[bincode(with = "turbo_bincode::serde_self_describing")]
...@@ -198,6 +202,18 @@ impl NextConfig { ...@@ -198,6 +202,18 @@ impl NextConfig {
new.experimental.turbopack_input_source_maps = Some(false); new.experimental.turbopack_input_source_maps = Some(false);
new.cell() 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( #[derive(
...@@ -2590,6 +2606,7 @@ impl NextConfig { ...@@ -2590,6 +2606,7 @@ impl NextConfig {
Ok(match (source_maps, input_source_maps) { Ok(match (source_maps, input_source_maps) {
(true, true) => SourceMapsType::Full, (true, true) => SourceMapsType::Full,
(true, false) => SourceMapsType::Partial, (true, false) => SourceMapsType::Partial,
(false, _) if self.build_analyze => SourceMapsType::Internal,
(false, _) => SourceMapsType::None, (false, _) => SourceMapsType::None,
} }
.cell()) .cell())
...@@ -2609,6 +2626,7 @@ impl NextConfig { ...@@ -2609,6 +2626,7 @@ impl NextConfig {
Ok(match (source_maps, input_source_maps) { Ok(match (source_maps, input_source_maps) {
(true, true) => SourceMapsType::Full, (true, true) => SourceMapsType::Full,
(true, false) => SourceMapsType::Partial, (true, false) => SourceMapsType::Partial,
(false, _) if self.build_analyze => SourceMapsType::Internal,
(false, _) => SourceMapsType::None, (false, _) => SourceMapsType::None,
} }
.cell()) .cell())
......
...@@ -166,7 +166,13 @@ async fn get_analyze_data_operation( ...@@ -166,7 +166,13 @@ async fn get_analyze_data_operation(
app_dir_only: bool, app_dir_only: bool,
) -> Result<Vc<OutputAssets>> { ) -> Result<Vc<OutputAssets>> {
let project = container.project(); 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 let analyze_output_root = project
.node_root() .node_root()
......
...@@ -209,6 +209,9 @@ pub struct NapiProjectOptions { ...@@ -209,6 +209,9 @@ pub struct NapiProjectOptions {
/// Whether to write the route hashes manifest. /// Whether to write the route hashes manifest.
pub write_routes_hashes_manifest: bool, 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. /// The version of Node.js that is available/currently running.
pub current_node_js_version: RcStr, pub current_node_js_version: RcStr,
...@@ -312,6 +315,7 @@ impl NapiProjectOptions { ...@@ -312,6 +315,7 @@ impl NapiProjectOptions {
browserslist_query, browserslist_query,
no_mangling, no_mangling,
write_routes_hashes_manifest, write_routes_hashes_manifest,
analyze,
current_node_js_version, current_node_js_version,
debug_build_paths, debug_build_paths,
deferred_entries, deferred_entries,
...@@ -341,6 +345,7 @@ impl NapiProjectOptions { ...@@ -341,6 +345,7 @@ impl NapiProjectOptions {
browserslist_query, browserslist_query,
no_mangling, no_mangling,
write_routes_hashes_manifest, write_routes_hashes_manifest,
analyze: analyze.unwrap_or(false),
current_node_js_version, current_node_js_version,
debug_build_paths: debug_build_paths.map(|p| DebugBuildPaths { debug_build_paths: debug_build_paths.map(|p| DebugBuildPaths {
app: p.app, app: p.app,
......
...@@ -432,6 +432,8 @@ export interface NapiProjectOptions { ...@@ -432,6 +432,8 @@ export interface NapiProjectOptions {
noMangling: boolean noMangling: boolean
/** Whether to write the route hashes manifest. */ /** Whether to write the route hashes manifest. */
writeRoutesHashesManifest: boolean 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. */ /** The version of Node.js that is available/currently running. */
currentNodeJsVersion: RcStr currentNodeJsVersion: RcStr
/** /**
......
...@@ -99,6 +99,7 @@ export async function turbopackBuild(telemetry: Telemetry): Promise<{ ...@@ -99,6 +99,7 @@ export async function turbopackBuild(telemetry: Telemetry): Promise<{
noMangling, noMangling,
writeRoutesHashesManifest: writeRoutesHashesManifest:
!!process.env.NEXT_TURBOPACK_WRITE_ROUTES_HASHES_MANIFEST, !!process.env.NEXT_TURBOPACK_WRITE_ROUTES_HASHES_MANIFEST,
analyze: NextBuildContext.analyze,
currentNodeJsVersion, currentNodeJsVersion,
isPersistentCachingEnabled: persistentCaching, isPersistentCachingEnabled: persistentCaching,
deferredEntries: config.experimental.deferredEntries, deferredEntries: config.experimental.deferredEntries,
......
'use client'
export function Demo() {
return <button onClick={() => alert('clicked')}>Demo</button>
}
import './styles.css'
export default function RootLayout({ export default function RootLayout({
children, children,
}: { }: {
......
import ClientEntry from './client-entry' import ClientEntry from './client-entry'
import { Demo } from './demo'
import './page.css' import './page.css'
export default function Page() { export default function Page() {
return ( return (
<div className="analyze-page"> <div className="analyze-page">
Hello World Hello World <Demo />
<ClientEntry /> <ClientEntry />
</div> </div>
) )
......
button {
color: rebeccapurple;
}
...@@ -8,7 +8,7 @@ import { validateGraphDump } from '../../lib/analyze-graph-schema' ...@@ -8,7 +8,7 @@ import { validateGraphDump } from '../../lib/analyze-graph-schema'
import { shouldUseTurbopack } from 'next-test-utils' import { shouldUseTurbopack } from 'next-test-utils'
import path from 'node:path' import path from 'node:path'
import type { ChildProcess } from 'node:child_process' import type { ChildProcess } from 'node:child_process'
import { existsSync, readFileSync, writeFileSync } from 'node:fs' import { existsSync, readFileSync, readdirSync, writeFileSync } from 'node:fs'
type RouteEntry = { type RouteEntry = {
route_entry_id: string route_entry_id: string
...@@ -547,6 +547,78 @@ describe('next analyze', () => { ...@@ -547,6 +547,78 @@ describe('next analyze', () => {
}) })
expect(replay.stderr).not.toContain('Analyzing a production build') expect(replay.stderr).not.toContain('Analyzing a production build')
}, 120_000) }, 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) => { ;['-o', '--output'].forEach((flag) => {
describe(`with ${flag} flag`, () => { describe(`with ${flag} flag`, () => {
it('writes output to .next/diagnostics/analyze path', async () => { it('writes output to .next/diagnostics/analyze path', async () => {
......
/** @type {import('next').NextConfig} */ /** @type {import('next').NextConfig} */
const nextConfig = {} const nextConfig = { generateBuildId: async () => 'fixed-analyze-id' }
module.exports = nextConfig module.exports = nextConfig
...@@ -776,16 +776,29 @@ impl ChunkingContext for BrowserChunkingContext { ...@@ -776,16 +776,29 @@ impl ChunkingContext for BrowserChunkingContext {
fn reference_chunk_source_maps(&self, _chunk: Vc<Box<dyn OutputAsset>>) -> Vc<bool> { fn reference_chunk_source_maps(&self, _chunk: Vc<Box<dyn OutputAsset>>) -> Vc<bool> {
Vc::cell(match self.source_maps_type { Vc::cell(match self.source_maps_type {
SourceMapsType::Full => true, SourceMapsType::Full => true,
SourceMapsType::Partial => true, SourceMapsType::Partial | SourceMapsType::Internal => true,
SourceMapsType::None => false, 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] #[turbo_tasks::function]
fn reference_module_source_maps(&self, _module: Vc<Box<dyn Module>>) -> Vc<bool> { fn reference_module_source_maps(&self, _module: Vc<Box<dyn Module>>) -> Vc<bool> {
Vc::cell(match self.source_maps_type { Vc::cell(match self.source_maps_type {
SourceMapsType::Full => true, SourceMapsType::Full => true,
SourceMapsType::Partial => true, SourceMapsType::Partial | SourceMapsType::Internal => true,
SourceMapsType::None => false, SourceMapsType::None => false,
}) })
} }
......
...@@ -123,7 +123,7 @@ impl OutputAssetsReference for EcmascriptBrowserChunk { ...@@ -123,7 +123,7 @@ impl OutputAssetsReference for EcmascriptBrowserChunk {
let chunk_references = this.chunk.references().await?; let chunk_references = this.chunk.references().await?;
let include_source_map = *this let include_source_map = *this
.chunking_context .chunking_context
.reference_chunk_source_maps(Vc::upcast(self)) .publish_chunk_source_maps(Vc::upcast(self))
.await? .await?
&& *self.own_content().has_source_map().await?; && *self.own_content().has_source_map().await?;
let ref_assets = chunk_references.assets.await?; let ref_assets = chunk_references.assets.await?;
......
...@@ -155,7 +155,13 @@ impl VersionedContent for EcmascriptBrowserChunkContent { ...@@ -155,7 +155,13 @@ impl VersionedContent for EcmascriptBrowserChunkContent {
Ok(AssetContent::file( Ok(AssetContent::file(
FileContent::Content(File::from( FileContent::Content(File::from(
self.code() 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?, .await?,
)) ))
.cell(), .cell(),
......
...@@ -279,7 +279,7 @@ impl OutputAssetsReference for EcmascriptBrowserEvaluateChunk { ...@@ -279,7 +279,7 @@ impl OutputAssetsReference for EcmascriptBrowserEvaluateChunk {
let include_source_map = *this let include_source_map = *this
.chunking_context .chunking_context
.reference_chunk_source_maps(Vc::upcast(self)) .publish_chunk_source_maps(Vc::upcast(self))
.await?; .await?;
if include_source_map { if include_source_map {
...@@ -313,10 +313,17 @@ impl OutputAsset for EcmascriptBrowserEvaluateChunk { ...@@ -313,10 +313,17 @@ impl OutputAsset for EcmascriptBrowserEvaluateChunk {
impl Asset for EcmascriptBrowserEvaluateChunk { impl Asset for EcmascriptBrowserEvaluateChunk {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> { async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> {
let this = self.await?;
Ok(AssetContent::file( Ok(AssetContent::file(
FileContent::Content(File::from( FileContent::Content(File::from(
self.code() 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?, .await?,
)) ))
.cell(), .cell(),
......
...@@ -123,7 +123,7 @@ impl OutputAssetsReference for EcmascriptBrowserRuntimeChunk { ...@@ -123,7 +123,7 @@ impl OutputAssetsReference for EcmascriptBrowserRuntimeChunk {
if *this if *this
.chunking_context .chunking_context
.reference_chunk_source_maps(Vc::upcast(self)) .publish_chunk_source_maps(Vc::upcast(self))
.await? .await?
{ {
references.push(ResolvedVc::upcast(self.source_map().to_resolved().await?)) references.push(ResolvedVc::upcast(self.source_map().to_resolved().await?))
...@@ -154,10 +154,17 @@ impl OutputAsset for EcmascriptBrowserRuntimeChunk { ...@@ -154,10 +154,17 @@ impl OutputAsset for EcmascriptBrowserRuntimeChunk {
impl Asset for EcmascriptBrowserRuntimeChunk { impl Asset for EcmascriptBrowserRuntimeChunk {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> { async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> {
let this = self.await?;
Ok(AssetContent::file( Ok(AssetContent::file(
FileContent::Content(File::from( FileContent::Content(File::from(
self.code() 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?, .await?,
)) ))
.cell(), .cell(),
......
...@@ -117,7 +117,7 @@ impl OutputAssetsReference for EcmascriptBrowserSingleEntryChunk { ...@@ -117,7 +117,7 @@ impl OutputAssetsReference for EcmascriptBrowserSingleEntryChunk {
if *this if *this
.chunking_context .chunking_context
.reference_chunk_source_maps(Vc::upcast(self)) .publish_chunk_source_maps(Vc::upcast(self))
.await? .await?
{ {
assets.push(ResolvedVc::upcast(self.source_map().to_resolved().await?)); assets.push(ResolvedVc::upcast(self.source_map().to_resolved().await?));
...@@ -144,10 +144,17 @@ impl OutputAsset for EcmascriptBrowserSingleEntryChunk { ...@@ -144,10 +144,17 @@ impl OutputAsset for EcmascriptBrowserSingleEntryChunk {
impl Asset for EcmascriptBrowserSingleEntryChunk { impl Asset for EcmascriptBrowserSingleEntryChunk {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> { async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> {
let this = self.await?;
Ok(AssetContent::file( Ok(AssetContent::file(
FileContent::Content(File::from( FileContent::Content(File::from(
self.code() 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?, .await?,
)) ))
.cell(), .cell(),
......
...@@ -126,10 +126,17 @@ impl OutputAsset for EcmascriptBrowserWorkerEntrypoint { ...@@ -126,10 +126,17 @@ impl OutputAsset for EcmascriptBrowserWorkerEntrypoint {
impl Asset for EcmascriptBrowserWorkerEntrypoint { impl Asset for EcmascriptBrowserWorkerEntrypoint {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> { async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> {
let this = self.await?;
Ok(AssetContent::file( Ok(AssetContent::file(
FileContent::Content(File::from( FileContent::Content(File::from(
self.code() 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?, .await?,
)) ))
.cell(), .cell(),
......
...@@ -62,6 +62,8 @@ pub enum SourceMapsType { ...@@ -62,6 +62,8 @@ pub enum SourceMapsType {
Full, Full,
/// Ignores existing input source maps, but writes source maps for output files. /// Ignores existing input source maps, but writes source maps for output files.
Partial, 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. /// Ignores the existence of source maps and does not write source maps for output files.
None, None,
} }
...@@ -375,6 +377,17 @@ pub trait ChunkingContext { ...@@ -375,6 +377,17 @@ pub trait ChunkingContext {
#[turbo_tasks::function] #[turbo_tasks::function]
fn reference_chunk_source_maps(self: Vc<Self>, chunk: Vc<Box<dyn OutputAsset>>) -> Vc<bool>; 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 /// Include Source Maps for modules
#[turbo_tasks::function] #[turbo_tasks::function]
fn reference_module_source_maps(self: Vc<Self>, module: Vc<Box<dyn Module>>) -> Vc<bool>; fn reference_module_source_maps(self: Vc<Self>, module: Vc<Box<dyn Module>>) -> Vc<bool>;
......
...@@ -117,14 +117,16 @@ impl Code { ...@@ -117,14 +117,16 @@ impl Code {
PersistedCode(self).resolved_cell() 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( pub async fn to_rope_with_magic_comments(
self: Vc<Self>, self: Vc<Self>,
publish_source_map: bool,
source_map_path_fn: impl FnOnce() -> Vc<SourceMapAsset>, source_map_path_fn: impl FnOnce() -> Vc<SourceMapAsset>,
) -> Result<Rope> { ) -> Result<Rope> {
let code = self.await?; let code = self.await?;
Ok( 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 mut rope_builder = RopeBuilder::default();
let debug_id = self.debug_id().await?; let debug_id = self.debug_id().await?;
// hand minified version of // hand minified version of
...@@ -157,7 +159,7 @@ impl Code { ...@@ -157,7 +159,7 @@ impl Code {
write!(rope_builder, "\n//# debugId={}", debug_id)?; 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?; let source_map_path = source_map_path_fn().path().await?;
write!( write!(
rope_builder, rope_builder,
......
...@@ -143,7 +143,13 @@ impl CssChunk { ...@@ -143,7 +143,13 @@ impl CssChunk {
async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> { async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> {
let code = self.code().await?; 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; use std::io::Write;
let mut rope_builder = RopeBuilder::default(); let mut rope_builder = RopeBuilder::default();
rope_builder.concat(code.source_code()); rope_builder.concat(code.source_code());
...@@ -279,7 +285,7 @@ impl OutputAssetsReference for CssChunk { ...@@ -279,7 +285,7 @@ impl OutputAssetsReference for CssChunk {
.await?; .await?;
let source_map = if *this let source_map = if *this
.chunking_context .chunking_context
.reference_chunk_source_maps(Vc::upcast(self)) .publish_chunk_source_maps(Vc::upcast(self))
.await? .await?
{ {
Some(ResolvedVc::upcast( Some(ResolvedVc::upcast(
......
...@@ -93,7 +93,7 @@ impl OutputAssetsReference for SingleItemCssChunk { ...@@ -93,7 +93,7 @@ impl OutputAssetsReference for SingleItemCssChunk {
let mut references = Vec::new(); let mut references = Vec::new();
if *this if *this
.chunking_context .chunking_context
.reference_chunk_source_maps(Vc::upcast(self)) .publish_chunk_source_maps(Vc::upcast(self))
.await? .await?
{ {
references.push(ResolvedVc::upcast( references.push(ResolvedVc::upcast(
...@@ -141,7 +141,13 @@ impl Asset for SingleItemCssChunk { ...@@ -141,7 +141,13 @@ impl Asset for SingleItemCssChunk {
async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> { async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> {
let code = self.code().await?; 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; use std::io::Write;
let mut rope_builder = RopeBuilder::default(); let mut rope_builder = RopeBuilder::default();
rope_builder.concat(code.source_code()); rope_builder.concat(code.source_code());
......
...@@ -1009,7 +1009,11 @@ impl EsmAssetReference { ...@@ -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); call_expr.set_span(PURE_SP);
} }
result.push(CodeGenerationHoistedStmt::new( result.push(CodeGenerationHoistedStmt::new(
......
...@@ -133,7 +133,7 @@ impl OutputAssetsReference for SingleFileEcmascriptOutput { ...@@ -133,7 +133,7 @@ impl OutputAssetsReference for SingleFileEcmascriptOutput {
let include_source_map = *this let include_source_map = *this
.chunking_context .chunking_context
.reference_chunk_source_maps(Vc::upcast(self)) .publish_chunk_source_maps(Vc::upcast(self))
.await?; .await?;
let references = if include_source_map { let references = if include_source_map {
......
...@@ -490,16 +490,29 @@ impl ChunkingContext for NodeJsChunkingContext { ...@@ -490,16 +490,29 @@ impl ChunkingContext for NodeJsChunkingContext {
fn reference_chunk_source_maps(&self, _chunk: Vc<Box<dyn OutputAsset>>) -> Vc<bool> { fn reference_chunk_source_maps(&self, _chunk: Vc<Box<dyn OutputAsset>>) -> Vc<bool> {
Vc::cell(match self.source_maps_type { Vc::cell(match self.source_maps_type {
SourceMapsType::Full => true, SourceMapsType::Full => true,
SourceMapsType::Partial => true, SourceMapsType::Partial | SourceMapsType::Internal => true,
SourceMapsType::None => false, 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] #[turbo_tasks::function]
fn reference_module_source_maps(&self, _module: Vc<Box<dyn Module>>) -> Vc<bool> { fn reference_module_source_maps(&self, _module: Vc<Box<dyn Module>>) -> Vc<bool> {
Vc::cell(match self.source_maps_type { Vc::cell(match self.source_maps_type {
SourceMapsType::Full => true, SourceMapsType::Full => true,
SourceMapsType::Partial => true, SourceMapsType::Partial | SourceMapsType::Internal => true,
SourceMapsType::None => false, SourceMapsType::None => false,
}) })
} }
......
...@@ -87,7 +87,7 @@ impl OutputAssetsReference for EcmascriptBuildNodeChunk { ...@@ -87,7 +87,7 @@ impl OutputAssetsReference for EcmascriptBuildNodeChunk {
let chunk_references = this.chunk.references().await?; let chunk_references = this.chunk.references().await?;
let include_source_map = *this let include_source_map = *this
.chunking_context .chunking_context
.reference_chunk_source_maps(Vc::upcast(self)) .publish_chunk_source_maps(Vc::upcast(self))
.await?; .await?;
let ref_assets = chunk_references.assets.await?; let ref_assets = chunk_references.assets.await?;
let mut assets = let mut assets =
......
...@@ -115,7 +115,13 @@ impl VersionedContent for EcmascriptNodeChunkContent { ...@@ -115,7 +115,13 @@ impl VersionedContent for EcmascriptNodeChunkContent {
Ok(AssetContent::file( Ok(AssetContent::file(
FileContent::Content(File::from( FileContent::Content(File::from(
self.code() 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?, .await?,
)) ))
.cell(), .cell(),
......
...@@ -188,7 +188,7 @@ impl OutputAssetsReference for EcmascriptBuildNodeEntryChunk { ...@@ -188,7 +188,7 @@ impl OutputAssetsReference for EcmascriptBuildNodeEntryChunk {
if *this if *this
.chunking_context .chunking_context
.reference_chunk_source_maps(Vc::upcast(self)) .publish_chunk_source_maps(Vc::upcast(self))
.await? .await?
{ {
assets.push(ResolvedVc::upcast(self.source_map().to_resolved().await?)) assets.push(ResolvedVc::upcast(self.source_map().to_resolved().await?))
......
...@@ -165,7 +165,7 @@ impl OutputAssetsReference for EcmascriptBuildNodeRuntimeChunk { ...@@ -165,7 +165,7 @@ impl OutputAssetsReference for EcmascriptBuildNodeRuntimeChunk {
if *this if *this
.chunking_context .chunking_context
.reference_chunk_source_maps(Vc::upcast(self)) .publish_chunk_source_maps(Vc::upcast(self))
.await? .await?
{ {
references.push(ResolvedVc::upcast(self.source_map().to_resolved().await?)) references.push(ResolvedVc::upcast(self.source_map().to_resolved().await?))
...@@ -194,10 +194,17 @@ impl OutputAsset for EcmascriptBuildNodeRuntimeChunk { ...@@ -194,10 +194,17 @@ impl OutputAsset for EcmascriptBuildNodeRuntimeChunk {
impl Asset for EcmascriptBuildNodeRuntimeChunk { impl Asset for EcmascriptBuildNodeRuntimeChunk {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> { async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> {
let this = self.await?;
Ok(AssetContent::file( Ok(AssetContent::file(
FileContent::Content(File::from( FileContent::Content(File::from(
self.code() 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?, .await?,
)) ))
.cell(), .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