Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bypass empty pages folder for layouts #40132

Merged
merged 14 commits into from Sep 3, 2022
17 changes: 7 additions & 10 deletions packages/next/build/entries.ts
Expand Up @@ -69,12 +69,14 @@ export function createPagesMapping({
pageExtensions,
pagePaths,
pagesType,
pagesDir,
}: {
hasServerComponents: boolean
isDev: boolean
pageExtensions: string[]
pagePaths: string[]
pagesType: 'pages' | 'root' | 'app'
pagesDir: string | undefined
}): { [page: string]: string } {
const previousPages: { [key: string]: string } = {}
const pages = pagePaths.reduce<{ [key: string]: string }>(
Expand Down Expand Up @@ -133,7 +135,7 @@ export function createPagesMapping({
// In development we always alias these to allow Webpack to fallback to
// the correct source file so that HMR can work properly when a file is
// added or removed.
const root = isDev ? PAGES_DIR_ALIAS : 'next/dist/pages'
const root = isDev && pagesDir ? PAGES_DIR_ALIAS : 'next/dist/pages'

return {
'/_app': `${root}/_app`,
Expand All @@ -149,7 +151,7 @@ interface CreateEntrypointsParams {
envFiles: LoadedEnvFiles
isDev?: boolean
pages: { [page: string]: string }
pagesDir: string
pagesDir?: string
previewMode: __ApiPreviewProps
rootDir: string
rootPaths?: Record<string, string>
Expand Down Expand Up @@ -366,7 +368,7 @@ export async function createEntrypoints(params: CreateEntrypointsParams) {

// Handle paths that have aliases
const pageFilePath = (() => {
if (absolutePagePath.startsWith(PAGES_DIR_ALIAS)) {
if (absolutePagePath.startsWith(PAGES_DIR_ALIAS) && pagesDir) {
return absolutePagePath.replace(PAGES_DIR_ALIAS, pagesDir)
}

Expand Down Expand Up @@ -444,12 +446,7 @@ export async function createEntrypoints(params: CreateEntrypointsParams) {
})
}
} else {
server[serverBundlePath] = isServerComponent
? {
import: mappings[page],
layer: WEBPACK_LAYERS.server,
}
: [mappings[page]]
server[serverBundlePath] = [mappings[page]]
}
},
onEdgeServer: () => {
Expand Down Expand Up @@ -490,7 +487,7 @@ export async function createEntrypoints(params: CreateEntrypointsParams) {
await Promise.all(Object.keys(pages).map(getEntryHandler(pages, 'pages')))

if (nestedMiddleware.length > 0) {
throw new NestedMiddlewareError(nestedMiddleware, rootDir, pagesDir)
throw new NestedMiddlewareError(nestedMiddleware, rootDir, pagesDir!)
}

return {
Expand Down
39 changes: 25 additions & 14 deletions packages/next/build/index.ts
Expand Up @@ -314,7 +314,9 @@ export default async function build(
eventCliSession(dir, config, {
webpackVersion: 5,
cliCommand: 'build',
isSrcDir: path.relative(dir, pagesDir!).startsWith('src'),
isSrcDir:
(!!pagesDir && path.relative(dir, pagesDir).startsWith('src')) ||
(!!appDir && path.relative(dir, appDir).startsWith('src')),
hasNowJson: !!(await findUp('now.json', { cwd: dir })),
isCustomServer: null,
})
Expand Down Expand Up @@ -395,7 +397,8 @@ export default async function build(
config.eslint?.dirs,
config.experimental.cpus,
config.experimental.workerThreads,
telemetry
telemetry,
!!config.experimental.appDir
)
}),
])
Expand Down Expand Up @@ -436,14 +439,16 @@ export default async function build(

const isLikeServerless = isTargetLikeServerless(target)

const pagesPaths = await nextBuildSpan
.traceChild('collect-pages')
.traceAsyncFn(() =>
recursiveReadDir(
pagesDir,
new RegExp(`\\.(?:${config.pageExtensions.join('|')})$`)
)
)
const pagesPaths = pagesDir
? await nextBuildSpan
.traceChild('collect-pages')
.traceAsyncFn(() =>
recursiveReadDir(
pagesDir,
new RegExp(`\\.(?:${config.pageExtensions.join('|')})$`)
)
)
: []

let appPaths: string[] | undefined

Expand All @@ -462,9 +467,11 @@ export default async function build(
`^${MIDDLEWARE_FILENAME}\\.(?:${config.pageExtensions.join('|')})$`
)

const rootPaths = (
await flatReaddir(join(pagesDir, '..'), middlewareDetectionRegExp)
).map((absoluteFile) => absoluteFile.replace(dir, ''))
const rootPaths = pagesDir
? (
await flatReaddir(join(pagesDir, '..'), middlewareDetectionRegExp)
).map((absoluteFile) => absoluteFile.replace(dir, ''))
: []

// needed for static exporting since we want to replace with HTML
// files
Expand All @@ -487,6 +494,7 @@ export default async function build(
pageExtensions: config.pageExtensions,
pagesType: 'pages',
pagePaths: pagesPaths,
pagesDir,
})
)

Expand All @@ -502,6 +510,7 @@ export default async function build(
isDev: false,
pagesType: 'app',
pageExtensions: config.pageExtensions,
pagesDir: pagesDir,
})
)
}
Expand All @@ -514,6 +523,7 @@ export default async function build(
pageExtensions: config.pageExtensions,
pagePaths: rootPaths,
pagesType: 'root',
pagesDir: pagesDir,
})
}

