File size: 5,265 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import type { NextConfigComplete } from '../../server/config-shared'
import path from 'path'

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
  isRspack: boolean
  appDir: boolean | null
  pagesDir: boolean | null
  staticStaleTime: number | null
  dynamicStaleTime: number | null
  reactCompiler: boolean
  reactCompilerCompilationMode: string | null
  reactCompilerPanicThreshold: string | null
}

function hasBabelConfig(dir: string): boolean {
  try {
    const noopFile = path.join(dir, 'noop.js')
    const res = (
      require('next/dist/compiled/babel/core') as typeof import('next/dist/compiled/babel/core')
    ).loadPartialConfig({
      cwd: dir,
      filename: noopFile,
      sourceFileName: noopFile,
    }) as any
    const isForTooling =
      res.options?.presets?.every(
        (e: any) => e?.file?.request === 'next/babel'
      ) && res.options?.plugins?.length === 0
    return res.hasFilesystemConfig() && !isForTooling
  } catch {
    return false
  }
}

export function eventCliSession(
  dir: string,
  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'
  >
): { 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: hasBabelConfig(dir),
    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,
    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.experimental.reactCompiler),
    reactCompilerCompilationMode:
      typeof nextConfig.experimental.reactCompiler !== 'boolean'
        ? nextConfig.experimental.reactCompiler?.compilationMode ?? null
        : null,
    reactCompilerPanicThreshold:
      typeof nextConfig.experimental.reactCompiler !== 'boolean'
        ? nextConfig.experimental.reactCompiler?.panicThreshold ?? null
        : null,
  }
  return [{ eventName: EVENT_VERSION, payload }]
}