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

Detect Invalid Pages Before Optimize #10418

Merged
merged 1 commit into from Feb 4, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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