Skip to content

Commit

Permalink
Check next.config.js settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Timer committed Feb 5, 2020
1 parent 4203d7f commit 5f39205
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 25 deletions.
4 changes: 2 additions & 2 deletions packages/next/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ import loadConfig, {
import {
eventBuildCompleted,
eventBuildOptimize,
eventCliSession,
eventNextPlugins,
eventVersion,
} from '../telemetry/events'
import { Telemetry } from '../telemetry/storage'
import { CompilerResult, runCompiler } from './compiler'
Expand Down Expand Up @@ -157,7 +157,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
let hasPublicDir = false

telemetry.record(
eventVersion({
eventCliSession(PHASE_PRODUCTION_BUILD, dir, {
cliCommand: 'build',
isSrcDir: path.relative(dir, pagesDir!).startsWith('src'),
hasNowJson: !!(await findUp('now.json', { cwd: dir })),
Expand Down
4 changes: 2 additions & 2 deletions packages/next/export/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
import loadConfig, {
isTargetLikeServerless,
} from '../next-server/server/config'
import { eventVersion } from '../telemetry/events'
import { eventCliSession } from '../telemetry/events'
import { Telemetry } from '../telemetry/storage'

const mkdirp = promisify(mkdirpModule)
Expand Down Expand Up @@ -104,7 +104,7 @@ export default async function(

if (telemetry) {
telemetry.record(
eventVersion({
eventCliSession(PHASE_EXPORT, distDir, {
cliCommand: 'export',
isSrcDir: null,
hasNowJson: !!(await findUp('now.json', { cwd: dir })),
Expand Down
2 changes: 1 addition & 1 deletion packages/next/next-server/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ function assignDefaults(userConfig: { [key: string]: any }) {
return result
}

function normalizeConfig(phase: string, config: any) {
export function normalizeConfig(phase: string, config: any) {
if (typeof config === 'function') {
config = config(phase, { defaultConfig })

Expand Down
10 changes: 5 additions & 5 deletions packages/next/server/next-dev-server.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import AmpHtmlValidator from 'amphtml-validator'
import findUp from 'find-up'
import fs from 'fs'
import { IncomingMessage, ServerResponse } from 'http'
import { join, relative } from 'path'
import React from 'react'
import { UrlWithParsedQuery } from 'url'
import { promisify } from 'util'
import Watchpack from 'watchpack'
import findUp from 'find-up'
import { ampValidation } from '../build/output/index'
import * as Log from '../build/output/log'
import checkCustomRoutes from '../lib/check-custom-routes'
import { PUBLIC_DIR_MIDDLEWARE_CONFLICT } from '../lib/constants'
import { findPagesDir } from '../lib/find-pages-dir'
import { verifyTypeScriptSetup } from '../lib/verifyTypeScriptSetup'
Expand All @@ -21,13 +22,12 @@ import {
} from '../next-server/lib/router/utils'
import Server, { ServerConstructor } from '../next-server/server/next-server'
import { normalizePagePath } from '../next-server/server/normalize-page-path'
import Router, { route, Params } from '../next-server/server/router'
import { eventVersion } from '../telemetry/events'
import Router, { Params, route } from '../next-server/server/router'
import { eventCliSession } from '../telemetry/events'
import { Telemetry } from '../telemetry/storage'
import ErrorDebug from './error-debug'
import HotReloader from './hot-reloader'
import { findPageFile } from './lib/find-page-file'
import checkCustomRoutes from '../lib/check-custom-routes'

if (typeof React.Suspense === 'undefined') {
throw new Error(
Expand Down Expand Up @@ -230,7 +230,7 @@ export default class DevServer extends Server {

const telemetry = new Telemetry({ distDir: this.distDir })
telemetry.record(
eventVersion({
eventCliSession(PHASE_DEVELOPMENT_SERVER, this.distDir, {
cliCommand: 'dev',
isSrcDir: relative(this.dir, this.pagesDir!).startsWith('src'),
hasNowJson: !!(await findUp('now.json', { cwd: this.dir })),
Expand Down
96 changes: 81 additions & 15 deletions packages/next/telemetry/events/version.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
import Babel from '@babel/core'
import findUp from 'find-up'
import {
CONFIG_FILE,
PHASE_DEVELOPMENT_SERVER,
PHASE_EXPORT,
PHASE_PRODUCTION_BUILD,
} from '../../next-server/lib/constants'
import { normalizeConfig } from '../../next-server/server/config'

const EVENT_VERSION = 'NEXT_CLI_SESSION_STARTED'

type EventCliSessionStarted = {
Expand All @@ -7,27 +17,83 @@ type EventCliSessionStarted = {
isSrcDir: boolean | null
hasNowJson: boolean
isCustomServer: boolean | null
hasNextConfig: boolean
buildTarget: string
hasWebpackConfig: boolean
hasBabelConfig: boolean
}

function hasBabelConfig(dir: string): boolean {
try {
const res = Babel.loadPartialConfig({ cwd: dir }) as any
return res.hasFilesystemConfig()
} catch {
return false
}
}

type NextConfigurationPhase =
| typeof PHASE_DEVELOPMENT_SERVER
| typeof PHASE_PRODUCTION_BUILD
| typeof PHASE_EXPORT

function getNextConfig(
phase: NextConfigurationPhase,
dir: string
): { [key: string]: any } | null {
try {
const configurationPath = findUp.sync(CONFIG_FILE, {
cwd: dir,
})

if (configurationPath) {
// This should've already been loaded, and thus should be cached / won't
// be re-evaluated.
const configurationModule = require(configurationPath)

// Re-normalize the configuration.
return normalizeConfig(
phase,
configurationModule.default || configurationModule
)
}
} catch {
// ignored
}
return null
}

export function eventVersion(
event: Omit<EventCliSessionStarted, 'nextVersion' | 'nodeVersion'>
export function eventCliSession(
phase: NextConfigurationPhase,
dir: string,
event: Omit<
EventCliSessionStarted,
| 'nextVersion'
| 'nodeVersion'
| 'hasNextConfig'
| 'buildTarget'
| 'hasWebpackConfig'
| 'hasBabelConfig'
>
): { eventName: string; payload: EventCliSessionStarted }[] {
// This should be an invariant, if it fails our build tooling is broken.
if (typeof process.env.__NEXT_VERSION !== 'string') {
return []
}

return [
{
eventName: EVENT_VERSION,
payload: {
nextVersion: process.env.__NEXT_VERSION,
nodeVersion: process.version,
cliCommand: event.cliCommand,
isSrcDir: event.isSrcDir,
hasNowJson: event.hasNowJson,
isCustomServer: event.isCustomServer,
} as EventCliSessionStarted,
},
]
const userConfiguration = getNextConfig(phase, dir)

const payload: EventCliSessionStarted = {
nextVersion: process.env.__NEXT_VERSION,
nodeVersion: process.version,
cliCommand: event.cliCommand,
isSrcDir: event.isSrcDir,
hasNowJson: event.hasNowJson,
isCustomServer: event.isCustomServer,
hasNextConfig: !!userConfiguration,
buildTarget: userConfiguration?.target ?? 'default',
hasWebpackConfig: typeof userConfiguration?.webpack === 'function',
hasBabelConfig: hasBabelConfig(dir),
}
return [{ eventName: EVENT_VERSION, payload }]
}

0 comments on commit 5f39205

Please sign in to comment.