2020-02-14 21:42:44 +01:00
|
|
|
import path from 'path'
|
2021-10-16 14:22:42 +02:00
|
|
|
import { NextConfigComplete } from '../../server/config-shared'
|
2020-02-14 21:42:44 +01:00
|
|
|
|
2019-09-05 02:31:35 +02:00
|
|
|
const EVENT_VERSION = 'NEXT_CLI_SESSION_STARTED'
|
2019-08-29 18:43:06 +02:00
|
|
|
|
2019-09-05 02:31:35 +02:00
|
|
|
type EventCliSessionStarted = {
|
|
|
|
nextVersion: string
|
|
|
|
nodeVersion: string
|
|
|
|
cliCommand: string
|
2019-10-23 04:42:16 +02:00
|
|
|
isSrcDir: boolean | null
|
2019-11-08 18:03:50 +01:00
|
|
|
hasNowJson: boolean
|
|
|
|
isCustomServer: boolean | null
|
2020-02-14 21:42:44 +01:00
|
|
|
hasNextConfig: boolean
|
|
|
|
buildTarget: string
|
|
|
|
hasWebpackConfig: boolean
|
|
|
|
hasBabelConfig: boolean
|
2020-10-27 14:46:18 +01:00
|
|
|
basePathEnabled: boolean
|
|
|
|
i18nEnabled: boolean
|
|
|
|
imageEnabled: boolean
|
|
|
|
locales: string | null
|
|
|
|
localeDomainsCount: number | null
|
|
|
|
localeDetectionEnabled: boolean | null
|
|
|
|
imageDomainsCount: number | null
|
|
|
|
imageSizes: string | null
|
|
|
|
imageLoader: string | null
|
2022-04-20 00:47:23 +02:00
|
|
|
imageFormats: string | null
|
2020-10-27 14:46:18 +01:00
|
|
|
trailingSlashEnabled: boolean
|
|
|
|
reactStrictMode: boolean
|
2021-04-20 16:46:40 +02:00
|
|
|
webpackVersion: number | null
|
2020-02-14 21:42:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function hasBabelConfig(dir: string): boolean {
|
|
|
|
try {
|
|
|
|
const noopFile = path.join(dir, 'noop.js')
|
2020-11-06 03:33:14 +01:00
|
|
|
const res = require('next/dist/compiled/babel/core').loadPartialConfig({
|
2020-02-14 21:42:44 +01:00
|
|
|
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,
|
2021-10-16 14:22:42 +02:00
|
|
|
nextConfig: NextConfigComplete,
|
2020-02-14 21:42:44 +01:00
|
|
|
event: Omit<
|
|
|
|
EventCliSessionStarted,
|
|
|
|
| 'nextVersion'
|
|
|
|
| 'nodeVersion'
|
|
|
|
| 'hasNextConfig'
|
|
|
|
| 'buildTarget'
|
|
|
|
| 'hasWebpackConfig'
|
|
|
|
| 'hasBabelConfig'
|
2020-10-27 14:46:18 +01:00
|
|
|
| 'basePathEnabled'
|
|
|
|
| 'i18nEnabled'
|
|
|
|
| 'imageEnabled'
|
|
|
|
| 'locales'
|
|
|
|
| 'localeDomainsCount'
|
|
|
|
| 'localeDetectionEnabled'
|
|
|
|
| 'imageDomainsCount'
|
|
|
|
| 'imageSizes'
|
|
|
|
| 'imageLoader'
|
2022-04-20 00:47:23 +02:00
|
|
|
| 'imageFormats'
|
2020-10-27 14:46:18 +01:00
|
|
|
| 'trailingSlashEnabled'
|
|
|
|
| 'reactStrictMode'
|
2020-02-14 21:42:44 +01:00
|
|
|
>
|
2019-10-10 19:18:07 +02:00
|
|
|
): { eventName: string; payload: EventCliSessionStarted }[] {
|
2019-08-29 18:43:06 +02:00
|
|
|
// This should be an invariant, if it fails our build tooling is broken.
|
|
|
|
if (typeof process.env.__NEXT_VERSION !== 'string') {
|
2019-10-10 19:18:07 +02:00
|
|
|
return []
|
2019-08-29 18:43:06 +02:00
|
|
|
}
|
|
|
|
|
2021-10-16 14:22:42 +02:00
|
|
|
const { images, i18n } = nextConfig || {}
|
2020-10-27 14:46:18 +01:00
|
|
|
|
2020-02-14 21:42:44 +01:00
|
|
|
const payload: EventCliSessionStarted = {
|
|
|
|
nextVersion: process.env.__NEXT_VERSION,
|
|
|
|
nodeVersion: process.version,
|
|
|
|
cliCommand: event.cliCommand,
|
|
|
|
isSrcDir: event.isSrcDir,
|
|
|
|
hasNowJson: event.hasNowJson,
|
|
|
|
isCustomServer: event.isCustomServer,
|
2021-10-16 14:22:42 +02:00
|
|
|
hasNextConfig: nextConfig.configOrigin !== 'default',
|
|
|
|
buildTarget: nextConfig.target === 'server' ? 'default' : nextConfig.target,
|
|
|
|
hasWebpackConfig: typeof nextConfig?.webpack === 'function',
|
2020-02-14 21:42:44 +01:00
|
|
|
hasBabelConfig: hasBabelConfig(dir),
|
2020-10-27 14:46:18 +01:00
|
|
|
imageEnabled: !!images,
|
2021-10-16 14:22:42 +02:00
|
|
|
basePathEnabled: !!nextConfig?.basePath,
|
2020-10-27 14:46:18 +01:00
|
|
|
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,
|
2021-10-16 14:22:42 +02:00
|
|
|
imageSizes: images?.imageSizes ? images.imageSizes.join(',') : null,
|
2020-10-27 14:46:18 +01:00
|
|
|
imageLoader: images?.loader,
|
2022-04-20 00:47:23 +02:00
|
|
|
imageFormats: images?.formats ? images.formats.join(',') : null,
|
2021-10-16 14:22:42 +02:00
|
|
|
trailingSlashEnabled: !!nextConfig?.trailingSlash,
|
|
|
|
reactStrictMode: !!nextConfig?.reactStrictMode,
|
2021-04-20 16:46:40 +02:00
|
|
|
webpackVersion: event.webpackVersion || null,
|
2020-02-14 21:42:44 +01:00
|
|
|
}
|
|
|
|
return [{ eventName: EVENT_VERSION, payload }]
|
2019-08-29 18:43:06 +02:00
|
|
|
}
|