Skip to content

Commit

Permalink
Fix trace ignore handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Oct 10, 2023
1 parent 52356a0 commit e881399
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
18 changes: 7 additions & 11 deletions packages/next/src/build/collect-build-traces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,16 @@ export async function collectBuildTraces({
})
}
}
const ignores = [
const serverIgnores = [
'**/*.d.ts',
'**/*.map',
isStandalone ? null : '**/next/dist/compiled/jest-worker/**/*',
'**/next/dist/compiled/webpack/(bundle4|bundle5).js',
'**/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
Expand All @@ -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,
})
Expand Down Expand Up @@ -321,7 +323,7 @@ export async function collectBuildTraces({
[minimalServerTracedFiles, minimalFiles],
] as [Set<string>, string[]][]) {
for (const file of files) {
if (!ignoreFn(path.join(traceContext, file))) {
if (!serverIgnoreFn(path.join(traceContext, file))) {
addToTracedFiles(traceContext, file, set)
}
}
Expand All @@ -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
Expand All @@ -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, '/')
)
Expand Down
41 changes: 41 additions & 0 deletions test/integration/image-optimizer/test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:${
Expand Down Expand Up @@ -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()
Expand All @@ -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)
})

Expand Down

0 comments on commit e881399

Please sign in to comment.