Skip to content

Commit

Permalink
update server layer detection
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed May 7, 2024
1 parent 35c0c2b commit 9c5991e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 21 deletions.
4 changes: 2 additions & 2 deletions packages/next/src/build/swc/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
StyledComponentsConfig,
} from '../../server/config-shared'
import type { ResolvedBaseUrl } from '../load-jsconfig'
import { isWebpackServerOnlyLayer } from '../utils'

const nextDistPath =
/(next[\\/]dist[\\/]shared[\\/]lib)|(next[\\/]dist[\\/]client)|(next[\\/]dist[\\/]pages)/
Expand Down Expand Up @@ -78,8 +79,7 @@ function getBaseSWCOptions({
serverComponents?: boolean
bundleLayer?: WebpackLayerName
}) {
const isReactServerLayer =
bundleLayer === WEBPACK_LAYERS.reactServerComponents
const isReactServerLayer = isWebpackServerOnlyLayer(bundleLayer)
const parserConfig = getParserOptions({ filename, jsConfig })
const paths = jsConfig?.compilerOptions?.paths
const enableDecorators = Boolean(
Expand Down
21 changes: 15 additions & 6 deletions packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ export default async function getBaseWebpackConfig(
// This will cause some performance overhead but
// acceptable as Babel will not be recommended.
getSwcLoader({
serverComponents: true,
serverComponents: false,
bundleLayer: WEBPACK_LAYERS.middleware,
}),
babelLoader,
Expand Down Expand Up @@ -560,7 +560,7 @@ export default async function getBaseWebpackConfig(
const apiRoutesLayerLoaders =
hasAppDir && useSWCLoader
? getSwcLoader({
serverComponents: true,
serverComponents: false,
bundleLayer: WEBPACK_LAYERS.api,
})
: defaultLoaders.babel
Expand Down Expand Up @@ -1203,7 +1203,10 @@ export default async function getBaseWebpackConfig(
// Alias server-only and client-only to proper exports based on bundling layers
{
issuerLayer: {
or: WEBPACK_LAYERS.GROUP.serverOnly,
or: [
...WEBPACK_LAYERS.GROUP.serverOnly,
...WEBPACK_LAYERS.GROUP.neutralTarget,
],
},
resolve: {
// Error on client-only but allow server-only
Expand All @@ -1212,7 +1215,10 @@ export default async function getBaseWebpackConfig(
},
{
issuerLayer: {
not: WEBPACK_LAYERS.GROUP.serverOnly,
not: [
...WEBPACK_LAYERS.GROUP.serverOnly,
...WEBPACK_LAYERS.GROUP.neutralTarget,
],
},
resolve: {
// Error on server-only but allow client-only
Expand Down Expand Up @@ -1241,7 +1247,10 @@ export default async function getBaseWebpackConfig(
],
loader: 'next-invalid-import-error-loader',
issuerLayer: {
not: WEBPACK_LAYERS.GROUP.serverOnly,
not: [
...WEBPACK_LAYERS.GROUP.serverOnly,
...WEBPACK_LAYERS.GROUP.neutralTarget,
],
},
options: {
message:
Expand All @@ -1258,7 +1267,7 @@ export default async function getBaseWebpackConfig(
],
loader: 'empty-loader',
issuerLayer: {
not: WEBPACK_LAYERS.GROUP.serverOnly,
or: WEBPACK_LAYERS.GROUP.neutralTarget,
},
},
...(hasAppDir
Expand Down
3 changes: 3 additions & 0 deletions packages/next/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ const WEBPACK_LAYERS = {
WEBPACK_LAYERS_NAMES.appRouteHandler,
WEBPACK_LAYERS_NAMES.instrument,
WEBPACK_LAYERS_NAMES.middleware,
],
neutralTarget: [
// pages api
WEBPACK_LAYERS_NAMES.api,
],
clientOnly: [
Expand Down
11 changes: 3 additions & 8 deletions test/e2e/module-layer/module-layer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,10 @@ describe('module layer', () => {
await next.patchFile(pagesApiFile, pagesApiContent)
})

it('should error when import client packages in pages/api', async () => {
const existingCliOutputLength = next.cliOutput.length
it('should not error when import client packages in pages/api', async () => {
await retry(async () => {
await next.fetch('/api/mixed')
const newCliOutput = next.cliOutput.slice(existingCliOutputLength)
expect(newCliOutput).toContain('./pages/api/mixed.js')
expect(newCliOutput).toContain(
`'client-only' cannot be imported from a Server Component module. It should only be used from a Client Component.`
)
const { status } = await next.fetch('/api/mixed')
expect(status).toBe(200)
})
})
})
Expand Down
6 changes: 1 addition & 5 deletions test/e2e/module-layer/pages/api/hello.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import 'server-only'
import React from 'react'

export default function handler(req, res) {
if (React.useState) {
throw new Error('React.useState should not be defined in server layer')
}
return res.send('pages/api/hello.js:')
return res.send('pages/api/hello.js')
}

0 comments on commit 9c5991e

Please sign in to comment.