Skip to content

Commit

Permalink
Detect Invalid Pages Before Optimize (#10418)
Browse files Browse the repository at this point in the history
  • Loading branch information
Timer committed Feb 4, 2020
1 parent 44e234e commit bc81379
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
15 changes: 7 additions & 8 deletions packages/next/build/index.ts
@@ -1,5 +1,6 @@
import chalk from 'chalk'
import ciEnvironment from 'ci-info'
import escapeStringRegexp from 'escape-string-regexp'
import findUp from 'find-up'
import fs from 'fs'
import Worker from 'jest-worker'
Expand All @@ -11,14 +12,14 @@ import { promisify } from 'util'
import formatWebpackMessages from '../client/dev/error-overlay/format-webpack-messages'
import checkCustomRoutes, {
getRedirectStatus,
RouteType,
Header,
Redirect,
Rewrite,
Header,
RouteType,
} from '../lib/check-custom-routes'
import {
PUBLIC_DIR_MIDDLEWARE_CONFLICT,
PAGES_404_GET_INITIAL_PROPS_ERROR,
PUBLIC_DIR_MIDDLEWARE_CONFLICT,
} from '../lib/constants'
import { findPagesDir } from '../lib/find-pages-dir'
import { recursiveDelete } from '../lib/recursive-delete'
Expand All @@ -44,7 +45,7 @@ import loadConfig, {
isTargetLikeServerless,
} from '../next-server/server/config'
import {
eventBuildDuration,
eventBuildCompleted,
eventBuildOptimize,
eventNextPlugins,
eventVersion,
Expand All @@ -56,17 +57,16 @@ import { generateBuildId } from './generate-build-id'
import { isWriteable } from './is-writeable'
import createSpinner from './spinner'
import {
isPageStatic,
collectPages,
getPageSizeInKb,
hasCustomAppGetInitialProps,
isPageStatic,
PageInfo,
printCustomRoutes,
printTreeView,
} from './utils'
import getBaseWebpackConfig from './webpack-config'
import { writeBuildId } from './write-build-id'
import escapeStringRegexp from 'escape-string-regexp'

const fsAccess = promisify(fs.access)
const fsUnlink = promisify(fs.unlink)
Expand Down Expand Up @@ -394,8 +394,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
} else {
console.log(chalk.green('Compiled successfully.\n'))
telemetry.record(
eventBuildDuration({
totalPageCount: pagePaths.length,
eventBuildCompleted(pagePaths, {
durationInSeconds: webpackBuildEnd[0],
})
)
Expand Down
29 changes: 22 additions & 7 deletions packages/next/telemetry/events/build.ts
@@ -1,15 +1,34 @@
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'
type EventBuildCompleted = {
durationInSeconds: number
totalPageCount: number
hasDunderPages: boolean
hasTestPages: boolean
}

export function eventBuildDuration(
event: EventBuildCompleted
export function eventBuildCompleted(
pagePaths: string[],
event: Omit<
EventBuildCompleted,
'totalPageCount' | 'hasDunderPages' | 'hasTestPages'
>
): { eventName: string; payload: EventBuildCompleted } {
return {
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)
),
},
}
}

Expand All @@ -23,10 +42,6 @@ type EventBuildOptimized = {
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(
pagePaths: string[],
event: Omit<
Expand Down
9 changes: 7 additions & 2 deletions test/integration/telemetry/test/index.test.js
Expand Up @@ -108,8 +108,13 @@ describe('Telemetry CLI', () => {
path.join(appDir, 'pages', 'hello.test.skip')
)

expect(stderr).toMatch(/hasDunderPages.*?true/)
expect(stderr).toMatch(/hasTestPages.*?true/)
const event1 = /NEXT_BUILD_COMPLETED[\s\S]+?{([\s\S]+?)}/.exec(stderr).pop()
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 () => {
Expand Down

0 comments on commit bc81379

Please sign in to comment.