Unverified Commit 7c7fd3e8 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Record presence of `reportWebVitals` (#13155)

Closes #12897
parent 7cf3c3d2
......@@ -69,6 +69,7 @@ import {
PageInfo,
printCustomRoutes,
printTreeView,
getNamedExports,
} from './utils'
import getBaseWebpackConfig from './webpack-config'
import { writeBuildId } from './write-build-id'
......@@ -466,6 +467,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
)
let customAppGetInitialProps: boolean | undefined
let namedExports: Array<string> | undefined
process.env.NEXT_PHASE = PHASE_PRODUCTION_BUILD
......@@ -538,6 +540,17 @@ export default async function build(dir: string, conf = null): Promise<void> {
runtimeEnvConfig
)
namedExports = getNamedExports(
isLikeServerless
? serverBundle
: path.join(
distDir,
SERVER_DIRECTORY,
`/static/${buildId}/pages/_app.js`
),
runtimeEnvConfig
)
if (customAppGetInitialProps) {
console.warn(
chalk.bold.yellow(`Warning: `) +
......@@ -921,6 +934,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
pagePaths.length -
(staticPages.size + ssgPages.size + serverPropsPages.size),
hasStatic404: useStatic404,
hasReportWebVitals: namedExports?.includes('reportWebVitals') ?? false,
})
)
......
......@@ -789,3 +789,11 @@ export function hasCustomGetInitialProps(
}
return mod.getInitialProps !== mod.origGetInitialProps
}
export function getNamedExports(
bundle: string,
runtimeEnvConfig: any
): Array<string> {
require('../next-server/lib/runtime-config').setConfig(runtimeEnvConfig)
return Object.keys(require(bundle))
}
......@@ -44,6 +44,7 @@ type EventBuildOptimized = {
hasDunderPages: boolean
hasTestPages: boolean
hasStatic404: boolean
hasReportWebVitals: boolean
}
export function eventBuildOptimize(
......
import App from 'next/app'
export default App
export function reportWebVitals(metric) {
console.log(metric)
}
......@@ -369,4 +369,55 @@ describe('Telemetry CLI', () => {
expect(stderr).toMatch(/isSrcDir.*?true/)
})
it('detect reportWebVitals correctly for `next build`', async () => {
// Case 1: When _app.js does not exist.
let build = await nextBuild(appDir, [], {
stderr: true,
env: { NEXT_TELEMETRY_DEBUG: 1 },
})
let event1 = /NEXT_BUILD_OPTIMIZED[\s\S]+?{([\s\S]+?)}/
.exec(build.stderr)
.pop()
expect(event1).toMatch(/hasReportWebVitals.*?false/)
// Case 2: When _app.js exist with reportWebVitals function.
await fs.rename(
path.join(appDir, 'pages', '_app_withreportwebvitals.empty'),
path.join(appDir, 'pages', '_app.js')
)
build = await nextBuild(appDir, [], {
stderr: true,
env: { NEXT_TELEMETRY_DEBUG: 1 },
})
await fs.rename(
path.join(appDir, 'pages', '_app.js'),
path.join(appDir, 'pages', '_app_withreportwebvitals.empty')
)
event1 = /NEXT_BUILD_OPTIMIZED[\s\S]+?{([\s\S]+?)}/.exec(build.stderr).pop()
expect(event1).toMatch(/hasReportWebVitals.*?true/)
// Case 3: When _app.js exist without reportWebVitals function.
await fs.rename(
path.join(appDir, 'pages', '_app_withoutreportwebvitals.empty'),
path.join(appDir, 'pages', '_app.js')
)
build = await nextBuild(appDir, [], {
stderr: true,
env: { NEXT_TELEMETRY_DEBUG: 1 },
})
await fs.rename(
path.join(appDir, 'pages', '_app.js'),
path.join(appDir, 'pages', '_app_withoutreportwebvitals.empty')
)
event1 = /NEXT_BUILD_OPTIMIZED[\s\S]+?{([\s\S]+?)}/.exec(build.stderr).pop()
expect(event1).toMatch(/hasReportWebVitals.*?false/)
})
})
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