next.js/packages/next/src/telemetry/events/version.ts
version.ts145 lines4.7 KB
import type { NextConfigComplete } from '../../server/config-shared'

const EVENT_VERSION = 'NEXT_CLI_SESSION_STARTED'

type EventCliSessionStarted = {
  nextVersion: string
  nodeVersion: string
  cliCommand: string
  isSrcDir: boolean | null
  hasNowJson: boolean
  isCustomServer: boolean | null
  hasNextConfig: boolean
  buildTarget: string
  hasWebpackConfig: boolean
  hasBabelConfig: boolean
  basePathEnabled: boolean
  i18nEnabled: boolean
  imageEnabled: boolean
  imageFutureEnabled: boolean
  locales: string | null
  localeDomainsCount: number | null
  localeDetectionEnabled: boolean | null
  imageDomainsCount: number | null
  imageRemotePatternsCount: number | null
  imageLocalPatternsCount: number | null
  imageQualities: string | null
  imageSizes: string | null
  imageLoader: string | null
  imageFormats: string | null
  nextConfigOutput: string | null
  trailingSlashEnabled: boolean
  reactStrictMode: boolean
  webpackVersion: number | null
  turboFlag: boolean
  useTurbopackWorkerAssetPrefix: boolean
  isRspack: boolean
  appDir: boolean | null
  pagesDir: boolean | null
  staticStaleTime: number | null
  dynamicStaleTime: number | null
  reactCompiler: boolean
  reactCompilerCompilationMode: string | null
  reactCompilerPanicThreshold: string | null
  adapterPath: boolean
}

export function eventCliSession(
  nextConfig: NextConfigComplete,
  event: Omit<
    EventCliSessionStarted,
    | 'nextVersion'
    | 'nodeVersion'
    | 'hasNextConfig'
    | 'buildTarget'
    | 'hasWebpackConfig'
    | 'hasBabelConfig'
    | 'basePathEnabled'
    | 'i18nEnabled'
    | 'imageEnabled'
    | 'imageFutureEnabled'
    | 'locales'
    | 'localeDomainsCount'
    | 'localeDetectionEnabled'
    | 'imageDomainsCount'
    | 'imageRemotePatternsCount'
    | 'imageLocalPatternsCount'
    | 'imageQualities'
    | 'imageSizes'
    | 'imageLoader'
    | 'imageFormats'
    | 'nextConfigOutput'
    | 'trailingSlashEnabled'
    | 'reactStrictMode'
    | 'staticStaleTime'
    | 'dynamicStaleTime'
    | 'reactCompiler'
    | 'reactCompilerCompilationMode'
    | 'reactCompilerPanicThreshold'
    | 'isRspack'
    | 'adapterPath'
    | 'useTurbopackWorkerAssetPrefix'
  >
): { 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 []
  }

  const { images, i18n } = nextConfig || {}

  const payload: EventCliSessionStarted = {
    nextVersion: process.env.__NEXT_VERSION,
    nodeVersion: process.version,
    cliCommand: event.cliCommand,
    isSrcDir: event.isSrcDir,
    hasNowJson: event.hasNowJson,
    isCustomServer: event.isCustomServer,
    hasNextConfig: nextConfig.configOrigin !== 'default',
    buildTarget: 'default',
    hasWebpackConfig: typeof nextConfig?.webpack === 'function',
    hasBabelConfig: false,
    imageEnabled: !!images,
    imageFutureEnabled: !!images,
    basePathEnabled: !!nextConfig?.basePath,
    i18nEnabled: !!i18n,
    locales: i18n?.locales ? i18n.locales.join(',') : null,
    localeDomainsCount: i18n?.domains ? i18n.domains.length : null,
    localeDetectionEnabled: !i18n ? null : i18n.localeDetection !== false,
    imageDomainsCount: images?.domains ? images.domains.length : null,
    imageRemotePatternsCount: images?.remotePatterns
      ? images.remotePatterns.length
      : null,
    imageLocalPatternsCount: images?.localPatterns
      ? images.localPatterns.length
      : null,
    imageSizes: images?.imageSizes ? images.imageSizes.join(',') : null,
    imageQualities: images?.qualities ? images.qualities.join(',') : null,
    imageLoader: images?.loader,
    imageFormats: images?.formats ? images.formats.join(',') : null,
    nextConfigOutput: nextConfig?.output || null,
    trailingSlashEnabled: !!nextConfig?.trailingSlash,
    reactStrictMode: !!nextConfig?.reactStrictMode,
    webpackVersion: event.webpackVersion || null,
    turboFlag: event.turboFlag || false,
    useTurbopackWorkerAssetPrefix:
      nextConfig?.experimental?.turbopackWorkerAssetPrefix !== undefined,
    isRspack: process.env.NEXT_RSPACK !== undefined,
    appDir: event.appDir,
    pagesDir: event.pagesDir,
    staticStaleTime: nextConfig.experimental.staleTimes?.static ?? null,
    dynamicStaleTime: nextConfig.experimental.staleTimes?.dynamic ?? null,
    reactCompiler: Boolean(nextConfig.reactCompiler),
    reactCompilerCompilationMode:
      typeof nextConfig.reactCompiler !== 'boolean'
        ? (nextConfig.reactCompiler?.compilationMode ?? null)
        : null,
    reactCompilerPanicThreshold:
      typeof nextConfig.reactCompiler !== 'boolean'
        ? (nextConfig.reactCompiler?.panicThreshold ?? null)
        : null,
    adapterPath: !!nextConfig?.adapterPath,
  }
  return [{ eventName: EVENT_VERSION, payload }]
}
Quest for Codev2.0.0
/
SIGN IN