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 = [ ...@@ -60,6 +60,24 @@ rustflags = [
"-Clink-arg=/Brepro", "-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 # Musl targets: disable static CRT linking. We produce dynamic .node shared
# libraries (N-API addons), not static binaries. # libraries (N-API addons), not static binaries.
[target.'cfg(all(target_os = "linux", target_env = "musl"))'] [target.'cfg(all(target_os = "linux", target_env = "musl"))']
......
...@@ -71,6 +71,7 @@ jobs: ...@@ -71,6 +71,7 @@ jobs:
with: with:
skipInstallBuild: 'yes' skipInstallBuild: 'yes'
stepName: 'build-native' stepName: 'build-native'
uploadNativeArtifact: 'next-swc-linux'
secrets: inherit secrets: inherit
build-native-windows: build-native-windows:
...@@ -83,6 +84,7 @@ jobs: ...@@ -83,6 +84,7 @@ jobs:
stepName: 'build-native-windows' stepName: 'build-native-windows'
runs_on_labels: '["windows","self-hosted","x64"]' runs_on_labels: '["windows","self-hosted","x64"]'
buildNativeTarget: 'x86_64-pc-windows-msvc' buildNativeTarget: 'x86_64-pc-windows-msvc'
uploadNativeArtifact: 'next-swc-windows'
secrets: inherit secrets: inherit
...@@ -698,7 +700,7 @@ jobs: ...@@ -698,7 +700,7 @@ jobs:
test-unit-windows: test-unit-windows:
name: 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' }} if: ${{ needs.changes.outputs.docs-only == 'false' }}
strategy: strategy:
...@@ -866,14 +868,7 @@ jobs: ...@@ -866,14 +868,7 @@ jobs:
test-dev-windows: test-dev-windows:
name: test dev windows name: test dev windows
needs: needs: ['optimize-ci', 'changes', 'build-native-windows', 'build-next']
[
'optimize-ci',
'changes',
'build-native-windows',
'build-native',
'build-next',
]
if: ${{ needs.optimize-ci.outputs.skip == 'false' && needs.changes.outputs.docs-only == 'false' }} if: ${{ needs.optimize-ci.outputs.skip == 'false' && needs.changes.outputs.docs-only == 'false' }}
uses: ./.github/workflows/build_reusable.yml uses: ./.github/workflows/build_reusable.yml
...@@ -896,14 +891,7 @@ jobs: ...@@ -896,14 +891,7 @@ jobs:
test-integration-windows: test-integration-windows:
name: test integration windows name: test integration windows
needs: needs: ['optimize-ci', 'changes', 'build-native-windows', 'build-next']
[
'optimize-ci',
'changes',
'build-native-windows',
'build-native',
'build-next',
]
if: ${{ needs.optimize-ci.outputs.skip == 'false' && needs.changes.outputs.docs-only == 'false' }} if: ${{ needs.optimize-ci.outputs.skip == 'false' && needs.changes.outputs.docs-only == 'false' }}
uses: ./.github/workflows/build_reusable.yml uses: ./.github/workflows/build_reusable.yml
...@@ -926,14 +914,7 @@ jobs: ...@@ -926,14 +914,7 @@ jobs:
test-prod-windows: test-prod-windows:
name: test prod windows name: test prod windows
needs: needs: ['optimize-ci', 'changes', 'build-native-windows', 'build-next']
[
'optimize-ci',
'changes',
'build-native-windows',
'build-native',
'build-next',
]
if: ${{ needs.optimize-ci.outputs.skip == 'false' && needs.changes.outputs.docs-only == 'false' }} if: ${{ needs.optimize-ci.outputs.skip == 'false' && needs.changes.outputs.docs-only == 'false' }}
uses: ./.github/workflows/build_reusable.yml uses: ./.github/workflows/build_reusable.yml
......
...@@ -85,6 +85,12 @@ on: ...@@ -85,6 +85,12 @@ on:
type: string type: string
default: '' 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: browser:
description: 'Browser to use for tests' description: 'Browser to use for tests'
required: false required: false
...@@ -207,6 +213,11 @@ jobs: ...@@ -207,6 +213,11 @@ jobs:
- run: corepack enable - run: corepack enable
- run: pwd - 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 - run: rm -rf .git
- uses: actions/checkout@v4 - uses: actions/checkout@v4
...@@ -232,14 +243,14 @@ jobs: ...@@ -232,14 +243,14 @@ jobs:
# local action -> needs to run after checkout # local action -> needs to run after checkout
- name: Install Rust - name: Install Rust
uses: ./.github/actions/setup-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 - name: Install nextest
if: ${{ inputs.needsNextest == 'yes' }} if: ${{ inputs.needsNextest == 'yes' }}
uses: taiki-e/install-action@3a0adc33ab45d7b9b9da91822dd2b3c0151704be # nextest uses: taiki-e/install-action@3a0adc33ab45d7b9b9da91822dd2b3c0151704be # nextest
- run: rustc --version - 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}" - run: corepack prepare --activate yarn@1.22.19 && npm i -g "@napi-rs/cli@${NAPI_CLI_VERSION}"
...@@ -258,12 +269,29 @@ jobs: ...@@ -258,12 +269,29 @@ jobs:
- name: Start sccache - name: Start sccache
uses: ./.github/actions/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: with:
turbo-token: ${{ secrets.TURBO_REMOTE_CACHE_TOKEN }} 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 }} - 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 - name: Upload next-swc artifact
if: ${{ inputs.uploadSwcArtifact == 'yes' }} 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( ...@@ -30,7 +30,7 @@ pub async fn get_next_client_transforms_rules(
) -> Result<Vec<ModuleRule>> { ) -> Result<Vec<ModuleRule>> {
let mut rules = vec![]; 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 enable_mdx_rs = next_config.mdx_rs().await?.is_some();
let page_extensions: Vec<String> = next_config let page_extensions: Vec<String> = next_config
.page_extensions() .page_extensions()
...@@ -40,14 +40,13 @@ pub async fn get_next_client_transforms_rules( ...@@ -40,14 +40,13 @@ pub async fn get_next_client_transforms_rules(
.collect(); .collect();
if !foreign_code { 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() { if !modularize_imports_config.await?.is_empty() {
rules.push(get_next_modularize_imports_rule( rules.push(
modularize_imports_config, get_next_modularize_imports_rule(modularize_imports_config, enable_mdx_rs).await?,
enable_mdx_rs, );
));
} }
// This is purely a performance optimization: // This is purely a performance optimization:
...@@ -64,11 +63,11 @@ pub async fn get_next_client_transforms_rules( ...@@ -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(); let is_development = mode.await?.is_development();
if 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?; let use_cache_enabled = *next_config.enable_use_cache().await?;
...@@ -78,17 +77,20 @@ pub async fn get_next_client_transforms_rules( ...@@ -78,17 +77,20 @@ pub async fn get_next_client_transforms_rules(
match &context_ty { match &context_ty {
ClientContextType::Pages { pages_dir } => { ClientContextType::Pages { pages_dir } => {
if !foreign_code { if !foreign_code {
rules.push(get_next_pages_transforms_rule( rules.push(
pages_dir.clone(), get_next_pages_transforms_rule(
ExportFilter::StripDataExports, pages_dir.clone(),
enable_mdx_rs, ExportFilter::StripDataExports,
vec![], enable_mdx_rs,
&page_extensions, vec![],
)?); &page_extensions,
rules.push(get_next_disallow_export_all_in_page_rule( )
enable_mdx_rs, .await?,
pages_dir.clone(), );
)); rules.push(
get_next_disallow_export_all_in_page_rule(enable_mdx_rs, pages_dir.clone())
.await?,
);
} }
} }
ClientContextType::App { .. } => { ClientContextType::App { .. } => {
...@@ -109,8 +111,8 @@ pub async fn get_next_client_transforms_rules( ...@@ -109,8 +111,8 @@ pub async fn get_next_client_transforms_rules(
}; };
if !foreign_code { if !foreign_code {
rules.push(get_next_cjs_optimizer_rule(enable_mdx_rs)); rules.push(get_next_cjs_optimizer_rule(enable_mdx_rs).await?);
rules.push(get_next_pure_rule(enable_mdx_rs)); rules.push(get_next_pure_rule(enable_mdx_rs).await?);
rules.push( rules.push(
get_next_dynamic_transform_rule(false, false, is_app_dir, mode, enable_mdx_rs).await?, get_next_dynamic_transform_rule(false, false, is_app_dir, mode, enable_mdx_rs).await?,
......
...@@ -27,7 +27,8 @@ use turbopack_core::{ ...@@ -27,7 +27,8 @@ use turbopack_core::{
}; };
use turbopack_css::chunk::CssChunkType; use turbopack_css::chunk::CssChunkType;
use turbopack_ecmascript::{ 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::{ use turbopack_ecmascript_plugins::transform::directives::{
client::ClientDirectiveTransformer, client_disallowed::ClientDisallowedDirectiveTransformer, client::ClientDirectiveTransformer, client_disallowed::ClientDisallowedDirectiveTransformer,
...@@ -752,17 +753,18 @@ pub async fn get_server_module_options_context( ...@@ -752,17 +753,18 @@ pub async fn get_server_module_options_context(
ecmascript_client_reference_transition_name, ecmascript_client_reference_transition_name,
.. ..
} => { } => {
let client_directive_transformer = ecmascript_client_reference_transition_name.map( let client_directive_transformer =
|ecmascript_client_reference_transition_name| { if let Some(name) = ecmascript_client_reference_transition_name {
get_ecma_transform_rule( Some(get_ecma_transform_rule(
Box::new(ClientDirectiveTransformer::new( client_directive_transform_plugin(name)
ecmascript_client_reference_transition_name, .to_resolved()
)), .await?,
enable_mdx_rs.is_some(), enable_mdx_rs.is_some(),
EcmascriptTransformStage::Preprocess, EcmascriptTransformStage::Preprocess,
) ))
}, } else {
); None
};
foreign_next_server_rules.extend(internal_custom_rules); foreign_next_server_rules.extend(internal_custom_rules);
foreign_next_server_rules.extend(client_directive_transformer.clone()); foreign_next_server_rules.extend(client_directive_transformer.clone());
...@@ -839,9 +841,9 @@ pub async fn get_server_module_options_context( ...@@ -839,9 +841,9 @@ pub async fn get_server_module_options_context(
ecmascript_client_reference_transition_name ecmascript_client_reference_transition_name
{ {
common_next_server_rules.push(get_ecma_transform_rule( common_next_server_rules.push(get_ecma_transform_rule(
Box::new(ClientDirectiveTransformer::new( client_directive_transform_plugin(ecmascript_client_reference_transition_name)
ecmascript_client_reference_transition_name, .to_resolved()
)), .await?,
enable_mdx_rs.is_some(), enable_mdx_rs.is_some(),
EcmascriptTransformStage::Preprocess, EcmascriptTransformStage::Preprocess,
)); ));
...@@ -910,26 +912,28 @@ pub async fn get_server_module_options_context( ...@@ -910,26 +912,28 @@ pub async fn get_server_module_options_context(
app_dir, app_dir,
ecmascript_client_reference_transition_name, ecmascript_client_reference_transition_name,
} => { } => {
let custom_source_transform_rules: Vec<ModuleRule> = vec![ let directive_transform_rule =
if let Some(ecmascript_client_reference_transition_name) = if let Some(name) = ecmascript_client_reference_transition_name {
ecmascript_client_reference_transition_name
{
get_ecma_transform_rule( get_ecma_transform_rule(
Box::new(ClientDirectiveTransformer::new( client_directive_transform_plugin(name)
ecmascript_client_reference_transition_name, .to_resolved()
)), .await?,
enable_mdx_rs.is_some(), enable_mdx_rs.is_some(),
EcmascriptTransformStage::Preprocess, EcmascriptTransformStage::Preprocess,
) )
} else { } else {
get_ecma_transform_rule( get_ecma_transform_rule(
Box::new(ClientDisallowedDirectiveTransformer::new( client_disallowed_directive_transform_plugin(rcstr!(
"next/dist/client/use-client-disallowed.js".to_string(), "next/dist/client/use-client-disallowed.js"
)), ))
.to_resolved()
.await?,
enable_mdx_rs.is_some(), enable_mdx_rs.is_some(),
EcmascriptTransformStage::Preprocess, 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?, 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( ...@@ -993,6 +997,19 @@ pub async fn get_server_module_options_context(
Ok(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)] #[derive(Clone, Debug, PartialEq, Eq, Hash, TaskInput, TraceRawVcs, Encode, Decode)]
pub struct ServerChunkingContextOptions { pub struct ServerChunkingContextOptions {
pub mode: Vc<NextMode>, pub mode: Vc<NextMode>,
......
...@@ -37,7 +37,7 @@ pub async fn get_next_server_transforms_rules( ...@@ -37,7 +37,7 @@ pub async fn get_next_server_transforms_rules(
) -> Result<Vec<ModuleRule>> { ) -> Result<Vec<ModuleRule>> {
let mut rules = vec![]; 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 mdx_rs = next_config.mdx_rs().await?.is_some();
let page_extensions: Vec<String> = next_config let page_extensions: Vec<String> = next_config
.page_extensions() .page_extensions()
...@@ -47,16 +47,13 @@ pub async fn get_next_server_transforms_rules( ...@@ -47,16 +47,13 @@ pub async fn get_next_server_transforms_rules(
.collect(); .collect();
if !foreign_code { 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() { if !modularize_imports_config.await?.is_empty() {
rules.push(get_next_modularize_imports_rule( rules.push(get_next_modularize_imports_rule(modularize_imports_config, mdx_rs).await?);
modularize_imports_config,
mdx_rs,
));
} }
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 { .. }) { if !matches!(context_ty, ServerContextType::AppRSC { .. }) {
rules.extend([ rules.extend([
...@@ -84,19 +81,21 @@ pub async fn get_next_server_transforms_rules( ...@@ -84,19 +81,21 @@ pub async fn get_next_server_transforms_rules(
let is_server_components = match &context_ty { let is_server_components = match &context_ty {
ServerContextType::Pages { pages_dir } | ServerContextType::PagesApi { pages_dir } => { ServerContextType::Pages { pages_dir } | ServerContextType::PagesApi { pages_dir } => {
if !foreign_code { if !foreign_code {
rules.push(get_next_disallow_export_all_in_page_rule( rules.push(
mdx_rs, get_next_disallow_export_all_in_page_rule(mdx_rs, pages_dir.clone()).await?,
pages_dir.clone(), );
)); rules.push(
rules.push(get_next_pages_transforms_rule( get_next_pages_transforms_rule(
pages_dir.clone(), pages_dir.clone(),
ExportFilter::StripDefaultExport, ExportFilter::StripDefaultExport,
mdx_rs, mdx_rs,
vec![RuleCondition::ReferenceType(ReferenceTypeCondition::Entry( vec![RuleCondition::ReferenceType(ReferenceTypeCondition::Entry(
Some(EntryReferenceSubType::PageData), Some(EntryReferenceSubType::PageData),
))], ))],
&page_extensions, &page_extensions,
)?); )
.await?,
);
} }
false false
} }
...@@ -158,10 +157,7 @@ pub async fn get_next_server_transforms_rules( ...@@ -158,10 +157,7 @@ pub async fn get_next_server_transforms_rules(
}; };
if is_app_dir { if is_app_dir {
rules.push(get_next_debug_instant_stack_rule( rules.push(get_next_debug_instant_stack_rule(mdx_rs, page_extensions.clone()).await?);
mdx_rs,
page_extensions.clone(),
));
} }
if is_app_dir && if is_app_dir &&
...@@ -170,7 +166,7 @@ pub async fn get_next_server_transforms_rules( ...@@ -170,7 +166,7 @@ pub async fn get_next_server_transforms_rules(
next_runtime != NextRuntime::Edge && next_runtime != NextRuntime::Edge &&
*next_config.enable_cache_components().await? *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 { if !foreign_code {
...@@ -179,8 +175,8 @@ pub async fn get_next_server_transforms_rules( ...@@ -179,8 +175,8 @@ pub async fn get_next_server_transforms_rules(
.await?, .await?,
); );
rules.push(get_next_cjs_optimizer_rule(mdx_rs)); rules.push(get_next_cjs_optimizer_rule(mdx_rs).await?);
rules.push(get_next_pure_rule(mdx_rs)); rules.push(get_next_pure_rule(mdx_rs).await?);
// [NOTE]: this rule only works in prod config // [NOTE]: this rule only works in prod config
// https://github.com/vercel/next.js/blob/a1d0259ea06592c5ca6df882e9b1d0d0121c5083/packages/next/src/build/swc/options.ts#L409 // 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( ...@@ -194,16 +190,19 @@ pub async fn get_next_server_transforms_rules(
let mode = *mode.await?; let mode = *mode.await?;
if mode == NextMode::Development { 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 { if !foreign_code {
rules.push(next_edge_node_api_assert( rules.push(
mdx_rs, next_edge_node_api_assert(
matches!(context_ty, ServerContextType::Middleware { .. }) mdx_rs,
&& mode == NextMode::Build, matches!(context_ty, ServerContextType::Middleware { .. })
mode == NextMode::Build, && mode == NextMode::Build,
)); mode == NextMode::Build,
)
.await?,
);
} }
if matches!(context_ty, ServerContextType::AppRoute { .. }) { if matches!(context_ty, ServerContextType::AppRoute { .. }) {
...@@ -246,14 +245,14 @@ pub async fn get_next_server_internal_transforms_rules( ...@@ -246,14 +245,14 @@ pub async fn get_next_server_internal_transforms_rules(
match context_ty { match context_ty {
ServerContextType::Pages { .. } => { ServerContextType::Pages { .. } => {
// Apply next/font transforms to foreign code // 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::PagesApi { .. } => {}
ServerContextType::AppSSR { .. } => { ServerContextType::AppSSR { .. } => {
rules.push(get_next_font_transform_rule(mdx_rs)); rules.push(get_next_font_transform_rule(mdx_rs).await?);
} }
ServerContextType::AppRSC { .. } => { ServerContextType::AppRSC { .. } => {
rules.push(get_next_font_transform_rule(mdx_rs)); rules.push(get_next_font_transform_rule(mdx_rs).await?);
} }
ServerContextType::AppRoute { .. } => {} ServerContextType::AppRoute { .. } => {}
ServerContextType::Middleware { .. } => {} ServerContextType::Middleware { .. } => {}
......
...@@ -2,25 +2,32 @@ use anyhow::Result; ...@@ -2,25 +2,32 @@ use anyhow::Result;
use async_trait::async_trait; use async_trait::async_trait;
use next_custom_transforms::transforms::debug_fn_name::debug_fn_name; use next_custom_transforms::transforms::debug_fn_name::debug_fn_name;
use swc_core::ecma::{ast::Program, visit::VisitMutWith}; 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::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 super::module_rule_match_js_no_url;
pub fn get_debug_fn_name_rule(enable_mdx_rs: bool) -> ModuleRule { pub async fn get_debug_fn_name_rule(enable_mdx_rs: bool) -> Result<ModuleRule> {
let debug_fn_name_transform = EcmascriptInputTransform::Plugin(ResolvedVc::cell(Box::new( let debug_fn_name_transform =
DebugFnNameTransformer {}, EcmascriptInputTransform::Plugin(debug_fn_name_transform_plugin().to_resolved().await?);
) as _));
ModuleRule::new( // TODO: use get_ecma_transform_rule instead
Ok(ModuleRule::new(
module_rule_match_js_no_url(enable_mdx_rs), module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::ExtendEcmascriptTransforms { vec![ModuleRuleEffect::ExtendEcmascriptTransforms {
preprocess: ResolvedVc::cell(vec![]), preprocess: ResolvedVc::cell(vec![]),
main: ResolvedVc::cell(vec![]), main: ResolvedVc::cell(vec![]),
postprocess: ResolvedVc::cell(vec![debug_fn_name_transform]), 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)] #[derive(Debug)]
......
use anyhow::Result; use anyhow::Result;
use turbo_tasks::Vc; use turbo_tasks::Vc;
use turbopack::module_options::ModuleRule; use turbopack::module_options::ModuleRule;
use turbopack_ecmascript::{CustomTransformer, TransformPlugin};
use turbopack_ecmascript_plugins::transform::emotion::EmotionTransformer; use turbopack_ecmascript_plugins::transform::emotion::EmotionTransformer;
use super::get_ecma_transform_rule; use super::get_ecma_transform_rule;
...@@ -11,9 +12,37 @@ use crate::{ ...@@ -11,9 +12,37 @@ use crate::{
pub async fn get_emotion_transform_rule(next_config: Vc<NextConfig>) -> Result<Option<ModuleRule>> { 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 enable_mdx_rs = next_config.mdx_rs().await?.is_some();
let module_rule = next_config
let has_config = next_config
.compiler() .compiler()
.await? .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 .emotion
.as_ref() .as_ref()
.and_then(|config| match config { .and_then(|config| match config {
...@@ -23,13 +52,8 @@ pub async fn get_emotion_transform_rule(next_config: Vc<NextConfig>) -> Result<O ...@@ -23,13 +52,8 @@ pub async fn get_emotion_transform_rule(next_config: Vc<NextConfig>) -> Result<O
EmotionTransformOptionsOrBoolean::Options(value) => EmotionTransformer::new(value), EmotionTransformOptionsOrBoolean::Options(value) => EmotionTransformer::new(value),
_ => None, _ => None,
}) })
.map(|transformer| { .context("emotion config must exist")?;
get_ecma_transform_rule( Ok(Vc::cell(
Box::new(transformer), Box::new(transformer) as Box<dyn CustomTransformer + Send + Sync>
enable_mdx_rs, ))
EcmascriptTransformStage::Main,
)
});
Ok(module_rule)
} }
...@@ -35,7 +35,7 @@ use turbo_tasks::ResolvedVc; ...@@ -35,7 +35,7 @@ use turbo_tasks::ResolvedVc;
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use turbopack::module_options::{ModuleRule, ModuleRuleEffect, ModuleType, RuleCondition}; use turbopack::module_options::{ModuleRule, ModuleRuleEffect, ModuleType, RuleCondition};
use turbopack_core::reference_type::ReferenceTypeCondition; use turbopack_core::reference_type::ReferenceTypeCondition;
use turbopack_ecmascript::{CustomTransformer, EcmascriptInputTransform}; use turbopack_ecmascript::{EcmascriptInputTransform, TransformPlugin};
use crate::next_image::{StructuredImageModuleType, module::BlurPlaceholderMode}; use crate::next_image::{StructuredImageModuleType, module::BlurPlaceholderMode};
...@@ -134,11 +134,11 @@ pub(crate) enum EcmascriptTransformStage { ...@@ -134,11 +134,11 @@ pub(crate) enum EcmascriptTransformStage {
/// Create a new module rule for the given ecmatransform, runs against /// Create a new module rule for the given ecmatransform, runs against
/// any ecmascript (with mdx if enabled) except url reference type /// any ecmascript (with mdx if enabled) except url reference type
pub(crate) fn get_ecma_transform_rule( pub(crate) fn get_ecma_transform_rule(
transformer: Box<dyn CustomTransformer + Send + Sync>, transformer: ResolvedVc<TransformPlugin>,
enable_mdx_rs: bool, enable_mdx_rs: bool,
stage: EcmascriptTransformStage, stage: EcmascriptTransformStage,
) -> ModuleRule { ) -> ModuleRule {
let transformer = EcmascriptInputTransform::Plugin(ResolvedVc::cell(transformer as _)); let transformer = EcmascriptInputTransform::Plugin(transformer);
let (preprocess, main, postprocess) = match stage { let (preprocess, main, postprocess) = match stage {
EcmascriptTransformStage::Preprocess => (vec![transformer], vec![], vec![]), EcmascriptTransformStage::Preprocess => (vec![transformer], vec![], vec![]),
EcmascriptTransformStage::Main => (vec![], vec![transformer], vec![]), EcmascriptTransformStage::Main => (vec![], vec![transformer], vec![]),
......
...@@ -6,11 +6,13 @@ use bincode::{Decode, Encode}; ...@@ -6,11 +6,13 @@ use bincode::{Decode, Encode};
use modularize_imports::{Config, PackageConfig, modularize_imports}; use modularize_imports::{Config, PackageConfig, modularize_imports};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use swc_core::ecma::ast::Program; 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::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( #[derive(
Clone, Clone,
...@@ -58,21 +60,35 @@ pub enum Transform { ...@@ -58,21 +60,35 @@ pub enum Transform {
} }
/// Returns a rule which applies the Next.js modularize imports transform. /// Returns a rule which applies the Next.js modularize imports transform.
pub fn get_next_modularize_imports_rule( pub async fn get_next_modularize_imports_rule(
modularize_imports_config: &FxIndexMap<String, ModularizeImportPackageConfig>, modularize_imports_config: Vc<ModularizeImports>,
enable_mdx_rs: bool, enable_mdx_rs: bool,
) -> ModuleRule { ) -> Result<ModuleRule> {
let transformer = EcmascriptInputTransform::Plugin(ResolvedVc::cell(Box::new( let transformer = EcmascriptInputTransform::Plugin(
ModularizeImportsTransformer::new(modularize_imports_config), modularize_imports_transform_plugin(modularize_imports_config)
) as _)); .to_resolved()
ModuleRule::new( .await?,
);
// TODO: use get_ecma_transform_rule instead
Ok(ModuleRule::new(
module_rule_match_js_no_url(enable_mdx_rs), module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::ExtendEcmascriptTransforms { vec![ModuleRuleEffect::ExtendEcmascriptTransforms {
preprocess: ResolvedVc::cell(vec![]), preprocess: ResolvedVc::cell(vec![]),
main: ResolvedVc::cell(vec![]), main: ResolvedVc::cell(vec![]),
postprocess: ResolvedVc::cell(vec![transformer]), 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)] #[derive(Debug)]
......
...@@ -7,13 +7,31 @@ use swc_core::{ ...@@ -7,13 +7,31 @@ use swc_core::{
common::SyntaxContext, common::SyntaxContext,
ecma::{ast::*, visit::VisitMutWith}, ecma::{ast::*, visit::VisitMutWith},
}; };
use turbo_tasks::ResolvedVc; use turbo_tasks::{ResolvedVc, Vc};
use turbopack::module_options::{ModuleRule, ModuleRuleEffect}; 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 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 // [NOTE]: This isn't user configurable config
// (https://github.com/vercel/next.js/blob/a1d0259ea06592c5ca6df882e9b1d0d0121c5083/packages/next/src/build/swc/options.ts#L395) // (https://github.com/vercel/next.js/blob/a1d0259ea06592c5ca6df882e9b1d0d0121c5083/packages/next/src/build/swc/options.ts#L395)
// build it internally without accepting customization. // build it internally without accepting customization.
...@@ -47,18 +65,7 @@ pub fn get_next_cjs_optimizer_rule(enable_mdx_rs: bool) -> ModuleRule { ...@@ -47,18 +65,7 @@ pub fn get_next_cjs_optimizer_rule(enable_mdx_rs: bool) -> ModuleRule {
}, },
)]), )]),
}; };
Vc::cell(Box::new(NextCjsOptimizer { config }) as Box<dyn CustomTransformer + Send + Sync>)
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]),
}],
)
} }
#[derive(Debug)] #[derive(Debug)]
......
...@@ -2,29 +2,39 @@ use anyhow::Result; ...@@ -2,29 +2,39 @@ use anyhow::Result;
use async_trait::async_trait; use async_trait::async_trait;
use next_custom_transforms::transforms::debug_instant_stack::debug_instant_stack; use next_custom_transforms::transforms::debug_instant_stack::debug_instant_stack;
use swc_core::ecma::ast::Program; use swc_core::ecma::ast::Program;
use turbo_tasks::ResolvedVc; use turbo_tasks::{ResolvedVc, Vc};
use turbopack::module_options::{ModuleRule, ModuleRuleEffect}; 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 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, enable_mdx_rs: bool,
page_extensions: Vec<String>, page_extensions: Vec<String>,
) -> ModuleRule { ) -> Result<ModuleRule> {
let transform = let transform = EcmascriptInputTransform::Plugin(
EcmascriptInputTransform::Plugin(ResolvedVc::cell(Box::new(NextDebugInstantStack { next_debug_instant_stack_transform_plugin(page_extensions)
page_extensions, .to_resolved()
}) as _)); .await?,
);
ModuleRule::new( // TODO: use get_ecma_transform_rule instead
Ok(ModuleRule::new(
module_rule_match_js_no_url(enable_mdx_rs), module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::ExtendEcmascriptTransforms { vec![ModuleRuleEffect::ExtendEcmascriptTransforms {
preprocess: ResolvedVc::cell(vec![]), preprocess: ResolvedVc::cell(vec![]),
main: ResolvedVc::cell(vec![]), main: ResolvedVc::cell(vec![]),
postprocess: ResolvedVc::cell(vec![transform]), 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)] #[derive(Debug)]
......
...@@ -2,28 +2,37 @@ use anyhow::Result; ...@@ -2,28 +2,37 @@ use anyhow::Result;
use async_trait::async_trait; use async_trait::async_trait;
use next_custom_transforms::transforms::disallow_re_export_all_in_page::disallow_re_export_all_in_page; use next_custom_transforms::transforms::disallow_re_export_all_in_page::disallow_re_export_all_in_page;
use swc_core::ecma::ast::*; use swc_core::ecma::ast::*;
use turbo_tasks::ResolvedVc; use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use turbopack::module_options::{ModuleRule, ModuleRuleEffect}; 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; 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, enable_mdx_rs: bool,
pages_dir: FileSystemPath, pages_dir: FileSystemPath,
) -> ModuleRule { ) -> Result<ModuleRule> {
let transformer = EcmascriptInputTransform::Plugin(ResolvedVc::cell(Box::new( let transformer = EcmascriptInputTransform::Plugin(
NextDisallowReExportAllInPage, next_disallow_re_export_all_in_page_transform_plugin()
) as _)); .to_resolved()
ModuleRule::new( .await?,
);
Ok(ModuleRule::new(
module_rule_match_pages_page_file(enable_mdx_rs, pages_dir), module_rule_match_pages_page_file(enable_mdx_rs, pages_dir),
vec![ModuleRuleEffect::ExtendEcmascriptTransforms { vec![ModuleRuleEffect::ExtendEcmascriptTransforms {
preprocess: ResolvedVc::cell(vec![]), preprocess: ResolvedVc::cell(vec![]),
main: ResolvedVc::cell(vec![]), main: ResolvedVc::cell(vec![]),
postprocess: ResolvedVc::cell(vec![transformer]), 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)] #[derive(Debug)]
......
...@@ -4,7 +4,9 @@ use next_custom_transforms::transforms::dynamic::{NextDynamicMode, next_dynamic} ...@@ -4,7 +4,9 @@ use next_custom_transforms::transforms::dynamic::{NextDynamicMode, next_dynamic}
use swc_core::{atoms::atom, common::FileName, ecma::ast::Program}; use swc_core::{atoms::atom, common::FileName, ecma::ast::Program};
use turbo_tasks::{ResolvedVc, Vc}; use turbo_tasks::{ResolvedVc, Vc};
use turbopack::module_options::{ModuleRule, ModuleRuleEffect}; 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 super::module_rule_match_js_no_url;
use crate::mode::NextMode; use crate::mode::NextMode;
...@@ -17,13 +19,12 @@ pub async fn get_next_dynamic_transform_rule( ...@@ -17,13 +19,12 @@ pub async fn get_next_dynamic_transform_rule(
mode: Vc<NextMode>, mode: Vc<NextMode>,
enable_mdx_rs: bool, enable_mdx_rs: bool,
) -> Result<ModuleRule> { ) -> Result<ModuleRule> {
let dynamic_transform = let dynamic_transform = EcmascriptInputTransform::Plugin(
EcmascriptInputTransform::Plugin(ResolvedVc::cell(Box::new(NextJsDynamic { next_dynamic_transform_plugin(is_server_compiler, is_react_server_layer, is_app_dir, mode)
is_server_compiler, .to_resolved()
is_react_server_layer, .await?,
is_app_dir, );
mode: *mode.await?, // TODO: use get_ecma_transform_rule instead
}) as _));
Ok(ModuleRule::new( Ok(ModuleRule::new(
module_rule_match_js_no_url(enable_mdx_rs), module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::ExtendEcmascriptTransforms { vec![ModuleRuleEffect::ExtendEcmascriptTransforms {
...@@ -34,6 +35,21 @@ pub async fn get_next_dynamic_transform_rule( ...@@ -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)] #[derive(Debug)]
struct NextJsDynamic { struct NextJsDynamic {
is_server_compiler: bool, is_server_compiler: bool,
...@@ -58,7 +74,6 @@ impl CustomTransformer for NextJsDynamic { ...@@ -58,7 +74,6 @@ impl CustomTransformer for NextJsDynamic {
FileName::Real(ctx.file_path_str.into()).into(), FileName::Real(ctx.file_path_str.into()).into(),
None, None,
)); ));
Ok(()) Ok(())
} }
} }
...@@ -5,30 +5,44 @@ use swc_core::{ ...@@ -5,30 +5,44 @@ use swc_core::{
common::SyntaxContext, common::SyntaxContext,
ecma::{ast::*, utils::ExprCtx, visit::VisitWith}, ecma::{ast::*, utils::ExprCtx, visit::VisitWith},
}; };
use turbo_tasks::ResolvedVc; use turbo_tasks::{ResolvedVc, Vc};
use turbopack::module_options::{ModuleRule, ModuleRuleEffect}; 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 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, enable_mdx_rs: bool,
should_error_for_node_apis: bool, should_error_for_node_apis: bool,
is_production: bool, is_production: bool,
) -> ModuleRule { ) -> Result<ModuleRule> {
let transformer = let transformer = EcmascriptInputTransform::Plugin(
EcmascriptInputTransform::Plugin(ResolvedVc::cell(Box::new(NextEdgeNodeApiAssert { next_edge_node_api_assert_transform_plugin(should_error_for_node_apis, is_production)
should_error_for_node_apis, .to_resolved()
is_production, .await?,
}) as _)); );
ModuleRule::new( // TODO: use get_ecma_transform_rule instead
Ok(ModuleRule::new(
module_rule_match_js_no_url(enable_mdx_rs), module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::ExtendEcmascriptTransforms { vec![ModuleRuleEffect::ExtendEcmascriptTransforms {
preprocess: ResolvedVc::cell(vec![]), preprocess: ResolvedVc::cell(vec![]),
main: ResolvedVc::cell(vec![]), main: ResolvedVc::cell(vec![]),
postprocess: ResolvedVc::cell(vec![transformer]), 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)] #[derive(Debug)]
......
...@@ -5,26 +5,20 @@ use swc_core::{ ...@@ -5,26 +5,20 @@ use swc_core::{
atoms::{Wtf8Atom, atom}, atoms::{Wtf8Atom, atom},
ecma::{ast::Program, visit::VisitMutWith}, ecma::{ast::Program, visit::VisitMutWith},
}; };
use turbo_tasks::ResolvedVc; use turbo_tasks::{ResolvedVc, Vc};
use turbopack::module_options::{ModuleRule, ModuleRuleEffect}; 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 super::module_rule_match_js_no_url;
/// Returns a rule which applies the Next.js font transform. /// Returns a rule which applies the Next.js font transform.
pub fn get_next_font_transform_rule(enable_mdx_rs: bool) -> ModuleRule { pub async fn get_next_font_transform_rule(enable_mdx_rs: bool) -> Result<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(),
];
let transformer = let transformer =
EcmascriptInputTransform::Plugin(ResolvedVc::cell( EcmascriptInputTransform::Plugin(next_font_transform_plugin().to_resolved().await?);
Box::new(NextJsFont { font_loaders }) as _ // TODO: use get_ecma_transform_rule instead
)); Ok(ModuleRule::new(
ModuleRule::new(
// TODO: Only match in pages (not pages/api), app/, etc. // TODO: Only match in pages (not pages/api), app/, etc.
module_rule_match_js_no_url(enable_mdx_rs), module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::ExtendEcmascriptTransforms { vec![ModuleRuleEffect::ExtendEcmascriptTransforms {
...@@ -32,7 +26,18 @@ pub fn get_next_font_transform_rule(enable_mdx_rs: bool) -> ModuleRule { ...@@ -32,7 +26,18 @@ pub fn get_next_font_transform_rule(enable_mdx_rs: bool) -> ModuleRule {
main: ResolvedVc::cell(vec![]), main: ResolvedVc::cell(vec![]),
postprocess: ResolvedVc::cell(vec![transformer]), 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)] #[derive(Debug)]
......
...@@ -2,18 +2,24 @@ use anyhow::Result; ...@@ -2,18 +2,24 @@ use anyhow::Result;
use async_trait::async_trait; use async_trait::async_trait;
use next_custom_transforms::transforms::lint_codemod_comments::lint_codemod_comments; use next_custom_transforms::transforms::lint_codemod_comments::lint_codemod_comments;
use swc_core::ecma::{ast::Program, visit::VisitWith}; use swc_core::ecma::{ast::Program, visit::VisitWith};
use turbo_tasks::Vc;
use turbopack::module_options::ModuleRule; use turbopack::module_options::ModuleRule;
use turbopack_ecmascript::{CustomTransformer, TransformContext}; use turbopack_ecmascript::{CustomTransformer, TransformContext, TransformPlugin};
use super::get_ecma_transform_rule; use super::get_ecma_transform_rule;
use crate::next_shared::transforms::EcmascriptTransformStage; use crate::next_shared::transforms::EcmascriptTransformStage;
pub fn get_next_lint_transform_rule(enable_mdx_rs: bool) -> ModuleRule { pub async fn get_next_lint_transform_rule(enable_mdx_rs: bool) -> Result<ModuleRule> {
get_ecma_transform_rule( Ok(get_ecma_transform_rule(
Box::new(LintTransformer {}), lint_transform_plugin().to_resolved().await?,
enable_mdx_rs, enable_mdx_rs,
EcmascriptTransformStage::Preprocess, EcmascriptTransformStage::Preprocess,
) ))
}
#[turbo_tasks::function]
fn lint_transform_plugin() -> Vc<TransformPlugin> {
Vc::cell(Box::new(LintTransformer {}) as Box<dyn CustomTransformer + Send + Sync>)
} }
#[derive(Debug)] #[derive(Debug)]
...@@ -24,7 +30,6 @@ impl CustomTransformer for LintTransformer { ...@@ -24,7 +30,6 @@ impl CustomTransformer for LintTransformer {
#[tracing::instrument(level = tracing::Level::TRACE, name = "next_custom_lint", skip_all)] #[tracing::instrument(level = tracing::Level::TRACE, name = "next_custom_lint", skip_all)]
async fn transform(&self, program: &mut Program, ctx: &TransformContext<'_>) -> Result<()> { async fn transform(&self, program: &mut Program, ctx: &TransformContext<'_>) -> Result<()> {
program.visit_with(&mut lint_codemod_comments(ctx.comments)); program.visit_with(&mut lint_codemod_comments(ctx.comments));
Ok(()) Ok(())
} }
} }
...@@ -2,24 +2,34 @@ use anyhow::Result; ...@@ -2,24 +2,34 @@ use anyhow::Result;
use async_trait::async_trait; use async_trait::async_trait;
use next_custom_transforms::transforms::middleware_dynamic::next_middleware_dynamic; use next_custom_transforms::transforms::middleware_dynamic::next_middleware_dynamic;
use swc_core::ecma::{ast::*, visit::VisitMutWith}; use swc_core::ecma::{ast::*, visit::VisitMutWith};
use turbo_tasks::ResolvedVc; use turbo_tasks::{ResolvedVc, Vc};
use turbopack::module_options::{ModuleRule, ModuleRuleEffect}; 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 super::module_rule_match_js_no_url;
pub fn get_middleware_dynamic_assert_rule(enable_mdx_rs: bool) -> ModuleRule { pub async fn get_middleware_dynamic_assert_rule(enable_mdx_rs: bool) -> Result<ModuleRule> {
let transformer = EcmascriptInputTransform::Plugin(ResolvedVc::cell(Box::new( let transformer = EcmascriptInputTransform::Plugin(
NextMiddlewareDynamicAssert {}, next_middleware_dynamic_assert_transform_plugin()
) as _)); .to_resolved()
ModuleRule::new( .await?,
);
// TODO: use get_ecma_transform_rule instead
Ok(ModuleRule::new(
module_rule_match_js_no_url(enable_mdx_rs), module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::ExtendEcmascriptTransforms { vec![ModuleRuleEffect::ExtendEcmascriptTransforms {
preprocess: ResolvedVc::cell(vec![]), preprocess: ResolvedVc::cell(vec![]),
main: ResolvedVc::cell(vec![]), main: ResolvedVc::cell(vec![]),
postprocess: ResolvedVc::cell(vec![transformer]), 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)] #[derive(Debug)]
......
...@@ -2,29 +2,39 @@ use anyhow::Result; ...@@ -2,29 +2,39 @@ use anyhow::Result;
use async_trait::async_trait; use async_trait::async_trait;
use next_custom_transforms::transforms::optimize_server_react::{Config, optimize_server_react}; use next_custom_transforms::transforms::optimize_server_react::{Config, optimize_server_react};
use swc_core::ecma::ast::*; use swc_core::ecma::ast::*;
use turbo_tasks::ResolvedVc; use turbo_tasks::{ResolvedVc, Vc};
use turbopack::module_options::{ModuleRule, ModuleRuleEffect}; 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 super::module_rule_match_js_no_url;
#[allow(dead_code)] #[allow(dead_code)]
pub fn get_next_optimize_server_react_rule( pub async fn get_next_optimize_server_react_rule(
enable_mdx_rs: bool, enable_mdx_rs: bool,
optimize_use_state: bool, optimize_use_state: bool,
) -> ModuleRule { ) -> Result<ModuleRule> {
let transformer = let transformer = EcmascriptInputTransform::Plugin(
EcmascriptInputTransform::Plugin(ResolvedVc::cell(Box::new(NextOptimizeServerReact { next_optimize_server_react_transform_plugin(optimize_use_state)
optimize_use_state, .to_resolved()
}) as _)); .await?,
ModuleRule::new( );
// TODO: use get_ecma_transform_rule instead
Ok(ModuleRule::new(
module_rule_match_js_no_url(enable_mdx_rs), module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::ExtendEcmascriptTransforms { vec![ModuleRuleEffect::ExtendEcmascriptTransforms {
preprocess: ResolvedVc::cell(vec![]), preprocess: ResolvedVc::cell(vec![]),
main: ResolvedVc::cell(vec![]), main: ResolvedVc::cell(vec![]),
postprocess: ResolvedVc::cell(vec![transformer]), 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)] #[derive(Debug)]
......
...@@ -2,23 +2,31 @@ use anyhow::Result; ...@@ -2,23 +2,31 @@ use anyhow::Result;
use async_trait::async_trait; use async_trait::async_trait;
use next_custom_transforms::transforms::pure::pure_magic; use next_custom_transforms::transforms::pure::pure_magic;
use swc_core::ecma::{ast::*, visit::VisitMutWith}; use swc_core::ecma::{ast::*, visit::VisitMutWith};
use turbo_tasks::ResolvedVc; use turbo_tasks::{ResolvedVc, Vc};
use turbopack::module_options::{ModuleRule, ModuleRuleEffect}; 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 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 = let transformer =
EcmascriptInputTransform::Plugin(ResolvedVc::cell(Box::new(NextPure {}) as _)); EcmascriptInputTransform::Plugin(next_pure_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), module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::ExtendEcmascriptTransforms { vec![ModuleRuleEffect::ExtendEcmascriptTransforms {
preprocess: ResolvedVc::cell(vec![]), preprocess: ResolvedVc::cell(vec![]),
main: ResolvedVc::cell(vec![]), main: ResolvedVc::cell(vec![]),
postprocess: ResolvedVc::cell(vec![transformer]), 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)] #[derive(Debug)]
......
...@@ -8,7 +8,7 @@ use swc_core::{ ...@@ -8,7 +8,7 @@ use swc_core::{
use turbo_tasks::Vc; use turbo_tasks::Vc;
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use turbopack::module_options::ModuleRule; use turbopack::module_options::ModuleRule;
use turbopack_ecmascript::{CustomTransformer, TransformContext}; use turbopack_ecmascript::{CustomTransformer, TransformContext, TransformPlugin};
use super::get_ecma_transform_rule; use super::get_ecma_transform_rule;
use crate::{next_config::NextConfig, next_shared::transforms::EcmascriptTransformStage}; use crate::{next_config::NextConfig, next_shared::transforms::EcmascriptTransformStage};
...@@ -44,47 +44,48 @@ pub async fn get_next_react_server_components_transform_rule( ...@@ -44,47 +44,48 @@ pub async fn get_next_react_server_components_transform_rule(
.map(|s| s.to_string()) .map(|s| s.to_string())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
Ok(get_ecma_transform_rule( Ok(get_ecma_transform_rule(
Box::new(NextJsReactServerComponents::new( next_react_server_components_transform_plugin(
is_react_server_layer, is_react_server_layer,
cache_components_enabled, cache_components_enabled,
use_cache_enabled, use_cache_enabled,
taint_enabled, taint_enabled,
app_dir, app_dir,
page_extensions, page_extensions,
)), )
.to_resolved()
.await?,
enable_mdx_rs, enable_mdx_rs,
EcmascriptTransformStage::Preprocess, EcmascriptTransformStage::Preprocess,
)) ))
} }
#[derive(Debug)] #[turbo_tasks::function]
struct NextJsReactServerComponents { fn next_react_server_components_transform_plugin(
is_react_server_layer: bool, is_react_server_layer: bool,
cache_components_enabled: bool, cache_components_enabled: bool,
use_cache_enabled: bool, use_cache_enabled: bool,
taint_enabled: bool, taint_enabled: bool,
app_dir: Option<FileSystemPath>, app_dir: Option<FileSystemPath>,
page_extensions: Vec<String>, 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 { #[derive(Debug)]
fn new( struct NextJsReactServerComponents {
is_react_server_layer: bool, is_react_server_layer: bool,
cache_components_enabled: bool, cache_components_enabled: bool,
use_cache_enabled: bool, use_cache_enabled: bool,
taint_enabled: bool, taint_enabled: bool,
app_dir: Option<FileSystemPath>, app_dir: Option<FileSystemPath>,
page_extensions: Vec<String>, page_extensions: Vec<String>,
) -> Self {
Self {
is_react_server_layer,
cache_components_enabled,
use_cache_enabled,
taint_enabled,
app_dir,
page_extensions,
}
}
} }
#[async_trait] #[async_trait]
......
use anyhow::Result; use anyhow::Result;
use async_trait::async_trait; use async_trait::async_trait;
use bincode::{Decode, Encode};
use next_custom_transforms::transforms::strip_page_exports::{ use next_custom_transforms::transforms::strip_page_exports::{
ExportFilter, next_transform_strip_page_exports, ExportFilter, next_transform_strip_page_exports,
}; };
use swc_core::ecma::ast::Program; use swc_core::ecma::ast::Program;
use turbo_tasks::ResolvedVc; use turbo_tasks::{ResolvedVc, TaskInput, Vc, trace::TraceRawVcs};
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use turbopack::module_options::{ModuleRule, ModuleRuleEffect, RuleCondition}; 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; 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. /// 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, pages_dir: FileSystemPath,
export_filter: ExportFilter, export_filter: ExportFilter,
enable_mdx_rs: bool, enable_mdx_rs: bool,
...@@ -20,10 +48,11 @@ pub fn get_next_pages_transforms_rule( ...@@ -20,10 +48,11 @@ pub fn get_next_pages_transforms_rule(
page_extensions: &[String], page_extensions: &[String],
) -> Result<ModuleRule> { ) -> Result<ModuleRule> {
// Apply the Next SSG transform to all pages. // Apply the Next SSG transform to all pages.
let strip_transform = let strip_transform = EcmascriptInputTransform::Plugin(
EcmascriptInputTransform::Plugin(ResolvedVc::cell(Box::new(NextJsStripPageExports { next_strip_page_exports_transform_plugin(export_filter.into())
export_filter, .to_resolved()
}) as _)); .await?,
);
let document_exclusions: Vec<RuleCondition> = page_extensions let document_exclusions: Vec<RuleCondition> = page_extensions
.iter() .iter()
.map(|ext| { .map(|ext| {
...@@ -53,6 +82,15 @@ pub fn get_next_pages_transforms_rule( ...@@ -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)] #[derive(Debug)]
struct NextJsStripPageExports { struct NextJsStripPageExports {
export_filter: ExportFilter, export_filter: ExportFilter,
......
...@@ -2,18 +2,26 @@ use anyhow::Result; ...@@ -2,18 +2,26 @@ use anyhow::Result;
use async_trait::async_trait; use async_trait::async_trait;
use next_custom_transforms::transforms::track_dynamic_imports::*; use next_custom_transforms::transforms::track_dynamic_imports::*;
use swc_core::ecma::ast::Program; use swc_core::ecma::ast::Program;
use turbo_tasks::Vc;
use turbopack::module_options::ModuleRule; use turbopack::module_options::ModuleRule;
use turbopack_ecmascript::{CustomTransformer, TransformContext}; use turbopack_ecmascript::{CustomTransformer, TransformContext, TransformPlugin};
use super::get_ecma_transform_rule; use super::get_ecma_transform_rule;
use crate::next_shared::transforms::EcmascriptTransformStage; use crate::next_shared::transforms::EcmascriptTransformStage;
pub fn get_next_track_dynamic_imports_transform_rule(mdx_rs: bool) -> ModuleRule { pub async fn get_next_track_dynamic_imports_transform_rule(mdx_rs: bool) -> Result<ModuleRule> {
get_ecma_transform_rule( Ok(get_ecma_transform_rule(
Box::new(NextTrackDynamicImports {}), next_track_dynamic_imports_transform_plugin()
.to_resolved()
.await?,
mdx_rs, mdx_rs,
EcmascriptTransformStage::Postprocess, 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)] #[derive(Debug)]
......
...@@ -3,7 +3,7 @@ use async_trait::async_trait; ...@@ -3,7 +3,7 @@ use async_trait::async_trait;
use swc_core::ecma::ast::Program; use swc_core::ecma::ast::Program;
use turbo_tasks::Vc; use turbo_tasks::Vc;
use turbopack::module_options::ModuleRule; use turbopack::module_options::ModuleRule;
use turbopack_ecmascript::{CustomTransformer, TransformContext}; use turbopack_ecmascript::{CustomTransformer, TransformContext, TransformPlugin};
use super::get_ecma_transform_rule; use super::get_ecma_transform_rule;
use crate::{ use crate::{
...@@ -17,7 +17,33 @@ pub async fn get_react_remove_properties_transform_rule( ...@@ -17,7 +17,33 @@ pub async fn get_react_remove_properties_transform_rule(
) -> Result<Option<ModuleRule>> { ) -> Result<Option<ModuleRule>> {
let enable_mdx_rs = next_config.mdx_rs().await?.is_some(); 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() .compiler()
.await? .await?
.react_remove_properties .react_remove_properties
...@@ -33,15 +59,11 @@ pub async fn get_react_remove_properties_transform_rule( ...@@ -33,15 +59,11 @@ pub async fn get_react_remove_properties_transform_rule(
}), }),
), ),
}) })
.map(|config| { .context("react_remove_properties config must exist")?;
get_ecma_transform_rule( Ok(Vc::cell(
Box::new(ReactRemovePropertiesTransformer { config }), Box::new(ReactRemovePropertiesTransformer { config })
enable_mdx_rs, as Box<dyn CustomTransformer + Send + Sync>,
EcmascriptTransformStage::Preprocess, ))
)
});
Ok(module_rule)
} }
#[derive(Debug)] #[derive(Debug)]
......
...@@ -2,6 +2,7 @@ use anyhow::Result; ...@@ -2,6 +2,7 @@ use anyhow::Result;
use turbo_tasks::Vc; use turbo_tasks::Vc;
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use turbopack::module_options::ModuleRule; use turbopack::module_options::ModuleRule;
use turbopack_ecmascript::{CustomTransformer, TransformPlugin};
use turbopack_ecmascript_plugins::transform::relay::RelayTransformer; use turbopack_ecmascript_plugins::transform::relay::RelayTransformer;
use crate::{ use crate::{
...@@ -15,13 +16,30 @@ pub async fn get_relay_transform_rule( ...@@ -15,13 +16,30 @@ pub async fn get_relay_transform_rule(
project_path: FileSystemPath, project_path: FileSystemPath,
) -> Result<Option<ModuleRule>> { ) -> Result<Option<ModuleRule>> {
let enable_mdx_rs = next_config.mdx_rs().await?.is_some(); let enable_mdx_rs = next_config.mdx_rs().await?.is_some();
let module_rule = next_config.compiler().await?.relay.as_ref().map(|config| { if next_config.compiler().await?.relay.is_some() {
get_ecma_transform_rule( let plugin = relay_transform_plugin(next_config, project_path)
Box::new(RelayTransformer::new(config, &project_path)), .to_resolved()
.await?;
Ok(Some(get_ecma_transform_rule(
plugin,
enable_mdx_rs, enable_mdx_rs,
EcmascriptTransformStage::Preprocess, 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; ...@@ -3,7 +3,7 @@ use async_trait::async_trait;
use swc_core::{common::SyntaxContext, ecma::ast::Program}; use swc_core::{common::SyntaxContext, ecma::ast::Program};
use turbo_tasks::Vc; use turbo_tasks::Vc;
use turbopack::module_options::ModuleRule; use turbopack::module_options::ModuleRule;
use turbopack_ecmascript::{CustomTransformer, TransformContext}; use turbopack_ecmascript::{CustomTransformer, TransformContext, TransformPlugin};
use super::get_ecma_transform_rule; use super::get_ecma_transform_rule;
use crate::{ use crate::{
...@@ -17,7 +17,33 @@ pub async fn get_remove_console_transform_rule( ...@@ -17,7 +17,33 @@ pub async fn get_remove_console_transform_rule(
) -> Result<Option<ModuleRule>> { ) -> Result<Option<ModuleRule>> {
let enable_mdx_rs = next_config.mdx_rs().await?.is_some(); 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() .compiler()
.await? .await?
.remove_console .remove_console
...@@ -36,15 +62,10 @@ pub async fn get_remove_console_transform_rule( ...@@ -36,15 +62,10 @@ pub async fn get_remove_console_transform_rule(
}, },
)), )),
}) })
.map(|config| { .context("remove_console config must exist")?;
get_ecma_transform_rule( Ok(Vc::cell(
Box::new(RemoveConsoleTransformer { config }), Box::new(RemoveConsoleTransformer { config }) as Box<dyn CustomTransformer + Send + Sync>
enable_mdx_rs, ))
EcmascriptTransformStage::Preprocess,
)
});
Ok(module_rule)
} }
#[derive(Debug)] #[derive(Debug)]
......
use anyhow::Result; use anyhow::Result;
use async_trait::async_trait; use async_trait::async_trait;
use bincode::{Decode, Encode};
use next_custom_transforms::transforms::server_actions::{ use next_custom_transforms::transforms::server_actions::{
Config, ServerActionsMode, server_actions, Config, ServerActionsMode, server_actions,
}; };
use swc_core::{common::FileName, ecma::ast::Program}; use swc_core::{common::FileName, ecma::ast::Program};
use turbo_rcstr::RcStr; 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::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 super::module_rule_match_js_no_url;
use crate::{mode::NextMode, next_config::CacheKinds}; use crate::{mode::NextMode, next_config::CacheKinds};
#[derive(Debug)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, TaskInput, TraceRawVcs, Encode, Decode)]
pub enum ActionsTransform { pub enum ActionsTransform {
/// Browser and SSR /// Browser and SSR
Client, Client,
...@@ -29,14 +32,18 @@ pub async fn get_server_actions_transform_rule( ...@@ -29,14 +32,18 @@ pub async fn get_server_actions_transform_rule(
use_cache_enabled: bool, use_cache_enabled: bool,
cache_kinds: ResolvedVc<CacheKinds>, cache_kinds: ResolvedVc<CacheKinds>,
) -> Result<ModuleRule> { ) -> Result<ModuleRule> {
let transformer = let transformer = EcmascriptInputTransform::Plugin(
EcmascriptInputTransform::Plugin(ResolvedVc::cell(Box::new(NextServerActions { next_server_actions_transform_plugin(
mode: *mode.await?, mode,
transform, transform,
encryption_key, *encryption_key,
use_cache_enabled, use_cache_enabled,
cache_kinds, *cache_kinds,
}) as _)); )
.to_resolved()
.await?,
);
// TODO: use get_ecma_transform_rule instead
Ok(ModuleRule::new( Ok(ModuleRule::new(
module_rule_match_js_no_url(enable_mdx_rs), module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::ExtendEcmascriptTransforms { vec![ModuleRuleEffect::ExtendEcmascriptTransforms {
...@@ -47,9 +54,26 @@ pub async fn get_server_actions_transform_rule( ...@@ -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)] #[derive(Debug)]
struct NextServerActions { struct NextServerActions {
transform: ActionsTransform, is_react_server_layer: bool,
encryption_key: ResolvedVc<RcStr>, encryption_key: ResolvedVc<RcStr>,
use_cache_enabled: bool, use_cache_enabled: bool,
cache_kinds: ResolvedVc<CacheKinds>, cache_kinds: ResolvedVc<CacheKinds>,
...@@ -64,7 +88,7 @@ impl CustomTransformer for NextServerActions { ...@@ -64,7 +88,7 @@ impl CustomTransformer for NextServerActions {
&FileName::Real(ctx.file_path_str.into()), &FileName::Real(ctx.file_path_str.into()),
Some(ctx.query_str.clone()), Some(ctx.query_str.clone()),
Config { 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(), is_development: self.mode.is_development(),
use_cache_enabled: self.use_cache_enabled, use_cache_enabled: self.use_cache_enabled,
hash_salt: self.encryption_key.await?.to_string(), hash_salt: self.encryption_key.await?.to_string(),
......
use anyhow::Result; use anyhow::Result;
use turbo_tasks::Vc; use turbo_tasks::Vc;
use turbopack::module_options::ModuleRule; use turbopack::module_options::ModuleRule;
use turbopack_ecmascript::{CustomTransformer, TransformPlugin};
use turbopack_ecmascript_plugins::transform::styled_components::StyledComponentsTransformer; use turbopack_ecmascript_plugins::transform::styled_components::StyledComponentsTransformer;
use crate::{ use crate::{
...@@ -13,9 +14,39 @@ pub async fn get_styled_components_transform_rule( ...@@ -13,9 +14,39 @@ pub async fn get_styled_components_transform_rule(
) -> Result<Option<ModuleRule>> { ) -> Result<Option<ModuleRule>> {
let enable_mdx_rs = next_config.mdx_rs().await?.is_some(); let enable_mdx_rs = next_config.mdx_rs().await?.is_some();
let module_rule = next_config let has_config = next_config
.compiler() .compiler()
.await? .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 .styled_components
.as_ref() .as_ref()
.and_then(|config| match config { .and_then(|config| match config {
...@@ -27,13 +58,8 @@ pub async fn get_styled_components_transform_rule( ...@@ -27,13 +58,8 @@ pub async fn get_styled_components_transform_rule(
} }
_ => None, _ => None,
}) })
.map(|transformer| { .context("styled_components config must exist")?;
get_ecma_transform_rule( Ok(Vc::cell(
Box::new(transformer), Box::new(transformer) as Box<dyn CustomTransformer + Send + Sync>
enable_mdx_rs, ))
EcmascriptTransformStage::Main,
)
});
Ok(module_rule)
} }
...@@ -2,6 +2,7 @@ use anyhow::Result; ...@@ -2,6 +2,7 @@ use anyhow::Result;
use turbo_tasks::Vc; use turbo_tasks::Vc;
use turbopack::module_options::ModuleRule; use turbopack::module_options::ModuleRule;
use turbopack_core::environment::RuntimeVersions; use turbopack_core::environment::RuntimeVersions;
use turbopack_ecmascript::{CustomTransformer, TransformPlugin};
use turbopack_ecmascript_plugins::transform::styled_jsx::StyledJsxTransformer; use turbopack_ecmascript_plugins::transform::styled_jsx::StyledJsxTransformer;
use super::get_ecma_transform_rule; use super::get_ecma_transform_rule;
...@@ -13,12 +14,22 @@ pub async fn get_styled_jsx_transform_rule( ...@@ -13,12 +14,22 @@ pub async fn get_styled_jsx_transform_rule(
target_browsers: Vc<RuntimeVersions>, target_browsers: Vc<RuntimeVersions>,
) -> Result<Option<ModuleRule>> { ) -> Result<Option<ModuleRule>> {
let enable_mdx_rs = next_config.mdx_rs().await?.is_some(); let enable_mdx_rs = next_config.mdx_rs().await?.is_some();
let versions = *target_browsers.await?; let plugin = styled_jsx_transform_plugin(target_browsers)
.to_resolved()
let transformer = StyledJsxTransformer::new(versions); .await?;
Ok(Some(get_ecma_transform_rule( Ok(Some(get_ecma_transform_rule(
Box::new(transformer), plugin,
enable_mdx_rs, enable_mdx_rs,
EcmascriptTransformStage::Main, 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 anyhow::Result;
use bincode::{Decode, Encode};
use serde::{Deserialize, Serialize};
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::Vc; use turbo_tasks::{ResolvedVc, Vc, trace::TraceRawVcs};
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use turbopack::module_options::ModuleRule; use turbopack::module_options::ModuleRule;
use turbopack_core::{context::AssetContext, resolve::origin::ResolveOrigin}; use turbopack_core::{context::AssetContext, resolve::origin::ResolveOrigin};
use turbopack_ecmascript::{CustomTransformer, TransformPlugin};
use crate::next_config::NextConfig; 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( pub async fn get_swc_ecma_transform_plugin_rule(
next_config: Vc<NextConfig>, next_config: Vc<NextConfig>,
project_path: FileSystemPath, project_path: FileSystemPath,
...@@ -61,9 +87,7 @@ pub async fn get_swc_ecma_transform_rule_impl( ...@@ -61,9 +87,7 @@ pub async fn get_swc_ecma_transform_rule_impl(
reference_type::{CommonJsReferenceSubType, ReferenceType}, reference_type::{CommonJsReferenceSubType, ReferenceType},
resolve::{ResolveErrorMode, error::handle_resolve_error, parse::Request, resolve}, resolve::{ResolveErrorMode, error::handle_resolve_error, parse::Request, resolve},
}; };
use turbopack_ecmascript_plugins::transform::swc_ecma_transform_plugins::{ use turbopack_ecmascript_plugins::transform::swc_ecma_transform_plugins::SwcPluginModule;
SwcEcmaTransformPluginsTransformer, SwcPluginModule,
};
use turbopack_resolve::{ use turbopack_resolve::{
resolve::resolve_options, resolve_options_context::ResolveOptionsContext, resolve::resolve_options, resolve_options_context::ResolveOptionsContext,
}; };
...@@ -134,7 +158,7 @@ pub async fn get_swc_ecma_transform_rule_impl( ...@@ -134,7 +158,7 @@ pub async fn get_swc_ecma_transform_rule_impl(
Ok(Some(( Ok(Some((
SwcPluginModule::new(name.clone(), file.content().to_bytes().to_vec()) SwcPluginModule::new(name.clone(), file.content().to_bytes().to_vec())
.resolved_cell(), .resolved_cell(),
config.clone(), JsonValue(config.clone()),
))) )))
} }
}) })
...@@ -142,8 +166,25 @@ pub async fn get_swc_ecma_transform_rule_impl( ...@@ -142,8 +166,25 @@ pub async fn get_swc_ecma_transform_rule_impl(
.await?; .await?;
Ok(Some(get_ecma_transform_rule( Ok(Some(get_ecma_transform_rule(
Box::new(SwcEcmaTransformPluginsTransformer::new(plugins)), swc_ecma_transform_plugins_transform_plugin(plugins)
.to_resolved()
.await?,
enable_mdx_rs, enable_mdx_rs,
EcmascriptTransformStage::Main, 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( ...@@ -393,6 +393,9 @@ pub async fn parse_segment_config_from_source(
EcmascriptModuleAssetType::Ecmascript EcmascriptModuleAssetType::Ecmascript
}, },
EcmascriptInputTransforms::empty(), 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,
false, false,
) )
......
...@@ -13,11 +13,13 @@ related: ...@@ -13,11 +13,13 @@ related:
- app/guides/prefetching - 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. 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). 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. > **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 ...@@ -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. 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} ```ts filename="next.config.ts" highlight={5-7}
import type { NextConfig } from 'next' import type { NextConfig } from 'next'
...@@ -100,12 +104,12 @@ const nextConfig: NextConfig = { ...@@ -100,12 +104,12 @@ const nextConfig: NextConfig = {
export default 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. - **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. - **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. 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 ...@@ -122,7 +126,7 @@ This is also why client-side hooks behave differently. `useSearchParams()` suspe
</details> </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: 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 ...@@ -149,7 +153,7 @@ There is no need to write an `instant()` test for every navigation. Build-time v
## Fixing a navigation that blocks ## 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" ```tsx filename="app/shop/[slug]/page.tsx"
import { db } from '@/lib/db' import { db } from '@/lib/db'
...@@ -190,9 +194,11 @@ On an initial page load, the root `<Suspense>` catches the async work and stream ...@@ -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. 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 ### 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} ```tsx filename="app/shop/[slug]/page.tsx" highlight={3-6}
import { db } from '@/lib/db' import { db } from '@/lib/db'
...@@ -216,20 +222,49 @@ export default async function ProductPage(props: PageProps<'/shop/[slug]'>) { ...@@ -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`. ```tsx filename="app/shop/[slug]/page.tsx"
- **Inventory** must be fresh each request. Leave it uncached and let it stream behind a `<Suspense>` fallback. 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 { Suspense } from 'react'
import { db } from '@/lib/db' import { db } from '@/lib/db'
...@@ -250,30 +285,6 @@ export default function ProductPage(props: PageProps<'/shop/[slug]'>) { ...@@ -250,30 +285,6 @@ export default function ProductPage(props: PageProps<'/shop/[slug]'>) {
</div> </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. 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 @@ ...@@ -15,5 +15,5 @@
"registry": "https://registry.npmjs.org/" "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", "name": "create-next-app",
"version": "16.2.1-canary.42", "version": "16.2.1-canary.44",
"keywords": [ "keywords": [
"react", "react",
"next", "next",
......
{ {
"name": "eslint-config-next", "name": "eslint-config-next",
"version": "16.2.1-canary.42", "version": "16.2.1-canary.44",
"description": "ESLint configuration used by Next.js.", "description": "ESLint configuration used by Next.js.",
"license": "MIT", "license": "MIT",
"repository": { "repository": {
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
"dist" "dist"
], ],
"dependencies": { "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-node": "^0.3.6",
"eslint-import-resolver-typescript": "^3.5.2", "eslint-import-resolver-typescript": "^3.5.2",
"eslint-plugin-import": "^2.32.0", "eslint-plugin-import": "^2.32.0",
......
{ {
"name": "@next/eslint-plugin-internal", "name": "@next/eslint-plugin-internal",
"private": true, "private": true,
"version": "16.2.1-canary.42", "version": "16.2.1-canary.44",
"description": "ESLint plugin for working on Next.js.", "description": "ESLint plugin for working on Next.js.",
"exports": { "exports": {
".": "./src/eslint-plugin-internal.js" ".": "./src/eslint-plugin-internal.js"
......
{ {
"name": "@next/eslint-plugin-next", "name": "@next/eslint-plugin-next",
"version": "16.2.1-canary.42", "version": "16.2.1-canary.44",
"description": "ESLint plugin for Next.js.", "description": "ESLint plugin for Next.js.",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",
......
{ {
"name": "@next/font", "name": "@next/font",
"private": true, "private": true,
"version": "16.2.1-canary.42", "version": "16.2.1-canary.44",
"repository": { "repository": {
"url": "vercel/next.js", "url": "vercel/next.js",
"directory": "packages/font" "directory": "packages/font"
......
{ {
"name": "@next/bundle-analyzer", "name": "@next/bundle-analyzer",
"version": "16.2.1-canary.42", "version": "16.2.1-canary.44",
"main": "index.js", "main": "index.js",
"types": "index.d.ts", "types": "index.d.ts",
"license": "MIT", "license": "MIT",
......
{ {
"name": "@next/codemod", "name": "@next/codemod",
"version": "16.2.1-canary.42", "version": "16.2.1-canary.44",
"license": "MIT", "license": "MIT",
"repository": { "repository": {
"type": "git", "type": "git",
......
{ {
"name": "@next/env", "name": "@next/env",
"version": "16.2.1-canary.42", "version": "16.2.1-canary.44",
"keywords": [ "keywords": [
"react", "react",
"next", "next",
......
{ {
"name": "@next/mdx", "name": "@next/mdx",
"version": "16.2.1-canary.42", "version": "16.2.1-canary.44",
"main": "index.js", "main": "index.js",
"license": "MIT", "license": "MIT",
"repository": { "repository": {
......
{ {
"name": "@next/playwright", "name": "@next/playwright",
"version": "16.2.1-canary.42", "version": "16.2.1-canary.44",
"repository": { "repository": {
"url": "vercel/next.js", "url": "vercel/next.js",
"directory": "packages/next-playwright" "directory": "packages/next-playwright"
......
{ {
"name": "@next/plugin-storybook", "name": "@next/plugin-storybook",
"version": "16.2.1-canary.42", "version": "16.2.1-canary.44",
"repository": { "repository": {
"url": "vercel/next.js", "url": "vercel/next.js",
"directory": "packages/next-plugin-storybook" "directory": "packages/next-plugin-storybook"
......
{ {
"name": "@next/polyfill-module", "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+)", "description": "A standard library polyfill for ES Modules supporting browsers (Edge 16+, Firefox 60+, Chrome 61+, Safari 10.1+)",
"main": "dist/polyfill-module.js", "main": "dist/polyfill-module.js",
"license": "MIT", "license": "MIT",
......
{ {
"name": "@next/polyfill-nomodule", "name": "@next/polyfill-nomodule",
"version": "16.2.1-canary.42", "version": "16.2.1-canary.44",
"description": "A polyfill for non-dead, nomodule browsers.", "description": "A polyfill for non-dead, nomodule browsers.",
"main": "dist/polyfill-nomodule.js", "main": "dist/polyfill-nomodule.js",
"license": "MIT", "license": "MIT",
......
{ {
"name": "@next/routing", "name": "@next/routing",
"version": "16.2.1-canary.42", "version": "16.2.1-canary.44",
"keywords": [ "keywords": [
"react", "react",
"next", "next",
......
{ {
"name": "next-rspack", "name": "next-rspack",
"version": "16.2.1-canary.42", "version": "16.2.1-canary.44",
"repository": { "repository": {
"url": "vercel/next.js", "url": "vercel/next.js",
"directory": "packages/next-rspack" "directory": "packages/next-rspack"
......
{ {
"name": "@next/swc", "name": "@next/swc",
"version": "16.2.1-canary.42", "version": "16.2.1-canary.44",
"private": true, "private": true,
"files": [ "files": [
"native/" "native/"
......
...@@ -1160,5 +1160,6 @@ ...@@ -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.", "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\\`.", "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.", "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", "name": "next",
"version": "16.2.1-canary.42", "version": "16.2.1-canary.44",
"description": "The React Framework", "description": "The React Framework",
"main": "./dist/server/next.js", "main": "./dist/server/next.js",
"license": "MIT", "license": "MIT",
...@@ -101,7 +101,7 @@ ...@@ -101,7 +101,7 @@
] ]
}, },
"dependencies": { "dependencies": {
"@next/env": "16.2.1-canary.42", "@next/env": "16.2.1-canary.44",
"@swc/helpers": "0.5.15", "@swc/helpers": "0.5.15",
"baseline-browser-mapping": "^2.9.19", "baseline-browser-mapping": "^2.9.19",
"caniuse-lite": "^1.0.30001579", "caniuse-lite": "^1.0.30001579",
...@@ -165,11 +165,11 @@ ...@@ -165,11 +165,11 @@
"@modelcontextprotocol/sdk": "1.18.1", "@modelcontextprotocol/sdk": "1.18.1",
"@mswjs/interceptors": "0.23.0", "@mswjs/interceptors": "0.23.0",
"@napi-rs/triples": "1.2.0", "@napi-rs/triples": "1.2.0",
"@next/font": "16.2.1-canary.42", "@next/font": "16.2.1-canary.44",
"@next/polyfill-module": "16.2.1-canary.42", "@next/polyfill-module": "16.2.1-canary.44",
"@next/polyfill-nomodule": "16.2.1-canary.42", "@next/polyfill-nomodule": "16.2.1-canary.44",
"@next/react-refresh-utils": "16.2.1-canary.42", "@next/react-refresh-utils": "16.2.1-canary.44",
"@next/swc": "16.2.1-canary.42", "@next/swc": "16.2.1-canary.44",
"@opentelemetry/api": "1.6.0", "@opentelemetry/api": "1.6.0",
"@playwright/test": "1.58.2", "@playwright/test": "1.58.2",
"@rspack/core": "1.6.7", "@rspack/core": "1.6.7",
......
...@@ -604,7 +604,7 @@ internal ...@@ -604,7 +604,7 @@ internal
.addOption( .addOption(
new Option( new Option(
'--mcp-port <mcpPort>', '--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) ).argParser(parseValidPositiveInteger)
) )
.action( .action(
......
...@@ -142,6 +142,7 @@ export default function nextJest(options: { dir?: string } = {}) { ...@@ -142,6 +142,7 @@ export default function nextJest(options: { dir?: string } = {}) {
isEsmProject, isEsmProject,
pagesDir, pagesDir,
imageConfig, imageConfig,
configDir: options.dir,
} }
return { return {
......
...@@ -43,6 +43,7 @@ export interface JestTransformerConfig extends TransformerConfig { ...@@ -43,6 +43,7 @@ export interface JestTransformerConfig extends TransformerConfig {
jsConfig: any jsConfig: any
resolvedBaseUrl?: ResolvedBaseUrl resolvedBaseUrl?: ResolvedBaseUrl
pagesDir?: string pagesDir?: string
configDir?: string
serverComponents?: boolean serverComponents?: boolean
isEsmProject: boolean isEsmProject: boolean
modularizeImports?: NextConfig['modularizeImports'] modularizeImports?: NextConfig['modularizeImports']
...@@ -90,6 +91,7 @@ const createTransformer: TransformerCreator< ...@@ -90,6 +91,7 @@ const createTransformer: TransformerCreator<
jestConfig.testEnvironment === 'node' || jestConfig.testEnvironment === 'node' ||
jestConfig.testEnvironment.includes('jest-environment-node'), jestConfig.testEnvironment.includes('jest-environment-node'),
filename, filename,
configDir: inputOptions?.configDir,
jsConfig: inputOptions?.jsConfig, jsConfig: inputOptions?.jsConfig,
resolvedBaseUrl: inputOptions?.resolvedBaseUrl, resolvedBaseUrl: inputOptions?.resolvedBaseUrl,
pagesDir: inputOptions?.pagesDir, pagesDir: inputOptions?.pagesDir,
......
...@@ -60,6 +60,7 @@ function getBaseSWCOptions({ ...@@ -60,6 +60,7 @@ function getBaseSWCOptions({
hasReactRefresh, hasReactRefresh,
globalWindow, globalWindow,
esm, esm,
configDir,
modularizeImports, modularizeImports,
swcPlugins, swcPlugins,
compilerOptions, compilerOptions,
...@@ -83,6 +84,7 @@ function getBaseSWCOptions({ ...@@ -83,6 +84,7 @@ function getBaseSWCOptions({
hasReactRefresh: boolean hasReactRefresh: boolean
globalWindow: boolean globalWindow: boolean
esm: boolean esm: boolean
configDir?: string
modularizeImports?: NextConfig['modularizeImports'] modularizeImports?: NextConfig['modularizeImports']
compilerOptions: NextConfig['compiler'] compilerOptions: NextConfig['compiler']
swcPlugins: ExperimentalConfig['swcPlugins'] swcPlugins: ExperimentalConfig['swcPlugins']
...@@ -115,7 +117,10 @@ function getBaseSWCOptions({ ...@@ -115,7 +117,10 @@ function getBaseSWCOptions({
) )
const plugins = (swcPlugins ?? []) const plugins = (swcPlugins ?? [])
.filter(Array.isArray) .filter(Array.isArray)
.map(([name, options]: any) => [require.resolve(name), options]) .map(([name, options]: any) => [
require.resolve(name, configDir ? { paths: [configDir] } : undefined),
options,
])
return { return {
jsc: { jsc: {
...@@ -311,6 +316,7 @@ export function getJestSWCOptions({ ...@@ -311,6 +316,7 @@ export function getJestSWCOptions({
filename, filename,
esm, esm,
modularizeImports, modularizeImports,
configDir,
swcPlugins, swcPlugins,
compilerOptions, compilerOptions,
jsConfig, jsConfig,
...@@ -322,6 +328,7 @@ export function getJestSWCOptions({ ...@@ -322,6 +328,7 @@ export function getJestSWCOptions({
isServer: boolean isServer: boolean
filename: string filename: string
esm: boolean esm: boolean
configDir?: string
modularizeImports?: NextConfig['modularizeImports'] modularizeImports?: NextConfig['modularizeImports']
swcPlugins: ExperimentalConfig['swcPlugins'] swcPlugins: ExperimentalConfig['swcPlugins']
compilerOptions: NextConfig['compiler'] compilerOptions: NextConfig['compiler']
...@@ -337,6 +344,7 @@ export function getJestSWCOptions({ ...@@ -337,6 +344,7 @@ export function getJestSWCOptions({
jest: true, jest: true,
development: false, development: false,
hasReactRefresh: false, hasReactRefresh: false,
configDir,
globalWindow: !isServer, globalWindow: !isServer,
modularizeImports, modularizeImports,
swcPlugins, swcPlugins,
...@@ -393,6 +401,8 @@ export function getLoaderSWCOptions({ ...@@ -393,6 +401,8 @@ export function getLoaderSWCOptions({
isPageFile, isPageFile,
isCacheComponents, isCacheComponents,
hasReactRefresh, hasReactRefresh,
// The folder containing the next.config.js, used for resolving relative config paths.
configDir,
modularizeImports, modularizeImports,
optimizeServerReact, optimizeServerReact,
optimizePackageImports, optimizePackageImports,
...@@ -420,6 +430,7 @@ export function getLoaderSWCOptions({ ...@@ -420,6 +430,7 @@ export function getLoaderSWCOptions({
appDir?: string appDir?: string
isPageFile: boolean isPageFile: boolean
hasReactRefresh: boolean hasReactRefresh: boolean
configDir: string
optimizeServerReact?: boolean optimizeServerReact?: boolean
modularizeImports: NextConfig['modularizeImports'] modularizeImports: NextConfig['modularizeImports']
isCacheComponents?: boolean isCacheComponents?: boolean
...@@ -448,6 +459,7 @@ export function getLoaderSWCOptions({ ...@@ -448,6 +459,7 @@ export function getLoaderSWCOptions({
development, development,
globalWindow: !isServer, globalWindow: !isServer,
hasReactRefresh, hasReactRefresh,
configDir,
modularizeImports, modularizeImports,
swcPlugins, swcPlugins,
compilerOptions, compilerOptions,
......
...@@ -152,6 +152,7 @@ async function loaderTransform( ...@@ -152,6 +152,7 @@ async function loaderTransform(
!!nextConfig.experimental?.allowDevelopmentBuild, !!nextConfig.experimental?.allowDevelopmentBuild,
isCacheComponents: nextConfig.cacheComponents, isCacheComponents: nextConfig.cacheComponents,
hasReactRefresh, hasReactRefresh,
configDir: rootDir,
modularizeImports: nextConfig?.modularizeImports, modularizeImports: nextConfig?.modularizeImports,
optimizePackageImports: nextConfig?.experimental?.optimizePackageImports, optimizePackageImports: nextConfig?.experimental?.optimizePackageImports,
swcPlugins: nextConfig?.experimental?.swcPlugins, swcPlugins: nextConfig?.experimental?.swcPlugins,
......
...@@ -9,7 +9,6 @@ const { StreamableHTTPServerTransport } = ...@@ -9,7 +9,6 @@ const { StreamableHTTPServerTransport } =
require('next/dist/compiled/@modelcontextprotocol/sdk/server/streamableHttp') as typeof import('next/dist/compiled/@modelcontextprotocol/sdk/server/streamableHttp') 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_WS_PORT = 5747
const DEFAULT_MCP_PORT = 5748 // Keep in sync with query-trace.ts
/** 100 internal ticks = 1 µs */ /** 100 internal ticks = 1 µs */
const TICKS_PER_US = 100 const TICKS_PER_US = 100
...@@ -85,7 +84,7 @@ export async function startTurboTraceServerCli( ...@@ -85,7 +84,7 @@ export async function startTurboTraceServerCli(
mcpPort: number | undefined mcpPort: number | undefined
) { ) {
const wsPort = port ?? DEFAULT_WS_PORT const wsPort = port ?? DEFAULT_WS_PORT
const httpPort = mcpPort ?? DEFAULT_MCP_PORT const httpPort = mcpPort ?? wsPort + 1
let bindings let bindings
try { try {
......
...@@ -5460,12 +5460,22 @@ async function logMessagesAndSendErrorsToBrowser( ...@@ -5460,12 +5460,22 @@ async function logMessagesAndSendErrorsToBrowser(
const { clientModules } = getClientReferenceManifest() const { clientModules } = getClientReferenceManifest()
const errorsFlightStream = renderToWebFlightStream( let errorsFlightStream: AnyStream
ctx.componentMod, if (process.env.__NEXT_USE_NODE_STREAMS) {
{ errors, errorCodes }, errorsFlightStream = renderToNodeFlightStream(
clientModules, ctx.componentMod,
{ filterStackFrame } { errors, errorCodes },
) clientModules,
{ filterStackFrame }
)
} else {
errorsFlightStream = renderToWebFlightStream(
ctx.componentMod,
{ errors, errorCodes },
clientModules,
{ filterStackFrame }
)
}
sendErrorsToBrowser(errorsFlightStream, htmlRequestId) sendErrorsToBrowser(errorsFlightStream, htmlRequestId)
} }
...@@ -6038,13 +6048,20 @@ async function validateInstantConfigs( ...@@ -6038,13 +6048,20 @@ async function validateInstantConfigs(
const clientReferenceManifest = getClientReferenceManifest() const clientReferenceManifest = getClientReferenceManifest()
const renderFlightStream = process.env.__NEXT_USE_NODE_STREAMS
? renderToNodeFlightStream
: renderToWebFlightStream
const createDebugChannel = process.env.__NEXT_USE_NODE_STREAMS
? createNodeDebugChannel
: createWebDebugChannel
const { const {
cache, cache,
payload: initialRscPayload, payload: initialRscPayload,
stageEndTimes, stageEndTimes,
} = await collectStagedSegmentData( } = await collectStagedSegmentData(
ctx.componentMod, ctx.componentMod,
renderToWebFlightStream, renderFlightStream,
{ {
[RenderStage.Static]: accumulatedChunks.staticChunks, [RenderStage.Static]: accumulatedChunks.staticChunks,
[RenderStage.Runtime]: accumulatedChunks.runtimeChunks, [RenderStage.Runtime]: accumulatedChunks.runtimeChunks,
...@@ -6053,7 +6070,8 @@ async function validateInstantConfigs( ...@@ -6053,7 +6070,8 @@ async function validateInstantConfigs(
debugChunks, debugChunks,
startTime, startTime,
hasRuntimePrefetch, hasRuntimePrefetch,
clientReferenceManifest clientReferenceManifest,
createDebugChannel
) )
const { implicitTags, nonce, workStore } = ctx const { implicitTags, nonce, workStore } = ctx
...@@ -6121,13 +6139,14 @@ async function validateInstantConfigs( ...@@ -6121,13 +6139,14 @@ async function validateInstantConfigs(
const { stream: serverStream, debugStream } = const { stream: serverStream, debugStream } =
await createCombinedPayloadStream( await createCombinedPayloadStream(
ctx.componentMod, ctx.componentMod,
renderToWebFlightStream, renderFlightStream,
payloadResult.payload, payloadResult.payload,
extraChunksController, extraChunksController,
reactController.signal, reactController.signal,
clientReferenceManifest, clientReferenceManifest,
startTime, startTime,
isDebugChannelEnabled isDebugChannelEnabled,
createDebugChannel
) )
const instantValidationState = createInstantValidationState( const instantValidationState = createInstantValidationState(
...@@ -6371,6 +6390,9 @@ async function renderWithRestartOnCacheMissInValidation( ...@@ -6371,6 +6390,9 @@ async function renderWithRestartOnCacheMissInValidation(
}> { }> {
const { componentMod: ComponentMod } = ctx const { componentMod: ComponentMod } = ctx
const { clientModules } = getClientReferenceManifest() const { clientModules } = getClientReferenceManifest()
const renderFlightStream = process.env.__NEXT_USE_NODE_STREAMS
? renderToNodeFlightStream
: renderToWebFlightStream
let startTime = -Infinity let startTime = -Infinity
let requestStore: RequestStore = initialRequestStore let requestStore: RequestStore = initialRequestStore
...@@ -6435,7 +6457,7 @@ async function renderWithRestartOnCacheMissInValidation( ...@@ -6435,7 +6457,7 @@ async function renderWithRestartOnCacheMissInValidation(
const stream = workUnitAsyncStorage.run( const stream = workUnitAsyncStorage.run(
requestStore, requestStore,
renderToWebFlightStream, renderFlightStream,
ComponentMod, ComponentMod,
initialRscPayload, initialRscPayload,
clientModules, clientModules,
...@@ -6536,7 +6558,7 @@ async function renderWithRestartOnCacheMissInValidation( ...@@ -6536,7 +6558,7 @@ async function renderWithRestartOnCacheMissInValidation(
const stream = workUnitAsyncStorage.run( const stream = workUnitAsyncStorage.run(
requestStore, requestStore,
renderToWebFlightStream, renderFlightStream,
ComponentMod, ComponentMod,
finalRscPayload, finalRscPayload,
clientModules, clientModules,
......
...@@ -41,7 +41,7 @@ import { ...@@ -41,7 +41,7 @@ import {
createNodeStreamWithLateRelease, createNodeStreamWithLateRelease,
createNodeStreamFromChunks, createNodeStreamFromChunks,
} from './stream-utils' } from './stream-utils'
import { createWebDebugChannel } from '../debug-channel-server' import type { DebugChannelPair } from '../debug-channel-server'
import type { FlightComponentMod } from '../stream-ops' import type { FlightComponentMod } from '../stream-ops'
// eslint-disable-next-line import/no-extraneous-dependencies // eslint-disable-next-line import/no-extraneous-dependencies
...@@ -217,7 +217,8 @@ export async function collectStagedSegmentData( ...@@ -217,7 +217,8 @@ export async function collectStagedSegmentData(
fullPageDebugChunks: Uint8Array[] | null, fullPageDebugChunks: Uint8Array[] | null,
startTime: number, startTime: number,
hasRuntimePrefetch: boolean, hasRuntimePrefetch: boolean,
clientReferenceManifest: ClientReferenceManifest clientReferenceManifest: ClientReferenceManifest,
createDebugChannel: () => DebugChannelPair | undefined
) { ) {
const debugChannelAbortController = new AbortController() const debugChannelAbortController = new AbortController()
const debugStream = fullPageDebugChunks const debugStream = fullPageDebugChunks
...@@ -297,7 +298,7 @@ export async function collectStagedSegmentData( ...@@ -297,7 +298,7 @@ export async function collectStagedSegmentData(
cacheEntry: SegmentCacheItem cacheEntry: SegmentCacheItem
): Promise<void> => { ): Promise<void> => {
const segmentDebugChannel = cacheEntry.debugChunks const segmentDebugChannel = cacheEntry.debugChunks
? createWebDebugChannel() ? createDebugChannel()
: undefined : undefined
const itemStream = renderFlightStream( const itemStream = renderFlightStream(
...@@ -527,7 +528,8 @@ export async function createCombinedPayloadStream( ...@@ -527,7 +528,8 @@ export async function createCombinedPayloadStream(
renderSignal: AbortSignal, renderSignal: AbortSignal,
clientReferenceManifest: ClientReferenceManifest, clientReferenceManifest: ClientReferenceManifest,
startTime: number, startTime: number,
isDebugChannelEnabled: boolean isDebugChannelEnabled: boolean,
createDebugChannel: () => DebugChannelPair | undefined
) { ) {
// Collect all the chunks so that we're not dependent on timing of the render. // Collect all the chunks so that we're not dependent on timing of the render.
...@@ -536,7 +538,7 @@ export async function createCombinedPayloadStream( ...@@ -536,7 +538,7 @@ export async function createCombinedPayloadStream(
const allChunks: Uint8Array[] = [] const allChunks: Uint8Array[] = []
const debugChunks: Uint8Array[] | null = isDebugChannelEnabled ? [] : null const debugChunks: Uint8Array[] | null = isDebugChannelEnabled ? [] : null
const debugChannel = isDebugChannelEnabled ? createWebDebugChannel() : null const debugChannel = isDebugChannelEnabled ? createDebugChannel() : null
let streamFinished: Promise<any> let streamFinished: Promise<any>
......
...@@ -118,7 +118,12 @@ describe('getDynamicHTMLPostponedState', () => { ...@@ -118,7 +118,12 @@ describe('getDynamicHTMLPostponedState', () => {
expect(parsed).toEqual({ expect(parsed).toEqual({
type: DynamicState.HTML, type: DynamicState.HTML,
data: [1, { [value]: value }], data: [1, { [value]: value }],
renderResumeDataCache: createPrerenderResumeDataCache(), renderResumeDataCache: {
cache: new Map(),
fetch: new Map(),
encryptedBoundArgs: new Map(),
decryptedBoundArgs: new Map(),
},
}) })
// The replacements have been replaced. // The replacements have been replaced.
...@@ -148,7 +153,12 @@ describe('parsePostponedState', () => { ...@@ -148,7 +153,12 @@ describe('parsePostponedState', () => {
expect(parsed).toEqual({ expect(parsed).toEqual({
type: DynamicState.HTML, type: DynamicState.HTML,
data: expect.any(Object), 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. // Ensure that the replacement worked and removed all the placeholders.
...@@ -164,7 +174,12 @@ describe('parsePostponedState', () => { ...@@ -164,7 +174,12 @@ describe('parsePostponedState', () => {
expect(parsed).toEqual({ expect(parsed).toEqual({
type: DynamicState.HTML, type: DynamicState.HTML,
data: expect.any(Object), 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', () => { ...@@ -175,7 +190,12 @@ describe('parsePostponedState', () => {
// Ensure that it parsed it correctly. // Ensure that it parsed it correctly.
expect(parsed).toEqual({ expect(parsed).toEqual({
type: DynamicState.DATA, 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 { ...@@ -209,6 +209,24 @@ class NextTracerImpl implements NextTracer {
return trace.getTracer('next.js', '0.0.1') 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 { public getContext(): ContextAPI {
return context return context
} }
...@@ -277,6 +295,10 @@ class NextTracerImpl implements NextTracer { ...@@ -277,6 +295,10 @@ class NextTracerImpl implements NextTracer {
public trace<T>(...args: Array<any>) { public trace<T>(...args: Array<any>) {
const [type, fnOrOptions, fnOrEmpty] = args const [type, fnOrOptions, fnOrEmpty] = args
if (!NEXT_OTEL_PERFORMANCE_PREFIX && !this.isTracingEnabled()) {
return typeof fnOrOptions === 'function' ? fnOrOptions() : fnOrEmpty()
}
// coerce options form overload // coerce options form overload
const { const {
fn, fn,
......
...@@ -140,9 +140,12 @@ describe('stringifyResumeDataCache', () => { ...@@ -140,9 +140,12 @@ describe('stringifyResumeDataCache', () => {
describe('parseResumeDataCache', () => { describe('parseResumeDataCache', () => {
it('parses an empty cache', () => { it('parses an empty cache', () => {
expect(createRenderResumeDataCache('null', undefined)).toEqual( const parsed = createRenderResumeDataCache('null', undefined)
createPrerenderResumeDataCache() 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 () => { it('parses a filled cache', async () => {
......
...@@ -39,6 +39,16 @@ export interface RenderResumeDataCache { ...@@ -39,6 +39,16 @@ export interface RenderResumeDataCache {
* enforce immutability. * enforce immutability.
*/ */
readonly decryptedBoundArgs: Omit<DecryptedBoundArgsCacheStore, 'set'> 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 { ...@@ -74,6 +84,20 @@ export interface PrerenderResumeDataCache {
* operations to build the cache during pre-rendering. * operations to build the cache during pre-rendering.
*/ */
readonly decryptedBoundArgs: DecryptedBoundArgsCacheStore 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 = { type ResumeStoreSerialized = {
...@@ -156,6 +180,9 @@ export function createPrerenderResumeDataCache( ...@@ -156,6 +180,9 @@ export function createPrerenderResumeDataCache(
fetch: new Map(source.fetch), fetch: new Map(source.fetch),
encryptedBoundArgs: new Map(source.encryptedBoundArgs), encryptedBoundArgs: new Map(source.encryptedBoundArgs),
decryptedBoundArgs: new Map(source.decryptedBoundArgs), decryptedBoundArgs: new Map(source.decryptedBoundArgs),
dynamicCacheKeys: source.dynamicCacheKeys
? new Set(source.dynamicCacheKeys)
: new Set(),
} }
} else { } else {
return { return {
...@@ -163,6 +190,7 @@ export function createPrerenderResumeDataCache( ...@@ -163,6 +190,7 @@ export function createPrerenderResumeDataCache(
fetch: new Map(), fetch: new Map(),
encryptedBoundArgs: new Map(), encryptedBoundArgs: new Map(),
decryptedBoundArgs: new Map(), decryptedBoundArgs: new Map(),
dynamicCacheKeys: new Set(),
} }
} }
} }
......
...@@ -1550,6 +1550,32 @@ export async function cache( ...@@ -1550,6 +1550,32 @@ export async function cache(
const implicitTags = workUnitStore.implicitTags?.tags ?? [] const implicitTags = workUnitStore.implicitTags?.tags ?? []
if (renderResumeDataCache) { 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) const cacheSignal = getCacheSignal(workUnitStore)
if (cacheSignal) { if (cacheSignal) {
...@@ -1802,14 +1828,38 @@ export async function cache( ...@@ -1802,14 +1828,38 @@ export async function cache(
// an async function, before being passed into the "use cache" // an async function, before being passed into the "use cache"
// function, which escapes the instrumentation. // function, which escapes the instrumentation.
if (workUnitStore.allowEmptyStaticShell) { if (workUnitStore.allowEmptyStaticShell) {
if (prerenderResumeDataCache) {
prerenderResumeDataCache.dynamicCacheKeys.add(serializedCacheKey)
}
return makeHangingPromise( return makeHangingPromise(
workUnitStore.renderSignal, workUnitStore.renderSignal,
workStore.route, workStore.route,
'dynamic "use cache"' 'dynamic "use cache"'
) )
} }
break // fallthrough
case 'prerender-runtime': 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-ppr':
case 'prerender-legacy': case 'prerender-legacy':
case 'request': case 'request':
...@@ -2006,6 +2056,9 @@ export async function cache( ...@@ -2006,6 +2056,9 @@ export async function cache(
) )
if (result.type === 'prerender-dynamic') { if (result.type === 'prerender-dynamic') {
if (prerenderResumeDataCache) {
prerenderResumeDataCache.dynamicCacheKeys.add(serializedCacheKey)
}
return result.hangingPromise return result.hangingPromise
} }
......
{ {
"name": "@next/react-refresh-utils", "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.", "description": "An experimental package providing utilities for React Refresh.",
"repository": { "repository": {
"url": "vercel/next.js", "url": "vercel/next.js",
......
{ {
"name": "@next/third-parties", "name": "@next/third-parties",
"version": "16.2.1-canary.42", "version": "16.2.1-canary.44",
"repository": { "repository": {
"url": "vercel/next.js", "url": "vercel/next.js",
"directory": "packages/third-parties" "directory": "packages/third-parties"
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
"third-party-capital": "1.0.20" "third-party-capital": "1.0.20"
}, },
"devDependencies": { "devDependencies": {
"next": "16.2.1-canary.42", "next": "16.2.1-canary.44",
"outdent": "0.8.0", "outdent": "0.8.0",
"prettier": "2.5.1", "prettier": "2.5.1",
"typescript": "6.0.2" "typescript": "6.0.2"
......
...@@ -1023,7 +1023,7 @@ importers: ...@@ -1023,7 +1023,7 @@ importers:
packages/eslint-config-next: packages/eslint-config-next:
dependencies: dependencies:
'@next/eslint-plugin-next': '@next/eslint-plugin-next':
specifier: 16.2.1-canary.42 specifier: 16.2.1-canary.44
version: link:../eslint-plugin-next version: link:../eslint-plugin-next
eslint: eslint:
specifier: '>=9.0.0' specifier: '>=9.0.0'
...@@ -1100,7 +1100,7 @@ importers: ...@@ -1100,7 +1100,7 @@ importers:
packages/next: packages/next:
dependencies: dependencies:
'@next/env': '@next/env':
specifier: 16.2.1-canary.42 specifier: 16.2.1-canary.44
version: link:../next-env version: link:../next-env
'@swc/helpers': '@swc/helpers':
specifier: 0.5.15 specifier: 0.5.15
...@@ -1221,19 +1221,19 @@ importers: ...@@ -1221,19 +1221,19 @@ importers:
specifier: 1.2.0 specifier: 1.2.0
version: 1.2.0 version: 1.2.0
'@next/font': '@next/font':
specifier: 16.2.1-canary.42 specifier: 16.2.1-canary.44
version: link:../font version: link:../font
'@next/polyfill-module': '@next/polyfill-module':
specifier: 16.2.1-canary.42 specifier: 16.2.1-canary.44
version: link:../next-polyfill-module version: link:../next-polyfill-module
'@next/polyfill-nomodule': '@next/polyfill-nomodule':
specifier: 16.2.1-canary.42 specifier: 16.2.1-canary.44
version: link:../next-polyfill-nomodule version: link:../next-polyfill-nomodule
'@next/react-refresh-utils': '@next/react-refresh-utils':
specifier: 16.2.1-canary.42 specifier: 16.2.1-canary.44
version: link:../react-refresh-utils version: link:../react-refresh-utils
'@next/swc': '@next/swc':
specifier: 16.2.1-canary.42 specifier: 16.2.1-canary.44
version: link:../next-swc version: link:../next-swc
'@opentelemetry/api': '@opentelemetry/api':
specifier: 1.6.0 specifier: 1.6.0
...@@ -1967,7 +1967,7 @@ importers: ...@@ -1967,7 +1967,7 @@ importers:
version: 1.0.20 version: 1.0.20
devDependencies: devDependencies:
next: next:
specifier: 16.2.1-canary.42 specifier: 16.2.1-canary.44
version: link:../next version: link:../next
outdent: outdent:
specifier: 0.8.0 specifier: 0.8.0
...@@ -29,6 +29,8 @@ ignoredBuiltDependencies: ...@@ -29,6 +29,8 @@ ignoredBuiltDependencies:
- ssh2 - ssh2
- unrs-resolver - unrs-resolver
- wasm-pack - 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 blockExoticSubdeps: true
minimumReleaseAge: 2880 # 48 hrs minimumReleaseAge: 2880 # 48 hrs
minimumReleaseAgeExclude: 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' ...@@ -3,6 +3,7 @@ import path from 'path'
import execa from 'execa' import execa from 'execa'
import fs from 'fs' import fs from 'fs'
import fsp from 'fs/promises' import fsp from 'fs/promises'
import { outdent } from 'outdent'
;(async function () { ;(async function () {
if (process.env.NEXT_SKIP_NATIVE_POSTINSTALL) { if (process.env.NEXT_SKIP_NATIVE_POSTINSTALL) {
console.log( console.log(
...@@ -58,20 +59,43 @@ import fsp from 'fs/promises' ...@@ -58,20 +59,43 @@ import fsp from 'fs/promises'
packageManager, packageManager,
} }
fs.writeFileSync(path.join(tmpdir, 'package.json'), JSON.stringify(pkgJson)) 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 = [ const args = [
'add', 'add',
`next@${nextVersion}`, `next@${nextVersion}`,
'--lockfile=false', '--lockfile=false',
'--ignore-workspace',
'--ignore-scripts', '--ignore-scripts',
] ]
if (preferOffline) { if (preferOffline) {
args.push('--prefer-offline') args.push('--prefer-offline')
} }
let { stdout } = await execa('pnpm', args, { cwd: tmpdir }) await execa('pnpm', args, { cwd: tmpdir })
console.log(stdout)
let pkgs = fs.readdirSync(path.join(tmpdir, 'node_modules/@next')) let pkgs = fs.readdirSync(path.join(tmpdir, 'node_modules/@next'))
fs.mkdirSync(path.join(cwd, 'node_modules/@next'), { recursive: true }) fs.mkdirSync(path.join(cwd, 'node_modules/@next'), { recursive: true })
...@@ -91,7 +115,6 @@ import fsp from 'fs/promises' ...@@ -91,7 +115,6 @@ import fsp from 'fs/promises'
fs.rmSync(tmpdir, { recursive: true, force: true }) fs.rmSync(tmpdir, { recursive: true, force: true })
console.log('Installed the following binary packages:', pkgs) console.log('Installed the following binary packages:', pkgs)
} catch (e) { } catch (e) {
console.error(e) throw new Error('Failed to install @next/swc binary packages', { cause: e })
console.error('Failed to load @next/swc binary packages')
} }
})() })()
...@@ -3421,11 +3421,12 @@ describe('Cache Components Errors', () => { ...@@ -3421,11 +3421,12 @@ describe('Cache Components Errors', () => {
if (isDebugPrerender) { if (isDebugPrerender) {
expect(output).toMatchInlineSnapshot(` 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 "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 Page (app/use-cache-params/[slug]/page.tsx:1:16)
at main (<anonymous>) > 1 | export default async function Page({
at body (<anonymous>) | ^
at html (<anonymous>) 2 | params,
at Root [Server] (<anonymous>) 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. 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 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', () => { ...@@ -3447,16 +3448,12 @@ describe('Cache Components Errors', () => {
} }
} else { } else {
if (isDebugPrerender) { if (isDebugPrerender) {
expect(output).toMatchInlineSnapshot(` // Webpack does not ignore the stack frame that points into
"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 // Next.js internals, and is also flaky on resolving the exact
at Page [Server] (<anonymous>) // location, so we don't assert on the stack frames here.
at <FIXME-library-internal> expect(output).toInclude(
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: 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'
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]"
`)
} else { } else {
expect(output).toMatchInlineSnapshot(` 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 "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', () => { ...@@ -19,6 +19,7 @@ describe('swcEnvOptions', () => {
serverReferenceHashSalt: 'test-salt', serverReferenceHashSalt: 'test-salt',
bundleLayer: undefined, bundleLayer: undefined,
cacheHandlers: undefined, cacheHandlers: undefined,
configDir: '/',
} }
it('should not include extra env options when swcEnvOptions is not set', () => { it('should not include extra env options when swcEnvOptions is not set', () => {
......
...@@ -9,7 +9,7 @@ use bincode::{Decode, Encode}; ...@@ -9,7 +9,7 @@ use bincode::{Decode, Encode};
use indexmap::Equivalent; use indexmap::Equivalent;
use num_bigint::BigInt; use num_bigint::BigInt;
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use smallvec::SmallVec; use smallvec::{SmallVec, smallvec};
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{FxIndexMap, NonLocalValue, ResolvedVc, Vc, trace::TraceRawVcs}; use turbo_tasks::{FxIndexMap, NonLocalValue, ResolvedVc, Vc, trace::TraceRawVcs};
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
...@@ -340,6 +340,19 @@ impl CompileTimeDefines { ...@@ -340,6 +340,19 @@ impl CompileTimeDefines {
pub fn empty() -> Vc<Self> { pub fn empty() -> Vc<Self> {
Vc::cell(FxIndexMap::default()) 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)] #[derive(Debug, Clone, Copy, PartialEq, Eq, TraceRawVcs, NonLocalValue, Encode, Decode)]
......
...@@ -108,16 +108,20 @@ impl ChunkGroupInfo { ...@@ -108,16 +108,20 @@ impl ChunkGroupInfo {
if let Some(idx) = self.chunk_groups.get_index_of(&chunk_group) { if let Some(idx) = self.chunk_groups.get_index_of(&chunk_group) {
Ok(Vc::cell(idx)) Ok(Vc::cell(idx))
} else { } else {
bail!( if cfg!(debug_assertions) {
"Couldn't find chunk group index for {} in {}", bail!(
chunk_group.debug_str(self).await?, "Couldn't find chunk group index for {} in {}",
self.chunk_groups chunk_group.debug_str(self).await?,
.iter() self.chunk_groups
.map(|c| c.debug_str(self)) .iter()
.try_join() .map(|c| c.debug_str(self))
.await? .try_join()
.join(", ") .await?
); .join(", ")
);
} else {
bail!("Couldn't find chunk group index")
}
} }
} }
} }
......
...@@ -4,7 +4,7 @@ use std::{ ...@@ -4,7 +4,7 @@ use std::{
mem::take, mem::take,
}; };
use anyhow::{Context, Result}; use anyhow::{Context, Result, bail};
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
use either::Either; use either::Either;
use petgraph::graph::{DiGraph, EdgeIndex, NodeIndex}; use petgraph::graph::{DiGraph, EdgeIndex, NodeIndex};
...@@ -79,19 +79,23 @@ pub struct ModuleBatchesGraph { ...@@ -79,19 +79,23 @@ pub struct ModuleBatchesGraph {
impl ModuleBatchesGraph { impl ModuleBatchesGraph {
pub async fn get_entry_index(&self, entry: ResolvedVc<Box<dyn Module>>) -> Result<NodeIndex> { pub async fn get_entry_index(&self, entry: ResolvedVc<Box<dyn Module>>) -> Result<NodeIndex> {
let Some(entry) = self.entries.get(&entry) else { let Some(entry) = self.entries.get(&entry) else {
let possible_entries = format!( if cfg!(debug_assertions) {
"{:#?}", let possible_entries = format!(
self.entries "{:#?}",
.keys() self.entries
.map(|e| e.ident().to_string()) .keys()
.try_join() .map(|e| e.ident().to_string())
.await? .try_join()
); .await?
turbobail!( );
"Entry {} is not in graph (possible entries: {})", turbobail!(
entry.ident(), "Entry {} is not in graph (possible entries: {})",
possible_entries entry.ident(),
); possible_entries
);
} else {
bail!("Entry is not in graph");
}
}; };
Ok(*entry) Ok(*entry)
} }
......
...@@ -163,8 +163,7 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer { ...@@ -163,8 +163,7 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer {
let transform_metadata_context = Arc::new(TransformPluginMetadataContext::new( let transform_metadata_context = Arc::new(TransformPluginMetadataContext::new(
Some(ctx.file_path_str.to_string()), Some(ctx.file_path_str.to_string()),
//[TODO]: Support env-related variable injection, i.e process.env.NODE_ENV ctx.node_env.to_string(),
"development".to_string(),
None, None,
)); ));
......
...@@ -20,7 +20,7 @@ use turbopack_core::{ ...@@ -20,7 +20,7 @@ use turbopack_core::{
use turbopack_ecmascript::{ use turbopack_ecmascript::{
AnalyzeMode, AnalyzeMode,
analyzer::{ analyzer::{
graph::{EvalContext, VarGraph, VarMeta, create_graph}, graph::{EvalContext, VarGraph, create_graph},
imports::ImportAttributes, imports::ImportAttributes,
linker::link, linker::link,
test_utils::{early_visitor, visitor}, test_utils::{early_visitor, visitor},
...@@ -66,7 +66,6 @@ pub fn benchmark(c: &mut Criterion) { ...@@ -66,7 +66,6 @@ pub fn benchmark(c: &mut Criterion) {
top_level_mark, top_level_mark,
Default::default(), Default::default(),
None, None,
None,
); );
let var_graph = create_graph( let var_graph = create_graph(
&program, &program,
...@@ -116,7 +115,7 @@ fn bench_link(b: &mut Bencher, input: &BenchInput) { ...@@ -116,7 +115,7 @@ fn bench_link(b: &mut Bencher, input: &BenchInput) {
b.to_async(rt).iter(|| async { b.to_async(rt).iter(|| async {
let var_cache = Default::default(); 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 { VcStorage::with(async {
let compile_time_info = CompileTimeInfo::builder( let compile_time_info = CompileTimeInfo::builder(
Environment::new(ExecutionEnvironment::NodeJsLambda( Environment::new(ExecutionEnvironment::NodeJsLambda(
......
...@@ -22,8 +22,7 @@ use swc_core::{ ...@@ -22,8 +22,7 @@ use swc_core::{
}, },
}; };
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::ResolvedVc; use turbopack_core::resolve::ExportUsage;
use turbopack_core::{resolve::ExportUsage, source::Source};
use super::{ use super::{
ConstantValue, ImportMap, JsValue, ObjectPart, WellKnownFunctionKind, is_unresolved_id, ConstantValue, ImportMap, JsValue, ObjectPart, WellKnownFunctionKind, is_unresolved_id,
...@@ -272,26 +271,34 @@ impl Effect { ...@@ -272,26 +271,34 @@ impl Effect {
} }
} }
#[derive(Debug)]
pub enum AssignmentScope { pub enum AssignmentScope {
/// assigned in the root scope
ModuleEval, ModuleEval,
/// assigned in a function scopes
Function, Function,
} }
/// Tracks the locations where this was assigned to:
/// This is used to track the _liveness_ of exports.
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum AssignmentScopes { pub enum AssignmentScopes {
/// assigned only in the root scope
AllInModuleEvalScope, AllInModuleEvalScope,
/// assigned in any set of function scopes
AllInFunctionScopes, AllInFunctionScopes,
/// assigned in both module and function scopes
Mixed, Mixed,
} }
impl AssignmentScopes { impl AssignmentScopes {
fn new(initial: AssignmentScope) -> Self { pub fn new(initial: AssignmentScope) -> Self {
match initial { match initial {
AssignmentScope::ModuleEval => AssignmentScopes::AllInModuleEvalScope, AssignmentScope::ModuleEval => AssignmentScopes::AllInModuleEvalScope,
AssignmentScope::Function => AssignmentScopes::AllInFunctionScopes, 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 the other assignment kind is the same as the current one, return the current one.
if self == Self::new(other) { if self == Self::new(other) {
self self
...@@ -301,37 +308,6 @@ impl AssignmentScopes { ...@@ -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)] #[derive(Clone, Debug)]
pub enum DeclUsage { pub enum DeclUsage {
SideEffects, SideEffects,
...@@ -358,7 +334,7 @@ impl DeclUsage { ...@@ -358,7 +334,7 @@ impl DeclUsage {
#[derive(Debug)] #[derive(Debug)]
pub struct VarGraph { 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`]. /// Map [`JsValue::FreeVar`] names to their [`Id`] to facilitate lookups into [`Self::values`].
/// ///
...@@ -449,14 +425,11 @@ impl EvalContext { ...@@ -449,14 +425,11 @@ impl EvalContext {
top_level_mark: Mark, top_level_mark: Mark,
force_free_values: Arc<FxHashSet<Id>>, force_free_values: Arc<FxHashSet<Id>>,
comments: Option<&dyn Comments>, comments: Option<&dyn Comments>,
source: Option<ResolvedVc<Box<dyn Source>>>,
) -> Self { ) -> Self {
Self { Self {
unresolved_mark, unresolved_mark,
top_level_mark, top_level_mark,
imports: module.map_or(ImportMap::default(), |m| { imports: module.map_or(ImportMap::default(), |m| ImportMap::analyze(m, comments)),
ImportMap::analyze(m, source, comments)
}),
force_free_values, force_free_values,
} }
} }
...@@ -889,14 +862,8 @@ impl EvalContext { ...@@ -889,14 +862,8 @@ impl EvalContext {
let js_value = try_with_handler(cm, Default::default(), |_| { let js_value = try_with_handler(cm, Default::default(), |_| {
GLOBALS.set(&Default::default(), || { GLOBALS.set(&Default::default(), || {
let expr = parse_single_expr_lit(expr_lit); let expr = parse_single_expr_lit(expr_lit);
let eval_context = EvalContext::new( let eval_context =
None, EvalContext::new(None, Mark::new(), Mark::new(), Default::default(), None);
Mark::new(),
Mark::new(),
Default::default(),
None,
None,
);
Ok(eval_context.eval(&expr)) Ok(eval_context.eval(&expr))
}) })
...@@ -1541,15 +1508,10 @@ impl Analyzer<'_> { ...@@ -1541,15 +1508,10 @@ impl Analyzer<'_> {
self.data.free_var_ids.insert(id.0.clone(), id.clone()); 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) { if let Some(prev) = self.data.values.get_mut(&id) {
prev.add_alt(value, kind); prev.add_alt(value);
} else { } 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. // TODO(kdy1): We may need to report an error for this.
// Variables declared with `var` are hoisted, but using undefined as its // Variables declared with `var` are hoisted, but using undefined as its
......
...@@ -6,7 +6,6 @@ use rustc_hash::{FxHashMap, FxHashSet}; ...@@ -6,7 +6,6 @@ use rustc_hash::{FxHashMap, FxHashSet};
use swc_core::ecma::ast::Id; use swc_core::ecma::ast::Id;
use super::{JsValue, graph::VarGraph}; use super::{JsValue, graph::VarGraph};
use crate::analyzer::graph::VarMeta;
pub async fn link<'a, B, RB, F, RF>( pub async fn link<'a, B, RB, F, RF>(
graph: &VarGraph, graph: &VarGraph,
...@@ -123,7 +122,7 @@ where ...@@ -123,7 +122,7 @@ where
if let Some(val) = var_cache_lock.as_deref().and_then(|cache| cache.get(&var)) { if let Some(val) = var_cache_lock.as_deref().and_then(|cache| cache.get(&var)) {
total_nodes += val.total_nodes(); total_nodes += val.total_nodes();
done.push(val.clone()); 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()); cycle_stack.insert(var.clone());
work_queue_stack.push(Step::LeaveVar(var)); work_queue_stack.push(Step::LeaveVar(var));
total_nodes += value.total_nodes(); 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