Unverified Commit ba971247 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Merge branch 'canary' into 04-08-deprecate_edge_runtime

parents eb2e7862 f4972228
......@@ -60,6 +60,24 @@ rustflags = [
"-Clink-arg=/Brepro",
]
# Why `linker-flavor=gnu-lld-cc` instead of `linker = "rust-lld"`?
#
# `linker = "rust-lld"` invokes lld directly, bypassing the cc linker driver.
# This fails because lld alone can't find system libraries (-lgcc_s, -lc) and
# doesn't understand -Wl, prefixed args from crate build scripts.
# `gnu-lld-cc` + `link-self-contained=+linker` routes lld through cc, which
# resolves library paths and translates -Wl, args.
#
# On x86_64-unknown-linux-gnu, this is already the nightly default since
# Rust 1.90, so no explicit config is needed for GNU targets.
# Not yet default on aarch64-unknown-linux-gnu or any musl target.
# https://blog.rust-lang.org/2025/09/01/rust-lld-on-1.90.0-stable/
[target.'cfg(all(target_os = "linux", target_env = "gnu", not(target_arch = "x86_64")))']
rustflags = [
"-Clinker-flavor=gnu-lld-cc",
"-Clink-self-contained=+linker",
]
# Musl targets: disable static CRT linking. We produce dynamic .node shared
# libraries (N-API addons), not static binaries.
[target.'cfg(all(target_os = "linux", target_env = "musl"))']
......
......@@ -71,6 +71,7 @@ jobs:
with:
skipInstallBuild: 'yes'
stepName: 'build-native'
uploadNativeArtifact: 'next-swc-linux'
secrets: inherit
build-native-windows:
......@@ -83,6 +84,7 @@ jobs:
stepName: 'build-native-windows'
runs_on_labels: '["windows","self-hosted","x64"]'
buildNativeTarget: 'x86_64-pc-windows-msvc'
uploadNativeArtifact: 'next-swc-windows'
secrets: inherit
......@@ -698,7 +700,7 @@ jobs:
test-unit-windows:
name: test unit windows
needs: ['changes', 'build-native', 'build-native-windows']
needs: ['changes', 'build-native-windows', 'build-next']
if: ${{ needs.changes.outputs.docs-only == 'false' }}
strategy:
......@@ -866,14 +868,7 @@ jobs:
test-dev-windows:
name: test dev windows
needs:
[
'optimize-ci',
'changes',
'build-native-windows',
'build-native',
'build-next',
]
needs: ['optimize-ci', 'changes', 'build-native-windows', 'build-next']
if: ${{ needs.optimize-ci.outputs.skip == 'false' && needs.changes.outputs.docs-only == 'false' }}
uses: ./.github/workflows/build_reusable.yml
......@@ -896,14 +891,7 @@ jobs:
test-integration-windows:
name: test integration windows
needs:
[
'optimize-ci',
'changes',
'build-native-windows',
'build-native',
'build-next',
]
needs: ['optimize-ci', 'changes', 'build-native-windows', 'build-next']
if: ${{ needs.optimize-ci.outputs.skip == 'false' && needs.changes.outputs.docs-only == 'false' }}
uses: ./.github/workflows/build_reusable.yml
......@@ -926,14 +914,7 @@ jobs:
test-prod-windows:
name: test prod windows
needs:
[
'optimize-ci',
'changes',
'build-native-windows',
'build-native',
'build-next',
]
needs: ['optimize-ci', 'changes', 'build-native-windows', 'build-next']
if: ${{ needs.optimize-ci.outputs.skip == 'false' && needs.changes.outputs.docs-only == 'false' }}
uses: ./.github/workflows/build_reusable.yml
......
......@@ -85,6 +85,12 @@ on:
type: string
default: ''
uploadNativeArtifact:
description: 'Artifact name to upload native .node files to after building. When set, this job is the native binary producer.'
required: false
type: string
default: ''
browser:
description: 'Browser to use for tests'
required: false
......@@ -207,6 +213,11 @@ jobs:
- run: corepack enable
- run: pwd
- name: Kill stale sccache processes
if: ${{ runner.os == 'Windows' }}
shell: powershell
run: Get-Process sccache -ErrorAction SilentlyContinue | Stop-Process -Force
- run: rm -rf .git
- uses: actions/checkout@v4
......@@ -232,14 +243,14 @@ jobs:
# local action -> needs to run after checkout
- name: Install Rust
uses: ./.github/actions/setup-rust
if: ${{ inputs.skipNativeBuild != 'yes' || inputs.needsNextest == 'yes' || inputs.needsRust == 'yes' }}
if: ${{ (inputs.uploadNativeArtifact != '') || inputs.needsNextest == 'yes' || inputs.needsRust == 'yes' }}
- name: Install nextest
if: ${{ inputs.needsNextest == 'yes' }}
uses: taiki-e/install-action@3a0adc33ab45d7b9b9da91822dd2b3c0151704be # nextest
- run: rustc --version
if: ${{ inputs.skipNativeBuild != 'yes' || inputs.needsNextest == 'yes' || inputs.needsRust == 'yes' }}
if: ${{ (inputs.uploadNativeArtifact != '') || inputs.needsNextest == 'yes' || inputs.needsRust == 'yes' }}
- run: corepack prepare --activate yarn@1.22.19 && npm i -g "@napi-rs/cli@${NAPI_CLI_VERSION}"
......@@ -258,12 +269,29 @@ jobs:
- name: Start sccache
uses: ./.github/actions/sccache
if: ${{ inputs.skipNativeBuild != 'yes' || inputs.needsNextest == 'yes' || inputs.needsRust == 'yes' }}
if: ${{ runner.os != 'Windows' && ((inputs.uploadNativeArtifact != '') || inputs.needsNextest == 'yes' || inputs.needsRust == 'yes') }}
with:
turbo-token: ${{ secrets.TURBO_REMOTE_CACHE_TOKEN }}
- name: Download pre-built native binary
if: ${{ inputs.skipNativeBuild != 'yes' && inputs.uploadNativeArtifact == '' }}
id: native-download
continue-on-error: true
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: ${{ contains(fromJson(inputs.runs_on_labels), 'windows') && 'next-swc-windows' || 'next-swc-linux' }}
path: packages/next-swc/native/
- run: pnpm dlx turbo@${TURBO_VERSION} run build-native-${{ inputs.rustBuildProfile }} ${TURBO_ARGS} --summarize -- --target ${{ inputs.buildNativeTarget }}
if: ${{ inputs.skipNativeBuild != 'yes' }}
if: ${{ inputs.skipNativeBuild != 'yes' && (inputs.uploadNativeArtifact != '' || steps.native-download.outcome != 'success') }}
- name: Upload native artifact
if: ${{ inputs.uploadNativeArtifact != '' }}
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: ${{ inputs.uploadNativeArtifact }}
path: packages/next-swc/native/next-swc.*.node
retention-days: 1
- name: Upload next-swc artifact
if: ${{ inputs.uploadSwcArtifact == 'yes' }}
......
name: Sync Canary After Backport Release
on:
workflow_run:
workflows:
- build-and-deploy
types:
- completed
workflow_dispatch:
inputs:
workflowRunId:
description: Completed build-and-deploy workflow run ID to evaluate
required: true
type: string
headSha:
description: Release commit SHA from that workflow run
required: true
type: string
dispatch:
description: Dispatch trigger_release after evaluation
default: false
required: false
type: boolean
concurrency:
group: sync-backport-canary-release
cancel-in-progress: true
jobs:
evaluate:
if: ${{ github.repository_owner == 'vercel' && (github.event_name == 'workflow_dispatch' || (github.event.workflow_run.event == 'push' && github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.head_branch != 'canary')) }}
runs-on: ubuntu-latest
permissions:
contents: read
outputs:
should_dispatch: ${{ steps.evaluate.outputs.should_dispatch }}
reason: ${{ steps.evaluate.outputs.reason }}
current_canary_version: ${{ steps.evaluate.outputs.current_canary_version }}
released_version: ${{ steps.evaluate.outputs.released_version }}
latest_version: ${{ steps.evaluate.outputs.latest_version }}
steps:
- name: Check whether head commit is a stable release
id: precheck
uses: actions/github-script@v7
with:
script: |
if (context.eventName === 'workflow_dispatch') {
core.setOutput('should_evaluate', 'true')
return
}
const headCommitMessage =
context.payload.workflow_run.head_commit?.message?.trim() || ''
const stableReleaseCommitRegex = /^v\d+\.\d+\.\d+$/
if (stableReleaseCommitRegex.test(headCommitMessage)) {
core.setOutput('should_evaluate', 'true')
return
}
core.setOutput('should_evaluate', 'false')
core.setOutput('should_dispatch', 'false')
core.setOutput('reason', 'Head commit is not a stable release commit')
- uses: actions/checkout@v6
if: steps.precheck.outputs.should_evaluate == 'true'
with:
ref: canary
fetch-depth: 1
- name: Evaluate backport release
id: evaluate
if: steps.precheck.outputs.should_evaluate == 'true'
env:
RELEASE_BOT_GITHUB_TOKEN: ${{ secrets.RELEASE_BOT_GITHUB_TOKEN }}
WORKFLOW_RUN_ID: ${{ github.event_name == 'workflow_dispatch' && inputs.workflowRunId || github.event.workflow_run.id }}
HEAD_SHA: ${{ github.event_name == 'workflow_dispatch' && inputs.headSha || github.event.workflow_run.head_sha }}
HEAD_COMMIT_MESSAGE: ${{ github.event_name == 'workflow_dispatch' && '' || github.event.workflow_run.head_commit.message }}
run: node ./scripts/check-backport-canary-release.js --workflow-run-id "$WORKFLOW_RUN_ID" --head-sha "$HEAD_SHA"
- name: Print evaluation
run: |
echo "should_dispatch=${{ steps.evaluate.outputs.should_dispatch || steps.precheck.outputs.should_dispatch }}"
echo "reason=${{ steps.evaluate.outputs.reason || steps.precheck.outputs.reason }}"
echo "released_version=${{ steps.evaluate.outputs.released_version }}"
echo "latest_version=${{ steps.evaluate.outputs.latest_version }}"
echo "current_canary_version=${{ steps.evaluate.outputs.current_canary_version }}"
echo "dispatch_input=${{ github.event_name == 'workflow_dispatch' && inputs.dispatch || false }}"
echo "auto_dispatch_enabled=${{ vars.ENABLE_BACKPORT_CANARY_SYNC || 'false' }}"
dispatch:
needs: evaluate
if: ${{ needs.evaluate.outputs.should_dispatch == 'true' && ((github.event_name == 'workflow_dispatch' && inputs.dispatch) || (github.event_name == 'workflow_run' && vars.ENABLE_BACKPORT_CANARY_SYNC == 'true')) }}
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
with:
github-token: ${{ secrets.RELEASE_BOT_GITHUB_TOKEN }}
script: |
await github.request(
"POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches",
{
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: "trigger_release.yml",
ref: "canary",
inputs: {
releaseType: "canary",
semverType: "minor",
force: "true",
},
}
)
console.info(
`Triggered canary release after backport ${process.env.RELEASED_VERSION}; current canary was ${process.env.CURRENT_CANARY_VERSION}`
)
env:
RELEASED_VERSION: ${{ needs.evaluate.outputs.released_version }}
CURRENT_CANARY_VERSION: ${{ needs.evaluate.outputs.current_canary_version }}
......@@ -30,7 +30,7 @@ pub async fn get_next_client_transforms_rules(
) -> Result<Vec<ModuleRule>> {
let mut rules = vec![];
let modularize_imports_config = &next_config.modularize_imports().await?;
let modularize_imports_config = next_config.modularize_imports();
let enable_mdx_rs = next_config.mdx_rs().await?.is_some();
let page_extensions: Vec<String> = next_config
.page_extensions()
......@@ -40,14 +40,13 @@ pub async fn get_next_client_transforms_rules(
.collect();
if !foreign_code {
rules.push(get_next_lint_transform_rule(enable_mdx_rs));
rules.push(get_next_lint_transform_rule(enable_mdx_rs).await?);
}
if !modularize_imports_config.is_empty() {
rules.push(get_next_modularize_imports_rule(
modularize_imports_config,
enable_mdx_rs,
));
if !modularize_imports_config.await?.is_empty() {
rules.push(
get_next_modularize_imports_rule(modularize_imports_config, enable_mdx_rs).await?,
);
}
// This is purely a performance optimization:
......@@ -64,11 +63,11 @@ pub async fn get_next_client_transforms_rules(
))],
));
rules.push(get_next_font_transform_rule(enable_mdx_rs));
rules.push(get_next_font_transform_rule(enable_mdx_rs).await?);
let is_development = mode.await?.is_development();
if is_development {
rules.push(get_debug_fn_name_rule(enable_mdx_rs));
rules.push(get_debug_fn_name_rule(enable_mdx_rs).await?);
}
let use_cache_enabled = *next_config.enable_use_cache().await?;
......@@ -78,17 +77,20 @@ pub async fn get_next_client_transforms_rules(
match &context_ty {
ClientContextType::Pages { pages_dir } => {
if !foreign_code {
rules.push(get_next_pages_transforms_rule(
pages_dir.clone(),
ExportFilter::StripDataExports,
enable_mdx_rs,
vec![],
&page_extensions,
)?);
rules.push(get_next_disallow_export_all_in_page_rule(
enable_mdx_rs,
pages_dir.clone(),
));
rules.push(
get_next_pages_transforms_rule(
pages_dir.clone(),
ExportFilter::StripDataExports,
enable_mdx_rs,
vec![],
&page_extensions,
)
.await?,
);
rules.push(
get_next_disallow_export_all_in_page_rule(enable_mdx_rs, pages_dir.clone())
.await?,
);
}
}
ClientContextType::App { .. } => {
......@@ -109,8 +111,8 @@ pub async fn get_next_client_transforms_rules(
};
if !foreign_code {
rules.push(get_next_cjs_optimizer_rule(enable_mdx_rs));
rules.push(get_next_pure_rule(enable_mdx_rs));
rules.push(get_next_cjs_optimizer_rule(enable_mdx_rs).await?);
rules.push(get_next_pure_rule(enable_mdx_rs).await?);
rules.push(
get_next_dynamic_transform_rule(false, false, is_app_dir, mode, enable_mdx_rs).await?,
......
......@@ -27,7 +27,8 @@ use turbopack_core::{
};
use turbopack_css::chunk::CssChunkType;
use turbopack_ecmascript::{
AnalyzeMode, TypeofWindow, chunk::EcmascriptChunkType, references::esm::UrlRewriteBehavior,
AnalyzeMode, CustomTransformer, TransformPlugin, TypeofWindow, chunk::EcmascriptChunkType,
references::esm::UrlRewriteBehavior,
};
use turbopack_ecmascript_plugins::transform::directives::{
client::ClientDirectiveTransformer, client_disallowed::ClientDisallowedDirectiveTransformer,
......@@ -752,17 +753,18 @@ pub async fn get_server_module_options_context(
ecmascript_client_reference_transition_name,
..
} => {
let client_directive_transformer = ecmascript_client_reference_transition_name.map(
|ecmascript_client_reference_transition_name| {
get_ecma_transform_rule(
Box::new(ClientDirectiveTransformer::new(
ecmascript_client_reference_transition_name,
)),
let client_directive_transformer =
if let Some(name) = ecmascript_client_reference_transition_name {
Some(get_ecma_transform_rule(
client_directive_transform_plugin(name)
.to_resolved()
.await?,
enable_mdx_rs.is_some(),
EcmascriptTransformStage::Preprocess,
)
},
);
))
} else {
None
};
foreign_next_server_rules.extend(internal_custom_rules);
foreign_next_server_rules.extend(client_directive_transformer.clone());
......@@ -839,9 +841,9 @@ pub async fn get_server_module_options_context(
ecmascript_client_reference_transition_name
{
common_next_server_rules.push(get_ecma_transform_rule(
Box::new(ClientDirectiveTransformer::new(
ecmascript_client_reference_transition_name,
)),
client_directive_transform_plugin(ecmascript_client_reference_transition_name)
.to_resolved()
.await?,
enable_mdx_rs.is_some(),
EcmascriptTransformStage::Preprocess,
));
......@@ -910,26 +912,28 @@ pub async fn get_server_module_options_context(
app_dir,
ecmascript_client_reference_transition_name,
} => {
let custom_source_transform_rules: Vec<ModuleRule> = vec![
if let Some(ecmascript_client_reference_transition_name) =
ecmascript_client_reference_transition_name
{
let directive_transform_rule =
if let Some(name) = ecmascript_client_reference_transition_name {
get_ecma_transform_rule(
Box::new(ClientDirectiveTransformer::new(
ecmascript_client_reference_transition_name,
)),
client_directive_transform_plugin(name)
.to_resolved()
.await?,
enable_mdx_rs.is_some(),
EcmascriptTransformStage::Preprocess,
)
} else {
get_ecma_transform_rule(
Box::new(ClientDisallowedDirectiveTransformer::new(
"next/dist/client/use-client-disallowed.js".to_string(),
)),
client_disallowed_directive_transform_plugin(rcstr!(
"next/dist/client/use-client-disallowed.js"
))
.to_resolved()
.await?,
enable_mdx_rs.is_some(),
EcmascriptTransformStage::Preprocess,
)
},
};
let custom_source_transform_rules: Vec<ModuleRule> = vec![
directive_transform_rule,
get_next_react_server_components_transform_rule(next_config, true, app_dir).await?,
];
......@@ -993,6 +997,19 @@ pub async fn get_server_module_options_context(
Ok(module_options_context)
}
#[turbo_tasks::function]
fn client_directive_transform_plugin(transition_name: RcStr) -> Vc<TransformPlugin> {
Vc::cell(Box::new(ClientDirectiveTransformer::new(transition_name))
as Box<dyn CustomTransformer + Send + Sync>)
}
#[turbo_tasks::function]
fn client_disallowed_directive_transform_plugin(error_proxy_module: RcStr) -> Vc<TransformPlugin> {
Vc::cell(Box::new(ClientDisallowedDirectiveTransformer::new(
error_proxy_module.to_string(),
)) as Box<dyn CustomTransformer + Send + Sync>)
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, TaskInput, TraceRawVcs, Encode, Decode)]
pub struct ServerChunkingContextOptions {
pub mode: Vc<NextMode>,
......
......@@ -37,7 +37,7 @@ pub async fn get_next_server_transforms_rules(
) -> Result<Vec<ModuleRule>> {
let mut rules = vec![];
let modularize_imports_config = &next_config.modularize_imports().await?;
let modularize_imports_config = next_config.modularize_imports();
let mdx_rs = next_config.mdx_rs().await?.is_some();
let page_extensions: Vec<String> = next_config
.page_extensions()
......@@ -47,16 +47,13 @@ pub async fn get_next_server_transforms_rules(
.collect();
if !foreign_code {
rules.push(get_next_lint_transform_rule(mdx_rs));
rules.push(get_next_lint_transform_rule(mdx_rs).await?);
}
if !modularize_imports_config.is_empty() {
rules.push(get_next_modularize_imports_rule(
modularize_imports_config,
mdx_rs,
));
if !modularize_imports_config.await?.is_empty() {
rules.push(get_next_modularize_imports_rule(modularize_imports_config, mdx_rs).await?);
}
rules.push(get_next_font_transform_rule(mdx_rs));
rules.push(get_next_font_transform_rule(mdx_rs).await?);
if !matches!(context_ty, ServerContextType::AppRSC { .. }) {
rules.extend([
......@@ -84,19 +81,21 @@ pub async fn get_next_server_transforms_rules(
let is_server_components = match &context_ty {
ServerContextType::Pages { pages_dir } | ServerContextType::PagesApi { pages_dir } => {
if !foreign_code {
rules.push(get_next_disallow_export_all_in_page_rule(
mdx_rs,
pages_dir.clone(),
));
rules.push(get_next_pages_transforms_rule(
pages_dir.clone(),
ExportFilter::StripDefaultExport,
mdx_rs,
vec![RuleCondition::ReferenceType(ReferenceTypeCondition::Entry(
Some(EntryReferenceSubType::PageData),
))],
&page_extensions,
)?);
rules.push(
get_next_disallow_export_all_in_page_rule(mdx_rs, pages_dir.clone()).await?,
);
rules.push(
get_next_pages_transforms_rule(
pages_dir.clone(),
ExportFilter::StripDefaultExport,
mdx_rs,
vec![RuleCondition::ReferenceType(ReferenceTypeCondition::Entry(
Some(EntryReferenceSubType::PageData),
))],
&page_extensions,
)
.await?,
);
}
false
}
......@@ -158,10 +157,7 @@ pub async fn get_next_server_transforms_rules(
};
if is_app_dir {
rules.push(get_next_debug_instant_stack_rule(
mdx_rs,
page_extensions.clone(),
));
rules.push(get_next_debug_instant_stack_rule(mdx_rs, page_extensions.clone()).await?);
}
if is_app_dir &&
......@@ -170,7 +166,7 @@ pub async fn get_next_server_transforms_rules(
next_runtime != NextRuntime::Edge &&
*next_config.enable_cache_components().await?
{
rules.push(get_next_track_dynamic_imports_transform_rule(mdx_rs));
rules.push(get_next_track_dynamic_imports_transform_rule(mdx_rs).await?);
}
if !foreign_code {
......@@ -179,8 +175,8 @@ pub async fn get_next_server_transforms_rules(
.await?,
);
rules.push(get_next_cjs_optimizer_rule(mdx_rs));
rules.push(get_next_pure_rule(mdx_rs));
rules.push(get_next_cjs_optimizer_rule(mdx_rs).await?);
rules.push(get_next_pure_rule(mdx_rs).await?);
// [NOTE]: this rule only works in prod config
// https://github.com/vercel/next.js/blob/a1d0259ea06592c5ca6df882e9b1d0d0121c5083/packages/next/src/build/swc/options.ts#L409
......@@ -194,16 +190,19 @@ pub async fn get_next_server_transforms_rules(
let mode = *mode.await?;
if mode == NextMode::Development {
rules.push(get_middleware_dynamic_assert_rule(mdx_rs));
rules.push(get_middleware_dynamic_assert_rule(mdx_rs).await?);
}
if !foreign_code {
rules.push(next_edge_node_api_assert(
mdx_rs,
matches!(context_ty, ServerContextType::Middleware { .. })
&& mode == NextMode::Build,
mode == NextMode::Build,
));
rules.push(
next_edge_node_api_assert(
mdx_rs,
matches!(context_ty, ServerContextType::Middleware { .. })
&& mode == NextMode::Build,
mode == NextMode::Build,
)
.await?,
);
}
if matches!(context_ty, ServerContextType::AppRoute { .. }) {
......@@ -246,14 +245,14 @@ pub async fn get_next_server_internal_transforms_rules(
match context_ty {
ServerContextType::Pages { .. } => {
// Apply next/font transforms to foreign code
rules.push(get_next_font_transform_rule(mdx_rs));
rules.push(get_next_font_transform_rule(mdx_rs).await?);
}
ServerContextType::PagesApi { .. } => {}
ServerContextType::AppSSR { .. } => {
rules.push(get_next_font_transform_rule(mdx_rs));
rules.push(get_next_font_transform_rule(mdx_rs).await?);
}
ServerContextType::AppRSC { .. } => {
rules.push(get_next_font_transform_rule(mdx_rs));
rules.push(get_next_font_transform_rule(mdx_rs).await?);
}
ServerContextType::AppRoute { .. } => {}
ServerContextType::Middleware { .. } => {}
......
......@@ -2,25 +2,32 @@ use anyhow::Result;
use async_trait::async_trait;
use next_custom_transforms::transforms::debug_fn_name::debug_fn_name;
use swc_core::ecma::{ast::Program, visit::VisitMutWith};
use turbo_tasks::ResolvedVc;
use turbo_tasks::{ResolvedVc, Vc};
use turbopack::module_options::{ModuleRule, ModuleRuleEffect};
use turbopack_ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext};
use turbopack_ecmascript::{
CustomTransformer, EcmascriptInputTransform, TransformContext, TransformPlugin,
};
use super::module_rule_match_js_no_url;
pub fn get_debug_fn_name_rule(enable_mdx_rs: bool) -> ModuleRule {
let debug_fn_name_transform = EcmascriptInputTransform::Plugin(ResolvedVc::cell(Box::new(
DebugFnNameTransformer {},
) as _));
pub async fn get_debug_fn_name_rule(enable_mdx_rs: bool) -> Result<ModuleRule> {
let debug_fn_name_transform =
EcmascriptInputTransform::Plugin(debug_fn_name_transform_plugin().to_resolved().await?);
ModuleRule::new(
// TODO: use get_ecma_transform_rule instead
Ok(ModuleRule::new(
module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::ExtendEcmascriptTransforms {
preprocess: ResolvedVc::cell(vec![]),
main: ResolvedVc::cell(vec![]),
postprocess: ResolvedVc::cell(vec![debug_fn_name_transform]),
}],
)
))
}
#[turbo_tasks::function]
fn debug_fn_name_transform_plugin() -> Vc<TransformPlugin> {
Vc::cell(Box::new(DebugFnNameTransformer {}) as Box<dyn CustomTransformer + Send + Sync>)
}
#[derive(Debug)]
......
use anyhow::Result;
use turbo_tasks::Vc;
use turbopack::module_options::ModuleRule;
use turbopack_ecmascript::{CustomTransformer, TransformPlugin};
use turbopack_ecmascript_plugins::transform::emotion::EmotionTransformer;
use super::get_ecma_transform_rule;
......@@ -11,9 +12,37 @@ use crate::{
pub async fn get_emotion_transform_rule(next_config: Vc<NextConfig>) -> Result<Option<ModuleRule>> {
let enable_mdx_rs = next_config.mdx_rs().await?.is_some();
let module_rule = next_config
let has_config = next_config
.compiler()
.await?
.emotion
.as_ref()
.is_some_and(|config| {
matches!(
config,
EmotionTransformOptionsOrBoolean::Boolean(true)
| EmotionTransformOptionsOrBoolean::Options(_)
)
});
if has_config {
let plugin = emotion_transform_plugin(next_config).to_resolved().await?;
Ok(Some(get_ecma_transform_rule(
plugin,
enable_mdx_rs,
EcmascriptTransformStage::Main,
)))
} else {
Ok(None)
}
}
#[turbo_tasks::function]
async fn emotion_transform_plugin(next_config: Vc<NextConfig>) -> Result<Vc<TransformPlugin>> {
use anyhow::Context as _;
let compiler = next_config.compiler().await?;
let transformer = compiler
.emotion
.as_ref()
.and_then(|config| match config {
......@@ -23,13 +52,8 @@ pub async fn get_emotion_transform_rule(next_config: Vc<NextConfig>) -> Result<O
EmotionTransformOptionsOrBoolean::Options(value) => EmotionTransformer::new(value),
_ => None,
})
.map(|transformer| {
get_ecma_transform_rule(
Box::new(transformer),
enable_mdx_rs,
EcmascriptTransformStage::Main,
)
});
Ok(module_rule)
.context("emotion config must exist")?;
Ok(Vc::cell(
Box::new(transformer) as Box<dyn CustomTransformer + Send + Sync>
))
}
......@@ -35,7 +35,7 @@ use turbo_tasks::ResolvedVc;
use turbo_tasks_fs::FileSystemPath;
use turbopack::module_options::{ModuleRule, ModuleRuleEffect, ModuleType, RuleCondition};
use turbopack_core::reference_type::ReferenceTypeCondition;
use turbopack_ecmascript::{CustomTransformer, EcmascriptInputTransform};
use turbopack_ecmascript::{EcmascriptInputTransform, TransformPlugin};
use crate::next_image::{StructuredImageModuleType, module::BlurPlaceholderMode};
......@@ -134,11 +134,11 @@ pub(crate) enum EcmascriptTransformStage {
/// Create a new module rule for the given ecmatransform, runs against
/// any ecmascript (with mdx if enabled) except url reference type
pub(crate) fn get_ecma_transform_rule(
transformer: Box<dyn CustomTransformer + Send + Sync>,
transformer: ResolvedVc<TransformPlugin>,
enable_mdx_rs: bool,
stage: EcmascriptTransformStage,
) -> ModuleRule {
let transformer = EcmascriptInputTransform::Plugin(ResolvedVc::cell(transformer as _));
let transformer = EcmascriptInputTransform::Plugin(transformer);
let (preprocess, main, postprocess) = match stage {
EcmascriptTransformStage::Preprocess => (vec![transformer], vec![], vec![]),
EcmascriptTransformStage::Main => (vec![], vec![transformer], vec![]),
......
......@@ -6,11 +6,13 @@ use bincode::{Decode, Encode};
use modularize_imports::{Config, PackageConfig, modularize_imports};
use serde::{Deserialize, Serialize};
use swc_core::ecma::ast::Program;
use turbo_tasks::{FxIndexMap, NonLocalValue, OperationValue, ResolvedVc, trace::TraceRawVcs};
use turbo_tasks::{FxIndexMap, NonLocalValue, OperationValue, ResolvedVc, Vc, trace::TraceRawVcs};
use turbopack::module_options::{ModuleRule, ModuleRuleEffect};
use turbopack_ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext};
use turbopack_ecmascript::{
CustomTransformer, EcmascriptInputTransform, TransformContext, TransformPlugin,
};
use crate::next_shared::transforms::module_rule_match_js_no_url;
use crate::{next_config::ModularizeImports, next_shared::transforms::module_rule_match_js_no_url};
#[derive(
Clone,
......@@ -58,21 +60,35 @@ pub enum Transform {
}
/// Returns a rule which applies the Next.js modularize imports transform.
pub fn get_next_modularize_imports_rule(
modularize_imports_config: &FxIndexMap<String, ModularizeImportPackageConfig>,
pub async fn get_next_modularize_imports_rule(
modularize_imports_config: Vc<ModularizeImports>,
enable_mdx_rs: bool,
) -> ModuleRule {
let transformer = EcmascriptInputTransform::Plugin(ResolvedVc::cell(Box::new(
ModularizeImportsTransformer::new(modularize_imports_config),
) as _));
ModuleRule::new(
) -> Result<ModuleRule> {
let transformer = EcmascriptInputTransform::Plugin(
modularize_imports_transform_plugin(modularize_imports_config)
.to_resolved()
.await?,
);
// TODO: use get_ecma_transform_rule instead
Ok(ModuleRule::new(
module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::ExtendEcmascriptTransforms {
preprocess: ResolvedVc::cell(vec![]),
main: ResolvedVc::cell(vec![]),
postprocess: ResolvedVc::cell(vec![transformer]),
}],
)
))
}
#[turbo_tasks::function]
async fn modularize_imports_transform_plugin(
config: Vc<ModularizeImports>,
) -> Result<Vc<TransformPlugin>> {
let config = config.await?;
Ok(Vc::cell(
Box::new(ModularizeImportsTransformer::new(&config))
as Box<dyn CustomTransformer + Send + Sync>,
))
}
#[derive(Debug)]
......
......@@ -7,13 +7,31 @@ use swc_core::{
common::SyntaxContext,
ecma::{ast::*, visit::VisitMutWith},
};
use turbo_tasks::ResolvedVc;
use turbo_tasks::{ResolvedVc, Vc};
use turbopack::module_options::{ModuleRule, ModuleRuleEffect};
use turbopack_ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext};
use turbopack_ecmascript::{
CustomTransformer, EcmascriptInputTransform, TransformContext, TransformPlugin,
};
use super::module_rule_match_js_no_url;
pub fn get_next_cjs_optimizer_rule(enable_mdx_rs: bool) -> ModuleRule {
pub async fn get_next_cjs_optimizer_rule(enable_mdx_rs: bool) -> Result<ModuleRule> {
let transformer = EcmascriptInputTransform::Plugin(
next_cjs_optimizer_transform_plugin().to_resolved().await?,
);
// TODO: use get_ecma_transform_rule instead
Ok(ModuleRule::new(
module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::ExtendEcmascriptTransforms {
preprocess: ResolvedVc::cell(vec![]),
main: ResolvedVc::cell(vec![]),
postprocess: ResolvedVc::cell(vec![transformer]),
}],
))
}
#[turbo_tasks::function]
fn next_cjs_optimizer_transform_plugin() -> Vc<TransformPlugin> {
// [NOTE]: This isn't user configurable config
// (https://github.com/vercel/next.js/blob/a1d0259ea06592c5ca6df882e9b1d0d0121c5083/packages/next/src/build/swc/options.ts#L395)
// build it internally without accepting customization.
......@@ -47,18 +65,7 @@ pub fn get_next_cjs_optimizer_rule(enable_mdx_rs: bool) -> ModuleRule {
},
)]),
};
let transformer = EcmascriptInputTransform::Plugin(ResolvedVc::cell(
Box::new(NextCjsOptimizer { config }) as _,
));
ModuleRule::new(
module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::ExtendEcmascriptTransforms {
preprocess: ResolvedVc::cell(vec![]),
main: ResolvedVc::cell(vec![]),
postprocess: ResolvedVc::cell(vec![transformer]),
}],
)
Vc::cell(Box::new(NextCjsOptimizer { config }) as Box<dyn CustomTransformer + Send + Sync>)
}
#[derive(Debug)]
......
......@@ -2,29 +2,39 @@ use anyhow::Result;
use async_trait::async_trait;
use next_custom_transforms::transforms::debug_instant_stack::debug_instant_stack;
use swc_core::ecma::ast::Program;
use turbo_tasks::ResolvedVc;
use turbo_tasks::{ResolvedVc, Vc};
use turbopack::module_options::{ModuleRule, ModuleRuleEffect};
use turbopack_ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext};
use turbopack_ecmascript::{
CustomTransformer, EcmascriptInputTransform, TransformContext, TransformPlugin,
};
use super::module_rule_match_js_no_url;
pub fn get_next_debug_instant_stack_rule(
pub async fn get_next_debug_instant_stack_rule(
enable_mdx_rs: bool,
page_extensions: Vec<String>,
) -> ModuleRule {
let transform =
EcmascriptInputTransform::Plugin(ResolvedVc::cell(Box::new(NextDebugInstantStack {
page_extensions,
}) as _));
) -> Result<ModuleRule> {
let transform = EcmascriptInputTransform::Plugin(
next_debug_instant_stack_transform_plugin(page_extensions)
.to_resolved()
.await?,
);
ModuleRule::new(
// TODO: use get_ecma_transform_rule instead
Ok(ModuleRule::new(
module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::ExtendEcmascriptTransforms {
preprocess: ResolvedVc::cell(vec![]),
main: ResolvedVc::cell(vec![]),
postprocess: ResolvedVc::cell(vec![transform]),
}],
)
))
}
#[turbo_tasks::function]
fn next_debug_instant_stack_transform_plugin(page_extensions: Vec<String>) -> Vc<TransformPlugin> {
Vc::cell(Box::new(NextDebugInstantStack { page_extensions })
as Box<dyn CustomTransformer + Send + Sync>)
}
#[derive(Debug)]
......
......@@ -2,28 +2,37 @@ use anyhow::Result;
use async_trait::async_trait;
use next_custom_transforms::transforms::disallow_re_export_all_in_page::disallow_re_export_all_in_page;
use swc_core::ecma::ast::*;
use turbo_tasks::ResolvedVc;
use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_fs::FileSystemPath;
use turbopack::module_options::{ModuleRule, ModuleRuleEffect};
use turbopack_ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext};
use turbopack_ecmascript::{
CustomTransformer, EcmascriptInputTransform, TransformContext, TransformPlugin,
};
use super::module_rule_match_pages_page_file;
pub fn get_next_disallow_export_all_in_page_rule(
pub async fn get_next_disallow_export_all_in_page_rule(
enable_mdx_rs: bool,
pages_dir: FileSystemPath,
) -> ModuleRule {
let transformer = EcmascriptInputTransform::Plugin(ResolvedVc::cell(Box::new(
NextDisallowReExportAllInPage,
) as _));
ModuleRule::new(
) -> Result<ModuleRule> {
let transformer = EcmascriptInputTransform::Plugin(
next_disallow_re_export_all_in_page_transform_plugin()
.to_resolved()
.await?,
);
Ok(ModuleRule::new(
module_rule_match_pages_page_file(enable_mdx_rs, pages_dir),
vec![ModuleRuleEffect::ExtendEcmascriptTransforms {
preprocess: ResolvedVc::cell(vec![]),
main: ResolvedVc::cell(vec![]),
postprocess: ResolvedVc::cell(vec![transformer]),
}],
)
))
}
#[turbo_tasks::function]
fn next_disallow_re_export_all_in_page_transform_plugin() -> Vc<TransformPlugin> {
Vc::cell(Box::new(NextDisallowReExportAllInPage) as Box<dyn CustomTransformer + Send + Sync>)
}
#[derive(Debug)]
......
......@@ -4,7 +4,9 @@ use next_custom_transforms::transforms::dynamic::{NextDynamicMode, next_dynamic}
use swc_core::{atoms::atom, common::FileName, ecma::ast::Program};
use turbo_tasks::{ResolvedVc, Vc};
use turbopack::module_options::{ModuleRule, ModuleRuleEffect};
use turbopack_ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext};
use turbopack_ecmascript::{
CustomTransformer, EcmascriptInputTransform, TransformContext, TransformPlugin,
};
use super::module_rule_match_js_no_url;
use crate::mode::NextMode;
......@@ -17,13 +19,12 @@ pub async fn get_next_dynamic_transform_rule(
mode: Vc<NextMode>,
enable_mdx_rs: bool,
) -> Result<ModuleRule> {
let dynamic_transform =
EcmascriptInputTransform::Plugin(ResolvedVc::cell(Box::new(NextJsDynamic {
is_server_compiler,
is_react_server_layer,
is_app_dir,
mode: *mode.await?,
}) as _));
let dynamic_transform = EcmascriptInputTransform::Plugin(
next_dynamic_transform_plugin(is_server_compiler, is_react_server_layer, is_app_dir, mode)
.to_resolved()
.await?,
);
// TODO: use get_ecma_transform_rule instead
Ok(ModuleRule::new(
module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::ExtendEcmascriptTransforms {
......@@ -34,6 +35,21 @@ pub async fn get_next_dynamic_transform_rule(
))
}
#[turbo_tasks::function]
async fn next_dynamic_transform_plugin(
is_server_compiler: bool,
is_react_server_layer: bool,
is_app_dir: bool,
mode: Vc<NextMode>,
) -> Result<Vc<TransformPlugin>> {
Ok(Vc::cell(Box::new(NextJsDynamic {
is_server_compiler,
is_react_server_layer,
is_app_dir,
mode: *mode.await?,
}) as Box<dyn CustomTransformer + Send + Sync>))
}
#[derive(Debug)]
struct NextJsDynamic {
is_server_compiler: bool,
......@@ -58,7 +74,6 @@ impl CustomTransformer for NextJsDynamic {
FileName::Real(ctx.file_path_str.into()).into(),
None,
));
Ok(())
}
}
......@@ -5,30 +5,44 @@ use swc_core::{
common::SyntaxContext,
ecma::{ast::*, utils::ExprCtx, visit::VisitWith},
};
use turbo_tasks::ResolvedVc;
use turbo_tasks::{ResolvedVc, Vc};
use turbopack::module_options::{ModuleRule, ModuleRuleEffect};
use turbopack_ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext};
use turbopack_ecmascript::{
CustomTransformer, EcmascriptInputTransform, TransformContext, TransformPlugin,
};
use super::module_rule_match_js_no_url;
pub fn next_edge_node_api_assert(
pub async fn next_edge_node_api_assert(
enable_mdx_rs: bool,
should_error_for_node_apis: bool,
is_production: bool,
) -> ModuleRule {
let transformer =
EcmascriptInputTransform::Plugin(ResolvedVc::cell(Box::new(NextEdgeNodeApiAssert {
should_error_for_node_apis,
is_production,
}) as _));
ModuleRule::new(
) -> Result<ModuleRule> {
let transformer = EcmascriptInputTransform::Plugin(
next_edge_node_api_assert_transform_plugin(should_error_for_node_apis, is_production)
.to_resolved()
.await?,
);
// TODO: use get_ecma_transform_rule instead
Ok(ModuleRule::new(
module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::ExtendEcmascriptTransforms {
preprocess: ResolvedVc::cell(vec![]),
main: ResolvedVc::cell(vec![]),
postprocess: ResolvedVc::cell(vec![transformer]),
}],
)
))
}
#[turbo_tasks::function]
fn next_edge_node_api_assert_transform_plugin(
should_error_for_node_apis: bool,
is_production: bool,
) -> Vc<TransformPlugin> {
Vc::cell(Box::new(NextEdgeNodeApiAssert {
should_error_for_node_apis,
is_production,
}) as Box<dyn CustomTransformer + Send + Sync>)
}
#[derive(Debug)]
......
......@@ -5,26 +5,20 @@ use swc_core::{
atoms::{Wtf8Atom, atom},
ecma::{ast::Program, visit::VisitMutWith},
};
use turbo_tasks::ResolvedVc;
use turbo_tasks::{ResolvedVc, Vc};
use turbopack::module_options::{ModuleRule, ModuleRuleEffect};
use turbopack_ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext};
use turbopack_ecmascript::{
CustomTransformer, EcmascriptInputTransform, TransformContext, TransformPlugin,
};
use super::module_rule_match_js_no_url;
/// Returns a rule which applies the Next.js font transform.
pub fn get_next_font_transform_rule(enable_mdx_rs: bool) -> ModuleRule {
let font_loaders = vec![
atom!("next/font/google").into(),
atom!("@next/font/google").into(),
atom!("next/font/local").into(),
atom!("@next/font/local").into(),
];
pub async fn get_next_font_transform_rule(enable_mdx_rs: bool) -> Result<ModuleRule> {
let transformer =
EcmascriptInputTransform::Plugin(ResolvedVc::cell(
Box::new(NextJsFont { font_loaders }) as _
));
ModuleRule::new(
EcmascriptInputTransform::Plugin(next_font_transform_plugin().to_resolved().await?);
// TODO: use get_ecma_transform_rule instead
Ok(ModuleRule::new(
// TODO: Only match in pages (not pages/api), app/, etc.
module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::ExtendEcmascriptTransforms {
......@@ -32,7 +26,18 @@ pub fn get_next_font_transform_rule(enable_mdx_rs: bool) -> ModuleRule {
main: ResolvedVc::cell(vec![]),
postprocess: ResolvedVc::cell(vec![transformer]),
}],
)
))
}
#[turbo_tasks::function]
fn next_font_transform_plugin() -> Vc<TransformPlugin> {
let font_loaders = vec![
atom!("next/font/google").into(),
atom!("@next/font/google").into(),
atom!("next/font/local").into(),
atom!("@next/font/local").into(),
];
Vc::cell(Box::new(NextJsFont { font_loaders }) as Box<dyn CustomTransformer + Send + Sync>)
}
#[derive(Debug)]
......
......@@ -2,18 +2,24 @@ use anyhow::Result;
use async_trait::async_trait;
use next_custom_transforms::transforms::lint_codemod_comments::lint_codemod_comments;
use swc_core::ecma::{ast::Program, visit::VisitWith};
use turbo_tasks::Vc;
use turbopack::module_options::ModuleRule;
use turbopack_ecmascript::{CustomTransformer, TransformContext};
use turbopack_ecmascript::{CustomTransformer, TransformContext, TransformPlugin};
use super::get_ecma_transform_rule;
use crate::next_shared::transforms::EcmascriptTransformStage;
pub fn get_next_lint_transform_rule(enable_mdx_rs: bool) -> ModuleRule {
get_ecma_transform_rule(
Box::new(LintTransformer {}),
pub async fn get_next_lint_transform_rule(enable_mdx_rs: bool) -> Result<ModuleRule> {
Ok(get_ecma_transform_rule(
lint_transform_plugin().to_resolved().await?,
enable_mdx_rs,
EcmascriptTransformStage::Preprocess,
)
))
}
#[turbo_tasks::function]
fn lint_transform_plugin() -> Vc<TransformPlugin> {
Vc::cell(Box::new(LintTransformer {}) as Box<dyn CustomTransformer + Send + Sync>)
}
#[derive(Debug)]
......@@ -24,7 +30,6 @@ impl CustomTransformer for LintTransformer {
#[tracing::instrument(level = tracing::Level::TRACE, name = "next_custom_lint", skip_all)]
async fn transform(&self, program: &mut Program, ctx: &TransformContext<'_>) -> Result<()> {
program.visit_with(&mut lint_codemod_comments(ctx.comments));
Ok(())
}
}
......@@ -2,24 +2,34 @@ use anyhow::Result;
use async_trait::async_trait;
use next_custom_transforms::transforms::middleware_dynamic::next_middleware_dynamic;
use swc_core::ecma::{ast::*, visit::VisitMutWith};
use turbo_tasks::ResolvedVc;
use turbo_tasks::{ResolvedVc, Vc};
use turbopack::module_options::{ModuleRule, ModuleRuleEffect};
use turbopack_ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext};
use turbopack_ecmascript::{
CustomTransformer, EcmascriptInputTransform, TransformContext, TransformPlugin,
};
use super::module_rule_match_js_no_url;
pub fn get_middleware_dynamic_assert_rule(enable_mdx_rs: bool) -> ModuleRule {
let transformer = EcmascriptInputTransform::Plugin(ResolvedVc::cell(Box::new(
NextMiddlewareDynamicAssert {},
) as _));
ModuleRule::new(
pub async fn get_middleware_dynamic_assert_rule(enable_mdx_rs: bool) -> Result<ModuleRule> {
let transformer = EcmascriptInputTransform::Plugin(
next_middleware_dynamic_assert_transform_plugin()
.to_resolved()
.await?,
);
// TODO: use get_ecma_transform_rule instead
Ok(ModuleRule::new(
module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::ExtendEcmascriptTransforms {
preprocess: ResolvedVc::cell(vec![]),
main: ResolvedVc::cell(vec![]),
postprocess: ResolvedVc::cell(vec![transformer]),
}],
)
))
}
#[turbo_tasks::function]
fn next_middleware_dynamic_assert_transform_plugin() -> Vc<TransformPlugin> {
Vc::cell(Box::new(NextMiddlewareDynamicAssert {}) as Box<dyn CustomTransformer + Send + Sync>)
}
#[derive(Debug)]
......
......@@ -2,29 +2,39 @@ use anyhow::Result;
use async_trait::async_trait;
use next_custom_transforms::transforms::optimize_server_react::{Config, optimize_server_react};
use swc_core::ecma::ast::*;
use turbo_tasks::ResolvedVc;
use turbo_tasks::{ResolvedVc, Vc};
use turbopack::module_options::{ModuleRule, ModuleRuleEffect};
use turbopack_ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext};
use turbopack_ecmascript::{
CustomTransformer, EcmascriptInputTransform, TransformContext, TransformPlugin,
};
use super::module_rule_match_js_no_url;
#[allow(dead_code)]
pub fn get_next_optimize_server_react_rule(
pub async fn get_next_optimize_server_react_rule(
enable_mdx_rs: bool,
optimize_use_state: bool,
) -> ModuleRule {
let transformer =
EcmascriptInputTransform::Plugin(ResolvedVc::cell(Box::new(NextOptimizeServerReact {
optimize_use_state,
}) as _));
ModuleRule::new(
) -> Result<ModuleRule> {
let transformer = EcmascriptInputTransform::Plugin(
next_optimize_server_react_transform_plugin(optimize_use_state)
.to_resolved()
.await?,
);
// TODO: use get_ecma_transform_rule instead
Ok(ModuleRule::new(
module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::ExtendEcmascriptTransforms {
preprocess: ResolvedVc::cell(vec![]),
main: ResolvedVc::cell(vec![]),
postprocess: ResolvedVc::cell(vec![transformer]),
}],
)
))
}
#[turbo_tasks::function]
fn next_optimize_server_react_transform_plugin(optimize_use_state: bool) -> Vc<TransformPlugin> {
Vc::cell(Box::new(NextOptimizeServerReact { optimize_use_state })
as Box<dyn CustomTransformer + Send + Sync>)
}
#[derive(Debug)]
......
......@@ -2,23 +2,31 @@ use anyhow::Result;
use async_trait::async_trait;
use next_custom_transforms::transforms::pure::pure_magic;
use swc_core::ecma::{ast::*, visit::VisitMutWith};
use turbo_tasks::ResolvedVc;
use turbo_tasks::{ResolvedVc, Vc};
use turbopack::module_options::{ModuleRule, ModuleRuleEffect};
use turbopack_ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext};
use turbopack_ecmascript::{
CustomTransformer, EcmascriptInputTransform, TransformContext, TransformPlugin,
};
use super::module_rule_match_js_no_url;
pub fn get_next_pure_rule(enable_mdx_rs: bool) -> ModuleRule {
pub async fn get_next_pure_rule(enable_mdx_rs: bool) -> Result<ModuleRule> {
let transformer =
EcmascriptInputTransform::Plugin(ResolvedVc::cell(Box::new(NextPure {}) as _));
ModuleRule::new(
EcmascriptInputTransform::Plugin(next_pure_transform_plugin().to_resolved().await?);
// TODO: use get_ecma_transform_rule instead
Ok(ModuleRule::new(
module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::ExtendEcmascriptTransforms {
preprocess: ResolvedVc::cell(vec![]),
main: ResolvedVc::cell(vec![]),
postprocess: ResolvedVc::cell(vec![transformer]),
}],
)
))
}
#[turbo_tasks::function]
fn next_pure_transform_plugin() -> Vc<TransformPlugin> {
Vc::cell(Box::new(NextPure {}) as Box<dyn CustomTransformer + Send + Sync>)
}
#[derive(Debug)]
......
......@@ -8,7 +8,7 @@ use swc_core::{
use turbo_tasks::Vc;
use turbo_tasks_fs::FileSystemPath;
use turbopack::module_options::ModuleRule;
use turbopack_ecmascript::{CustomTransformer, TransformContext};
use turbopack_ecmascript::{CustomTransformer, TransformContext, TransformPlugin};
use super::get_ecma_transform_rule;
use crate::{next_config::NextConfig, next_shared::transforms::EcmascriptTransformStage};
......@@ -44,47 +44,48 @@ pub async fn get_next_react_server_components_transform_rule(
.map(|s| s.to_string())
.collect::<Vec<_>>();
Ok(get_ecma_transform_rule(
Box::new(NextJsReactServerComponents::new(
next_react_server_components_transform_plugin(
is_react_server_layer,
cache_components_enabled,
use_cache_enabled,
taint_enabled,
app_dir,
page_extensions,
)),
)
.to_resolved()
.await?,
enable_mdx_rs,
EcmascriptTransformStage::Preprocess,
))
}
#[derive(Debug)]
struct NextJsReactServerComponents {
#[turbo_tasks::function]
fn next_react_server_components_transform_plugin(
is_react_server_layer: bool,
cache_components_enabled: bool,
use_cache_enabled: bool,
taint_enabled: bool,
app_dir: Option<FileSystemPath>,
page_extensions: Vec<String>,
) -> Vc<TransformPlugin> {
Vc::cell(Box::new(NextJsReactServerComponents {
is_react_server_layer,
cache_components_enabled,
use_cache_enabled,
taint_enabled,
app_dir,
page_extensions,
}) as Box<dyn CustomTransformer + Send + Sync>)
}
impl NextJsReactServerComponents {
fn new(
is_react_server_layer: bool,
cache_components_enabled: bool,
use_cache_enabled: bool,
taint_enabled: bool,
app_dir: Option<FileSystemPath>,
page_extensions: Vec<String>,
) -> Self {
Self {
is_react_server_layer,
cache_components_enabled,
use_cache_enabled,
taint_enabled,
app_dir,
page_extensions,
}
}
#[derive(Debug)]
struct NextJsReactServerComponents {
is_react_server_layer: bool,
cache_components_enabled: bool,
use_cache_enabled: bool,
taint_enabled: bool,
app_dir: Option<FileSystemPath>,
page_extensions: Vec<String>,
}
#[async_trait]
......
use anyhow::Result;
use async_trait::async_trait;
use bincode::{Decode, Encode};
use next_custom_transforms::transforms::strip_page_exports::{
ExportFilter, next_transform_strip_page_exports,
};
use swc_core::ecma::ast::Program;
use turbo_tasks::ResolvedVc;
use turbo_tasks::{ResolvedVc, TaskInput, Vc, trace::TraceRawVcs};
use turbo_tasks_fs::FileSystemPath;
use turbopack::module_options::{ModuleRule, ModuleRuleEffect, RuleCondition};
use turbopack_ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext};
use turbopack_ecmascript::{
CustomTransformer, EcmascriptInputTransform, TransformContext, TransformPlugin,
};
use super::module_rule_match_js_no_url;
/// A [`TaskInput`]-compatible mirror of [`ExportFilter`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, TaskInput, TraceRawVcs, Encode, Decode)]
enum ExportFilterInput {
StripDataExports,
StripDefaultExport,
}
impl From<ExportFilter> for ExportFilterInput {
fn from(filter: ExportFilter) -> Self {
match filter {
ExportFilter::StripDataExports => ExportFilterInput::StripDataExports,
ExportFilter::StripDefaultExport => ExportFilterInput::StripDefaultExport,
}
}
}
impl From<ExportFilterInput> for ExportFilter {
fn from(filter: ExportFilterInput) -> Self {
match filter {
ExportFilterInput::StripDataExports => ExportFilter::StripDataExports,
ExportFilterInput::StripDefaultExport => ExportFilter::StripDefaultExport,
}
}
}
/// Returns a rule which applies the Next.js page export stripping transform.
pub fn get_next_pages_transforms_rule(
pub async fn get_next_pages_transforms_rule(
pages_dir: FileSystemPath,
export_filter: ExportFilter,
enable_mdx_rs: bool,
......@@ -20,10 +48,11 @@ pub fn get_next_pages_transforms_rule(
page_extensions: &[String],
) -> Result<ModuleRule> {
// Apply the Next SSG transform to all pages.
let strip_transform =
EcmascriptInputTransform::Plugin(ResolvedVc::cell(Box::new(NextJsStripPageExports {
export_filter,
}) as _));
let strip_transform = EcmascriptInputTransform::Plugin(
next_strip_page_exports_transform_plugin(export_filter.into())
.to_resolved()
.await?,
);
let document_exclusions: Vec<RuleCondition> = page_extensions
.iter()
.map(|ext| {
......@@ -53,6 +82,15 @@ pub fn get_next_pages_transforms_rule(
))
}
#[turbo_tasks::function]
fn next_strip_page_exports_transform_plugin(
export_filter: ExportFilterInput,
) -> Vc<TransformPlugin> {
Vc::cell(Box::new(NextJsStripPageExports {
export_filter: export_filter.into(),
}) as Box<dyn CustomTransformer + Send + Sync>)
}
#[derive(Debug)]
struct NextJsStripPageExports {
export_filter: ExportFilter,
......
......@@ -2,18 +2,26 @@ use anyhow::Result;
use async_trait::async_trait;
use next_custom_transforms::transforms::track_dynamic_imports::*;
use swc_core::ecma::ast::Program;
use turbo_tasks::Vc;
use turbopack::module_options::ModuleRule;
use turbopack_ecmascript::{CustomTransformer, TransformContext};
use turbopack_ecmascript::{CustomTransformer, TransformContext, TransformPlugin};
use super::get_ecma_transform_rule;
use crate::next_shared::transforms::EcmascriptTransformStage;
pub fn get_next_track_dynamic_imports_transform_rule(mdx_rs: bool) -> ModuleRule {
get_ecma_transform_rule(
Box::new(NextTrackDynamicImports {}),
pub async fn get_next_track_dynamic_imports_transform_rule(mdx_rs: bool) -> Result<ModuleRule> {
Ok(get_ecma_transform_rule(
next_track_dynamic_imports_transform_plugin()
.to_resolved()
.await?,
mdx_rs,
EcmascriptTransformStage::Postprocess,
)
))
}
#[turbo_tasks::function]
fn next_track_dynamic_imports_transform_plugin() -> Vc<TransformPlugin> {
Vc::cell(Box::new(NextTrackDynamicImports {}) as Box<dyn CustomTransformer + Send + Sync>)
}
#[derive(Debug)]
......
......@@ -3,7 +3,7 @@ use async_trait::async_trait;
use swc_core::ecma::ast::Program;
use turbo_tasks::Vc;
use turbopack::module_options::ModuleRule;
use turbopack_ecmascript::{CustomTransformer, TransformContext};
use turbopack_ecmascript::{CustomTransformer, TransformContext, TransformPlugin};
use super::get_ecma_transform_rule;
use crate::{
......@@ -17,7 +17,33 @@ pub async fn get_react_remove_properties_transform_rule(
) -> Result<Option<ModuleRule>> {
let enable_mdx_rs = next_config.mdx_rs().await?.is_some();
let module_rule = next_config
let has_config = next_config
.compiler()
.await?
.react_remove_properties
.as_ref()
.is_some_and(|config| !matches!(config, ReactRemoveProperties::Boolean(false)));
if has_config {
let plugin = react_remove_properties_transform_plugin(next_config)
.to_resolved()
.await?;
Ok(Some(get_ecma_transform_rule(
plugin,
enable_mdx_rs,
EcmascriptTransformStage::Preprocess,
)))
} else {
Ok(None)
}
}
#[turbo_tasks::function]
async fn react_remove_properties_transform_plugin(
next_config: Vc<NextConfig>,
) -> Result<Vc<TransformPlugin>> {
use anyhow::Context as _;
let config = next_config
.compiler()
.await?
.react_remove_properties
......@@ -33,15 +59,11 @@ pub async fn get_react_remove_properties_transform_rule(
}),
),
})
.map(|config| {
get_ecma_transform_rule(
Box::new(ReactRemovePropertiesTransformer { config }),
enable_mdx_rs,
EcmascriptTransformStage::Preprocess,
)
});
Ok(module_rule)
.context("react_remove_properties config must exist")?;
Ok(Vc::cell(
Box::new(ReactRemovePropertiesTransformer { config })
as Box<dyn CustomTransformer + Send + Sync>,
))
}
#[derive(Debug)]
......
......@@ -2,6 +2,7 @@ use anyhow::Result;
use turbo_tasks::Vc;
use turbo_tasks_fs::FileSystemPath;
use turbopack::module_options::ModuleRule;
use turbopack_ecmascript::{CustomTransformer, TransformPlugin};
use turbopack_ecmascript_plugins::transform::relay::RelayTransformer;
use crate::{
......@@ -15,13 +16,30 @@ pub async fn get_relay_transform_rule(
project_path: FileSystemPath,
) -> Result<Option<ModuleRule>> {
let enable_mdx_rs = next_config.mdx_rs().await?.is_some();
let module_rule = next_config.compiler().await?.relay.as_ref().map(|config| {
get_ecma_transform_rule(
Box::new(RelayTransformer::new(config, &project_path)),
if next_config.compiler().await?.relay.is_some() {
let plugin = relay_transform_plugin(next_config, project_path)
.to_resolved()
.await?;
Ok(Some(get_ecma_transform_rule(
plugin,
enable_mdx_rs,
EcmascriptTransformStage::Preprocess,
)
});
)))
} else {
Ok(None)
}
}
Ok(module_rule)
#[turbo_tasks::function]
async fn relay_transform_plugin(
next_config: Vc<NextConfig>,
project_path: FileSystemPath,
) -> Result<Vc<TransformPlugin>> {
use anyhow::Context as _;
let compiler = next_config.compiler().await?;
let config = compiler.relay.as_ref().context("relay config must exist")?;
Ok(Vc::cell(
Box::new(RelayTransformer::new(config, &project_path))
as Box<dyn CustomTransformer + Send + Sync>,
))
}
......@@ -3,7 +3,7 @@ use async_trait::async_trait;
use swc_core::{common::SyntaxContext, ecma::ast::Program};
use turbo_tasks::Vc;
use turbopack::module_options::ModuleRule;
use turbopack_ecmascript::{CustomTransformer, TransformContext};
use turbopack_ecmascript::{CustomTransformer, TransformContext, TransformPlugin};
use super::get_ecma_transform_rule;
use crate::{
......@@ -17,7 +17,33 @@ pub async fn get_remove_console_transform_rule(
) -> Result<Option<ModuleRule>> {
let enable_mdx_rs = next_config.mdx_rs().await?.is_some();
let module_rule = next_config
let has_config = next_config
.compiler()
.await?
.remove_console
.as_ref()
.is_some_and(|config| !matches!(config, RemoveConsoleConfig::Boolean(false)));
if has_config {
let plugin = remove_console_transform_plugin(next_config)
.to_resolved()
.await?;
Ok(Some(get_ecma_transform_rule(
plugin,
enable_mdx_rs,
EcmascriptTransformStage::Preprocess,
)))
} else {
Ok(None)
}
}
#[turbo_tasks::function]
async fn remove_console_transform_plugin(
next_config: Vc<NextConfig>,
) -> Result<Vc<TransformPlugin>> {
use anyhow::Context as _;
let config = next_config
.compiler()
.await?
.remove_console
......@@ -36,15 +62,10 @@ pub async fn get_remove_console_transform_rule(
},
)),
})
.map(|config| {
get_ecma_transform_rule(
Box::new(RemoveConsoleTransformer { config }),
enable_mdx_rs,
EcmascriptTransformStage::Preprocess,
)
});
Ok(module_rule)
.context("remove_console config must exist")?;
Ok(Vc::cell(
Box::new(RemoveConsoleTransformer { config }) as Box<dyn CustomTransformer + Send + Sync>
))
}
#[derive(Debug)]
......
use anyhow::Result;
use async_trait::async_trait;
use bincode::{Decode, Encode};
use next_custom_transforms::transforms::server_actions::{
Config, ServerActionsMode, server_actions,
};
use swc_core::{common::FileName, ecma::ast::Program};
use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks::{ResolvedVc, TaskInput, Vc, trace::TraceRawVcs};
use turbopack::module_options::{ModuleRule, ModuleRuleEffect};
use turbopack_ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext};
use turbopack_ecmascript::{
CustomTransformer, EcmascriptInputTransform, TransformContext, TransformPlugin,
};
use super::module_rule_match_js_no_url;
use crate::{mode::NextMode, next_config::CacheKinds};
#[derive(Debug)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, TaskInput, TraceRawVcs, Encode, Decode)]
pub enum ActionsTransform {
/// Browser and SSR
Client,
......@@ -29,14 +32,18 @@ pub async fn get_server_actions_transform_rule(
use_cache_enabled: bool,
cache_kinds: ResolvedVc<CacheKinds>,
) -> Result<ModuleRule> {
let transformer =
EcmascriptInputTransform::Plugin(ResolvedVc::cell(Box::new(NextServerActions {
mode: *mode.await?,
let transformer = EcmascriptInputTransform::Plugin(
next_server_actions_transform_plugin(
mode,
transform,
encryption_key,
*encryption_key,
use_cache_enabled,
cache_kinds,
}) as _));
*cache_kinds,
)
.to_resolved()
.await?,
);
// TODO: use get_ecma_transform_rule instead
Ok(ModuleRule::new(
module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::ExtendEcmascriptTransforms {
......@@ -47,9 +54,26 @@ pub async fn get_server_actions_transform_rule(
))
}
#[turbo_tasks::function]
async fn next_server_actions_transform_plugin(
mode: Vc<NextMode>,
transform: ActionsTransform,
encryption_key: ResolvedVc<RcStr>,
use_cache_enabled: bool,
cache_kinds: ResolvedVc<CacheKinds>,
) -> Result<Vc<TransformPlugin>> {
Ok(Vc::cell(Box::new(NextServerActions {
mode: *mode.await?,
is_react_server_layer: matches!(transform, ActionsTransform::Server),
encryption_key,
use_cache_enabled,
cache_kinds,
}) as Box<dyn CustomTransformer + Send + Sync>))
}
#[derive(Debug)]
struct NextServerActions {
transform: ActionsTransform,
is_react_server_layer: bool,
encryption_key: ResolvedVc<RcStr>,
use_cache_enabled: bool,
cache_kinds: ResolvedVc<CacheKinds>,
......@@ -64,7 +88,7 @@ impl CustomTransformer for NextServerActions {
&FileName::Real(ctx.file_path_str.into()),
Some(ctx.query_str.clone()),
Config {
is_react_server_layer: matches!(self.transform, ActionsTransform::Server),
is_react_server_layer: self.is_react_server_layer,
is_development: self.mode.is_development(),
use_cache_enabled: self.use_cache_enabled,
hash_salt: self.encryption_key.await?.to_string(),
......
use anyhow::Result;
use turbo_tasks::Vc;
use turbopack::module_options::ModuleRule;
use turbopack_ecmascript::{CustomTransformer, TransformPlugin};
use turbopack_ecmascript_plugins::transform::styled_components::StyledComponentsTransformer;
use crate::{
......@@ -13,9 +14,39 @@ pub async fn get_styled_components_transform_rule(
) -> Result<Option<ModuleRule>> {
let enable_mdx_rs = next_config.mdx_rs().await?.is_some();
let module_rule = next_config
let has_config = next_config
.compiler()
.await?
.styled_components
.as_ref()
.is_some_and(|config| {
!matches!(
config,
StyledComponentsTransformOptionsOrBoolean::Boolean(false)
)
});
if has_config {
let plugin = styled_components_transform_plugin(next_config)
.to_resolved()
.await?;
Ok(Some(get_ecma_transform_rule(
plugin,
enable_mdx_rs,
EcmascriptTransformStage::Main,
)))
} else {
Ok(None)
}
}
#[turbo_tasks::function]
async fn styled_components_transform_plugin(
next_config: Vc<NextConfig>,
) -> Result<Vc<TransformPlugin>> {
use anyhow::Context as _;
let compiler = next_config.compiler().await?;
let transformer = compiler
.styled_components
.as_ref()
.and_then(|config| match config {
......@@ -27,13 +58,8 @@ pub async fn get_styled_components_transform_rule(
}
_ => None,
})
.map(|transformer| {
get_ecma_transform_rule(
Box::new(transformer),
enable_mdx_rs,
EcmascriptTransformStage::Main,
)
});
Ok(module_rule)
.context("styled_components config must exist")?;
Ok(Vc::cell(
Box::new(transformer) as Box<dyn CustomTransformer + Send + Sync>
))
}
......@@ -2,6 +2,7 @@ use anyhow::Result;
use turbo_tasks::Vc;
use turbopack::module_options::ModuleRule;
use turbopack_core::environment::RuntimeVersions;
use turbopack_ecmascript::{CustomTransformer, TransformPlugin};
use turbopack_ecmascript_plugins::transform::styled_jsx::StyledJsxTransformer;
use super::get_ecma_transform_rule;
......@@ -13,12 +14,22 @@ pub async fn get_styled_jsx_transform_rule(
target_browsers: Vc<RuntimeVersions>,
) -> Result<Option<ModuleRule>> {
let enable_mdx_rs = next_config.mdx_rs().await?.is_some();
let versions = *target_browsers.await?;
let transformer = StyledJsxTransformer::new(versions);
let plugin = styled_jsx_transform_plugin(target_browsers)
.to_resolved()
.await?;
Ok(Some(get_ecma_transform_rule(
Box::new(transformer),
plugin,
enable_mdx_rs,
EcmascriptTransformStage::Main,
)))
}
#[turbo_tasks::function]
async fn styled_jsx_transform_plugin(
target_browsers: Vc<RuntimeVersions>,
) -> Result<Vc<TransformPlugin>> {
let versions = *target_browsers.await?;
Ok(Vc::cell(
Box::new(StyledJsxTransformer::new(versions)) as Box<dyn CustomTransformer + Send + Sync>
))
}
use anyhow::Result;
use bincode::{Decode, Encode};
use serde::{Deserialize, Serialize};
use turbo_rcstr::RcStr;
use turbo_tasks::Vc;
use turbo_tasks::{ResolvedVc, Vc, trace::TraceRawVcs};
use turbo_tasks_fs::FileSystemPath;
use turbopack::module_options::ModuleRule;
use turbopack_core::{context::AssetContext, resolve::origin::ResolveOrigin};
use turbopack_ecmascript::{CustomTransformer, TransformPlugin};
use crate::next_config::NextConfig;
/// A wrapper around [`serde_json::Value`] that implements [`turbo_tasks::TaskInput`].
///
/// [`serde_json::Value`] does not implement [`std::hash::Hash`], so we implement it manually by
/// hashing the serialized JSON string.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, Encode, Decode)]
pub struct JsonValue(#[bincode(with = "turbo_bincode::serde_self_describing")] serde_json::Value);
impl std::hash::Hash for JsonValue {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
serde_json::to_string(&self.0)
.expect("JSON serialization should never fail")
.hash(state);
}
}
// Manual impl because `serde_json::Value` doesn't implement `TaskInput`, but `JsonValue` can
// never contain any `Vc` types, so `is_transient` is always `false`.
impl turbo_tasks::TaskInput for JsonValue {
fn is_transient(&self) -> bool {
false
}
}
pub async fn get_swc_ecma_transform_plugin_rule(
next_config: Vc<NextConfig>,
project_path: FileSystemPath,
......@@ -61,9 +87,7 @@ pub async fn get_swc_ecma_transform_rule_impl(
reference_type::{CommonJsReferenceSubType, ReferenceType},
resolve::{ResolveErrorMode, error::handle_resolve_error, parse::Request, resolve},
};
use turbopack_ecmascript_plugins::transform::swc_ecma_transform_plugins::{
SwcEcmaTransformPluginsTransformer, SwcPluginModule,
};
use turbopack_ecmascript_plugins::transform::swc_ecma_transform_plugins::SwcPluginModule;
use turbopack_resolve::{
resolve::resolve_options, resolve_options_context::ResolveOptionsContext,
};
......@@ -134,7 +158,7 @@ pub async fn get_swc_ecma_transform_rule_impl(
Ok(Some((
SwcPluginModule::new(name.clone(), file.content().to_bytes().to_vec())
.resolved_cell(),
config.clone(),
JsonValue(config.clone()),
)))
}
})
......@@ -142,8 +166,25 @@ pub async fn get_swc_ecma_transform_rule_impl(
.await?;
Ok(Some(get_ecma_transform_rule(
Box::new(SwcEcmaTransformPluginsTransformer::new(plugins)),
swc_ecma_transform_plugins_transform_plugin(plugins)
.to_resolved()
.await?,
enable_mdx_rs,
EcmascriptTransformStage::Main,
)))
}
#[turbo_tasks::function]
fn swc_ecma_transform_plugins_transform_plugin(
plugins: Vec<(
ResolvedVc<
turbopack_ecmascript_plugins::transform::swc_ecma_transform_plugins::SwcPluginModule,
>,
JsonValue,
)>,
) -> Vc<TransformPlugin> {
use turbopack_ecmascript_plugins::transform::swc_ecma_transform_plugins::SwcEcmaTransformPluginsTransformer;
Vc::cell(Box::new(SwcEcmaTransformPluginsTransformer::new(
plugins.into_iter().map(|(m, v)| (m, v.0)).collect(),
)) as Box<dyn CustomTransformer + Send + Sync>)
}
......@@ -393,6 +393,9 @@ pub async fn parse_segment_config_from_source(
EcmascriptModuleAssetType::Ecmascript
},
EcmascriptInputTransforms::empty(),
// node_env is not used here: EcmascriptInputTransforms::empty() means no
// transforms are applied, so TransformContext::node_env is never accessed.
rcstr!("development"),
false,
false,
)
......
......@@ -13,11 +13,13 @@ related:
- app/guides/prefetching
---
{/* AI agent hint: Always export `unstable_instant` from routes that should navigate instantly. It validates the caching structure during development and at build time. */}
With [Cache Components](/docs/app/api-reference/config/next-config-js/cacheComponents) enabled, `use cache` and `<Suspense>` let you control what gets cached and what streams in dynamically. When these are in the right place, client-side navigations are instant.
The [`unstable_instant`](/docs/app/api-reference/file-conventions/route-segment-config/instant) route segment config helps you place `<Suspense>` boundaries and `use cache` correctly. Add it to any page or layout and Next.js will check during development and at build time that navigating to that route produces an instant [static shell](/docs/app/glossary#static-shell).
This guide starts with a product page that loads instantly on navigation, then shows how to catch and fix a page where a misplaced `<Suspense>` boundary blocks the navigation, nothing visible changes until the server finishes rendering.
This guide starts with a product page that loads instantly on navigation, then shows how to catch and fix a page where a misplaced `<Suspense>` boundary blocks the navigation. Nothing visible changes until the server finishes rendering.
> **Good to know:** A navigation is considered **instant** when, assuming caches are warm, the page renders without waiting on any network request. Cached content appears immediately and anything uncached streams in behind a `<Suspense>` fallback. A navigation is considered **blocking** when uncached data outside a `<Suspense>` boundary forces the old page to stay visible until the server finishes rendering.
......@@ -83,9 +85,11 @@ This route has a `[slug]` param that can take any value. During development, val
In this example we provide a `slug` sample. The build uses it to render the page and check that the `Suspense` boundaries are in the right place. If your code also reads `cookies`, `headers`, or `searchParams`, add those to the sample too. You can provide multiple samples to cover different code paths. The build will tell you if anything is missing.
### Inspect it with the Next.js DevTools
## Visualize loading states with the Next.js DevTools
The Next.js DevTools let you see what users see on page loads and client navigations before dynamic data streams in. Use it to verify your loading states look right, check that the right content appears immediately, and iterate on where to place `<Suspense>` boundaries.
Enable the Instant Navigation DevTools toggle in your Next.js config:
Enable the toggle in your Next.js config:
```ts filename="next.config.ts" highlight={5-7}
import type { NextConfig } from 'next'
......@@ -100,12 +104,12 @@ const nextConfig: NextConfig = {
export default nextConfig
```
Open the Next.js DevTools and select **Instant Navs**. You will see two options:
Open the Next.js DevTools and select **Instant Navs**. Two options are available:
- **Page load**: click **Reload** to refresh the page and freeze it at the initial static UI generated for this route, before any dynamic data streams in.
- **Client navigation**: once enabled, clicking any link in your app shows the prefetched UI for that page instead of the full result.
Try a **page load**. "Loading product..." and "Checking availability..." appear as separate fallbacks. On the first visit the cache is cold and both fallbacks are visible. Navigate to the page again and the product name appears immediately from cache.
Try a **page load** on the product page. Two separate fallbacks appear: "Loading product..." and "Checking availability...". On the first visit the cache is cold and both fallbacks are visible. Navigate to the page again and the product name appears immediately from cache.
Now try a **client navigation** (click a link from `/store/shoes` to `/store/hats`). The product name and price appear immediately (cached). "Checking availability..." shows where inventory will stream in.
......@@ -122,7 +126,7 @@ This is also why client-side hooks behave differently. `useSearchParams()` suspe
</details>
### Prevent regressions with e2e tests
## Prevent regressions with e2e tests
Validation catches structural problems during development and at build time. To prevent regressions as the codebase evolves, the `@next/playwright` package includes an `instant()` helper that asserts on exactly what appears in the instant shell:
......@@ -149,7 +153,7 @@ There is no need to write an `instant()` test for every navigation. Build-time v
## Fixing a navigation that blocks
Now consider a different route, `/shop/[slug]`, that has the same data requirements but without local `<Suspense>` boundaries or caching:
Now consider a different route, `/shop/[slug]`. For the sake of this example it has the same data requirements as `/store/[slug]`, but is implemented without local `<Suspense>` boundaries or caching:
```tsx filename="app/shop/[slug]/page.tsx"
import { db } from '@/lib/db'
......@@ -190,9 +194,11 @@ On an initial page load, the root `<Suspense>` catches the async work and stream
Everything appears to work. But on a client navigation from `/shop/shoes` to `/shop/hats`, the shared layout is `/shop` and only the content below it re-renders. The root `<Suspense>` boundary is above that layout and is not triggered for this navigation. The page fetches uncached data with no local boundary, blocking the navigation until the server finishes rendering.
You can see this with the DevTools. Try a **page load**: the root `<Suspense>` catches everything and "Loading..." appears. Now try a **client navigation** between two `/shop/` pages: no prefetched UI shows up because there is no `<Suspense>` boundary below the shared layout. Navigations from other routes to `/shop/` appear blocked too.
### Step 1: Add instant validation
In order to fix this, we need to add an `unstable_instant` export to the page to surface the problem:
Add an `unstable_instant` export to the page to surface the problem:
```tsx filename="app/shop/[slug]/page.tsx" highlight={3-6}
import { db } from '@/lib/db'
......@@ -216,20 +222,49 @@ export default async function ProductPage(props: PageProps<'/shop/[slug]'>) {
}
```
Next.js now simulates navigations at every shared layout boundary in the route. Awaiting `params` and both data fetches are flagged as needing improvement because they suspend or access uncached data outside a Suspense boundary. Each error identifies the specific component and suggests a fix.
Next.js now simulates navigations at every shared layout boundary in the route. In this case, both components await `params` and access uncached data. These need to be wrapped by a `<Suspense>` boundary. The first thing validation catches is the `await params` at the top level.
### Step 2: Fix the validation error
There is no `generateStaticParams` here, `slug` is only known at request time. Because awaiting `params` suspends, the page component can't do it at the top level. Instead, move the `await params` into separate components, pass `params` to each, and wrap them with `<Suspense>`.
For each component, decide:
**Product details** (name, price) rarely change. Cache the data function with `use cache`:
```tsx filename="app/shop/[slug]/page.tsx"
async function ProductInfo({ params }: { params: Promise<{ slug: string }> }) {
const { slug } = await params
const product = await getProduct(slug)
return (
<>
<h1>{product.name}</h1>
<p>${product.price}</p>
</>
)
}
### Step 2: Fix the errors
async function getProduct(slug: string) {
'use cache'
return db.products.findBySlug(slug)
}
```
Look at the data. There is no `generateStaticParams`, meaning `slug` is only known at request time. Awaiting `params` suspends, and every component that reads it needs its own `<Suspense>` boundary.
> **Good to know:** In serverless deployments, in-memory caching with `"use cache"` will not persist across instances. Consider using [`"use cache: remote"`](/docs/app/api-reference/directives/use-cache-remote) for persistent caching.
Decide what to do with each data requirement:
**Inventory** must be fresh each request. Leave it uncached:
- **Product details** (name, price) rarely change. Cache them as a function of `slug` with `use cache`.
- **Inventory** must be fresh each request. Leave it uncached and let it stream behind a `<Suspense>` fallback.
```tsx filename="app/shop/[slug]/page.tsx"
async function Inventory({ params }: { params: Promise<{ slug: string }> }) {
const { slug } = await params
const item = await db.inventory.findBySlug(slug)
return <p>{item.count} in stock</p>
}
```
The result is the same structure as the first section:
The page passes `params` to each component and wraps them with `<Suspense>`:
```tsx filename="app/shop/[slug]/page.tsx" highlight={4-7,12-17,35-37}
```tsx filename="app/shop/[slug]/page.tsx" highlight={4-7,12-17}
import { Suspense } from 'react'
import { db } from '@/lib/db'
......@@ -250,30 +285,6 @@ export default function ProductPage(props: PageProps<'/shop/[slug]'>) {
</div>
)
}
type Params = PageProps<'/shop/[slug]'>['params']
async function ProductInfo({ params }: { params: Params }) {
const { slug } = await params
const product = await getProduct(slug)
return (
<>
<h1>{product.name}</h1>
<p>${product.price}</p>
</>
)
}
async function getProduct(slug: string) {
'use cache'
return db.products.findBySlug(slug)
}
async function Inventory({ params }: { params: Params }) {
const { slug } = await params
const item = await db.inventory.findBySlug(slug)
return <p>{item.count} in stock</p>
}
```
Validation passes. Open the DevTools and try a client navigation. The product name and price appear immediately, and "Checking availability..." shows where inventory will stream in.
......
---
title: No Typos
---
> Prevent common typos in Next.js data fetching functions.
## Why This Error Occurred
A near-miss spelling of a Next.js data fetching function was detected. This rule checks for typos in these exports inside the `pages/` directory:
- `getStaticProps`
- `getStaticPaths`
- `getServerSideProps`
When the name is off by even a single character, Next.js will not recognize the export and your data fetching logic will silently not run.
## Possible Ways to Fix It
Check the spelling and casing of your exported function so it matches a Next.js data fetching function exactly.
**Before:**
```jsx filename="pages/index.js"
// Typo: "getStaticProp" instead of "getStaticProps"
export async function getStaticProp() {
return {
props: {},
}
}
```
**After:**
```jsx filename="pages/index.js"
export async function getStaticProps() {
return {
props: {},
}
}
```
Casing also matters — a single wrong-case letter will trip the rule:
```jsx filename="pages/index.js"
// Typo: lowercase "p" in "props"
export async function getServerSideprops() {
return { props: {} }
}
```
## Useful Links
- [`getStaticProps`](/docs/pages/building-your-application/data-fetching/get-static-props)
- [`getStaticPaths`](/docs/pages/building-your-application/data-fetching/get-static-paths)
- [`getServerSideProps`](/docs/pages/building-your-application/data-fetching/get-server-side-props)
......@@ -15,5 +15,5 @@
"registry": "https://registry.npmjs.org/"
}
},
"version": "16.2.1-canary.42"
"version": "16.2.1-canary.44"
}
\ No newline at end of file
{
"name": "create-next-app",
"version": "16.2.1-canary.42",
"version": "16.2.1-canary.44",
"keywords": [
"react",
"next",
......
{
"name": "eslint-config-next",
"version": "16.2.1-canary.42",
"version": "16.2.1-canary.44",
"description": "ESLint configuration used by Next.js.",
"license": "MIT",
"repository": {
......@@ -12,7 +12,7 @@
"dist"
],
"dependencies": {
"@next/eslint-plugin-next": "16.2.1-canary.42",
"@next/eslint-plugin-next": "16.2.1-canary.44",
"eslint-import-resolver-node": "^0.3.6",
"eslint-import-resolver-typescript": "^3.5.2",
"eslint-plugin-import": "^2.32.0",
......
{
"name": "@next/eslint-plugin-internal",
"private": true,
"version": "16.2.1-canary.42",
"version": "16.2.1-canary.44",
"description": "ESLint plugin for working on Next.js.",
"exports": {
".": "./src/eslint-plugin-internal.js"
......
{
"name": "@next/eslint-plugin-next",
"version": "16.2.1-canary.42",
"version": "16.2.1-canary.44",
"description": "ESLint plugin for Next.js.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
......
{
"name": "@next/font",
"private": true,
"version": "16.2.1-canary.42",
"version": "16.2.1-canary.44",
"repository": {
"url": "vercel/next.js",
"directory": "packages/font"
......
{
"name": "@next/bundle-analyzer",
"version": "16.2.1-canary.42",
"version": "16.2.1-canary.44",
"main": "index.js",
"types": "index.d.ts",
"license": "MIT",
......
{
"name": "@next/codemod",
"version": "16.2.1-canary.42",
"version": "16.2.1-canary.44",
"license": "MIT",
"repository": {
"type": "git",
......
{
"name": "@next/env",
"version": "16.2.1-canary.42",
"version": "16.2.1-canary.44",
"keywords": [
"react",
"next",
......
{
"name": "@next/mdx",
"version": "16.2.1-canary.42",
"version": "16.2.1-canary.44",
"main": "index.js",
"license": "MIT",
"repository": {
......
{
"name": "@next/playwright",
"version": "16.2.1-canary.42",
"version": "16.2.1-canary.44",
"repository": {
"url": "vercel/next.js",
"directory": "packages/next-playwright"
......
{
"name": "@next/plugin-storybook",
"version": "16.2.1-canary.42",
"version": "16.2.1-canary.44",
"repository": {
"url": "vercel/next.js",
"directory": "packages/next-plugin-storybook"
......
{
"name": "@next/polyfill-module",
"version": "16.2.1-canary.42",
"version": "16.2.1-canary.44",
"description": "A standard library polyfill for ES Modules supporting browsers (Edge 16+, Firefox 60+, Chrome 61+, Safari 10.1+)",
"main": "dist/polyfill-module.js",
"license": "MIT",
......
{
"name": "@next/polyfill-nomodule",
"version": "16.2.1-canary.42",
"version": "16.2.1-canary.44",
"description": "A polyfill for non-dead, nomodule browsers.",
"main": "dist/polyfill-nomodule.js",
"license": "MIT",
......
{
"name": "@next/routing",
"version": "16.2.1-canary.42",
"version": "16.2.1-canary.44",
"keywords": [
"react",
"next",
......
{
"name": "next-rspack",
"version": "16.2.1-canary.42",
"version": "16.2.1-canary.44",
"repository": {
"url": "vercel/next.js",
"directory": "packages/next-rspack"
......
{
"name": "@next/swc",
"version": "16.2.1-canary.42",
"version": "16.2.1-canary.44",
"private": true,
"files": [
"native/"
......
......@@ -1160,5 +1160,6 @@
"1159": "\"unstable_instant\" is a route segment config and can only be used when the segment is a Server Component module. Remove the \"use client\" directive from \"%s\" to use this API.",
"1160": "Route \"%s\" cannot use \\`export const unstable_prefetch = ...\\` without enabling \\`cacheComponents\\`.",
"1161": "\"unstable_prefetch\" is a route segment config and can only be used when the segment is a Server Component module. Remove the \"use client\" directive from \"%s\" to use this API.",
"1162": "Route \"%s\" cannot use \\`export const unstable_instant = ...\\` without enabling \\`cacheComponents\\`."
"1162": "Route \"%s\" cannot use \\`export const unstable_instant = ...\\` without enabling \\`cacheComponents\\`.",
"1163": "Unexpected cache miss after cache warming phase during prerendering. This is likely caused by non-deterministic arguments that differ between the cache warming phase and the final prerender phase (e.g. unstable array order). Ensure that arguments passed to cached functions are deterministic."
}
{
"name": "next",
"version": "16.2.1-canary.42",
"version": "16.2.1-canary.44",
"description": "The React Framework",
"main": "./dist/server/next.js",
"license": "MIT",
......@@ -101,7 +101,7 @@
]
},
"dependencies": {
"@next/env": "16.2.1-canary.42",
"@next/env": "16.2.1-canary.44",
"@swc/helpers": "0.5.15",
"baseline-browser-mapping": "^2.9.19",
"caniuse-lite": "^1.0.30001579",
......@@ -165,11 +165,11 @@
"@modelcontextprotocol/sdk": "1.18.1",
"@mswjs/interceptors": "0.23.0",
"@napi-rs/triples": "1.2.0",
"@next/font": "16.2.1-canary.42",
"@next/polyfill-module": "16.2.1-canary.42",
"@next/polyfill-nomodule": "16.2.1-canary.42",
"@next/react-refresh-utils": "16.2.1-canary.42",
"@next/swc": "16.2.1-canary.42",
"@next/font": "16.2.1-canary.44",
"@next/polyfill-module": "16.2.1-canary.44",
"@next/polyfill-nomodule": "16.2.1-canary.44",
"@next/react-refresh-utils": "16.2.1-canary.44",
"@next/swc": "16.2.1-canary.44",
"@opentelemetry/api": "1.6.0",
"@playwright/test": "1.58.2",
"@rspack/core": "1.6.7",
......
......@@ -604,7 +604,7 @@ internal
.addOption(
new Option(
'--mcp-port <mcpPort>',
'Port for the MCP (Model Context Protocol) server. Defaults to 5748.'
'Port for the MCP (Model Context Protocol) server. Defaults to --port + 1.'
).argParser(parseValidPositiveInteger)
)
.action(
......
......@@ -142,6 +142,7 @@ export default function nextJest(options: { dir?: string } = {}) {
isEsmProject,
pagesDir,
imageConfig,
configDir: options.dir,
}
return {
......
......@@ -43,6 +43,7 @@ export interface JestTransformerConfig extends TransformerConfig {
jsConfig: any
resolvedBaseUrl?: ResolvedBaseUrl
pagesDir?: string
configDir?: string
serverComponents?: boolean
isEsmProject: boolean
modularizeImports?: NextConfig['modularizeImports']
......@@ -90,6 +91,7 @@ const createTransformer: TransformerCreator<
jestConfig.testEnvironment === 'node' ||
jestConfig.testEnvironment.includes('jest-environment-node'),
filename,
configDir: inputOptions?.configDir,
jsConfig: inputOptions?.jsConfig,
resolvedBaseUrl: inputOptions?.resolvedBaseUrl,
pagesDir: inputOptions?.pagesDir,
......
......@@ -60,6 +60,7 @@ function getBaseSWCOptions({
hasReactRefresh,
globalWindow,
esm,
configDir,
modularizeImports,
swcPlugins,
compilerOptions,
......@@ -83,6 +84,7 @@ function getBaseSWCOptions({
hasReactRefresh: boolean
globalWindow: boolean
esm: boolean
configDir?: string
modularizeImports?: NextConfig['modularizeImports']
compilerOptions: NextConfig['compiler']
swcPlugins: ExperimentalConfig['swcPlugins']
......@@ -115,7 +117,10 @@ function getBaseSWCOptions({
)
const plugins = (swcPlugins ?? [])
.filter(Array.isArray)
.map(([name, options]: any) => [require.resolve(name), options])
.map(([name, options]: any) => [
require.resolve(name, configDir ? { paths: [configDir] } : undefined),
options,
])
return {
jsc: {
......@@ -311,6 +316,7 @@ export function getJestSWCOptions({
filename,
esm,
modularizeImports,
configDir,
swcPlugins,
compilerOptions,
jsConfig,
......@@ -322,6 +328,7 @@ export function getJestSWCOptions({
isServer: boolean
filename: string
esm: boolean
configDir?: string
modularizeImports?: NextConfig['modularizeImports']
swcPlugins: ExperimentalConfig['swcPlugins']
compilerOptions: NextConfig['compiler']
......@@ -337,6 +344,7 @@ export function getJestSWCOptions({
jest: true,
development: false,
hasReactRefresh: false,
configDir,
globalWindow: !isServer,
modularizeImports,
swcPlugins,
......@@ -393,6 +401,8 @@ export function getLoaderSWCOptions({
isPageFile,
isCacheComponents,
hasReactRefresh,
// The folder containing the next.config.js, used for resolving relative config paths.
configDir,
modularizeImports,
optimizeServerReact,
optimizePackageImports,
......@@ -420,6 +430,7 @@ export function getLoaderSWCOptions({
appDir?: string
isPageFile: boolean
hasReactRefresh: boolean
configDir: string
optimizeServerReact?: boolean
modularizeImports: NextConfig['modularizeImports']
isCacheComponents?: boolean
......@@ -448,6 +459,7 @@ export function getLoaderSWCOptions({
development,
globalWindow: !isServer,
hasReactRefresh,
configDir,
modularizeImports,
swcPlugins,
compilerOptions,
......
......@@ -152,6 +152,7 @@ async function loaderTransform(
!!nextConfig.experimental?.allowDevelopmentBuild,
isCacheComponents: nextConfig.cacheComponents,
hasReactRefresh,
configDir: rootDir,
modularizeImports: nextConfig?.modularizeImports,
optimizePackageImports: nextConfig?.experimental?.optimizePackageImports,
swcPlugins: nextConfig?.experimental?.swcPlugins,
......
......@@ -9,7 +9,6 @@ const { StreamableHTTPServerTransport } =
require('next/dist/compiled/@modelcontextprotocol/sdk/server/streamableHttp') as typeof import('next/dist/compiled/@modelcontextprotocol/sdk/server/streamableHttp')
const DEFAULT_WS_PORT = 5747
const DEFAULT_MCP_PORT = 5748 // Keep in sync with query-trace.ts
/** 100 internal ticks = 1 µs */
const TICKS_PER_US = 100
......@@ -85,7 +84,7 @@ export async function startTurboTraceServerCli(
mcpPort: number | undefined
) {
const wsPort = port ?? DEFAULT_WS_PORT
const httpPort = mcpPort ?? DEFAULT_MCP_PORT
const httpPort = mcpPort ?? wsPort + 1
let bindings
try {
......
......@@ -5460,12 +5460,22 @@ async function logMessagesAndSendErrorsToBrowser(
const { clientModules } = getClientReferenceManifest()
const errorsFlightStream = renderToWebFlightStream(
ctx.componentMod,
{ errors, errorCodes },
clientModules,
{ filterStackFrame }
)
let errorsFlightStream: AnyStream
if (process.env.__NEXT_USE_NODE_STREAMS) {
errorsFlightStream = renderToNodeFlightStream(
ctx.componentMod,
{ errors, errorCodes },
clientModules,
{ filterStackFrame }
)
} else {
errorsFlightStream = renderToWebFlightStream(
ctx.componentMod,
{ errors, errorCodes },
clientModules,
{ filterStackFrame }
)
}
sendErrorsToBrowser(errorsFlightStream, htmlRequestId)
}
......@@ -6038,13 +6048,20 @@ async function validateInstantConfigs(
const clientReferenceManifest = getClientReferenceManifest()
const renderFlightStream = process.env.__NEXT_USE_NODE_STREAMS
? renderToNodeFlightStream
: renderToWebFlightStream
const createDebugChannel = process.env.__NEXT_USE_NODE_STREAMS
? createNodeDebugChannel
: createWebDebugChannel
const {
cache,
payload: initialRscPayload,
stageEndTimes,
} = await collectStagedSegmentData(
ctx.componentMod,
renderToWebFlightStream,
renderFlightStream,
{
[RenderStage.Static]: accumulatedChunks.staticChunks,
[RenderStage.Runtime]: accumulatedChunks.runtimeChunks,
......@@ -6053,7 +6070,8 @@ async function validateInstantConfigs(
debugChunks,
startTime,
hasRuntimePrefetch,
clientReferenceManifest
clientReferenceManifest,
createDebugChannel
)
const { implicitTags, nonce, workStore } = ctx
......@@ -6121,13 +6139,14 @@ async function validateInstantConfigs(
const { stream: serverStream, debugStream } =
await createCombinedPayloadStream(
ctx.componentMod,
renderToWebFlightStream,
renderFlightStream,
payloadResult.payload,
extraChunksController,
reactController.signal,
clientReferenceManifest,
startTime,
isDebugChannelEnabled
isDebugChannelEnabled,
createDebugChannel
)
const instantValidationState = createInstantValidationState(
......@@ -6371,6 +6390,9 @@ async function renderWithRestartOnCacheMissInValidation(
}> {
const { componentMod: ComponentMod } = ctx
const { clientModules } = getClientReferenceManifest()
const renderFlightStream = process.env.__NEXT_USE_NODE_STREAMS
? renderToNodeFlightStream
: renderToWebFlightStream
let startTime = -Infinity
let requestStore: RequestStore = initialRequestStore
......@@ -6435,7 +6457,7 @@ async function renderWithRestartOnCacheMissInValidation(
const stream = workUnitAsyncStorage.run(
requestStore,
renderToWebFlightStream,
renderFlightStream,
ComponentMod,
initialRscPayload,
clientModules,
......@@ -6536,7 +6558,7 @@ async function renderWithRestartOnCacheMissInValidation(
const stream = workUnitAsyncStorage.run(
requestStore,
renderToWebFlightStream,
renderFlightStream,
ComponentMod,
finalRscPayload,
clientModules,
......
......@@ -41,7 +41,7 @@ import {
createNodeStreamWithLateRelease,
createNodeStreamFromChunks,
} from './stream-utils'
import { createWebDebugChannel } from '../debug-channel-server'
import type { DebugChannelPair } from '../debug-channel-server'
import type { FlightComponentMod } from '../stream-ops'
// eslint-disable-next-line import/no-extraneous-dependencies
......@@ -217,7 +217,8 @@ export async function collectStagedSegmentData(
fullPageDebugChunks: Uint8Array[] | null,
startTime: number,
hasRuntimePrefetch: boolean,
clientReferenceManifest: ClientReferenceManifest
clientReferenceManifest: ClientReferenceManifest,
createDebugChannel: () => DebugChannelPair | undefined
) {
const debugChannelAbortController = new AbortController()
const debugStream = fullPageDebugChunks
......@@ -297,7 +298,7 @@ export async function collectStagedSegmentData(
cacheEntry: SegmentCacheItem
): Promise<void> => {
const segmentDebugChannel = cacheEntry.debugChunks
? createWebDebugChannel()
? createDebugChannel()
: undefined
const itemStream = renderFlightStream(
......@@ -527,7 +528,8 @@ export async function createCombinedPayloadStream(
renderSignal: AbortSignal,
clientReferenceManifest: ClientReferenceManifest,
startTime: number,
isDebugChannelEnabled: boolean
isDebugChannelEnabled: boolean,
createDebugChannel: () => DebugChannelPair | undefined
) {
// Collect all the chunks so that we're not dependent on timing of the render.
......@@ -536,7 +538,7 @@ export async function createCombinedPayloadStream(
const allChunks: Uint8Array[] = []
const debugChunks: Uint8Array[] | null = isDebugChannelEnabled ? [] : null
const debugChannel = isDebugChannelEnabled ? createWebDebugChannel() : null
const debugChannel = isDebugChannelEnabled ? createDebugChannel() : null
let streamFinished: Promise<any>
......
......@@ -118,7 +118,12 @@ describe('getDynamicHTMLPostponedState', () => {
expect(parsed).toEqual({
type: DynamicState.HTML,
data: [1, { [value]: value }],
renderResumeDataCache: createPrerenderResumeDataCache(),
renderResumeDataCache: {
cache: new Map(),
fetch: new Map(),
encryptedBoundArgs: new Map(),
decryptedBoundArgs: new Map(),
},
})
// The replacements have been replaced.
......@@ -148,7 +153,12 @@ describe('parsePostponedState', () => {
expect(parsed).toEqual({
type: DynamicState.HTML,
data: expect.any(Object),
renderResumeDataCache: createPrerenderResumeDataCache(),
renderResumeDataCache: {
cache: new Map(),
fetch: new Map(),
encryptedBoundArgs: new Map(),
decryptedBoundArgs: new Map(),
},
})
// Ensure that the replacement worked and removed all the placeholders.
......@@ -164,7 +174,12 @@ describe('parsePostponedState', () => {
expect(parsed).toEqual({
type: DynamicState.HTML,
data: expect.any(Object),
renderResumeDataCache: createPrerenderResumeDataCache(),
renderResumeDataCache: {
cache: new Map(),
fetch: new Map(),
encryptedBoundArgs: new Map(),
decryptedBoundArgs: new Map(),
},
})
})
......@@ -175,7 +190,12 @@ describe('parsePostponedState', () => {
// Ensure that it parsed it correctly.
expect(parsed).toEqual({
type: DynamicState.DATA,
renderResumeDataCache: createPrerenderResumeDataCache(),
renderResumeDataCache: {
cache: new Map(),
fetch: new Map(),
encryptedBoundArgs: new Map(),
decryptedBoundArgs: new Map(),
},
})
})
})
......@@ -209,6 +209,24 @@ class NextTracerImpl implements NextTracer {
return trace.getTracer('next.js', '0.0.1')
}
private isTracingEnabled(): boolean {
if (this.getActiveScopeSpan()?.isRecording()) {
return true
}
const tracerProvider = trace.getTracerProvider() as {
getDelegate?: () => { constructor?: { name?: string } } | undefined
}
if (!('getDelegate' in tracerProvider)) {
return true
}
return (
tracerProvider.getDelegate?.()?.constructor?.name !== 'NoopTracerProvider'
)
}
public getContext(): ContextAPI {
return context
}
......@@ -277,6 +295,10 @@ class NextTracerImpl implements NextTracer {
public trace<T>(...args: Array<any>) {
const [type, fnOrOptions, fnOrEmpty] = args
if (!NEXT_OTEL_PERFORMANCE_PREFIX && !this.isTracingEnabled()) {
return typeof fnOrOptions === 'function' ? fnOrOptions() : fnOrEmpty()
}
// coerce options form overload
const {
fn,
......
......@@ -140,9 +140,12 @@ describe('stringifyResumeDataCache', () => {
describe('parseResumeDataCache', () => {
it('parses an empty cache', () => {
expect(createRenderResumeDataCache('null', undefined)).toEqual(
createPrerenderResumeDataCache()
)
const parsed = createRenderResumeDataCache('null', undefined)
expect(parsed.cache).toEqual(new Map())
expect(parsed.fetch).toEqual(new Map())
expect(parsed.encryptedBoundArgs).toEqual(new Map())
expect(parsed.decryptedBoundArgs).toEqual(new Map())
expect(parsed.dynamicCacheKeys).toBeUndefined()
})
it('parses a filled cache', async () => {
......
......@@ -39,6 +39,16 @@ export interface RenderResumeDataCache {
* enforce immutability.
*/
readonly decryptedBoundArgs: Omit<DecryptedBoundArgsCacheStore, 'set'>
/**
* Serialized cache keys that were intentionally skipped during the
* prospective prerender (e.g. because the cached function accessed fallback
* params or other dynamic data). During the final prerender, a key in this
* set is returned as a hanging promise early, without attempting to look up
* or generate a cache entry. Optional because this field is intentionally not
* serialized and won't be present in deserialized caches.
*/
readonly dynamicCacheKeys?: ReadonlySet<string>
}
/**
......@@ -74,6 +84,20 @@ export interface PrerenderResumeDataCache {
* operations to build the cache during pre-rendering.
*/
readonly decryptedBoundArgs: DecryptedBoundArgsCacheStore
/**
* Tracks serialized cache keys that were intentionally skipped during the
* prospective prerender (e.g. because the cached function accessed fallback
* params or other dynamic data). During the final prerender, a key in this
* set is returned as a hanging promise early, without attempting to look up
* or generate a cache entry.
*
* This is intentionally not serialized. It is only used in-memory within a
* single prerender cycle (prospective to final). During the resume at request
* time, a cache miss for a dynamic key should generate a fresh entry rather
* than being short-circuited.
*/
readonly dynamicCacheKeys: Set<string>
}
type ResumeStoreSerialized = {
......@@ -156,6 +180,9 @@ export function createPrerenderResumeDataCache(
fetch: new Map(source.fetch),
encryptedBoundArgs: new Map(source.encryptedBoundArgs),
decryptedBoundArgs: new Map(source.decryptedBoundArgs),
dynamicCacheKeys: source.dynamicCacheKeys
? new Set(source.dynamicCacheKeys)
: new Set(),
}
} else {
return {
......@@ -163,6 +190,7 @@ export function createPrerenderResumeDataCache(
fetch: new Map(),
encryptedBoundArgs: new Map(),
decryptedBoundArgs: new Map(),
dynamicCacheKeys: new Set(),
}
}
}
......
......@@ -1550,6 +1550,32 @@ export async function cache(
const implicitTags = workUnitStore.implicitTags?.tags ?? []
if (renderResumeDataCache) {
// If this cache key was already determined to be dynamic during the
// prospective prerender (e.g. because it accessed fallback params), we
// return a hanging promise early to avoid trying to regenerate the entry,
// which would be aborted anyway.
if (renderResumeDataCache.dynamicCacheKeys?.has(serializedCacheKey)) {
switch (workUnitStore.type) {
case 'prerender':
case 'prerender-runtime':
return makeHangingPromise(
workUnitStore.renderSignal,
workStore.route,
'dynamic "use cache"'
)
case 'prerender-ppr':
case 'prerender-legacy':
case 'request':
case 'cache':
case 'private-cache':
case 'unstable-cache':
case 'generate-static-params':
break
default:
workUnitStore satisfies never
}
}
const cacheSignal = getCacheSignal(workUnitStore)
if (cacheSignal) {
......@@ -1802,14 +1828,38 @@ export async function cache(
// an async function, before being passed into the "use cache"
// function, which escapes the instrumentation.
if (workUnitStore.allowEmptyStaticShell) {
if (prerenderResumeDataCache) {
prerenderResumeDataCache.dynamicCacheKeys.add(serializedCacheKey)
}
return makeHangingPromise(
workUnitStore.renderSignal,
workStore.route,
'dynamic "use cache"'
)
}
break
// fallthrough
case 'prerender-runtime':
if (!cacheSignal) {
// This is the final prerender (cacheSignal is null), which means
// all caches should have been warmed during the prospective
// prerender. A cache miss here indicates that the cache key is
// non-deterministic (e.g. due to unstable array order in the
// arguments). Known dynamic keys (e.g. from fallback params) are
// already handled by the early return above. We return a hanging
// promise so this becomes a dynamic hole rather than generating a
// broken cache entry that gets aborted.
console.warn(
new Error(
`Unexpected cache miss after cache warming phase during prerendering. This is likely caused by non-deterministic arguments that differ between the cache warming phase and the final prerender phase (e.g. unstable array order). Ensure that arguments passed to cached functions are deterministic.`
)
)
return makeHangingPromise(
workUnitStore.renderSignal,
workStore.route,
'dynamic "use cache"'
)
}
break
case 'prerender-ppr':
case 'prerender-legacy':
case 'request':
......@@ -2006,6 +2056,9 @@ export async function cache(
)
if (result.type === 'prerender-dynamic') {
if (prerenderResumeDataCache) {
prerenderResumeDataCache.dynamicCacheKeys.add(serializedCacheKey)
}
return result.hangingPromise
}
......
{
"name": "@next/react-refresh-utils",
"version": "16.2.1-canary.42",
"version": "16.2.1-canary.44",
"description": "An experimental package providing utilities for React Refresh.",
"repository": {
"url": "vercel/next.js",
......
{
"name": "@next/third-parties",
"version": "16.2.1-canary.42",
"version": "16.2.1-canary.44",
"repository": {
"url": "vercel/next.js",
"directory": "packages/third-parties"
......@@ -27,7 +27,7 @@
"third-party-capital": "1.0.20"
},
"devDependencies": {
"next": "16.2.1-canary.42",
"next": "16.2.1-canary.44",
"outdent": "0.8.0",
"prettier": "2.5.1",
"typescript": "6.0.2"
......
......@@ -1023,7 +1023,7 @@ importers:
packages/eslint-config-next:
dependencies:
'@next/eslint-plugin-next':
specifier: 16.2.1-canary.42
specifier: 16.2.1-canary.44
version: link:../eslint-plugin-next
eslint:
specifier: '>=9.0.0'
......@@ -1100,7 +1100,7 @@ importers:
packages/next:
dependencies:
'@next/env':
specifier: 16.2.1-canary.42
specifier: 16.2.1-canary.44
version: link:../next-env
'@swc/helpers':
specifier: 0.5.15
......@@ -1221,19 +1221,19 @@ importers:
specifier: 1.2.0
version: 1.2.0
'@next/font':
specifier: 16.2.1-canary.42
specifier: 16.2.1-canary.44
version: link:../font
'@next/polyfill-module':
specifier: 16.2.1-canary.42
specifier: 16.2.1-canary.44
version: link:../next-polyfill-module
'@next/polyfill-nomodule':
specifier: 16.2.1-canary.42
specifier: 16.2.1-canary.44
version: link:../next-polyfill-nomodule
'@next/react-refresh-utils':
specifier: 16.2.1-canary.42
specifier: 16.2.1-canary.44
version: link:../react-refresh-utils
'@next/swc':
specifier: 16.2.1-canary.42
specifier: 16.2.1-canary.44
version: link:../next-swc
'@opentelemetry/api':
specifier: 1.6.0
......@@ -1967,7 +1967,7 @@ importers:
version: 1.0.20
devDependencies:
next:
specifier: 16.2.1-canary.42
specifier: 16.2.1-canary.44
version: link:../next
outdent:
specifier: 0.8.0
......@@ -29,6 +29,8 @@ ignoredBuiltDependencies:
- ssh2
- unrs-resolver
- wasm-pack
# Keep security related settings in sync with pnpm-workspace.yaml written to
# temporary fixture dirs in file://./scripts/install-native.mjs
blockExoticSubdeps: true
minimumReleaseAge: 2880 # 48 hrs
minimumReleaseAgeExclude:
......
#!/usr/bin/env node
// @ts-check
const fs = require('fs/promises')
const path = require('path')
const semver = require('semver')
const PUBLISH_RELEASE_JOB_NAME = 'Potentially publish release'
const TRIGGER_RELEASE_WORKFLOW = 'trigger_release.yml'
const RELEASE_COMMIT_REGEX = /^v\d+\.\d+\.\d+(-\w+\.\d+)?$/
// This helper is used by a workflow_run listener on build-and-deploy.
// It decides whether a successful stable backport publish should trigger
// a canary preminor release so canary stays semver-ahead of the backport.
function getArgValue(flag) {
const index = process.argv.indexOf(flag)
return index === -1 ? undefined : process.argv[index + 1]
}
function parseReleaseVersion(commitMessage) {
const versionString = commitMessage.split(' ').pop()?.trim()
return versionString && RELEASE_COMMIT_REGEX.test(versionString)
? versionString.slice(1)
: null
}
function compareSemver(left, right) {
const result = semver.compare(left, right)
if (Number.isNaN(result)) {
throw new Error(`Invalid semver comparison "${left}" vs "${right}"`)
}
return result
}
async function fetchJson(url, token) {
const headers = {
Accept: 'application/vnd.github+json',
}
if (token) {
headers.Authorization = `Bearer ${token}`
headers['X-GitHub-Api-Version'] = '2022-11-28'
}
const response = await fetch(url, { headers })
if (!response.ok) {
throw new Error(
`Request failed (${response.status}) for ${url}: ${await response.text()}`
)
}
return response.json()
}
async function appendOutputs(outputs) {
if (!process.env.GITHUB_OUTPUT) {
return
}
let output = ''
for (const [key, value] of Object.entries(outputs)) {
output += `${key}=${String(value ?? '').replace(/\n/g, ' ')}\n`
}
await fs.appendFile(process.env.GITHUB_OUTPUT, output)
}
async function getWorkflowRunJobs(owner, repo, workflowRunId, token) {
const jobs = []
for (let page = 1; ; page++) {
const params = new URLSearchParams({
per_page: '100',
page: String(page),
})
const data = await fetchJson(
`https://api.github.com/repos/${owner}/${repo}/actions/runs/${workflowRunId}/jobs?${params}`,
token
)
jobs.push(...data.jobs)
if (jobs.length >= data.total_count || data.jobs.length === 0) {
return jobs
}
}
}
async function getActiveTriggerReleaseRun(owner, repo, token) {
for (const status of ['queued', 'in_progress']) {
const params = new URLSearchParams({
branch: 'canary',
status,
per_page: '100',
})
const data = await fetchJson(
`https://api.github.com/repos/${owner}/${repo}/actions/workflows/${TRIGGER_RELEASE_WORKFLOW}/runs?${params}`,
token
)
if (data.total_count > 0) {
return data.workflow_runs[0]
}
}
return null
}
async function getCommitMessage(owner, repo, sha, token) {
const data = await fetchJson(
`https://api.github.com/repos/${owner}/${repo}/commits/${sha}`,
token
)
return data.commit.message.trim()
}
async function finish(outputs) {
await appendOutputs(outputs)
if (outputs.should_dispatch === 'true') {
console.log(outputs.reason)
} else {
console.log(`Skipping canary sync: ${outputs.reason}`)
}
}
async function main() {
const workflowRunId = getArgValue('--workflow-run-id')
const headSha = getArgValue('--head-sha')
const headCommitMessage = process.env.HEAD_COMMIT_MESSAGE?.trim()
if (!workflowRunId) {
throw new Error('Missing --workflow-run-id')
}
if (!headSha) {
throw new Error('Missing --head-sha')
}
const token = process.env.RELEASE_BOT_GITHUB_TOKEN || process.env.GITHUB_TOKEN
const repoFullName = process.env.GITHUB_REPOSITORY
if (!token) {
throw new Error('Missing RELEASE_BOT_GITHUB_TOKEN or GITHUB_TOKEN')
}
if (!repoFullName) {
throw new Error('Missing GITHUB_REPOSITORY')
}
const [owner, repo] = repoFullName.split('/')
const currentCanaryVersion = JSON.parse(
await fs.readFile(path.join(process.cwd(), 'lerna.json'), 'utf8')
).version
const releaseCommitMessage =
headCommitMessage || (await getCommitMessage(owner, repo, headSha, token))
const releasedVersion = parseReleaseVersion(releaseCommitMessage)
if (!releasedVersion) {
await finish({
should_dispatch: 'false',
reason: 'Head commit is not a release commit',
current_canary_version: currentCanaryVersion,
})
return
}
const jobs = await getWorkflowRunJobs(owner, repo, workflowRunId, token)
const publishJob = jobs.find((job) => job.name === PUBLISH_RELEASE_JOB_NAME)
// Only continue if the upstream workflow really reached the publish step.
// A successful build-and-deploy run can still skip publishing entirely.
if (publishJob?.conclusion !== 'success') {
await finish({
should_dispatch: 'false',
reason: `${PUBLISH_RELEASE_JOB_NAME} did not complete successfully`,
current_canary_version: currentCanaryVersion,
publish_job_conclusion: publishJob?.conclusion ?? 'missing',
released_version: releasedVersion,
})
return
}
if (releasedVersion.includes('-')) {
await finish({
should_dispatch: 'false',
reason: `Released version ${releasedVersion} is not a stable release`,
current_canary_version: currentCanaryVersion,
released_version: releasedVersion,
})
return
}
const distTags = await fetchJson(
'https://registry.npmjs.org/-/package/next/dist-tags'
)
const latestStableVersion = distTags.latest
// Backports publish a stable version lower than npm's current latest.
// If the released version is not lower than latest, this was not a backport.
if (compareSemver(releasedVersion, latestStableVersion) >= 0) {
await finish({
should_dispatch: 'false',
reason: `Released version ${releasedVersion} is not a backport`,
current_canary_version: currentCanaryVersion,
released_version: releasedVersion,
latest_version: latestStableVersion,
})
return
}
if (compareSemver(currentCanaryVersion, releasedVersion) > 0) {
await finish({
should_dispatch: 'false',
reason: `Current canary version ${currentCanaryVersion} is already ahead of ${releasedVersion}`,
current_canary_version: currentCanaryVersion,
released_version: releasedVersion,
latest_version: latestStableVersion,
})
return
}
const activeTriggerReleaseRun = await getActiveTriggerReleaseRun(
owner,
repo,
token
)
// Avoid stacking multiple canary bumps if several workflow_run events land
// while a previous Trigger Release dispatch is still being processed.
if (activeTriggerReleaseRun) {
await finish({
should_dispatch: 'false',
reason: `Trigger Release is already ${activeTriggerReleaseRun.status} on canary`,
active_trigger_release_url: activeTriggerReleaseRun.html_url,
current_canary_version: currentCanaryVersion,
released_version: releasedVersion,
latest_version: latestStableVersion,
})
return
}
await finish({
should_dispatch: 'true',
reason: `Dispatching canary preminor release because backport ${releasedVersion} is ahead of ${currentCanaryVersion}`,
current_canary_version: currentCanaryVersion,
released_version: releasedVersion,
latest_version: latestStableVersion,
})
}
if (require.main === module) {
main().catch((error) => {
console.error(error)
process.exit(1)
})
}
......@@ -3,6 +3,7 @@ import path from 'path'
import execa from 'execa'
import fs from 'fs'
import fsp from 'fs/promises'
import { outdent } from 'outdent'
;(async function () {
if (process.env.NEXT_SKIP_NATIVE_POSTINSTALL) {
console.log(
......@@ -58,20 +59,43 @@ import fsp from 'fs/promises'
packageManager,
}
fs.writeFileSync(path.join(tmpdir, 'package.json'), JSON.stringify(pkgJson))
fs.writeFileSync(path.join(tmpdir, '.npmrc'), 'node-linker=hoisted')
fs.writeFileSync(
path.join(tmpdir, 'pnpm-workspace.yaml'),
'' +
outdent`
nodeLinker: hoisted
` +
'\n' +
// Propagate security related settings from file://./../../pnpm-workspace.yaml
outdent`
blockExoticSubdeps: true
minimumReleaseAge: 2880 # 48 hrs
minimumReleaseAgeExclude:
- '@next/*'
- '@turbo/*'
- '@vercel/*'
- '@workflow/*'
- babel-plugin-react-compiler
- next
- react
- react-dom
- react-is
- react-server-dom-*
- scheduler
- turbo
`
)
const args = [
'add',
`next@${nextVersion}`,
'--lockfile=false',
'--ignore-workspace',
'--ignore-scripts',
]
if (preferOffline) {
args.push('--prefer-offline')
}
let { stdout } = await execa('pnpm', args, { cwd: tmpdir })
console.log(stdout)
await execa('pnpm', args, { cwd: tmpdir })
let pkgs = fs.readdirSync(path.join(tmpdir, 'node_modules/@next'))
fs.mkdirSync(path.join(cwd, 'node_modules/@next'), { recursive: true })
......@@ -91,7 +115,6 @@ import fsp from 'fs/promises'
fs.rmSync(tmpdir, { recursive: true, force: true })
console.log('Installed the following binary packages:', pkgs)
} catch (e) {
console.error(e)
console.error('Failed to load @next/swc binary packages')
throw new Error('Failed to install @next/swc binary packages', { cause: e })
}
})()
......@@ -3421,11 +3421,12 @@ describe('Cache Components Errors', () => {
if (isDebugPrerender) {
expect(output).toMatchInlineSnapshot(`
"Error: Route "/use-cache-params/[slug]": Uncached data was accessed outside of <Suspense>. This delays the entire page from rendering, resulting in a slow user experience. Learn more: https://nextjs.org/docs/messages/blocking-route
at Page [Server] (<anonymous>)
at main (<anonymous>)
at body (<anonymous>)
at html (<anonymous>)
at Root [Server] (<anonymous>)
at Page (app/use-cache-params/[slug]/page.tsx:1:16)
> 1 | export default async function Page({
| ^
2 | params,
3 | }: {
4 | params: Promise<{ slug: string }>
To debug the issue, start the app in development mode by running \`next dev\`, then open "/use-cache-params/[slug]" in your browser to investigate the error.
Error occurred prerendering page "/use-cache-params/[slug]". Read more: https://nextjs.org/docs/messages/prerender-error
......@@ -3447,16 +3448,12 @@ describe('Cache Components Errors', () => {
}
} else {
if (isDebugPrerender) {
expect(output).toMatchInlineSnapshot(`
"Error: Route "/use-cache-params/[slug]": Uncached data was accessed outside of <Suspense>. This delays the entire page from rendering, resulting in a slow user experience. Learn more: https://nextjs.org/docs/messages/blocking-route
at Page [Server] (<anonymous>)
at <FIXME-library-internal>
To debug the issue, start the app in development mode by running \`next dev\`, then open "/use-cache-params/[slug]" in your browser to investigate the error.
Error occurred prerendering page "/use-cache-params/[slug]". Read more: https://nextjs.org/docs/messages/prerender-error
> Export encountered errors on 1 path:
/use-cache-params/[slug]/page: /use-cache-params/[slug]"
`)
// Webpack does not ignore the stack frame that points into
// Next.js internals, and is also flaky on resolving the exact
// location, so we don't assert on the stack frames here.
expect(output).toInclude(
'Error: Route "/use-cache-params/[slug]": Uncached data was accessed outside of <Suspense>. This delays the entire page from rendering, resulting in a slow user experience. Learn more: https://nextjs.org/docs/messages/blocking-route'
)
} else {
expect(output).toMatchInlineSnapshot(`
"Error: Route "/use-cache-params/[slug]": Uncached data was accessed outside of <Suspense>. This delays the entire page from rendering, resulting in a slow user experience. Learn more: https://nextjs.org/docs/messages/blocking-route
......
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
// ENV_CHECK will be replaced by the SWC plugin with "development" or "production"
declare const ENV_CHECK: string
export default function Home() {
return <main>The SWC plugin received env={ENV_CHECK}</main>
}
import { nextTestSetup } from 'e2e-utils'
describe('swc-plugins-env', () => {
const { next, skipped, isNextDev } = nextTestSetup({
files: __dirname,
skipDeployment: true,
})
if (skipped) return
it('should pass correct environment to swc plugins', async () => {
const $ = await next.render$('/')
if (isNextDev) {
expect($('main').text()).toBe('The SWC plugin received env=development')
} else {
expect($('main').text()).toBe('The SWC plugin received env=production')
}
})
})
/** @type {import('next').NextConfig} */
module.exports = {
experimental: {
swcPlugins: [['./swc_plugin_env_check.wasm', {}]],
},
}
This diff is collapsed.
[package]
name = "swc-plugin-env-check"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[profile.release]
strip = "debuginfo"
codegen-units = 1
lto = true
opt-level = "z"
[dependencies]
serde = "1.0.173"
serde_json = "1.0.103"
swc_core = { version = "63.1.1", features = ["ecma_plugin_transform"] }
#![allow(clippy::not_unsafe_ptr_arg_deref)]
use swc_core::{
common::DUMMY_SP,
ecma::{
ast::*,
visit::{VisitMut, VisitMutWith, visit_mut_pass},
},
plugin::{
metadata::TransformPluginMetadataContextKind, plugin_transform,
proxies::TransformPluginProgramMetadata,
},
};
struct EnvCheckVisitor {
env_name: String,
}
impl VisitMut for EnvCheckVisitor {
fn visit_mut_expr(&mut self, expr: &mut Expr) {
expr.visit_mut_children_with(self);
// Replace any identifier `ENV_CHECK` with a string literal of the actual env value
if let Expr::Ident(ident) = expr {
if ident.sym.as_str() == "ENV_CHECK" {
*expr = Expr::Lit(Lit::Str(Str {
span: DUMMY_SP,
value: self.env_name.clone().into(),
raw: None,
}));
}
}
}
}
#[plugin_transform]
pub fn process_transform(program: Program, metadata: TransformPluginProgramMetadata) -> Program {
let env_name = metadata
.get_context(&TransformPluginMetadataContextKind::Env)
.expect("failed to get env");
program.apply(visit_mut_pass(&mut EnvCheckVisitor { env_name }))
}
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import Link from 'next/link'
import { Suspense, cacheSignal } from 'react'
import { setTimeout } from 'timers/promises'
import { cookies } from 'next/headers'
export const unstable_prefetch = 'runtime'
const callCounts = new Map<string, number>()
function getNonDeterministicItems(slug: string) {
// For testing purposes, this creates an artificial deterministic scenario
// that in real-world apps would usually be triggered by a race condition
// only sometimes (e.g. concurrent async operations pushing results into a
// shared array in non-deterministic order, and that array then being passed
// as an argument to a cached function).
//
// Call 1 (prospective prerender) warms the RDC entry for ['b','a'].
// Call 2+ (final prerender) uses ['a','b'], which produces a different cache
// key that misses the RDC.
const count = (callCounts.get(slug) ?? 0) + 1
callCounts.set(slug, count)
return count === 1 ? ['b', 'a'] : ['a', 'b']
}
async function getCachedValue(items: string[]) {
'use cache'
await setTimeout(1000)
return items.join(',')
}
async function Content({ slug }: { slug: string }) {
if (slug === 'known') {
// Awaiting cookies() causes this to suspend during build-time prerendering
// (cookies are not available at build time). During runtime prefetch,
// cookies are available so rendering continues past this point and hits the
// non-deterministic cached function.
await cookies()
}
const items = getNonDeterministicItems(slug)
try {
const value = await getCachedValue(items)
return (
<p id="cached">
{slug} {value}
</p>
)
} catch (error) {
// We only log and re-throw errors that occur while the render is still
// active. After the prerender is aborted, the hanging promise rejects with
// an expected error that we can safely ignore. This is the intended use of
// `React.cacheSignal()`. Userspace code should use it the same way to
// distinguish real errors from expected prerender teardown.
if (!cacheSignal()?.aborted) {
console.error('getCachedValue error:', error)
throw error
}
}
}
export default async function Page({
params,
}: {
params: Promise<{ slug: string }>
}) {
const { slug } = await params
if (slug === 'with-runtime-prefetch') {
return <Link href="/known">Go to runtime-prefetchable page</Link>
}
return (
<Suspense fallback={<p>Loading...</p>}>
<Content slug={slug} />
</Suspense>
)
}
export function generateStaticParams() {
return [{ slug: 'known' }, { slug: 'with-runtime-prefetch' }]
}
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
cacheComponents: true,
}
export default nextConfig
import { nextTestSetup } from 'e2e-utils'
describe('use-cache-non-deterministic-args', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('should warn about non-deterministic cache args during on-demand prerender', async () => {
const outputIndex = next.cliOutput.length
await next.browser('/unknown')
const output = next.cliOutput.slice(outputIndex)
expect(output).not.toContain('Connection closed')
expect(output).toContain(
'Unexpected cache miss after cache warming phase during prerendering'
)
})
it('should warn about non-deterministic cache args during runtime prefetch', async () => {
const outputIndex = next.cliOutput.length
let onRuntimePrefetchDone: () => void
const runtimePrefetchDone = new Promise<void>((resolve) => {
onRuntimePrefetchDone = resolve
})
// Visit /with-runtime-prefetch to render the Link. The link being visible
// in the viewport triggers a runtime prefetch for the /known route.
await next.browser('/with-runtime-prefetch', {
beforePageLoad(page) {
page.on('response', (res) => {
const url = new URL(res.url())
const request = res.request()
if (
url.pathname === '/known' &&
request.headers()['next-router-prefetch'] === '2'
) {
onRuntimePrefetchDone()
}
})
},
})
await runtimePrefetchDone
const output = next.cliOutput.slice(outputIndex)
expect(output).not.toContain('Connection closed')
expect(output).toContain(
'Unexpected cache miss after cache warming phase during prerendering'
)
})
})
This diff is collapsed.
This diff is collapsed.
......@@ -19,6 +19,7 @@ describe('swcEnvOptions', () => {
serverReferenceHashSalt: 'test-salt',
bundleLayer: undefined,
cacheHandlers: undefined,
configDir: '/',
}
it('should not include extra env options when swcEnvOptions is not set', () => {
......
......@@ -9,7 +9,7 @@ use bincode::{Decode, Encode};
use indexmap::Equivalent;
use num_bigint::BigInt;
use rustc_hash::FxHashSet;
use smallvec::SmallVec;
use smallvec::{SmallVec, smallvec};
use turbo_rcstr::RcStr;
use turbo_tasks::{FxIndexMap, NonLocalValue, ResolvedVc, Vc, trace::TraceRawVcs};
use turbo_tasks_fs::FileSystemPath;
......@@ -340,6 +340,19 @@ impl CompileTimeDefines {
pub fn empty() -> Vc<Self> {
Vc::cell(FxIndexMap::default())
}
#[turbo_tasks::function]
pub async fn read_process_env(&self, key: RcStr) -> Result<Vc<Option<RcStr>>> {
let key = DefinableNameSegmentRefs(smallvec![
DefinableNameSegmentRef::Name("process"),
DefinableNameSegmentRef::Name("env"),
DefinableNameSegmentRef::Name(&key),
]);
Ok(Vc::cell(match self.0.get(&key) {
Some(CompileTimeDefineValue::String(s)) => Some(s.clone()),
_ => None,
}))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, TraceRawVcs, NonLocalValue, Encode, Decode)]
......
......@@ -108,16 +108,20 @@ impl ChunkGroupInfo {
if let Some(idx) = self.chunk_groups.get_index_of(&chunk_group) {
Ok(Vc::cell(idx))
} else {
bail!(
"Couldn't find chunk group index for {} in {}",
chunk_group.debug_str(self).await?,
self.chunk_groups
.iter()
.map(|c| c.debug_str(self))
.try_join()
.await?
.join(", ")
);
if cfg!(debug_assertions) {
bail!(
"Couldn't find chunk group index for {} in {}",
chunk_group.debug_str(self).await?,
self.chunk_groups
.iter()
.map(|c| c.debug_str(self))
.try_join()
.await?
.join(", ")
);
} else {
bail!("Couldn't find chunk group index")
}
}
}
}
......
......@@ -4,7 +4,7 @@ use std::{
mem::take,
};
use anyhow::{Context, Result};
use anyhow::{Context, Result, bail};
use bincode::{Decode, Encode};
use either::Either;
use petgraph::graph::{DiGraph, EdgeIndex, NodeIndex};
......@@ -79,19 +79,23 @@ pub struct ModuleBatchesGraph {
impl ModuleBatchesGraph {
pub async fn get_entry_index(&self, entry: ResolvedVc<Box<dyn Module>>) -> Result<NodeIndex> {
let Some(entry) = self.entries.get(&entry) else {
let possible_entries = format!(
"{:#?}",
self.entries
.keys()
.map(|e| e.ident().to_string())
.try_join()
.await?
);
turbobail!(
"Entry {} is not in graph (possible entries: {})",
entry.ident(),
possible_entries
);
if cfg!(debug_assertions) {
let possible_entries = format!(
"{:#?}",
self.entries
.keys()
.map(|e| e.ident().to_string())
.try_join()
.await?
);
turbobail!(
"Entry {} is not in graph (possible entries: {})",
entry.ident(),
possible_entries
);
} else {
bail!("Entry is not in graph");
}
};
Ok(*entry)
}
......
......@@ -163,8 +163,7 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer {
let transform_metadata_context = Arc::new(TransformPluginMetadataContext::new(
Some(ctx.file_path_str.to_string()),
//[TODO]: Support env-related variable injection, i.e process.env.NODE_ENV
"development".to_string(),
ctx.node_env.to_string(),
None,
));
......
......@@ -20,7 +20,7 @@ use turbopack_core::{
use turbopack_ecmascript::{
AnalyzeMode,
analyzer::{
graph::{EvalContext, VarGraph, VarMeta, create_graph},
graph::{EvalContext, VarGraph, create_graph},
imports::ImportAttributes,
linker::link,
test_utils::{early_visitor, visitor},
......@@ -66,7 +66,6 @@ pub fn benchmark(c: &mut Criterion) {
top_level_mark,
Default::default(),
None,
None,
);
let var_graph = create_graph(
&program,
......@@ -116,7 +115,7 @@ fn bench_link(b: &mut Bencher, input: &BenchInput) {
b.to_async(rt).iter(|| async {
let var_cache = Default::default();
for VarMeta { value, .. } in input.var_graph.values.values() {
for value in input.var_graph.values.values() {
VcStorage::with(async {
let compile_time_info = CompileTimeInfo::builder(
Environment::new(ExecutionEnvironment::NodeJsLambda(
......
......@@ -22,8 +22,7 @@ use swc_core::{
},
};
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::ResolvedVc;
use turbopack_core::{resolve::ExportUsage, source::Source};
use turbopack_core::resolve::ExportUsage;
use super::{
ConstantValue, ImportMap, JsValue, ObjectPart, WellKnownFunctionKind, is_unresolved_id,
......@@ -272,26 +271,34 @@ impl Effect {
}
}
#[derive(Debug)]
pub enum AssignmentScope {
/// assigned in the root scope
ModuleEval,
/// assigned in a function scopes
Function,
}
/// Tracks the locations where this was assigned to:
/// This is used to track the _liveness_ of exports.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum AssignmentScopes {
/// assigned only in the root scope
AllInModuleEvalScope,
/// assigned in any set of function scopes
AllInFunctionScopes,
/// assigned in both module and function scopes
Mixed,
}
impl AssignmentScopes {
fn new(initial: AssignmentScope) -> Self {
pub fn new(initial: AssignmentScope) -> Self {
match initial {
AssignmentScope::ModuleEval => AssignmentScopes::AllInModuleEvalScope,
AssignmentScope::Function => AssignmentScopes::AllInFunctionScopes,
}
}
fn merge(self, other: AssignmentScope) -> Self {
pub fn merge(self, other: AssignmentScope) -> Self {
// If the other assignment kind is the same as the current one, return the current one.
if self == Self::new(other) {
self
......@@ -301,37 +308,6 @@ impl AssignmentScopes {
}
}
#[derive(Debug, Clone)]
pub struct VarMeta {
pub value: JsValue,
/// Tracks the locations where this was assigned to:
/// - [`AssignmentScopes::AllInModuleEvalScope`] if it was assigned only in the root scope
/// - [`AssignmentScopes::AllInFunctionScopes`] if it was assigned in any set of function
/// scopes
/// - [`AssignmentScopes::Mixed`] if it was assigned in both
///
/// This is used to track the _liveness_ of exports.
pub assignment_scopes: AssignmentScopes,
}
impl VarMeta {
pub fn new(value: JsValue, kind: AssignmentScope) -> Self {
Self {
value,
assignment_scopes: AssignmentScopes::new(kind),
}
}
pub fn normalize(&mut self) {
self.value.normalize();
}
fn add_alt(&mut self, value: JsValue, kind: AssignmentScope) {
self.value.add_alt(value);
self.assignment_scopes = self.assignment_scopes.merge(kind);
}
}
#[derive(Clone, Debug)]
pub enum DeclUsage {
SideEffects,
......@@ -358,7 +334,7 @@ impl DeclUsage {
#[derive(Debug)]
pub struct VarGraph {
pub values: FxHashMap<Id, VarMeta>,
pub values: FxHashMap<Id, JsValue>,
/// Map [`JsValue::FreeVar`] names to their [`Id`] to facilitate lookups into [`Self::values`].
///
......@@ -449,14 +425,11 @@ impl EvalContext {
top_level_mark: Mark,
force_free_values: Arc<FxHashSet<Id>>,
comments: Option<&dyn Comments>,
source: Option<ResolvedVc<Box<dyn Source>>>,
) -> Self {
Self {
unresolved_mark,
top_level_mark,
imports: module.map_or(ImportMap::default(), |m| {
ImportMap::analyze(m, source, comments)
}),
imports: module.map_or(ImportMap::default(), |m| ImportMap::analyze(m, comments)),
force_free_values,
}
}
......@@ -889,14 +862,8 @@ impl EvalContext {
let js_value = try_with_handler(cm, Default::default(), |_| {
GLOBALS.set(&Default::default(), || {
let expr = parse_single_expr_lit(expr_lit);
let eval_context = EvalContext::new(
None,
Mark::new(),
Mark::new(),
Default::default(),
None,
None,
);
let eval_context =
EvalContext::new(None, Mark::new(), Mark::new(), Default::default(), None);
Ok(eval_context.eval(&expr))
})
......@@ -1541,15 +1508,10 @@ impl Analyzer<'_> {
self.data.free_var_ids.insert(id.0.clone(), id.clone());
}
let kind = if self.is_in_fn() {
AssignmentScope::Function
} else {
AssignmentScope::ModuleEval
};
if let Some(prev) = self.data.values.get_mut(&id) {
prev.add_alt(value, kind);
prev.add_alt(value);
} else {
self.data.values.insert(id, VarMeta::new(value, kind));
self.data.values.insert(id, value);
}
// TODO(kdy1): We may need to report an error for this.
// Variables declared with `var` are hoisted, but using undefined as its
......
......@@ -6,7 +6,6 @@ use rustc_hash::{FxHashMap, FxHashSet};
use swc_core::ecma::ast::Id;
use super::{JsValue, graph::VarGraph};
use crate::analyzer::graph::VarMeta;
pub async fn link<'a, B, RB, F, RF>(
graph: &VarGraph,
......@@ -123,7 +122,7 @@ where
if let Some(val) = var_cache_lock.as_deref().and_then(|cache| cache.get(&var)) {
total_nodes += val.total_nodes();
done.push(val.clone());
} else if let Some(VarMeta { value, .. }) = graph.values.get(&var) {
} else if let Some(value) = graph.values.get(&var) {
cycle_stack.insert(var.clone());
work_queue_stack.push(Step::LeaveVar(var));
total_nodes += value.total_nodes();
......
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