Unverified Commit 5fb1908e authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Turbopack build: Implement reactProductionProfiling (#67909)

Implements support for `reactProductionProfiling` and `--profile` for
Turbopack build.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->
parent ec34cdfb
...@@ -73,6 +73,7 @@ pub struct NextConfig { ...@@ -73,6 +73,7 @@ pub struct NextConfig {
pub experimental: ExperimentalConfig, pub experimental: ExperimentalConfig,
pub images: ImageConfig, pub images: ImageConfig,
pub page_extensions: Vec<RcStr>, pub page_extensions: Vec<RcStr>,
pub react_production_profiling: Option<bool>,
pub react_strict_mode: Option<bool>, pub react_strict_mode: Option<bool>,
pub transpile_packages: Option<Vec<RcStr>>, pub transpile_packages: Option<Vec<RcStr>>,
pub modularize_imports: Option<IndexMap<String, ModularizeImportPackageConfig>>, pub modularize_imports: Option<IndexMap<String, ModularizeImportPackageConfig>>,
...@@ -789,6 +790,11 @@ impl NextConfig { ...@@ -789,6 +790,11 @@ impl NextConfig {
Vc::cell(self.bundle_pages_router_dependencies.unwrap_or_default()) 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] #[turbo_tasks::function]
pub async fn server_external_packages(self: Vc<Self>) -> Result<Vc<Vec<RcStr>>> { pub async fn server_external_packages(self: Vc<Self>) -> Result<Vc<Vec<RcStr>>> {
Ok(Vc::cell( Ok(Vc::cell(
......
...@@ -169,6 +169,14 @@ pub async fn get_next_client_import_map( ...@@ -169,6 +169,14 @@ pub async fn get_next_client_import_map(
"next/dist/compiled/react-dom-experimental/static.browser", "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( import_map.insert_wildcard_alias(
"react-dom/", "react-dom/",
request_to_import_mapping( request_to_import_mapping(
...@@ -322,6 +330,7 @@ pub async fn get_next_server_import_map( ...@@ -322,6 +330,7 @@ pub async fn get_next_server_import_map(
import_map.insert_exact_alias("react", external); import_map.insert_exact_alias("react", external);
import_map.insert_wildcard_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", external);
import_map.insert_exact_alias("react-dom/client", external);
import_map.insert_wildcard_alias("react-dom/", external); import_map.insert_wildcard_alias("react-dom/", external);
import_map.insert_exact_alias("styled-jsx", external); import_map.insert_exact_alias("styled-jsx", external);
import_map.insert_exact_alias( import_map.insert_exact_alias(
...@@ -639,6 +648,17 @@ async fn insert_next_server_special_aliases( ...@@ -639,6 +648,17 @@ async fn insert_next_server_special_aliases(
Ok(()) 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( async fn rsc_aliases(
import_map: &mut ImportMap, import_map: &mut ImportMap,
project_path: Vc<FileSystemPath>, project_path: Vc<FileSystemPath>,
...@@ -649,6 +669,7 @@ async fn rsc_aliases( ...@@ -649,6 +669,7 @@ async fn rsc_aliases(
let ppr = *next_config.enable_ppr().await?; let ppr = *next_config.enable_ppr().await?;
let taint = *next_config.enable_taint().await?; let taint = *next_config.enable_taint().await?;
let react_channel = if ppr || taint { "-experimental" } else { "" }; let react_channel = if ppr || taint { "-experimental" } else { "" };
let react_client_package = get_react_client_package(&next_config).await?;
let mut alias = IndexMap::new(); let mut alias = IndexMap::new();
if matches!( if matches!(
...@@ -663,7 +684,7 @@ async fn rsc_aliases( ...@@ -663,7 +684,7 @@ async fn rsc_aliases(
"react/jsx-runtime" => format!("next/dist/compiled/react{react_channel}/jsx-runtime"), "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/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/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" => format!("next/dist/compiled/react-dom-experimental/static"),
"react-dom/static.edge" => format!("next/dist/compiled/react-dom-experimental/static.edge"), "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"), "react-dom/static.browser" => format!("next/dist/compiled/react-dom-experimental/static.browser"),
...@@ -841,6 +862,11 @@ async fn insert_next_shared_aliases( ...@@ -841,6 +862,11 @@ async fn insert_next_shared_aliases(
import_map.insert_singleton_alias("next", project_path); import_map.insert_singleton_alias("next", project_path);
import_map.insert_singleton_alias("react", project_path); import_map.insert_singleton_alias("react", project_path);
import_map.insert_singleton_alias("react-dom", 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( import_map.insert_alias(
// Make sure you can't import custom server as it'll cause all Next.js internals to be // Make sure you can't import custom server as it'll cause all Next.js internals to be
......
...@@ -697,6 +697,7 @@ export default async function build( ...@@ -697,6 +697,7 @@ export default async function build(
loadConfig(PHASE_PRODUCTION_BUILD, dir, { loadConfig(PHASE_PRODUCTION_BUILD, dir, {
// Log for next.config loading process // Log for next.config loading process
silent: false, silent: false,
reactProductionProfiling,
}), }),
turborepoAccessTraceResult turborepoAccessTraceResult
) )
......
...@@ -50,10 +50,9 @@ const unsupportedTurbopackNextConfigOptions = [ ...@@ -50,10 +50,9 @@ const unsupportedTurbopackNextConfigOptions = [
] ]
// The following will need to be supported by `next build --turbo` // 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. // TODO: Support disabling sourcemaps, currently they're always enabled.
// 'productionBrowserSourceMaps', // 'productionBrowserSourceMaps',
'reactProductionProfiling',
] ]
// check for babelrc, swc plugins // check for babelrc, swc plugins
......
...@@ -923,11 +923,13 @@ export default async function loadConfig( ...@@ -923,11 +923,13 @@ export default async function loadConfig(
rawConfig, rawConfig,
silent = true, silent = true,
onLoadUserConfig, onLoadUserConfig,
reactProductionProfiling,
}: { }: {
customConfig?: object | null customConfig?: object | null
rawConfig?: boolean rawConfig?: boolean
silent?: boolean silent?: boolean
onLoadUserConfig?: (conf: NextConfig) => void onLoadUserConfig?: (conf: NextConfig) => void
reactProductionProfiling?: boolean
} = {} } = {}
): Promise<NextConfigComplete> { ): Promise<NextConfigComplete> {
if (!process.env.__NEXT_PRIVATE_RENDER_WORKER) { if (!process.env.__NEXT_PRIVATE_RENDER_WORKER) {
...@@ -1084,6 +1086,10 @@ export default async function loadConfig( ...@@ -1084,6 +1086,10 @@ export default async function loadConfig(
: canonicalBase) || '' : canonicalBase) || ''
} }
if (reactProductionProfiling) {
userConfig.reactProductionProfiling = reactProductionProfiling
}
if ( if (
userConfig.experimental?.turbo?.loaders && userConfig.experimental?.turbo?.loaders &&
!userConfig.experimental?.turbo?.rules !userConfig.experimental?.turbo?.rules
......
...@@ -14107,13 +14107,12 @@ ...@@ -14107,13 +14107,12 @@
}, },
"test/integration/react-profiling-mode/test/index.test.js": { "test/integration/react-profiling-mode/test/index.test.js": {
"passed": [ "passed": [
"React Profiling Mode production mode without config enabled should not have used the react-dom profiling bundle" "React Profiling Mode production mode without config enabled should not have used the react-dom profiling bundle",
],
"failed": [
"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 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 pages",
"React Profiling Mode production mode with config enabled should have used the react-dom profiling bundle for server component" "React Profiling Mode production mode with config enabled should have used the react-dom profiling bundle for server component"
], ],
"failed": [],
"pending": [], "pending": [],
"flakey": [], "flakey": [],
"runtimeError": false "runtimeError": false
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