Files
Jude GaoandGitHub ec6c710845 feat: build lifecycle hooks - afterProductionCompile (#77345)
Add a `runAfterProductionCompile` hook in next.config.js that executes
after production build compilation finishes, but before running
post-compilation tasks like type checking and static page generation.
This hook provides access to project metadata including the project
directory and build output directory, making it useful for third-party
to collect build outputs like sourcemaps.
2025-05-02 08:44:45 -04:00

55 lines
1.7 KiB
TypeScript

import { nextTestSetup } from 'e2e-utils'
import { findAllTelemetryEvents } from 'next-test-utils'
describe('build-lifecycle-hooks', () => {
const { next } = nextTestSetup({
files: __dirname,
env: {
NEXT_TELEMETRY_DEBUG: '1',
},
})
it('should run runAfterProductionCompile', async () => {
const output = next.cliOutput
expect(output).toContain('')
expect(output).toContain(`Using distDir: ${next.testDir}/.next`)
expect(output).toContain(`Using projectDir: ${next.testDir}`)
expect(output).toContain(`Total files in ${next.testDir}/.next folder:`)
expect(output).toContain('Completed runAfterProductionCompile in')
// Ensure telemetry event is recorded
const events = findAllTelemetryEvents(output, 'NEXT_BUILD_FEATURE_USAGE')
expect(events).toContainEqual({
featureName: 'runAfterProductionCompile',
invocationCount: 1,
})
})
it('should allow throwing error in runAfterProductionCompile', async () => {
try {
await next.stop()
await next.patchFile('next.config.ts', (content) => {
return content.replace(
`import { after } from './after'`,
`import { after } from './bad-after'`
)
})
const getCliOutput = next.getCliOutputFromHere()
await next.build()
expect(getCliOutput()).toContain('error after production build')
expect(getCliOutput()).not.toContain(
'Completed runAfterProductionCompile in'
)
} finally {
await next.patchFile('next.config.ts', (content) => {
return content.replace(
`import { after } from './bad-after'`,
`import { after } from './after'`
)
})
}
})
})