From 552b9747ff9551884499f165499dd6db1d8c4907 Mon Sep 17 00:00:00 2001 From: Jimmy Lai Date: Tue, 17 Oct 2023 11:09:40 +0200 Subject: [PATCH] perf: fix tracing for routes (#56924) follow up to #56898 where I noticed that we don't apply any filtering to the trace files for the user routes, resulting in files that would need to be filtered like `caniuse` not being filtered out correctly. This fixes that. A lambda in my test project goes from `2.7MB` to `1.4MB` followup: add some snapshot tests before ``` Serverless function size info Serverless Function's pages: _not-found.js, index.js Large Dependencies Uncompressed size Compressed size node_modules/.pnpm/next@13.5.6-canary.1_react-dom@18.2.0_react@18.2.0 4.61 MB 1.35 MB node_modules/.pnpm/caniuse-lite@1.0.30001517 909.73 KB 327.14 KB node_modules/.pnpm/react-dom@18.2.0_react@18.2.0 546.21 KB 138.87 KB All dependencies 3.66 MB 2.01 MB Serverless Function's page: favicon.ico.js Large Dependencies Uncompressed size Compressed size node_modules/.pnpm/next@13.5.6-canary.1_react-dom@18.2.0_react@18.2.0 6.71 MB 2.05 MB node_modules/.pnpm/caniuse-lite@1.0.30001517 909.73 KB 327.14 KB node_modules/.pnpm/react-dom@18.2.0_react@18.2.0 546.21 KB 138.87 KB All dependencies 5.78 MB 2.71 MB Serverless Function's page: api/hello-world.js Large Dependencies Uncompressed size Compressed size node_modules/.pnpm/next@13.5.6-canary.1_react-dom@18.2.0_react@18.2.0 4.61 MB 1.35 MB node_modules/.pnpm/caniuse-lite@1.0.30001517 909.73 KB 327.14 KB node_modules/.pnpm/react-dom@18.2.0_react@18.2.0 546.21 KB 138.87 KB All dependencies 3.65 MB 2.01 MB ``` after ``` Large Dependencies Uncompressed size Compressed size node_modules/.pnpm/file+next-canary+next-13.5.6-canary.1.tgz_react-dom@18.2.0_react@18.2.0 2.87 MB 844.1 KB All dependencies 341.31 KB 992.45 KB Serverless Function's page: favicon.ico.js Large Dependencies Uncompressed size Compressed size node_modules/.pnpm/file+next-canary+next-13.5.6-canary.1.tgz_react-dom@18.2.0_react@18.2.0 4.97 MB 1.52 MB All dependencies 2.45 MB 1.67 MB Serverless Function's page: api/hello-world.js Large Dependencies Uncompressed size Compressed size node_modules/.pnpm/file+next-canary+next-13.5.6-canary.1.tgz_react-dom@18.2.0_react@18.2.0 2.87 MB 844.1 KB All dependencies 328.64 KB 989.23 KB ```` --- .../next/src/build/collect-build-traces.ts | 83 ++++++++++++------- 1 file changed, 52 insertions(+), 31 deletions(-) diff --git a/packages/next/src/build/collect-build-traces.ts b/packages/next/src/build/collect-build-traces.ts index 48918afc66ed9..54ef74ed3490a 100644 --- a/packages/next/src/build/collect-build-traces.ts +++ b/packages/next/src/build/collect-build-traces.ts @@ -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/**/*', @@ -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() @@ -372,9 +380,11 @@ export async function collectBuildTraces({ ] as [Set, 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) } @@ -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() - const cachedIgnoreFilesMinimal = new Map() + const cachedLookupIgnore = new Map() + const cachedLookupIgnoreMinimal = new Map() for (const [entries, tracedFiles] of [ [serverEntries, serverTracedFiles], @@ -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( @@ -475,6 +488,8 @@ export async function collectBuildTraces({ const { entryNameFilesMap } = buildTraceContext?.chunksTrace || {} + const cachedLookupIgnoreRoutes = new Map() + await Promise.all( [ ...(entryNameFilesMap @@ -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) + } } } @@ -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) }