Expand Down Expand Up @@ -1256,7 +1266,7 @@ export default async function build(
: appPaths?.find((p) => p.startsWith(actualPage + '/page.'))

const pageRuntime =
pageType === 'pages' && pagePath
pagesDir && pageType === 'pages' && pagePath
? (
await getPageStaticInfo({
pageFilePath: join(pagesDir, pagePath),
Expand Down Expand Up @@ -1913,6 +1923,7 @@ export default async function build(
await staticWorkers.end()
}
: undefined,
appPaths,
}
const exportConfig: any = {
...config,
Expand Down
29 changes: 24 additions & 5 deletions packages/next/build/utils.ts
Expand Up @@ -305,7 +305,7 @@ export async function printTreeView(
}: {
distPath: string
buildId: string
pagesDir: string
pagesDir?: string
pageExtensions: string[]
buildManifest: BuildManifest
appBuildManifest?: AppBuildManifest
Expand Down Expand Up @@ -345,7 +345,8 @@ export async function printTreeView(
.replace(/(?:^|[.-])([0-9a-z]{6})[0-9a-z]{14}(?=\.)/, '.$1')

// Check if we have a custom app.
const hasCustomApp = await findPageFile(pagesDir, '/_app', pageExtensions)
const hasCustomApp =
pagesDir && (await findPageFile(pagesDir, '/_app', pageExtensions, false))

const filterAndSortList = (list: ReadonlyArray<string>) =>
list
Expand Down Expand Up @@ -1081,7 +1082,13 @@ export async function isPageStatic({
getStaticProps: mod.getStaticProps,
}
} else {
componentsResult = await loadComponents(distDir, page, serverless)
componentsResult = await loadComponents(
distDir,
page,
serverless,
false,
false
)
}
const Comp = componentsResult.Component

Expand Down Expand Up @@ -1207,7 +1214,13 @@ export async function hasCustomGetInitialProps(
): Promise<boolean> {
require('../shared/lib/runtime-config').setConfig(runtimeEnvConfig)

const components = await loadComponents(distDir, page, isLikeServerless)
const components = await loadComponents(
distDir,
page,
isLikeServerless,
false,
false
)
let mod = components.ComponentMod

if (checkingApp) {
Expand All @@ -1226,7 +1239,13 @@ export async function getNamedExports(
runtimeEnvConfig: any
): Promise<Array<string>> {
require('../shared/lib/runtime-config').setConfig(runtimeEnvConfig)
const components = await loadComponents(distDir, page, isLikeServerless)
const components = await loadComponents(
distDir,
page,
isLikeServerless,
false,
false
)
let mod = components.ComponentMod

return Object.keys(mod)
Expand Down
38 changes: 23 additions & 15 deletions packages/next/build/webpack-config.ts
Expand Up @@ -518,7 +518,7 @@ export default async function getBaseWebpackConfig(
entrypoints: webpack.EntryObject
hasReactRoot: boolean
isDevFallback?: boolean
pagesDir: string
pagesDir?: string
reactProductionProfiling?: boolean
rewrites: CustomRoutes['rewrites']
runWebpackSpan: Span
Expand Down Expand Up @@ -794,24 +794,30 @@ export default async function getBaseWebpackConfig(

if (dev) {
customAppAliases[`${PAGES_DIR_ALIAS}/_app`] = [
...rawPageExtensions.reduce((prev, ext) => {
prev.push(path.join(pagesDir, `_app.${ext}`))
return prev
}, [] as string[]),
...(pagesDir
? rawPageExtensions.reduce((prev, ext) => {
prev.push(path.join(pagesDir, `_app.${ext}`))
return prev
}, [] as string[])
: []),
'next/dist/pages/_app.js',
]
customAppAliases[`${PAGES_DIR_ALIAS}/_error`] = [
...rawPageExtensions.reduce((prev, ext) => {
prev.push(path.join(pagesDir, `_error.${ext}`))
return prev
}, [] as string[]),
...(pagesDir
? rawPageExtensions.reduce((prev, ext) => {
prev.push(path.join(pagesDir, `_error.${ext}`))
return prev
}, [] as string[])
: []),
'next/dist/pages/_error.js',
]
customDocumentAliases[`${PAGES_DIR_ALIAS}/_document`] = [
...rawPageExtensions.reduce((prev, ext) => {
prev.push(path.join(pagesDir, `_document.${ext}`))
return prev
}, [] as string[]),
...(pagesDir
? rawPageExtensions.reduce((prev, ext) => {
prev.push(path.join(pagesDir, `_document.${ext}`))
return prev
}, [] as string[])
: []),
`next/dist/pages/_document.js`,
]
}
Expand Down Expand Up @@ -850,7 +856,7 @@ export default async function getBaseWebpackConfig(
...customDocumentAliases,
...customRootAliases,

[PAGES_DIR_ALIAS]: pagesDir,
...(pagesDir ? { [PAGES_DIR_ALIAS]: pagesDir } : {}),
...(appDir
? {
[APP_DIR_ALIAS]: appDir,
Expand Down Expand Up @@ -2051,7 +2057,9 @@ export default async function getBaseWebpackConfig(
webpackConfig = await buildConfiguration(webpackConfig, {
supportedBrowsers,
rootDirectory: dir,
customAppFile: new RegExp(escapeStringRegexp(path.join(pagesDir, `_app`))),
customAppFile: pagesDir
? new RegExp(escapeStringRegexp(path.join(pagesDir, `_app`)))
: undefined,
isDevelopment: dev,
isServer: isNodeServer || isEdgeServer,
isEdgeRuntime: isEdgeServer,
Expand Down
2 changes: 1 addition & 1 deletion packages/next/build/webpack/config/index.ts
Expand Up @@ -25,7 +25,7 @@ export async function build(
}: {
supportedBrowsers: string[] | undefined
rootDirectory: string
customAppFile: RegExp
customAppFile: RegExp | undefined
isDevelopment: boolean
isServer: boolean
isEdgeRuntime: boolean
Expand Down
2 changes: 1 addition & 1 deletion packages/next/build/webpack/config/utils.ts
Expand Up @@ -4,7 +4,7 @@ import type { NextConfigComplete } from '../../../server/config-shared'
export type ConfigurationContext = {
supportedBrowsers: string[] | undefined
rootDirectory: string
customAppFile: RegExp
customAppFile: RegExp | undefined

isDevelopment: boolean
isProduction: boolean
Expand Down
11 changes: 8 additions & 3 deletions packages/next/build/webpack/plugins/react-loadable-plugin.ts
Expand Up @@ -53,9 +53,14 @@ function getChunkGroupFromBlock(
function buildManifest(
_compiler: webpack.Compiler,
compilation: webpack.Compilation,
pagesDir: string,
pagesDir: string | undefined,
dev: boolean
) {
// If there's no pagesDir, output an empty manifest
if (!pagesDir) {
return {}
}

let manifest: { [k: string]: { id: string | number; files: string[] } } = {}

// This is allowed:
Expand Down Expand Up @@ -145,13 +150,13 @@ function buildManifest(

export class ReactLoadablePlugin {
private filename: string
private pagesDir: string
private pagesDir?: string
private runtimeAsset?: string
private dev: boolean

constructor(opts: {
filename: string
pagesDir: string
pagesDir?: string
runtimeAsset?: string
dev: boolean
}) {
Expand Down