Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record presence of reportWebVitals #13155

Merged
merged 3 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/next/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import {
PageInfo,
printCustomRoutes,
printTreeView,
getNamedExports,
} from './utils'
import getBaseWebpackConfig from './webpack-config'
import { writeBuildId } from './write-build-id'
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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: `) +
Expand Down Expand Up @@ -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,
})
)

Expand Down
8 changes: 8 additions & 0 deletions packages/next/build/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
1 change: 1 addition & 0 deletions packages/next/telemetry/events/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type EventBuildOptimized = {
hasDunderPages: boolean
hasTestPages: boolean
hasStatic404: boolean
hasReportWebVitals: boolean
}

export function eventBuildOptimize(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import App from 'next/app'
export default App
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import App from 'next/app'
export default App
export function reportWebVitals(metric) {
console.log(metric)
}
51 changes: 51 additions & 0 deletions test/integration/telemetry/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
})
})