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(
rootDir: dir,
pagesDir,
appDir,
hasServerComponents,
hasReactRefresh: dev && isClient,
hasServerComponents: true,
fileReading: config.experimental.swcFileReading,
nextConfig: config,
jsConfig,
......@@ -874,13 +874,16 @@ export default async function getBaseWebpackConfig(
: []
const swcLoaderForClientLayer = hasServerComponents
? useSWCLoader
? [getSwcLoader({ isServerLayer: false })]
? [getSwcLoader({ hasServerComponents, isServerLayer: false })]
: // When using Babel, we will have to add the SWC loader
// as an additional pass to handle RSC correctly.
// This will cause some performance overhead but
// acceptable as Babel will not be recommended.
[getSwcLoader({ isServerLayer: false }), getBabelLoader()]
: []
const swcLoaderForMiddlewareLayer = useSWCLoader
? getSwcLoader({ hasServerComponents: false })
: getBabelLoader()
// 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
......@@ -2022,7 +2025,7 @@ export default async function getBaseWebpackConfig(
{
test: codeCondition.test,
issuerLayer: WEBPACK_LAYERS.middleware,
use: defaultLoaders.babel,
use: swcLoaderForMiddlewareLayer,
},
...(hasServerComponents
? [
......
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
*/
......
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