Unverified Commit 552b9747 authored by Jimmy Lai's avatar Jimmy Lai Committed by GitHub

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
````

parent db214214
...@@ -269,11 +269,19 @@ export async function collectBuildTraces({ ...@@ -269,11 +269,19 @@ export async function collectBuildTraces({
}) })
} }
} }
const serverIgnores = [
const sharedIgnores = [
'**/*.d.ts', '**/*.d.ts',
'**/*.map', '**/*.map',
'**/next/dist/compiled/next-server/**/*.dev.js', '**/next/dist/compiled/next-server/**/*.dev.js',
'**/node_modules/react{,-dom,-dom-server-turbopack}/**/*.development.js', '**/node_modules/react{,-dom,-dom-server-turbopack}/**/*.development.js',
...additionalIgnores,
...(config.experimental.outputFileTracingIgnores || []),
]
const serverIgnores = [
...sharedIgnores,
isStandalone ? null : '**/next/dist/compiled/jest-worker/**/*', isStandalone ? null : '**/next/dist/compiled/jest-worker/**/*',
'**/next/dist/compiled/webpack/(bundle4|bundle5).js', '**/next/dist/compiled/webpack/(bundle4|bundle5).js',
'**/node_modules/webpack5/**/*', '**/node_modules/webpack5/**/*',
...@@ -294,32 +302,32 @@ export async function collectBuildTraces({ ...@@ -294,32 +302,32 @@ export async function collectBuildTraces({
? ['**/next/dist/compiled/@ampproject/toolbox-optimizer/**/*'] ? ['**/next/dist/compiled/@ampproject/toolbox-optimizer/**/*']
: []), : []),
...additionalIgnores,
...(isStandalone ? [] : TRACE_IGNORES), ...(isStandalone ? [] : TRACE_IGNORES),
...(config.experimental.outputFileTracingIgnores || []),
].filter(nonNullable) ].filter(nonNullable)
const minimalServerIgnores = [ const minimalServerIgnores = [
...serverIgnores, ...serverIgnores,
'**/next/dist/compiled/edge-runtime/**/*', '**/next/dist/compiled/edge-runtime/**/*',
'**/next/dist/server/web/sandbox/**/*', '**/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)) { if (path.isAbsolute(pathname) && !pathname.startsWith(root)) {
return true return true
} }
return isMatch( return isMatch(pathname, ignores, {
pathname, contains: true,
minimal ? minimalServerIgnores : serverIgnores, dot: true,
{ })
contains: true,
dot: true,
}
)
} }
const traceContext = path.join(nextServerEntry, '..', '..') const traceContext = path.join(nextServerEntry, '..', '..')
const serverTracedFiles = new Set<string>() const serverTracedFiles = new Set<string>()
...@@ -372,9 +380,11 @@ export async function collectBuildTraces({ ...@@ -372,9 +380,11 @@ export async function collectBuildTraces({
] as [Set<string>, string[]][]) { ] as [Set<string>, string[]][]) {
for (const file of files) { for (const file of files) {
if ( if (
!serverIgnoreFn(set === minimalServerTracedFiles)( !makeIgnoreFn(
path.join(traceContext, file) set === minimalServerTracedFiles
) ? minimalServerIgnores
: serverIgnores
)(path.join(traceContext, file))
) { ) {
addToTracedFiles(traceContext, file, set) addToTracedFiles(traceContext, file, set)
} }
...@@ -433,14 +443,13 @@ export async function collectBuildTraces({ ...@@ -433,14 +443,13 @@ export async function collectBuildTraces({
}) })
const reasons = result.reasons const reasons = result.reasons
const fileList = result.fileList const fileList = result.fileList
for (const file of result.esmFileList) { for (const file of result.esmFileList) {
fileList.add(file) fileList.add(file)
} }
const parentFilesMap = getFilesMapFromReasons(fileList, reasons) const parentFilesMap = getFilesMapFromReasons(fileList, reasons)
const cachedIgnoreFiles = new Map<string, boolean>() const cachedLookupIgnore = new Map<string, boolean>()
const cachedIgnoreFilesMinimal = new Map<string, boolean>() const cachedLookupIgnoreMinimal = new Map<string, boolean>()
for (const [entries, tracedFiles] of [ for (const [entries, tracedFiles] of [
[serverEntries, serverTracedFiles], [serverEntries, serverTracedFiles],
...@@ -458,11 +467,15 @@ export async function collectBuildTraces({ ...@@ -458,11 +467,15 @@ export async function collectBuildTraces({
if ( if (
!shouldIgnore( !shouldIgnore(
curFile, curFile,
serverIgnoreFn(tracedFiles === minimalServerTracedFiles), makeIgnoreFn(
tracedFiles === minimalServerTracedFiles
? minimalServerIgnores
: serverIgnores
),
reasons, reasons,
tracedFiles === minimalServerTracedFiles tracedFiles === minimalServerTracedFiles
? cachedIgnoreFilesMinimal ? cachedLookupIgnoreMinimal
: cachedIgnoreFiles : cachedLookupIgnore
) )
) { ) {
tracedFiles.add( tracedFiles.add(
...@@ -475,6 +488,8 @@ export async function collectBuildTraces({ ...@@ -475,6 +488,8 @@ export async function collectBuildTraces({
const { entryNameFilesMap } = buildTraceContext?.chunksTrace || {} const { entryNameFilesMap } = buildTraceContext?.chunksTrace || {}
const cachedLookupIgnoreRoutes = new Map<string, boolean>()
await Promise.all( await Promise.all(
[ [
...(entryNameFilesMap ...(entryNameFilesMap
...@@ -514,14 +529,20 @@ export async function collectBuildTraces({ ...@@ -514,14 +529,20 @@ export async function collectBuildTraces({
path.relative(outputFileTracingRoot, file) path.relative(outputFileTracingRoot, file)
) )
for (const curFile of curFiles || []) { for (const curFile of curFiles || []) {
curTracedFiles.add( if (
path !shouldIgnore(
.relative( curFile,
traceOutputDir, makeIgnoreFn(routesIgnores),
path.join(outputFileTracingRoot, curFile) reasons,
) cachedLookupIgnoreRoutes
)
) {
const filePath = path.join(outputFileTracingRoot, curFile)
const outputFile = path
.relative(traceOutputDir, filePath)
.replace(/\\/g, '/') .replace(/\\/g, '/')
) curTracedFiles.add(outputFile)
}
} }
} }
...@@ -556,7 +577,7 @@ export async function collectBuildTraces({ ...@@ -556,7 +577,7 @@ export async function collectBuildTraces({
for (const item of await fs.readdir(contextDir)) { for (const item of await fs.readdir(contextDir)) {
const itemPath = path.relative(root, path.join(contextDir, item)) const itemPath = path.relative(root, path.join(contextDir, item))
if (!serverIgnoreFn(false)(itemPath)) { if (!makeIgnoreFn(serverIgnores)(itemPath)) {
addToTracedFiles(root, itemPath, serverTracedFiles) addToTracedFiles(root, itemPath, serverTracedFiles)
addToTracedFiles(root, itemPath, minimalServerTracedFiles) addToTracedFiles(root, itemPath, minimalServerTracedFiles)
} }
......
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