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.
This commit is contained in:
Shu Ding 2023-05-25 19:49:26 +02:00 committed by GitHub
parent 3870abf049
commit 592f592b07
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 3 deletions

View file

@ -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
? [

View file

@ -1,5 +1,9 @@
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
*/