Skip to content

Commit

Permalink
Merge middleware, api layers into rsc layer
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed May 12, 2024
1 parent 6e59e6e commit b218bf2
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 45 deletions.
4 changes: 2 additions & 2 deletions packages/next/src/build/create-compiler-aliases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ export function createRSCAliases(
'react-dom$': `next/dist/server/future/route-modules/app-page/vendored/${layer}/react-dom`,
'react-server-dom-webpack/client.edge$': `next/dist/server/future/route-modules/app-page/vendored/${layer}/react-server-dom-webpack-client-edge`,
})
} else if (layer === WEBPACK_LAYERS.reactServerComponents) {
} else if (layer === WEBPACK_LAYERS.serverComponents) {
alias = Object.assign(alias, {
'react/jsx-runtime$': `next/dist/server/future/route-modules/app-page/vendored/${layer}/react-jsx-runtime`,
'react/jsx-dev-runtime$': `next/dist/server/future/route-modules/app-page/vendored/${layer}/react-jsx-dev-runtime`,
Expand All @@ -289,7 +289,7 @@ export function createRSCAliases(
}

if (isEdgeServer) {
if (layer === WEBPACK_LAYERS.reactServerComponents) {
if (layer === WEBPACK_LAYERS.serverComponents) {
alias = Object.assign(alias, {
react$: `next/dist/compiled/react${bundledReactChannel}/react.react-server`,
'next/dist/compiled/react$': `next/dist/compiled/react${bundledReactChannel}/react.react-server`,
Expand Down
18 changes: 8 additions & 10 deletions packages/next/src/build/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ export function getEdgeServerEntry(opts: {

return {
import: `next-edge-app-route-loader?${stringify(loaderParams)}!`,
layer: WEBPACK_LAYERS.reactServerComponents,
layer: WEBPACK_LAYERS.serverComponents,
}
}

Expand Down Expand Up @@ -452,7 +452,7 @@ export function getInstrumentationEntry(opts: {
export function getAppEntry(opts: Readonly<AppLoaderOptions>) {
return {
import: `next-app-loader?${stringify(opts)}!`,
layer: WEBPACK_LAYERS.reactServerComponents,
layer: WEBPACK_LAYERS.serverComponents,
}
}

Expand Down Expand Up @@ -796,13 +796,11 @@ export function finalizeEntrypoint({

switch (compilerType) {
case COMPILER_NAMES.server: {
const layer = isApi
? WEBPACK_LAYERS.api
: isInstrumentation
? WEBPACK_LAYERS.instrument
: isServerComponent
? WEBPACK_LAYERS.reactServerComponents
: undefined
const layer = isInstrumentation
? WEBPACK_LAYERS.instrument
: isServerComponent || isApi
? WEBPACK_LAYERS.serverComponents
: undefined

return {
publicPath: isApi ? '' : undefined,
Expand All @@ -815,7 +813,7 @@ export function finalizeEntrypoint({
return {
layer:
isMiddlewareFilename(name) || isApi || isInstrumentation
? WEBPACK_LAYERS.middleware
? WEBPACK_LAYERS.serverComponents
: undefined,
library: { name: ['_ENTRIES', `middleware_[name]`], type: 'assign' },
runtime: EDGE_RUNTIME_WEBPACK,
Expand Down
29 changes: 14 additions & 15 deletions packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ export default async function getBaseWebpackConfig(
// RSC loaders, prefer ESM, set `esm` to true
const swcServerLayerLoader = getSwcLoader({
serverComponents: true,
bundleLayer: WEBPACK_LAYERS.reactServerComponents,
bundleLayer: WEBPACK_LAYERS.serverComponents,
esm: true,
})
const swcSSRLayerLoader = getSwcLoader({
Expand Down Expand Up @@ -510,7 +510,7 @@ export default async function getBaseWebpackConfig(
// acceptable as Babel will not be recommended.
getSwcLoader({
serverComponents: false,
bundleLayer: WEBPACK_LAYERS.middleware,
bundleLayer: WEBPACK_LAYERS.serverComponents,
}),
babelLoader,
].filter(Boolean)
Expand Down Expand Up @@ -557,13 +557,12 @@ export default async function getBaseWebpackConfig(
// 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
// be performed.
const apiRoutesLayerLoaders =
hasAppDir && useSWCLoader
? getSwcLoader({
serverComponents: false,
bundleLayer: WEBPACK_LAYERS.api,
})
: defaultLoaders.babel
const apiRoutesLayerLoaders = useSWCLoader
? getSwcLoader({
serverComponents: true,
bundleLayer: WEBPACK_LAYERS.serverComponents,
})
: defaultLoaders.babel

const pageExtensions = config.pageExtensions

Expand Down Expand Up @@ -1345,7 +1344,7 @@ export default async function getBaseWebpackConfig(
alias: createRSCAliases(bundledReactChannel, {
// No server components profiling
reactProductionProfiling,
layer: WEBPACK_LAYERS.reactServerComponents,
layer: WEBPACK_LAYERS.serverComponents,
isEdgeServer,
}),
},
Expand Down Expand Up @@ -1376,7 +1375,7 @@ export default async function getBaseWebpackConfig(
resourceQuery: new RegExp(
WEBPACK_RESOURCE_QUERIES.edgeSSREntry
),
layer: WEBPACK_LAYERS.reactServerComponents,
layer: WEBPACK_LAYERS.serverComponents,
},
]
: []),
Expand All @@ -1403,7 +1402,7 @@ export default async function getBaseWebpackConfig(
// when react is acting as dependency of compiled/react-dom.
alias: createRSCAliases(bundledReactChannel, {
reactProductionProfiling,
layer: WEBPACK_LAYERS.reactServerComponents,
layer: WEBPACK_LAYERS.serverComponents,
isEdgeServer,
}),
},
Expand Down Expand Up @@ -1457,7 +1456,7 @@ export default async function getBaseWebpackConfig(
oneOf: [
{
...codeCondition,
issuerLayer: WEBPACK_LAYERS.api,
issuerLayer: WEBPACK_LAYERS.serverComponents,
parser: {
// Switch back to normal URL handling
url: true,
Expand All @@ -1466,7 +1465,7 @@ export default async function getBaseWebpackConfig(
},
{
test: codeCondition.test,
issuerLayer: WEBPACK_LAYERS.middleware,
issuerLayer: WEBPACK_LAYERS.serverComponents,
use: middlewareLayerLoaders,
},
{
Expand Down Expand Up @@ -1716,7 +1715,7 @@ export default async function getBaseWebpackConfig(
runtime = 'app-route'
break
case WEBPACK_LAYERS.serverSideRendering:
case WEBPACK_LAYERS.reactServerComponents:
case WEBPACK_LAYERS.serverComponents:
case WEBPACK_LAYERS.appPagesBrowser:
case WEBPACK_LAYERS.actionBrowser:
runtime = 'app-page'
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/build/webpack/config/blocks/css/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ const regexSassModules = /\.module\.(scss|sass)$/

const APP_LAYER_RULE = {
or: [
WEBPACK_LAYERS.reactServerComponents,
WEBPACK_LAYERS.serverComponents,
WEBPACK_LAYERS.serverSideRendering,
WEBPACK_LAYERS.appPagesBrowser,
],
}

const PAGES_LAYER_RULE = {
not: [
WEBPACK_LAYERS.reactServerComponents,
WEBPACK_LAYERS.serverComponents,
WEBPACK_LAYERS.serverSideRendering,
WEBPACK_LAYERS.appPagesBrowser,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ export class FlightClientEntryPlugin {
currentCompilerServerActions[id].workers[bundlePath] = ''
currentCompilerServerActions[id].layer[bundlePath] = fromClient
? WEBPACK_LAYERS.actionBrowser
: WEBPACK_LAYERS.reactServerComponents
: WEBPACK_LAYERS.serverComponents
}
}

Expand All @@ -923,7 +923,7 @@ export class FlightClientEntryPlugin {
name: entryName,
layer: fromClient
? WEBPACK_LAYERS.actionBrowser
: WEBPACK_LAYERS.reactServerComponents,
: WEBPACK_LAYERS.serverComponents,
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ export class NextTypesPlugin {
return
}
if (
mod.layer !== WEBPACK_LAYERS.reactServerComponents &&
mod.layer !== WEBPACK_LAYERS.serverComponents &&
mod.layer !== WEBPACK_LAYERS.appRouteHandler
)
return
Expand Down
16 changes: 8 additions & 8 deletions packages/next/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ const WEBPACK_LAYERS_NAMES = {
*/
shared: 'shared',
/**
* React Server Components layer (rsc).
* React Server Components layer, where apply react-server conditions.
*/
reactServerComponents: 'rsc',
serverComponents: 'react-server',
/**
* Server Side Rendering layer for app (ssr).
*/
Expand All @@ -129,11 +129,11 @@ const WEBPACK_LAYERS_NAMES = {
/**
* The layer for the API routes.
*/
api: 'api',
// api: 'api',
/**
* The layer for the middleware code.
*/
middleware: 'middleware',
// middleware: 'middleware',
/**
* The layer for the instrumentation hooks.
*/
Expand Down Expand Up @@ -163,23 +163,23 @@ const WEBPACK_LAYERS = {
...WEBPACK_LAYERS_NAMES,
GROUP: {
serverOnly: [
WEBPACK_LAYERS_NAMES.reactServerComponents,
WEBPACK_LAYERS_NAMES.serverComponents,
WEBPACK_LAYERS_NAMES.actionBrowser,
WEBPACK_LAYERS_NAMES.appMetadataRoute,
WEBPACK_LAYERS_NAMES.appRouteHandler,
WEBPACK_LAYERS_NAMES.instrument,
WEBPACK_LAYERS_NAMES.middleware,
// WEBPACK_LAYERS_NAMES.middleware,
],
neutralTarget: [
// pages api
WEBPACK_LAYERS_NAMES.api,
// WEBPACK_LAYERS_NAMES.api,
],
clientOnly: [
WEBPACK_LAYERS_NAMES.serverSideRendering,
WEBPACK_LAYERS_NAMES.appPagesBrowser,
],
app: [
WEBPACK_LAYERS_NAMES.reactServerComponents,
WEBPACK_LAYERS_NAMES.serverComponents,
WEBPACK_LAYERS_NAMES.actionBrowser,
WEBPACK_LAYERS_NAMES.appMetadataRoute,
WEBPACK_LAYERS_NAMES.appRouteHandler,
Expand Down
6 changes: 3 additions & 3 deletions packages/next/src/server/dev/hot-reloader-webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1153,7 +1153,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
.toString('hex')

if (
mod.layer === WEBPACK_LAYERS.reactServerComponents &&
mod.layer === WEBPACK_LAYERS.serverComponents &&
mod?.buildInfo?.rsc?.type !== 'client'
) {
chunksHashServerLayer.add(hash)
Expand All @@ -1168,7 +1168,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
)

if (
mod.layer === WEBPACK_LAYERS.reactServerComponents &&
mod.layer === WEBPACK_LAYERS.serverComponents &&
mod?.buildInfo?.rsc?.type !== 'client'
) {
chunksHashServerLayer.add(hash)
Expand Down Expand Up @@ -1202,7 +1202,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {

if (serverComponentChangedItems) {
const serverKey =
WEBPACK_LAYERS.reactServerComponents + ':' + key
WEBPACK_LAYERS.serverComponents + ':' + key
const prevServerHash = pageHashMap.get(serverKey)
const curServerHash = chunksHashServerLayer.toString()
if (prevServerHash && prevServerHash !== curServerHash) {
Expand Down
1 change: 1 addition & 0 deletions test/e2e/module-layer/node_modules/external-lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/e2e/module-layer/node_modules/external-lib/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions test/e2e/module-layer/pages/api/mixed.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import '../../lib/mixed-lib'
// import '../../lib/mixed-lib'
import { externalValue } from 'external-lib'

import React from 'react'

export default function handler(req, res) {
return res.send('pages/api/mixed.js:')
if (Object(React).useState) {
throw new Error('React.useState should not be available')
}
return res.send('pages/api/mixed.js:' + externalValue)
}

0 comments on commit b218bf2

Please sign in to comment.