Unverified Commit 1dcff1da authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

Pages Module Transition (#49962)

This serves to create the loader that will allow us to transition more
of the rendering logic into each of the bundled entrypoints. This only
applies to pages routes inside the `pages/` folder in the node
environment.

fix NEXT-985

---------
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent dd85daaf
...@@ -52,6 +52,8 @@ import { EdgeFunctionLoaderOptions } from './webpack/loaders/next-edge-function- ...@@ -52,6 +52,8 @@ import { EdgeFunctionLoaderOptions } from './webpack/loaders/next-edge-function-
import { isAppRouteRoute } from '../lib/is-app-route-route' import { isAppRouteRoute } from '../lib/is-app-route-route'
import { normalizeMetadataRoute } from '../lib/metadata/get-metadata-route' import { normalizeMetadataRoute } from '../lib/metadata/get-metadata-route'
import { fileExists } from '../lib/file-exists' import { fileExists } from '../lib/file-exists'
import { getRouteLoaderEntry } from './webpack/loaders/next-route-loader'
import { isInternalPathname } from '../lib/is-internal-pathname'
export async function getStaticInfoIncludingLayouts({ export async function getStaticInfoIncludingLayouts({
isInsideAppDir, isInsideAppDir,
...@@ -538,7 +540,7 @@ export async function createEntrypoints( ...@@ -538,7 +540,7 @@ export async function createEntrypoints(
// server compiler inject them instead. // server compiler inject them instead.
} else { } else {
client[clientBundlePath] = getClientEntry({ client[clientBundlePath] = getClientEntry({
absolutePagePath: mappings[page], absolutePagePath,
page, page,
}) })
} }
...@@ -549,7 +551,7 @@ export async function createEntrypoints( ...@@ -549,7 +551,7 @@ export async function createEntrypoints(
server[serverBundlePath] = getAppEntry({ server[serverBundlePath] = getAppEntry({
page, page,
name: serverBundlePath, name: serverBundlePath,
pagePath: mappings[page], pagePath: absolutePagePath,
appDir, appDir,
appPaths: matchedAppPaths, appPaths: matchedAppPaths,
pageExtensions, pageExtensions,
...@@ -558,16 +560,26 @@ export async function createEntrypoints( ...@@ -558,16 +560,26 @@ export async function createEntrypoints(
nextConfigOutput: config.output, nextConfigOutput: config.output,
preferredRegion: staticInfo.preferredRegion, preferredRegion: staticInfo.preferredRegion,
}) })
} else { } else if (isInstrumentationHookFile(page) && pagesType === 'root') {
if (isInstrumentationHookFile(page) && pagesType === 'root') {
server[serverBundlePath.replace('src/', '')] = { server[serverBundlePath.replace('src/', '')] = {
import: mappings[page], import: absolutePagePath,
// the '../' is needed to make sure the file is not chunked // the '../' is needed to make sure the file is not chunked
filename: `../${INSTRUMENTATION_HOOK_FILENAME}.js`, filename: `../${INSTRUMENTATION_HOOK_FILENAME}.js`,
} }
} else if (
!isAPIRoute(page) &&
!isMiddlewareFile(page) &&
!isInternalPathname(absolutePagePath)
) {
server[serverBundlePath] = [
getRouteLoaderEntry({
page,
absolutePagePath,
preferredRegion: staticInfo.preferredRegion,
}),
]
} else { } else {
server[serverBundlePath] = [mappings[page]] server[serverBundlePath] = [absolutePagePath]
}
} }
}, },
onEdgeServer: () => { onEdgeServer: () => {
...@@ -577,7 +589,7 @@ export async function createEntrypoints( ...@@ -577,7 +589,7 @@ export async function createEntrypoints(
appDirLoader = getAppEntry({ appDirLoader = getAppEntry({
name: serverBundlePath, name: serverBundlePath,
page, page,
pagePath: mappings[page], pagePath: absolutePagePath,
appDir: appDir!, appDir: appDir!,
appPaths: matchedAppPaths, appPaths: matchedAppPaths,
pageExtensions, pageExtensions,
...@@ -596,7 +608,7 @@ export async function createEntrypoints( ...@@ -596,7 +608,7 @@ export async function createEntrypoints(
edgeServer[normalizedServerBundlePath] = getEdgeServerEntry({ edgeServer[normalizedServerBundlePath] = getEdgeServerEntry({
...params, ...params,
rootDir, rootDir,
absolutePagePath: mappings[page], absolutePagePath: absolutePagePath,
bundlePath: clientBundlePath, bundlePath: clientBundlePath,
isDev: false, isDev: false,
isServerComponent, isServerComponent,
......
...@@ -1781,6 +1781,7 @@ export default async function getBaseWebpackConfig( ...@@ -1781,6 +1781,7 @@ export default async function getBaseWebpackConfig(
'next-middleware-asset-loader', 'next-middleware-asset-loader',
'next-middleware-wasm-loader', 'next-middleware-wasm-loader',
'next-app-loader', 'next-app-loader',
'next-route-loader',
'next-font-loader', 'next-font-loader',
'next-invalid-import-error-loader', 'next-invalid-import-error-loader',
'next-metadata-route-loader', 'next-metadata-route-loader',
......
import type { webpack } from 'next/dist/compiled/webpack/webpack'
import { stringify } from 'querystring'
import { getModuleBuildInfo } from './get-module-build-info'
/**
* The options for the route loader.
*/
type RouteLoaderOptions = {
/**
* The page name for this particular route.
*/
page: string
/**
* The preferred region for this route.
*/
preferredRegion: string | string[] | undefined
/**
* The absolute path to the userland page file.
*/
absolutePagePath: string
}
/**
* Returns the loader entry for a given page.
*
* @param query the options to create the loader entry
* @returns the encoded loader entry
*/
export function getRouteLoaderEntry(query: RouteLoaderOptions): string {
return `next-route-loader?${stringify(query)}!`
}
/**
* Handles the `next-route-loader` options.
* @returns the loader definition function
*/
const loader: webpack.LoaderDefinitionFunction<RouteLoaderOptions> =
function () {
const { page, preferredRegion, absolutePagePath } = this.getOptions()
// Ensure we only run this loader for as a module.
if (!this._module) {
throw new Error('Invariant: expected this to reference a module')
}
// Attach build info to the module.
const buildInfo = getModuleBuildInfo(this._module)
buildInfo.route = {
page,
absolutePagePath,
preferredRegion,
}
return `
// Next.js Route Loader
export * from ${JSON.stringify(absolutePagePath)}
export { default } from ${JSON.stringify(absolutePagePath)}
`
}
export default loader
...@@ -285,20 +285,24 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance { ...@@ -285,20 +285,24 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {
// don't include the entry itself in the trace // don't include the entry itself in the trace
entryFiles.delete(nodePath.join(outputPath, `../${entrypoint.name}.js`)) entryFiles.delete(nodePath.join(outputPath, `../${entrypoint.name}.js`))
assets[traceOutputName] = new sources.RawSource( const finalFiles: string[] = []
JSON.stringify({
version: TRACE_OUTPUT_VERSION, for (const file of new Set([
files: [
...new Set([
...entryFiles, ...entryFiles,
...allEntryFiles, ...allEntryFiles,
...(this.entryTraces.get(entrypoint.name) || []), ...(this.entryTraces.get(entrypoint.name) || []),
]), ])) {
].map((file) => { if (file) {
return nodePath finalFiles.push(
.relative(traceOutputPath, file) nodePath.relative(traceOutputPath, file).replace(/\\/g, '/')
.replace(/\\/g, '/') )
}), }
}
assets[traceOutputName] = new sources.RawSource(
JSON.stringify({
version: TRACE_OUTPUT_VERSION,
files: finalFiles,
}) })
) )
} }
...@@ -369,7 +373,8 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance { ...@@ -369,7 +373,8 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {
entryModMap.set(absolutePath, entryMod) entryModMap.set(absolutePath, entryMod)
entryNameMap.set(absolutePath, name) entryNameMap.set(absolutePath, name)
} }
} else { }
// If there was no `route` property, we can assume that it was something custom instead. // If there was no `route` property, we can assume that it was something custom instead.
// In order to trace these we add them to the additionalEntries map. // In order to trace these we add them to the additionalEntries map.
if (entryMod.request) { if (entryMod.request) {
...@@ -383,18 +388,11 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance { ...@@ -383,18 +388,11 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {
curMap.set(entryMod.resource, entryMod) curMap.set(entryMod.resource, entryMod)
} }
} }
}
if (entryMod && entryMod.resource) { if (entryMod && entryMod.resource) {
const normalizedResource = entryMod.resource.replace(
/\\/g,
'/'
)
if (normalizedResource.includes('pages/')) {
entryNameMap.set(entryMod.resource, name) entryNameMap.set(entryMod.resource, name)
entryModMap.set(entryMod.resource, entryMod) entryModMap.set(entryMod.resource, entryMod)
} else {
let curMap = additionalEntries.get(name) let curMap = additionalEntries.get(name)
if (!curMap) { if (!curMap) {
...@@ -406,7 +404,6 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance { ...@@ -406,7 +404,6 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {
} }
} }
} }
}
}) })
}) })
...@@ -545,6 +542,7 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance { ...@@ -545,6 +542,7 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {
this.tracingRoot, this.tracingRoot,
entry entry
) )
const curExtraEntries = additionalEntries.get(entryName) const curExtraEntries = additionalEntries.get(entryName)
const finalDeps = new Set<string>() const finalDeps = new Set<string>()
......
...@@ -136,7 +136,7 @@ export async function getNotFoundError( ...@@ -136,7 +136,7 @@ export async function getNotFoundError(
.filter( .filter(
(name) => (name) =>
name && name &&
!/next-(app|middleware|client-pages|flight-(client|server|client-entry))-loader\.js/.test( !/next-(app|middleware|client-pages|route|flight-(client|server|client-entry))-loader\.js/.test(
name name
) && ) &&
!/css-loader.+\.js/.test(name) !/css-loader.+\.js/.test(name)
......
...@@ -48,7 +48,7 @@ function formatMessage( ...@@ -48,7 +48,7 @@ function formatMessage(
message.moduleTrace && message.moduleTrace &&
message.moduleTrace.filter( message.moduleTrace.filter(
(trace: any) => (trace: any) =>
!/next-(middleware|client-pages|edge-function)-loader\.js/.test( !/next-(middleware|client-pages|route|edge-function)-loader\.js/.test(
trace.originName trace.originName
) )
) )
......
export function isInternalPathname(pathname: string): boolean {
return pathname.startsWith('next/dist/pages/')
}
...@@ -42,7 +42,11 @@ import { denormalizePagePath } from '../../shared/lib/page-path/denormalize-page ...@@ -42,7 +42,11 @@ import { denormalizePagePath } from '../../shared/lib/page-path/denormalize-page
import { normalizePathSep } from '../../shared/lib/page-path/normalize-path-sep' import { normalizePathSep } from '../../shared/lib/page-path/normalize-path-sep'
import getRouteFromEntrypoint from '../get-route-from-entrypoint' import getRouteFromEntrypoint from '../get-route-from-entrypoint'
import { fileExists } from '../../lib/file-exists' import { fileExists } from '../../lib/file-exists'
import { difference, isMiddlewareFilename } from '../../build/utils' import {
difference,
isMiddlewareFile,
isMiddlewareFilename,
} from '../../build/utils'
import { DecodeError } from '../../shared/lib/utils' import { DecodeError } from '../../shared/lib/utils'
import { Span, trace } from '../../trace' import { Span, trace } from '../../trace'
import { getProperError } from '../../lib/is-error' import { getProperError } from '../../lib/is-error'
...@@ -53,6 +57,9 @@ import { getRegistry } from '../../lib/helpers/get-registry' ...@@ -53,6 +57,9 @@ import { getRegistry } from '../../lib/helpers/get-registry'
import { RouteMatch } from '../future/route-matches/route-match' import { RouteMatch } from '../future/route-matches/route-match'
import type { Telemetry } from '../../telemetry/storage' import type { Telemetry } from '../../telemetry/storage'
import { parseVersionInfo, VersionInfo } from './parse-version-info' import { parseVersionInfo, VersionInfo } from './parse-version-info'
import { isAPIRoute } from '../../lib/is-api-route'
import { getRouteLoaderEntry } from '../../build/webpack/loaders/next-route-loader'
import { isInternalPathname } from '../../lib/is-internal-pathname'
function diff(a: Set<any>, b: Set<any>) { function diff(a: Set<any>, b: Set<any>) {
return new Set([...a].filter((v) => !b.has(v))) return new Set([...a].filter((v) => !b.has(v)))
...@@ -820,12 +827,10 @@ export default class HotReloader { ...@@ -820,12 +827,10 @@ export default class HotReloader {
) { ) {
relativeRequest = `./${relativeRequest}` relativeRequest = `./${relativeRequest}`
} }
entrypoints[bundlePath] = finalizeEntrypoint({
compilerType: COMPILER_NAMES.server, let value: { import: string; layer?: string } | string
name: bundlePath, if (isAppPath) {
isServerComponent, value = getAppEntry({
value: isAppPath
? getAppEntry({
name: bundlePath, name: bundlePath,
page, page,
appPaths: entryData.appPaths, appPaths: entryData.appPaths,
...@@ -846,7 +851,25 @@ export default class HotReloader { ...@@ -846,7 +851,25 @@ export default class HotReloader {
nextConfigOutput: this.config.output, nextConfigOutput: this.config.output,
preferredRegion: staticInfo.preferredRegion, preferredRegion: staticInfo.preferredRegion,
}) })
: relativeRequest, } else if (
!isAPIRoute(page) &&
!isMiddlewareFile(page) &&
!isInternalPathname(relativeRequest)
) {
value = getRouteLoaderEntry({
page,
absolutePagePath: relativeRequest,
preferredRegion: staticInfo.preferredRegion,
})
} else {
value = relativeRequest
}
entrypoints[bundlePath] = finalizeEntrypoint({
compilerType: COMPILER_NAMES.server,
name: bundlePath,
isServerComponent,
value,
hasAppDir, hasAppDir,
}) })
}, },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment