Unverified Commit bc813796 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Detect Invalid Pages Before Optimize (#10418)

parent 44e234e9
import chalk from 'chalk' import chalk from 'chalk'
import ciEnvironment from 'ci-info' import ciEnvironment from 'ci-info'
import escapeStringRegexp from 'escape-string-regexp'
import findUp from 'find-up' import findUp from 'find-up'
import fs from 'fs' import fs from 'fs'
import Worker from 'jest-worker' import Worker from 'jest-worker'
...@@ -11,14 +12,14 @@ import { promisify } from 'util' ...@@ -11,14 +12,14 @@ import { promisify } from 'util'
import formatWebpackMessages from '../client/dev/error-overlay/format-webpack-messages' import formatWebpackMessages from '../client/dev/error-overlay/format-webpack-messages'
import checkCustomRoutes, { import checkCustomRoutes, {
getRedirectStatus, getRedirectStatus,
RouteType, Header,
Redirect, Redirect,
Rewrite, Rewrite,
Header, RouteType,
} from '../lib/check-custom-routes' } from '../lib/check-custom-routes'
import { import {
PUBLIC_DIR_MIDDLEWARE_CONFLICT,
PAGES_404_GET_INITIAL_PROPS_ERROR, PAGES_404_GET_INITIAL_PROPS_ERROR,
PUBLIC_DIR_MIDDLEWARE_CONFLICT,
} from '../lib/constants' } from '../lib/constants'
import { findPagesDir } from '../lib/find-pages-dir' import { findPagesDir } from '../lib/find-pages-dir'
import { recursiveDelete } from '../lib/recursive-delete' import { recursiveDelete } from '../lib/recursive-delete'
...@@ -44,7 +45,7 @@ import loadConfig, { ...@@ -44,7 +45,7 @@ import loadConfig, {
isTargetLikeServerless, isTargetLikeServerless,
} from '../next-server/server/config' } from '../next-server/server/config'
import { import {
eventBuildDuration, eventBuildCompleted,
eventBuildOptimize, eventBuildOptimize,
eventNextPlugins, eventNextPlugins,
eventVersion, eventVersion,
...@@ -56,17 +57,16 @@ import { generateBuildId } from './generate-build-id' ...@@ -56,17 +57,16 @@ import { generateBuildId } from './generate-build-id'
import { isWriteable } from './is-writeable' import { isWriteable } from './is-writeable'
import createSpinner from './spinner' import createSpinner from './spinner'
import { import {
isPageStatic,
collectPages, collectPages,
getPageSizeInKb, getPageSizeInKb,
hasCustomAppGetInitialProps, hasCustomAppGetInitialProps,
isPageStatic,
PageInfo, PageInfo,
printCustomRoutes, printCustomRoutes,
printTreeView, printTreeView,
} 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'
import escapeStringRegexp from 'escape-string-regexp'
const fsAccess = promisify(fs.access) const fsAccess = promisify(fs.access)
const fsUnlink = promisify(fs.unlink) const fsUnlink = promisify(fs.unlink)
...@@ -394,8 +394,7 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -394,8 +394,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
} else { } else {
console.log(chalk.green('Compiled successfully.\n')) console.log(chalk.green('Compiled successfully.\n'))
telemetry.record( telemetry.record(
eventBuildDuration({ eventBuildCompleted(pagePaths, {
totalPageCount: pagePaths.length,
durationInSeconds: webpackBuildEnd[0], durationInSeconds: webpackBuildEnd[0],
}) })
) )
......
const REGEXP_DIRECTORY_DUNDER = /[\\/]__[^\\/]+(?<![\\/]__(?:tests|mocks))__[\\/]/i
const REGEXP_DIRECTORY_TESTS = /[\\/]__(tests|mocks)__[\\/]/i
const REGEXP_FILE_TEST = /\.(?:spec|test)\.[^.]+$/i
const EVENT_BUILD_DURATION = 'NEXT_BUILD_COMPLETED' const EVENT_BUILD_DURATION = 'NEXT_BUILD_COMPLETED'
type EventBuildCompleted = { type EventBuildCompleted = {
durationInSeconds: number durationInSeconds: number
totalPageCount: number totalPageCount: number
hasDunderPages: boolean
hasTestPages: boolean
} }
export function eventBuildDuration( export function eventBuildCompleted(
event: EventBuildCompleted pagePaths: string[],
event: Omit<
EventBuildCompleted,
'totalPageCount' | 'hasDunderPages' | 'hasTestPages'
>
): { eventName: string; payload: EventBuildCompleted } { ): { eventName: string; payload: EventBuildCompleted } {
return { return {
eventName: EVENT_BUILD_DURATION, eventName: EVENT_BUILD_DURATION,
payload: event, payload: {
...event,
totalPageCount: pagePaths.length,
hasDunderPages: pagePaths.some(path =>
REGEXP_DIRECTORY_DUNDER.test(path)
),
hasTestPages: pagePaths.some(
path => REGEXP_DIRECTORY_TESTS.test(path) || REGEXP_FILE_TEST.test(path)
),
},
} }
} }
...@@ -23,10 +42,6 @@ type EventBuildOptimized = { ...@@ -23,10 +42,6 @@ type EventBuildOptimized = {
hasTestPages: boolean hasTestPages: boolean
} }
const REGEXP_DIRECTORY_DUNDER = /[\\/]__[^\\/]+(?<![\\/]__(?:tests|mocks))__[\\/]/i
const REGEXP_DIRECTORY_TESTS = /[\\/]__(tests|mocks)__[\\/]/i
const REGEXP_FILE_TEST = /\.(?:spec|test)\.[^.]+$/i
export function eventBuildOptimize( export function eventBuildOptimize(
pagePaths: string[], pagePaths: string[],
event: Omit< event: Omit<
......
...@@ -108,8 +108,13 @@ describe('Telemetry CLI', () => { ...@@ -108,8 +108,13 @@ describe('Telemetry CLI', () => {
path.join(appDir, 'pages', 'hello.test.skip') path.join(appDir, 'pages', 'hello.test.skip')
) )
expect(stderr).toMatch(/hasDunderPages.*?true/) const event1 = /NEXT_BUILD_COMPLETED[\s\S]+?{([\s\S]+?)}/.exec(stderr).pop()
expect(stderr).toMatch(/hasTestPages.*?true/) expect(event1).toMatch(/hasDunderPages.*?true/)
expect(event1).toMatch(/hasTestPages.*?true/)
const event2 = /NEXT_BUILD_OPTIMIZED[\s\S]+?{([\s\S]+?)}/.exec(stderr).pop()
expect(event2).toMatch(/hasDunderPages.*?true/)
expect(event2).toMatch(/hasTestPages.*?true/)
}) })
it('detects isSrcDir dir correctly for `next dev`', async () => { it('detects isSrcDir dir correctly for `next dev`', async () => {
......
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