Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Nov 27, 2024
1 parent a756729 commit b3eb511
Showing 1 changed file with 36 additions and 25 deletions.
61 changes: 36 additions & 25 deletions packages/next/src/server/dev/turbopack/manifest-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import type {
EdgeFunctionDefinition,
MiddlewareManifest,
} from '../../../build/webpack/plugins/middleware-plugin'
import type { StatsAsset, StatsChunk, StatsChunkGroup, StatsModule, StatsCompilation as WebpackStats } from 'webpack'
import type {
StatsAsset,
StatsChunk,
StatsChunkGroup,
StatsModule,
StatsCompilation as WebpackStats,
} from 'webpack'
import type { BuildManifest } from '../../get-page-files'
import type { AppBuildManifest } from '../../../build/webpack/plugins/app-build-manifest-plugin'
import type { PagesManifest } from '../../../build/webpack/plugins/pages-manifest-plugin'
Expand Down Expand Up @@ -43,7 +49,11 @@ import { getEntryKey, type EntryKey } from './entry-key'
import type { CustomRoutes } from '../../../lib/load-custom-routes'
import { getSortedRoutes } from '../../../shared/lib/router/utils'
import { existsSync } from 'fs'
import { addMetadataIdToRoute, addRouteSuffix, removeRouteSuffix } from '../turbopack-utils'
import {
addMetadataIdToRoute,
addRouteSuffix,
removeRouteSuffix,
} from '../turbopack-utils'
import { tryToParsePath } from '../../../lib/try-to-parse-path'

interface InstrumentationDefinition {
Expand All @@ -55,7 +65,12 @@ type TurbopackMiddlewareManifest = MiddlewareManifest & {
instrumentation?: InstrumentationDefinition
}

const getManifestPath = (page: string, distDir: string, name: string, type: string) => {
const getManifestPath = (
page: string,
distDir: string,
name: string,
type: string
) => {
let manifestPath = posix.join(
distDir,
`server`,
Expand Down Expand Up @@ -91,18 +106,25 @@ async function readPartialManifest<T>(

// Check the ambiguity of /sitemap and /sitemap.xml
if (isSitemapRoute && !existsSync(manifestPath)) {
manifestPath = getManifestPath(pageName.replace(/\/sitemap\/route$/, '/sitemap.xml/route'), distDir, name, type)
manifestPath = getManifestPath(
pageName.replace(/\/sitemap\/route$/, '/sitemap.xml/route'),
distDir,
name,
type
)
}
// existsSync is faster than using the async version
if(!existsSync(manifestPath) && page.endsWith('/route')) {
if (!existsSync(manifestPath) && page.endsWith('/route')) {
// TODO: Improve implementation of metadata routes, currently it requires this extra check for the variants of the files that can be written.
let metadataPage = addRouteSuffix(addMetadataIdToRoute(removeRouteSuffix(page)))
let metadataPage = addRouteSuffix(
addMetadataIdToRoute(removeRouteSuffix(page))
)
manifestPath = getManifestPath(metadataPage, distDir, name, type)
}
if (existsSync(manifestPath)) {
return JSON.parse(await readFile(posix.join(manifestPath), 'utf-8')) as T
} else {
return undefined;
return undefined
}
}

Expand Down Expand Up @@ -288,19 +310,10 @@ export class TurbopackManifestLoader {
}

private async writeWebpackStats(): Promise<void> {
const webpackStats = this.mergeWebpackStats(
this.webpackStats.values()
)
const path = join(
this.distDir,
'server',
WEBPACK_STATS
)
const webpackStats = this.mergeWebpackStats(this.webpackStats.values())
const path = join(this.distDir, 'server', WEBPACK_STATS)
deleteCache(path)
await writeFileAtomic(
path,
JSON.stringify(webpackStats, null, 2)
)
await writeFileAtomic(path, JSON.stringify(webpackStats, null, 2))
}

async loadBuildManifest(
Expand All @@ -326,16 +339,14 @@ export class TurbopackManifestLoader {
}

private mergeWebpackStats(statsFiles: Iterable<WebpackStats>): WebpackStats {
const entrypoints: Record<string, StatsChunkGroup> = {};
const entrypoints: Record<string, StatsChunkGroup> = {}
const assets: Map<string, StatsAsset> = new Map()
const chunks: Map<string, StatsChunk> = new Map()
const modules: Map<string | number, StatsModule> = new Map()

for (const statsFile of statsFiles) {
if (statsFile.entrypoints) {
for (const [k, v] of Object.entries(
statsFile.entrypoints
)) {
for (const [k, v] of Object.entries(statsFile.entrypoints)) {
if (!entrypoints[k]) {
entrypoints[k] = v
}
Expand All @@ -360,10 +371,10 @@ export class TurbopackManifestLoader {

if (statsFile.modules) {
for (const module of statsFile.modules) {
const id = module.id;
const id = module.id
if (id != null) {
// Merge the chunk list for the module. This can vary across endpoints.
const existing = modules.get(id);
const existing = modules.get(id)
if (existing == null) {
modules.set(id, module)
} else if (module.chunks != null && existing.chunks != null) {
Expand Down

0 comments on commit b3eb511

Please sign in to comment.