Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turbopack build: Implement reactProductionProfiling #67909

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -789,6 +790,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
28 changes: 27 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 @@ -169,6 +169,14 @@ pub async fn get_next_client_import_map(
"next/dist/compiled/react-dom-experimental/static.browser",
),
);
let react_client_package = get_react_client_package(&next_config).await?;
import_map.insert_exact_alias(
"react-dom/client",
request_to_import_mapping(
app_dir,
&format!("next/dist/compiled/react-dom{react_flavor}/{react_client_package}"),
),
);
import_map.insert_wildcard_alias(
"react-dom/",
request_to_import_mapping(
Expand Down Expand Up @@ -322,6 +330,7 @@ pub async fn get_next_server_import_map(
import_map.insert_exact_alias("react", external);
import_map.insert_wildcard_alias("react/", external);
import_map.insert_exact_alias("react-dom", external);
import_map.insert_exact_alias("react-dom/client", external);
import_map.insert_wildcard_alias("react-dom/", external);
import_map.insert_exact_alias("styled-jsx", external);
import_map.insert_exact_alias(
Expand Down Expand Up @@ -639,6 +648,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 +669,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 +684,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 +862,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
3 changes: 1 addition & 2 deletions packages/next/src/lib/turbopack-warning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ 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',
]

// 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 @@ -14107,13 +14107,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
Loading