Unverified Commit f5cab2f5 authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

App Build Stats (#38884)



## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
Co-authored-by: 's avatarTim Neutkens <6324199+timneutkens@users.noreply.github.com>
Co-authored-by: 's avatarJJ Kasper <22380829+ijjk@users.noreply.github.com>
parent b15a976e
...@@ -19,7 +19,7 @@ import { ...@@ -19,7 +19,7 @@ import {
import { import {
CLIENT_STATIC_FILES_RUNTIME_AMP, CLIENT_STATIC_FILES_RUNTIME_AMP,
CLIENT_STATIC_FILES_RUNTIME_MAIN, CLIENT_STATIC_FILES_RUNTIME_MAIN,
CLIENT_STATIC_FILES_RUNTIME_MAIN_ROOT, CLIENT_STATIC_FILES_RUNTIME_MAIN_APP,
CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH, CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH,
EDGE_RUNTIME_WEBPACK, EDGE_RUNTIME_WEBPACK,
} from '../shared/lib/constants' } from '../shared/lib/constants'
...@@ -506,14 +506,14 @@ export function finalizeEntrypoint({ ...@@ -506,14 +506,14 @@ export function finalizeEntrypoint({
// Client special cases // Client special cases
name !== 'polyfills' && name !== 'polyfills' &&
name !== CLIENT_STATIC_FILES_RUNTIME_MAIN && name !== CLIENT_STATIC_FILES_RUNTIME_MAIN &&
name !== CLIENT_STATIC_FILES_RUNTIME_MAIN_ROOT && name !== CLIENT_STATIC_FILES_RUNTIME_MAIN_APP &&
name !== CLIENT_STATIC_FILES_RUNTIME_AMP && name !== CLIENT_STATIC_FILES_RUNTIME_AMP &&
name !== CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH name !== CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH
) { ) {
// TODO-APP: this is a temporary fix. @shuding is going to change the handling of server components // TODO-APP: this is a temporary fix. @shuding is going to change the handling of server components
if (appDir && entry.import.includes('flight')) { if (appDir && entry.import.includes('flight')) {
return { return {
dependOn: CLIENT_STATIC_FILES_RUNTIME_MAIN_ROOT, dependOn: CLIENT_STATIC_FILES_RUNTIME_MAIN_APP,
...entry, ...entry,
} }
} }
......
This diff is collapsed.
This diff is collapsed.
...@@ -19,7 +19,7 @@ import { CustomRoutes } from '../lib/load-custom-routes.js' ...@@ -19,7 +19,7 @@ import { CustomRoutes } from '../lib/load-custom-routes.js'
import { import {
CLIENT_STATIC_FILES_RUNTIME_AMP, CLIENT_STATIC_FILES_RUNTIME_AMP,
CLIENT_STATIC_FILES_RUNTIME_MAIN, CLIENT_STATIC_FILES_RUNTIME_MAIN,
CLIENT_STATIC_FILES_RUNTIME_MAIN_ROOT, CLIENT_STATIC_FILES_RUNTIME_MAIN_APP,
CLIENT_STATIC_FILES_RUNTIME_POLYFILLS_SYMBOL, CLIENT_STATIC_FILES_RUNTIME_POLYFILLS_SYMBOL,
CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH, CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH,
CLIENT_STATIC_FILES_RUNTIME_WEBPACK, CLIENT_STATIC_FILES_RUNTIME_WEBPACK,
...@@ -59,6 +59,7 @@ import browserslist from 'next/dist/compiled/browserslist' ...@@ -59,6 +59,7 @@ import browserslist from 'next/dist/compiled/browserslist'
import loadJsConfig from './load-jsconfig' import loadJsConfig from './load-jsconfig'
import { loadBindings } from './swc' import { loadBindings } from './swc'
import { clientComponentRegex } from './webpack/loaders/utils' import { clientComponentRegex } from './webpack/loaders/utils'
import { AppBuildManifestPlugin } from './webpack/plugins/app-build-manifest-plugin'
const watchOptions = Object.freeze({ const watchOptions = Object.freeze({
aggregateTimeout: 5, aggregateTimeout: 5,
...@@ -572,7 +573,7 @@ export default async function getBaseWebpackConfig( ...@@ -572,7 +573,7 @@ export default async function getBaseWebpackConfig(
.replace(/\\/g, '/'), .replace(/\\/g, '/'),
...(config.experimental.appDir ...(config.experimental.appDir
? { ? {
[CLIENT_STATIC_FILES_RUNTIME_MAIN_ROOT]: dev [CLIENT_STATIC_FILES_RUNTIME_MAIN_APP]: dev
? [ ? [
require.resolve( require.resolve(
`next/dist/compiled/@next/react-refresh-utils/dist/runtime` `next/dist/compiled/@next/react-refresh-utils/dist/runtime`
...@@ -1718,6 +1719,10 @@ export default async function getBaseWebpackConfig( ...@@ -1718,6 +1719,10 @@ export default async function getBaseWebpackConfig(
minimized: true, minimized: true,
}, },
}), }),
!!config.experimental.appDir &&
hasServerComponents &&
isClient &&
new AppBuildManifestPlugin({ dev }),
hasServerComponents && hasServerComponents &&
(isClient (isClient
? new FlightManifestPlugin({ ? new FlightManifestPlugin({
......
import { webpack, sources } from 'next/dist/compiled/webpack/webpack'
import {
APP_BUILD_MANIFEST,
CLIENT_STATIC_FILES_RUNTIME_AMP,
CLIENT_STATIC_FILES_RUNTIME_MAIN,
CLIENT_STATIC_FILES_RUNTIME_MAIN_APP,
CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH,
} from '../../../shared/lib/constants'
import type { webpack5 } from 'next/dist/compiled/webpack/webpack'
import { getEntrypointFiles } from './build-manifest-plugin'
import getAppRouteFromEntrypoint from '../../../server/get-app-route-from-entrypoint'
type Options = {
dev: boolean
}
export type AppBuildManifest = {
pages: Record<string, string[]>
}
const PLUGIN_NAME = 'AppBuildManifestPlugin'
export class AppBuildManifestPlugin {
private readonly dev: boolean
constructor(options: Options) {
this.dev = options.dev
}
public apply(compiler: any) {
compiler.hooks.compilation.tap(
PLUGIN_NAME,
(compilation: any, { normalModuleFactory }: any) => {
compilation.dependencyFactories.set(
(webpack as any).dependencies.ModuleDependency,
normalModuleFactory
)
compilation.dependencyTemplates.set(
(webpack as any).dependencies.ModuleDependency,
new (webpack as any).dependencies.NullDependency.Template()
)
}
)
compiler.hooks.make.tap(PLUGIN_NAME, (compilation: any) => {
compilation.hooks.processAssets.tap(
{
name: PLUGIN_NAME,
// @ts-ignore TODO: Remove ignore when webpack 5 is stable
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
},
(assets: any) => this.createAsset(assets, compilation)
)
})
}
private createAsset(assets: any, compilation: webpack5.Compilation) {
const manifest: AppBuildManifest = {
pages: {},
}
const systemEntrypoints = new Set<string>([
CLIENT_STATIC_FILES_RUNTIME_MAIN,
CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH,
CLIENT_STATIC_FILES_RUNTIME_AMP,
CLIENT_STATIC_FILES_RUNTIME_MAIN_APP,
])
const mainFiles = new Set(
getEntrypointFiles(
compilation.entrypoints.get(CLIENT_STATIC_FILES_RUNTIME_MAIN_APP)
)
)
for (const entrypoint of compilation.entrypoints.values()) {
if (!entrypoint.name) {
continue
}
if (systemEntrypoints.has(entrypoint.name)) {
continue
}
const pagePath = getAppRouteFromEntrypoint(entrypoint.name)
if (!pagePath) {
continue
}
const filesForPage = getEntrypointFiles(entrypoint)
manifest.pages[pagePath] = [...new Set([...mainFiles, ...filesForPage])]
}
const json = JSON.stringify(manifest, null, 2)
assets[APP_BUILD_MANIFEST] = new sources.RawSource(json)
}
}
...@@ -6,7 +6,7 @@ import { ...@@ -6,7 +6,7 @@ import {
MIDDLEWARE_BUILD_MANIFEST, MIDDLEWARE_BUILD_MANIFEST,
CLIENT_STATIC_FILES_PATH, CLIENT_STATIC_FILES_PATH,
CLIENT_STATIC_FILES_RUNTIME_MAIN, CLIENT_STATIC_FILES_RUNTIME_MAIN,
CLIENT_STATIC_FILES_RUNTIME_MAIN_ROOT, CLIENT_STATIC_FILES_RUNTIME_MAIN_APP,
CLIENT_STATIC_FILES_RUNTIME_POLYFILLS_SYMBOL, CLIENT_STATIC_FILES_RUNTIME_POLYFILLS_SYMBOL,
CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH, CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH,
CLIENT_STATIC_FILES_RUNTIME_AMP, CLIENT_STATIC_FILES_RUNTIME_AMP,
...@@ -155,7 +155,7 @@ export default class BuildManifestPlugin { ...@@ -155,7 +155,7 @@ export default class BuildManifestPlugin {
assetMap.rootMainFiles = [ assetMap.rootMainFiles = [
...new Set( ...new Set(
getEntrypointFiles( getEntrypointFiles(
entrypoints.get(CLIENT_STATIC_FILES_RUNTIME_MAIN_ROOT) entrypoints.get(CLIENT_STATIC_FILES_RUNTIME_MAIN_APP)
) )
), ),
] ]
...@@ -192,7 +192,7 @@ export default class BuildManifestPlugin { ...@@ -192,7 +192,7 @@ export default class BuildManifestPlugin {
CLIENT_STATIC_FILES_RUNTIME_MAIN, CLIENT_STATIC_FILES_RUNTIME_MAIN,
CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH, CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH,
CLIENT_STATIC_FILES_RUNTIME_AMP, CLIENT_STATIC_FILES_RUNTIME_AMP,
...(this.appDirEnabled ? [CLIENT_STATIC_FILES_RUNTIME_MAIN_ROOT] : []), ...(this.appDirEnabled ? [CLIENT_STATIC_FILES_RUNTIME_MAIN_APP] : []),
]) ])
for (const entrypoint of compilation.entrypoints.values()) { for (const entrypoint of compilation.entrypoints.values()) {
......
import matchBundle from './match-bundle'
// matches app/:path*.js
const APP_ROUTE_NAME_REGEX = /^app[/\\](.*)$/
export default function getAppRouteFromEntrypoint(entryFile: string) {
const pagePath = matchBundle(APP_ROUTE_NAME_REGEX, entryFile)
if (typeof pagePath === 'string' && !pagePath) {
return '/'
}
if (!pagePath) {
return null
}
return pagePath
}
import getRouteFromAssetPath from '../shared/lib/router/utils/get-route-from-asset-path' import getAppRouteFromEntrypoint from './get-app-route-from-entrypoint'
import matchBundle from './match-bundle'
// matches pages/:page*.js // matches pages/:page*.js
const SERVER_ROUTE_NAME_REGEX = /^pages[/\\](.*)$/ const SERVER_ROUTE_NAME_REGEX = /^pages[/\\](.*)$/
// matches app/:path*.js
const APP_ROUTE_NAME_REGEX = /^app[/\\](.*)$/
// matches static/pages/:page*.js // matches static/pages/:page*.js
const BROWSER_ROUTE_NAME_REGEX = /^static[/\\]pages[/\\](.*)$/ const BROWSER_ROUTE_NAME_REGEX = /^static[/\\]pages[/\\](.*)$/
function matchBundle(regex: RegExp, input: string): string | null {
const result = regex.exec(input)
if (!result) {
return null
}
return getRouteFromAssetPath(`/${result[1]}`)
}
export default function getRouteFromEntrypoint( export default function getRouteFromEntrypoint(
entryFile: string, entryFile: string,
app?: boolean app?: boolean
...@@ -28,8 +18,7 @@ export default function getRouteFromEntrypoint( ...@@ -28,8 +18,7 @@ export default function getRouteFromEntrypoint(
} }
if (app) { if (app) {
pagePath = matchBundle(APP_ROUTE_NAME_REGEX, entryFile) pagePath = getAppRouteFromEntrypoint(entryFile)
if (typeof pagePath === 'string' && !pagePath) pagePath = '/'
if (pagePath) return pagePath if (pagePath) return pagePath
} }
......
import getRouteFromAssetPath from '../shared/lib/router/utils/get-route-from-asset-path'
export default function matchBundle(
regex: RegExp,
input: string
): string | null {
const result = regex.exec(input)
if (!result) {
return null
}
return getRouteFromAssetPath(`/${result[1]}`)
}
...@@ -7,6 +7,7 @@ export const PAGES_MANIFEST = 'pages-manifest.json' ...@@ -7,6 +7,7 @@ export const PAGES_MANIFEST = 'pages-manifest.json'
export const APP_PATHS_MANIFEST = 'app-paths-manifest.json' export const APP_PATHS_MANIFEST = 'app-paths-manifest.json'
export const APP_PATH_ROUTES_MANIFEST = 'app-path-routes-manifest.json' export const APP_PATH_ROUTES_MANIFEST = 'app-path-routes-manifest.json'
export const BUILD_MANIFEST = 'build-manifest.json' export const BUILD_MANIFEST = 'build-manifest.json'
export const APP_BUILD_MANIFEST = 'app-build-manifest.json'
export const EXPORT_MARKER = 'export-marker.json' export const EXPORT_MARKER = 'export-marker.json'
export const EXPORT_DETAIL = 'export-detail.json' export const EXPORT_DETAIL = 'export-detail.json'
export const PRERENDER_MANIFEST = 'prerender-manifest.json' export const PRERENDER_MANIFEST = 'prerender-manifest.json'
...@@ -47,7 +48,7 @@ export const MIDDLEWARE_REACT_LOADABLE_MANIFEST = ...@@ -47,7 +48,7 @@ export const MIDDLEWARE_REACT_LOADABLE_MANIFEST =
// static/runtime/main.js // static/runtime/main.js
export const CLIENT_STATIC_FILES_RUNTIME_MAIN = `main` export const CLIENT_STATIC_FILES_RUNTIME_MAIN = `main`
export const CLIENT_STATIC_FILES_RUNTIME_MAIN_ROOT = `${CLIENT_STATIC_FILES_RUNTIME_MAIN}-app` export const CLIENT_STATIC_FILES_RUNTIME_MAIN_APP = `${CLIENT_STATIC_FILES_RUNTIME_MAIN}-app`
// static/runtime/react-refresh.js // static/runtime/react-refresh.js
export const CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH = `react-refresh` export const CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH = `react-refresh`
// static/runtime/amp.js // static/runtime/amp.js
......
// remove (name) from pathname as it's not considered for routing // remove (name) from pathname as it's not considered for routing
export function normalizeAppPath(pathname: string) { export function normalizeAppPath(pathname: string) {
let normalized = '' return pathname.split('/').reduce((acc, segment, index, segments) => {
const segments = pathname.split('/') // Empty segments are ignored.
if (!segment) {
return acc
}
segments.forEach((segment, index) => {
if (!segment) return
if (segment.startsWith('(') && segment.endsWith(')')) { if (segment.startsWith('(') && segment.endsWith(')')) {
return return acc
} }
if (segment === 'page' && index === segments.length - 1) { if (segment === 'page' && index === segments.length - 1) {
return return acc
} }
normalized += `/${segment}`
}) return acc + `/${segment}`
return normalized }, '')
} }
...@@ -195,7 +195,9 @@ class UrlNode { ...@@ -195,7 +195,9 @@ class UrlNode {
} }
} }
export function getSortedRoutes(normalizedPages: string[]): string[] { export function getSortedRoutes(
normalizedPages: ReadonlyArray<string>
): string[] {
// First the UrlNode is created, and every UrlNode can have only 1 dynamic segment // First the UrlNode is created, and every UrlNode can have only 1 dynamic segment
// Eg you can't have pages/[post]/abc.js and pages/[hello]/something-else.js // Eg you can't have pages/[post]/abc.js and pages/[hello]/something-else.js
// Only 1 dynamic segment per nesting level // Only 1 dynamic segment per nesting level
......
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