diff --git a/packages/next/src/build/collect-build-traces.ts b/packages/next/src/build/collect-build-traces.ts index 54f72c254a9..c0b60922cb9 100644 --- a/packages/next/src/build/collect-build-traces.ts +++ b/packages/next/src/build/collect-build-traces.ts @@ -233,7 +233,7 @@ export async function collectBuildTraces({ }) } } - const ignores = [ + const serverIgnores = [ '**/*.d.ts', '**/*.map', isStandalone ? null : '**/next/dist/compiled/jest-worker/**/*', @@ -241,6 +241,8 @@ export async function collectBuildTraces({ '**/node_modules/webpack5/**/*', '**/next/dist/server/lib/squoosh/**/*.wasm', '**/next/dist/server/lib/route-resolver*', + '**/next/dist/pages/**/*', + ...(ciEnvironment.hasNextSupport ? [ // only ignore image-optimizer code when @@ -261,12 +263,12 @@ export async function collectBuildTraces({ ...(config.experimental.outputFileTracingIgnores || []), ].filter(nonNullable) - const ignoreFn = (pathname: string) => { + const serverIgnoreFn = (pathname: string) => { if (path.isAbsolute(pathname) && !pathname.startsWith(root)) { return true } - return isMatch(pathname, ignores, { + return isMatch(pathname, serverIgnores, { contains: true, dot: true, }) @@ -321,7 +323,7 @@ export async function collectBuildTraces({ [minimalServerTracedFiles, minimalFiles], ] as [Set, string[]][]) { for (const file of files) { - if (!ignoreFn(path.join(traceContext, file))) { + if (!serverIgnoreFn(path.join(traceContext, file))) { addToTracedFiles(traceContext, file, set) } } @@ -336,7 +338,6 @@ export async function collectBuildTraces({ const result = await nodeFileTrace(chunksToTrace, { base: outputFileTracingRoot, processCwd: dir, - ignore: ignoreFn, mixedModules: true, }) const reasons = result.reasons @@ -360,12 +361,7 @@ export async function collectBuildTraces({ for (const curFile of curFiles || []) { const filePath = path.join(outputFileTracingRoot, curFile) - if ( - !isMatch(filePath, '**/next/dist/pages/**/*', { - dot: true, - contains: true, - }) - ) { + if (!serverIgnoreFn(filePath)) { tracedFiles.add( path.relative(distDir, filePath).replace(/\\/g, '/') ) diff --git a/test/integration/image-optimizer/test/util.ts b/test/integration/image-optimizer/test/util.ts index e392123553f..3eb9acf795d 100644 --- a/test/integration/image-optimizer/test/util.ts +++ b/test/integration/image-optimizer/test/util.ts @@ -147,6 +147,28 @@ export function runTests(ctx) { slowImageServer.stop() }) + if (!isDev && ctx.isSharp && ctx.nextConfigImages) { + it('should handle custom sharp usage', async () => { + const res = await fetchViaHTTP(ctx.appPort, '/api/custom-sharp') + + expect(res.status).toBe(200) + expect(await res.json()).toEqual({ success: true }) + const traceFile = await fs.readJson( + join( + ctx.appDir, + '.next', + 'server', + 'pages', + 'api', + 'custom-sharp.js.nft.json' + ) + ) + expect(traceFile.files.some((file) => file.includes('sharp/build'))).toBe( + true + ) + }) + } + if (domains.length > 0) { it('should normalize invalid status codes', async () => { const url = `http://localhost:${ @@ -1435,6 +1457,20 @@ export const setupTests = (ctx) => { }) curCtx.nextOutput = '' nextConfig.replace('{ /* replaceme */ }', json) + + if (curCtx.isSharp) { + await fs.writeFile( + join(curCtx.appDir, 'pages', 'api', 'custom-sharp.js'), + ` + import sharp from 'sharp' + export default function handler(req, res) { + console.log(sharp) + res.json({ success: true }) + } + ` + ) + } + await nextBuild(curCtx.appDir) await cleanImagesDir(ctx) curCtx.appPort = await findPort() @@ -1452,6 +1488,11 @@ export const setupTests = (ctx) => { }) afterAll(async () => { nextConfig.restore() + if (curCtx.isSharp) { + await fs.remove( + join(curCtx.appDir, 'pages', 'api', 'custom-sharp.js') + ) + } if (curCtx.app) await killApp(curCtx.app) })