Skip to content

Commit

Permalink
Turbopack build: Implement reactProductionProfiling
Browse files Browse the repository at this point in the history
  • Loading branch information
timneutkens committed Jul 18, 2024
1 parent 73f0868 commit d5f2d3c
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 6 deletions.
6 changes: 6 additions & 0 deletions packages/next-swc/crates/next-core/src/next_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub struct NextConfig {
pub experimental: ExperimentalConfig,
pub images: ImageConfig,
pub page_extensions: Vec<RcStr>,
pub react_production_profiling: Option<bool>,
pub react_strict_mode: Option<bool>,
pub transpile_packages: Option<Vec<RcStr>>,
pub modularize_imports: Option<IndexMap<String, ModularizeImportPackageConfig>>,
Expand Down Expand Up @@ -780,6 +781,11 @@ impl NextConfig {
Vc::cell(self.bundle_pages_router_dependencies.unwrap_or_default())
}

#[turbo_tasks::function]
pub fn enable_react_production_profiling(&self) -> Vc<bool> {
Vc::cell(self.react_production_profiling.unwrap_or_default())
}

#[turbo_tasks::function]
pub async fn server_external_packages(self: Vc<Self>) -> Result<Vc<Vec<RcStr>>> {
Ok(Vc::cell(
Expand Down
19 changes: 18 additions & 1 deletion packages/next-swc/crates/next-core/src/next_import_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,17 @@ async fn insert_next_server_special_aliases(
Ok(())
}

async fn get_react_client_package(&next_config: &Vc<NextConfig>) -> Result<&'static str> {
let react_production_profiling = *next_config.enable_react_production_profiling().await?;
let react_client_package = if react_production_profiling {
"profiling"
} else {
"client"
};

Ok(react_client_package)
}

async fn rsc_aliases(
import_map: &mut ImportMap,
project_path: Vc<FileSystemPath>,
Expand All @@ -649,6 +660,7 @@ async fn rsc_aliases(
let ppr = *next_config.enable_ppr().await?;
let taint = *next_config.enable_taint().await?;
let react_channel = if ppr || taint { "-experimental" } else { "" };
let react_client_package = get_react_client_package(&next_config).await?;

let mut alias = IndexMap::new();
if matches!(
Expand All @@ -663,7 +675,7 @@ async fn rsc_aliases(
"react/jsx-runtime" => format!("next/dist/compiled/react{react_channel}/jsx-runtime"),
"react/jsx-dev-runtime" => format!("next/dist/compiled/react{react_channel}/jsx-dev-runtime"),
"react/compiler-runtime" => format!("next/dist/compiled/react{react_channel}/compiler-runtime"),
"react-dom/client" => format!("next/dist/compiled/react-dom{react_channel}/client"),
"react-dom/client" => format!("next/dist/compiled/react-dom{react_channel}/{react_client_package}"),
"react-dom/static" => format!("next/dist/compiled/react-dom-experimental/static"),
"react-dom/static.edge" => format!("next/dist/compiled/react-dom-experimental/static.edge"),
"react-dom/static.browser" => format!("next/dist/compiled/react-dom-experimental/static.browser"),
Expand Down Expand Up @@ -841,6 +853,11 @@ async fn insert_next_shared_aliases(
import_map.insert_singleton_alias("next", project_path);
import_map.insert_singleton_alias("react", project_path);
import_map.insert_singleton_alias("react-dom", project_path);
let react_client_package = get_react_client_package(&next_config).await?;
import_map.insert_exact_alias(
"react-dom/client",
request_to_import_mapping(project_path, &format!("react-dom/{react_client_package}")),
);

import_map.insert_alias(
// Make sure you can't import custom server as it'll cause all Next.js internals to be
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,7 @@ export default async function build(
loadConfig(PHASE_PRODUCTION_BUILD, dir, {
// Log for next.config loading process
silent: false,
reactProductionProfiling,
}),
turborepoAccessTraceResult
)
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/lib/turbopack-warning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ const unsupportedTurbopackNextConfigOptions = [
]

// The following will need to be supported by `next build --turbo`
const unsupportedProductionSpecificTurbopackNextConfigOptions = [
const unsupportedProductionSpecificTurbopackNextConfigOptions: string[] = [
// TODO: Support disabling sourcemaps, currently they're always enabled.
// 'productionBrowserSourceMaps',
'reactProductionProfiling',
// 'reactProductionProfiling',
]

// check for babelrc, swc plugins
Expand Down
6 changes: 6 additions & 0 deletions packages/next/src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -923,11 +923,13 @@ export default async function loadConfig(
rawConfig,
silent = true,
onLoadUserConfig,
reactProductionProfiling,
}: {
customConfig?: object | null
rawConfig?: boolean
silent?: boolean
onLoadUserConfig?: (conf: NextConfig) => void
reactProductionProfiling?: boolean
} = {}
): Promise<NextConfigComplete> {
if (!process.env.__NEXT_PRIVATE_RENDER_WORKER) {
Expand Down Expand Up @@ -1084,6 +1086,10 @@ export default async function loadConfig(
: canonicalBase) || ''
}

if (reactProductionProfiling) {
userConfig.reactProductionProfiling = reactProductionProfiling
}

if (
userConfig.experimental?.turbo?.loaders &&
!userConfig.experimental?.turbo?.rules
Expand Down
5 changes: 2 additions & 3 deletions test/turbopack-build-tests-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14110,13 +14110,12 @@
},
"test/integration/react-profiling-mode/test/index.test.js": {
"passed": [
"React Profiling Mode production mode without config enabled should not have used the react-dom profiling bundle"
],
"failed": [
"React Profiling Mode production mode without config enabled should not have used the react-dom profiling bundle",
"React Profiling Mode production mode with config enabled should have used the react-dom profiling bundle for client component",
"React Profiling Mode production mode with config enabled should have used the react-dom profiling bundle for pages",
"React Profiling Mode production mode with config enabled should have used the react-dom profiling bundle for server component"
],
"failed": [],
"pending": [],
"flakey": [],
"runtimeError": false
Expand Down

0 comments on commit d5f2d3c

Please sign in to comment.