Skip to content

Commit

Permalink
fix file runtime build and flight server loader
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Mar 29, 2022
1 parent c283925 commit 9798aed
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 27 deletions.
24 changes: 13 additions & 11 deletions packages/next/build/entries.ts
Expand Up @@ -94,20 +94,20 @@ export function createPagesMapping(
// allow falling back to the correct source file so
// that HMR can work properly when a file is added/removed
if (isDev) {
pages['/_app'] = `${PAGES_DIR_ALIAS}/_app`
pages['/_error'] = `${PAGES_DIR_ALIAS}/_error`
pages['/_document'] = `${PAGES_DIR_ALIAS}/_document`
if (hasServerComponents) {
pages['/_app.server'] = `${PAGES_DIR_ALIAS}/_app.server`
}
pages['/_app'] = `${PAGES_DIR_ALIAS}/_app`
pages['/_error'] = `${PAGES_DIR_ALIAS}/_error`
pages['/_document'] = `${PAGES_DIR_ALIAS}/_document`
} else {
pages['/_app'] = pages['/_app'] || 'next/dist/pages/_app'
pages['/_error'] = pages['/_error'] || 'next/dist/pages/_error'
pages['/_document'] = pages['/_document'] || 'next/dist/pages/_document'
if (hasServerComponents) {
pages['/_app.server'] =
pages['/_app.server'] || 'next/dist/pages/_app.server'
}
pages['/_app'] = pages['/_app'] || 'next/dist/pages/_app'
pages['/_error'] = pages['/_error'] || 'next/dist/pages/_error'
pages['/_document'] = pages['/_document'] || 'next/dist/pages/_document'
}
return pages
}
Expand Down Expand Up @@ -137,6 +137,7 @@ export async function getPageRuntime(
encoding: 'utf8',
})
} catch (err) {
console.error(err)
return undefined
}

Expand Down Expand Up @@ -280,11 +281,12 @@ export async function createEntrypoints(
const isReserved = isReservedPage(page)
const isCustomError = isCustomErrorPage(page)
const isFlight = isFlightPage(config, absolutePagePath)
const isEdgeRuntime =
(await getPageRuntime(
join(pagesDir, absolutePagePath.slice(PAGES_DIR_ALIAS.length + 1)),
globalRuntime
)) === 'edge'
const isInternalPages = !absolutePagePath.includes(PAGES_DIR_ALIAS)
const pageFilePath = isInternalPages
? require.resolve(absolutePagePath)
: join(pagesDir, absolutePagePath.replace(PAGES_DIR_ALIAS, ''))
const pageRuntime = await getPageRuntime(pageFilePath, globalRuntime)
const isEdgeRuntime = pageRuntime === 'edge'

if (page.match(MIDDLEWARE_ROUTE)) {
const loaderOpts: MiddlewareLoaderOptions = {
Expand Down
14 changes: 11 additions & 3 deletions packages/next/build/webpack-config.ts
Expand Up @@ -977,6 +977,14 @@ export default async function getBaseWebpackConfig(
},
}

const rscCodeCondition = {
test: codeCondition.test,
include: codeCondition.include
? [...codeCondition.include, /node_modules/]
: /node_modules/,
exclude: () => false,
}

let webpackConfig: webpack.Configuration = {
parallelism: Number(process.env.NEXT_WEBPACK_PARALLELISM) || undefined,
externals: targetWeb
Expand Down Expand Up @@ -1199,7 +1207,7 @@ export default async function getBaseWebpackConfig(
? [
// RSC server compilation loaders
{
...codeCondition,
...rscCodeCondition,
use: {
loader: 'next-flight-server-loader',
options: {
Expand All @@ -1208,7 +1216,7 @@ export default async function getBaseWebpackConfig(
},
},
{
test: codeCondition.test,
...rscCodeCondition,
resourceQuery: /__sc_client__/,
use: {
loader: 'next-flight-client-loader',
Expand All @@ -1218,7 +1226,7 @@ export default async function getBaseWebpackConfig(
: [
// RSC client compilation loaders
{
...codeCondition,
...rscCodeCondition,
test: serverComponentsRegex,
use: {
loader: 'next-flight-server-loader',
Expand Down
Expand Up @@ -136,12 +136,6 @@ export default async function transformSource(
throw new Error('Expected source to have been transformed to a string.')
}

// We currently assume that all components are shared components (unsuffixed)
// from node_modules.
if (resourcePath.includes('/node_modules/')) {
return source
}

const rawRawPageExtensions = getRawPageExtensions(pageExtensions)
const isServerComponent = createServerComponentFilter(rawRawPageExtensions)
const isClientComponent = createClientComponentFilter(rawRawPageExtensions)
Expand Down
Expand Up @@ -17,13 +17,17 @@ export default async function middlewareSSRLoader(this: any) {

const stringifiedPagePath = stringifyRequest(this, absolutePagePath)
const stringifiedAppPath = stringifyRequest(this, absoluteAppPath)
const stringifiedAppServerPath = stringifyRequest(this, absoluteAppServerPath)
const stringifiedAppServerPath = absoluteAppServerPath
? stringifyRequest(this, absoluteAppServerPath)
: null

const stringifiedErrorPath = stringifyRequest(this, absoluteErrorPath)
const stringifiedDocumentPath = stringifyRequest(this, absoluteDocumentPath)
const stringified500Path = absolute500Path
? stringifyRequest(this, absolute500Path)
: 'null'
: null

console.log('stringifiedAppServerPath', stringifiedAppServerPath)

const transformed = `
import { adapter } from 'next/dist/server/web/adapter'
Expand All @@ -34,11 +38,16 @@ export default async function middlewareSSRLoader(this: any) {
import Document from ${stringifiedDocumentPath}
const appMod = require(${stringifiedAppPath})
const appServerMod = require(${stringifiedAppServerPath})
const appServerMod = ${
stringifiedAppServerPath ? `require(${stringifiedAppServerPath})` : 'null'
}
const pageMod = require(${stringifiedPagePath})
const errorMod = require(${stringifiedErrorPath})
const error500Mod = ${stringified500Path} ? require(${stringified500Path}) : null
const error500Mod = ${
stringified500Path ? `require(${stringified500Path})` : 'null'
}
const buildManifest = self.__BUILD_MANIFEST
const reactLoadableManifest = self.__REACT_LOADABLE_MANIFEST
const rscManifest = self.__RSC_MANIFEST
Expand All @@ -60,7 +69,7 @@ export default async function middlewareSSRLoader(this: any) {
buildManifest,
reactLoadableManifest,
serverComponentManifest: ${isServerComponent} ? rscManifest : null,
appServerMod: appServerMod,
appServerMod,
isServerComponent: ${isServerComponent},
config: ${stringifiedConfig},
buildId: ${JSON.stringify(buildId)},
Expand Down
2 changes: 1 addition & 1 deletion packages/next/server/render.tsx
Expand Up @@ -1341,7 +1341,7 @@ export async function renderToHTML(
) : (
<Body>
<AppContainerWithIsomorphicFiberStructure>
{isServerComponent && AppServerMod.__next_rsc__ ? (
{isServerComponent && !!AppMod.__next_rsc__ ? (
// _app.server.js is used.
<Component {...props.pageProps} router={router} />
) : (
Expand Down
Expand Up @@ -14,7 +14,7 @@ export default function Index({ header, router }) {
</div>
)
}

/*
export function getServerSideProps({ req }) {
const { headers } = req
const header = headers[headerKey] || ''
Expand All @@ -25,3 +25,4 @@ export function getServerSideProps({ req }) {
},
}
}
*/

0 comments on commit 9798aed

Please sign in to comment.