Unverified Commit 592f592b authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix SWC loader options for files outside of RSC layers (#50341)

The React Server Component transform of SWC shouldn't be enabled for
files that are not part of "app layers", for example middleware.
parent 3870abf0
...@@ -847,8 +847,8 @@ export default async function getBaseWebpackConfig( ...@@ -847,8 +847,8 @@ export default async function getBaseWebpackConfig(
rootDir: dir, rootDir: dir,
pagesDir, pagesDir,
appDir, appDir,
hasServerComponents,
hasReactRefresh: dev && isClient, hasReactRefresh: dev && isClient,
hasServerComponents: true,
fileReading: config.experimental.swcFileReading, fileReading: config.experimental.swcFileReading,
nextConfig: config, nextConfig: config,
jsConfig, jsConfig,
...@@ -874,13 +874,16 @@ export default async function getBaseWebpackConfig( ...@@ -874,13 +874,16 @@ export default async function getBaseWebpackConfig(
: [] : []
const swcLoaderForClientLayer = hasServerComponents const swcLoaderForClientLayer = hasServerComponents
? useSWCLoader ? useSWCLoader
? [getSwcLoader({ isServerLayer: false })] ? [getSwcLoader({ hasServerComponents, isServerLayer: false })]
: // When using Babel, we will have to add the SWC loader : // When using Babel, we will have to add the SWC loader
// as an additional pass to handle RSC correctly. // as an additional pass to handle RSC correctly.
// This will cause some performance overhead but // This will cause some performance overhead but
// acceptable as Babel will not be recommended. // acceptable as Babel will not be recommended.
[getSwcLoader({ isServerLayer: false }), getBabelLoader()] [getSwcLoader({ isServerLayer: false }), getBabelLoader()]
: [] : []
const swcLoaderForMiddlewareLayer = useSWCLoader
? getSwcLoader({ hasServerComponents: false })
: getBabelLoader()
// Loader for API routes needs to be differently configured as it shouldn't // Loader for API routes needs to be differently configured as it shouldn't
// have RSC transpiler enabled, so syntax checks such as invalid imports won't // have RSC transpiler enabled, so syntax checks such as invalid imports won't
...@@ -2022,7 +2025,7 @@ export default async function getBaseWebpackConfig( ...@@ -2022,7 +2025,7 @@ export default async function getBaseWebpackConfig(
{ {
test: codeCondition.test, test: codeCondition.test,
issuerLayer: WEBPACK_LAYERS.middleware, issuerLayer: WEBPACK_LAYERS.middleware,
use: defaultLoaders.babel, use: swcLoaderForMiddlewareLayer,
}, },
...(hasServerComponents ...(hasServerComponents
? [ ? [
......
import { NextResponse } from 'next/server' import { NextResponse } from 'next/server'
// It should be able to import `headers` inside middleware
import { headers } from 'next/headers'
console.log(!!headers)
/** /**
* @param {import('next/server').NextRequest} request * @param {import('next/server').NextRequest} request
*/ */
......
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