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 { ...@@ -69,6 +69,7 @@ import {
PageInfo, PageInfo,
printCustomRoutes, printCustomRoutes,
printTreeView, printTreeView,
getNamedExports,
} from './utils' } from './utils'
import getBaseWebpackConfig from './webpack-config' import getBaseWebpackConfig from './webpack-config'
import { writeBuildId } from './write-build-id' import { writeBuildId } from './write-build-id'
...@@ -466,6 +467,7 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -466,6 +467,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
) )
let customAppGetInitialProps: boolean | undefined let customAppGetInitialProps: boolean | undefined
let namedExports: Array<string> | undefined
process.env.NEXT_PHASE = PHASE_PRODUCTION_BUILD process.env.NEXT_PHASE = PHASE_PRODUCTION_BUILD
...@@ -538,6 +540,17 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -538,6 +540,17 @@ export default async function build(dir: string, conf = null): Promise<void> {
runtimeEnvConfig runtimeEnvConfig
) )
namedExports = getNamedExports(
isLikeServerless
? serverBundle
: path.join(
distDir,
SERVER_DIRECTORY,
`/static/${buildId}/pages/_app.js`
),
runtimeEnvConfig
)
if (customAppGetInitialProps) { if (customAppGetInitialProps) {
console.warn( console.warn(
chalk.bold.yellow(`Warning: `) + chalk.bold.yellow(`Warning: `) +
...@@ -921,6 +934,7 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -921,6 +934,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
pagePaths.length - pagePaths.length -
(staticPages.size + ssgPages.size + serverPropsPages.size), (staticPages.size + ssgPages.size + serverPropsPages.size),
hasStatic404: useStatic404, hasStatic404: useStatic404,
hasReportWebVitals: namedExports?.includes('reportWebVitals') ?? false,
}) })
) )
......
...@@ -789,3 +789,11 @@ export function hasCustomGetInitialProps( ...@@ -789,3 +789,11 @@ export function hasCustomGetInitialProps(
} }
return mod.getInitialProps !== mod.origGetInitialProps 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 = { ...@@ -44,6 +44,7 @@ type EventBuildOptimized = {
hasDunderPages: boolean hasDunderPages: boolean
hasTestPages: boolean hasTestPages: boolean
hasStatic404: boolean hasStatic404: boolean
hasReportWebVitals: boolean
} }
export function eventBuildOptimize( 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', () => { ...@@ -369,4 +369,55 @@ describe('Telemetry CLI', () => {
expect(stderr).toMatch(/isSrcDir.*?true/) 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