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

perf: fix tracing for routes #56924

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 52 additions & 31 deletions packages/next/src/build/collect-build-traces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,19 @@ export async function collectBuildTraces({
})
}
}
const serverIgnores = [

const sharedIgnores = [
'**/*.d.ts',
'**/*.map',
'**/next/dist/compiled/next-server/**/*.dev.js',
'**/node_modules/react{,-dom,-dom-server-turbopack}/**/*.development.js',

...additionalIgnores,
...(config.experimental.outputFileTracingIgnores || []),
]

const serverIgnores = [
...sharedIgnores,
isStandalone ? null : '**/next/dist/compiled/jest-worker/**/*',
'**/next/dist/compiled/webpack/(bundle4|bundle5).js',
'**/node_modules/webpack5/**/*',
Expand All @@ -294,32 +302,32 @@ export async function collectBuildTraces({
? ['**/next/dist/compiled/@ampproject/toolbox-optimizer/**/*']
: []),

...additionalIgnores,

...(isStandalone ? [] : TRACE_IGNORES),

...(config.experimental.outputFileTracingIgnores || []),
].filter(nonNullable)

const minimalServerIgnores = [
...serverIgnores,
'**/next/dist/compiled/edge-runtime/**/*',
'**/next/dist/server/web/sandbox/**/*',
'**/next/dist/server/post-process.js',
]

const routesIgnores = [
...sharedIgnores,
'**/next/dist/compiled/next-server/**/*',
'**/next/dist/server/optimize-amp.js',
'**/next/dist/server/post-process.js',
]

const serverIgnoreFn = (minimal: boolean) => (pathname: string) => {
const makeIgnoreFn = (ignores: string[]) => (pathname: string) => {
if (path.isAbsolute(pathname) && !pathname.startsWith(root)) {
return true
}

return isMatch(
pathname,
minimal ? minimalServerIgnores : serverIgnores,
{
contains: true,
dot: true,
}
)
return isMatch(pathname, ignores, {
contains: true,
dot: true,
})
}
const traceContext = path.join(nextServerEntry, '..', '..')
const serverTracedFiles = new Set<string>()
Expand Down Expand Up @@ -372,9 +380,11 @@ export async function collectBuildTraces({
] as [Set<string>, string[]][]) {
for (const file of files) {
if (
!serverIgnoreFn(set === minimalServerTracedFiles)(
path.join(traceContext, file)
)
!makeIgnoreFn(
set === minimalServerTracedFiles
? minimalServerIgnores
: serverIgnores
)(path.join(traceContext, file))
) {
addToTracedFiles(traceContext, file, set)
}
Expand Down Expand Up @@ -433,14 +443,13 @@ export async function collectBuildTraces({
})
const reasons = result.reasons
const fileList = result.fileList

for (const file of result.esmFileList) {
fileList.add(file)
}

const parentFilesMap = getFilesMapFromReasons(fileList, reasons)
const cachedIgnoreFiles = new Map<string, boolean>()
const cachedIgnoreFilesMinimal = new Map<string, boolean>()
const cachedLookupIgnore = new Map<string, boolean>()
const cachedLookupIgnoreMinimal = new Map<string, boolean>()

for (const [entries, tracedFiles] of [
[serverEntries, serverTracedFiles],
Expand All @@ -458,11 +467,15 @@ export async function collectBuildTraces({
if (
!shouldIgnore(
curFile,
serverIgnoreFn(tracedFiles === minimalServerTracedFiles),
makeIgnoreFn(
tracedFiles === minimalServerTracedFiles
? minimalServerIgnores
: serverIgnores
),
reasons,
tracedFiles === minimalServerTracedFiles
? cachedIgnoreFilesMinimal
: cachedIgnoreFiles
? cachedLookupIgnoreMinimal
: cachedLookupIgnore
)
) {
tracedFiles.add(
Expand All @@ -475,6 +488,8 @@ export async function collectBuildTraces({

const { entryNameFilesMap } = buildTraceContext?.chunksTrace || {}

const cachedLookupIgnoreRoutes = new Map<string, boolean>()

await Promise.all(
[
...(entryNameFilesMap
Expand Down Expand Up @@ -514,14 +529,20 @@ export async function collectBuildTraces({
path.relative(outputFileTracingRoot, file)
)
for (const curFile of curFiles || []) {
curTracedFiles.add(
path
.relative(
traceOutputDir,
path.join(outputFileTracingRoot, curFile)
)
if (
!shouldIgnore(
curFile,
makeIgnoreFn(routesIgnores),
reasons,
cachedLookupIgnoreRoutes
)
) {
const filePath = path.join(outputFileTracingRoot, curFile)
const outputFile = path
.relative(traceOutputDir, filePath)
.replace(/\\/g, '/')
)
curTracedFiles.add(outputFile)
}
}
}

Expand Down Expand Up @@ -556,7 +577,7 @@ export async function collectBuildTraces({

for (const item of await fs.readdir(contextDir)) {
const itemPath = path.relative(root, path.join(contextDir, item))
if (!serverIgnoreFn(false)(itemPath)) {
if (!makeIgnoreFn(serverIgnores)(itemPath)) {
addToTracedFiles(root, itemPath, serverTracedFiles)
addToTracedFiles(root, itemPath, minimalServerTracedFiles)
}
Expand Down