Skip to content

Commit

Permalink
Turbopack build: Implement reactProductionProfiling (#67909)
Browse files Browse the repository at this point in the history
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 #

-->
  • Loading branch information
timneutkens authored Jul 22, 2024
1 parent ec34cdf commit 5fb1908
Show file tree
Hide file tree
Showing 6 changed files with 43 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 @@ -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

0 comments on commit 5fb1908

Please sign in to comment.