From 1f3178e7433ed28d65bdb0f0219334e8dfc90c69 Mon Sep 17 00:00:00 2001 From: OJ Kwon <1210596+kwonoj@users.noreply.github.com> Date: Fri, 17 Nov 2023 12:59:01 -0800 Subject: [PATCH] fix(next-core): allow runtime segment option in pages/api (#58409) ### What This PR fixes turbopack to allow runtime segment option (https://nextjs.org/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes#segment-runtime-Option) in pages/api. Previously it only allows config object (https://nextjs.org/docs/app/building-your-application/routing/middleware#matcher), so the single runtime export like `export const runtime = 'edge'` didn't work. PR updates logic to parse config in the module to allow single segment export as well. It doesn't allow _both_, if config object is exported it'll short-curcuit to read values as config object should able to specify runtime without separate runtime segment. Closes PACK-1961 --- .../next-swc/crates/next-core/src/util.rs | 60 +++++++++++++++++-- test/turbopack-tests-manifest.json | 9 ++- 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/packages/next-swc/crates/next-core/src/util.rs b/packages/next-swc/crates/next-core/src/util.rs index baa18cd58c..381f78a1ce 100644 --- a/packages/next-swc/crates/next-core/src/util.rs +++ b/packages/next-swc/crates/next-core/src/util.rs @@ -2,7 +2,7 @@ use anyhow::{bail, Context, Result}; use indexmap::{IndexMap, IndexSet}; use serde::{de::DeserializeOwned, Deserialize, Serialize}; use serde_json::Value as JsonValue; -use swc_core::ecma::ast::Program; +use swc_core::ecma::ast::{Expr, Lit, Program}; use turbo_tasks::{trace::TraceRawVcs, TaskInput, ValueDefault, ValueToString, Vc}; use turbo_tasks_fs::{rope::Rope, util::join_path, File}; use turbopack_binding::{ @@ -224,9 +224,11 @@ pub async fn parse_config_from_source(module: Vc>) -> Result>) -> Result { + config.runtime = NextRuntime::Edge; + } + "nodejs" => { + config.runtime = NextRuntime::NodeJs; + } + _ => { + runtime_value_issue.emit(); + } + } + + return Ok(config.cell()); + } else { + runtime_value_issue.emit(); + } + } else { + NextSourceConfigParsingIssue { + ident: module.ident(), + detail: Vc::cell( + "The exported segment runtime option must contain an \ + variable initializer." + .to_string(), + ), + } + .cell() + .emit() + } + } } } } diff --git a/test/turbopack-tests-manifest.json b/test/turbopack-tests-manifest.json index b753b9e5c2..fb21bd4b84 100644 --- a/test/turbopack-tests-manifest.json +++ b/test/turbopack-tests-manifest.json @@ -2454,6 +2454,7 @@ "test/e2e/app-dir/app-middleware/app-middleware.test.ts": { "passed": [ "app dir - middleware without pages dir Updates headers", + "app dir - middleware with middleware in src dir works without crashing when using requestAsyncStorage", "app-dir with middleware Mutate request headers for Edge Functions Supports draft mode", "app-dir with middleware Mutate request headers for Serverless Functions Adds new headers", "app-dir with middleware Mutate request headers for Serverless Functions Deletes headers", @@ -2463,14 +2464,12 @@ "app-dir with middleware Mutate request headers for next/headers Deletes headers", "app-dir with middleware Mutate request headers for next/headers Supports draft mode", "app-dir with middleware Mutate request headers for next/headers Updates headers", - "app-dir with middleware should filter correctly after middleware rewrite" - ], - "failed": [ - "app dir - middleware with middleware in src dir works without crashing when using requestAsyncStorage", "app-dir with middleware Mutate request headers for Edge Functions Adds new headers", "app-dir with middleware Mutate request headers for Edge Functions Deletes headers", - "app-dir with middleware Mutate request headers for Edge Functions Updates headers" + "app-dir with middleware Mutate request headers for Edge Functions Updates headers", + "app-dir with middleware should filter correctly after middleware rewrite" ], + "failed": [], "pending": [], "flakey": [], "runtimeError": false