import { calculateHashesForCreateNodes, loadConfigFile, getNamedInputs, PluginCache, hashObject, workspaceDataDirectory, getReadyCheckReadyWhen, } from '@nx/devkit/internal'; import { AggregateCreateNodesError, CreateNodesContext, createNodesFromFiles, CreateNodesResultArray, CreateNodes, detectPackageManager, getPackageManagerCommand, NxJsonConfiguration, TargetConfiguration, } from '@nx/devkit'; import { getLockFileName } from '@nx/js'; import { addBuildAndWatchDepsTargets, isUsingTsSolutionSetup, } from '@nx/js/internal'; import type { NextConfig } from 'next'; import { readdirSync } from 'fs'; import { dirname, join } from 'path'; export interface NextPluginOptions { buildTargetName?: string; devTargetName?: string; startTargetName?: string; /** * @deprecated Use `startTargetName` instead. */ serveStaticTargetName?: string; buildDepsTargetName?: string; watchDepsTargetName?: string; } const nextConfigBlob = '**/next.config.{ts,js,cjs,mjs}'; // Next.js takes its port and hostname from the CLI and env, not from its // config, so the check gets no config file const readyWhen = getReadyCheckReadyWhen('@nx/next/ready-check'); type NextTargets = Record>; export const createNodes: CreateNodes = [ nextConfigBlob, async (configFiles, options, context) => { const optionsHash = hashObject(options); const cachePath = join(workspaceDataDirectory, `next-${optionsHash}.json`); const targetsCache = new PluginCache(cachePath); const isTsSolutionSetup = isUsingTsSolutionSetup(); const packageManager = detectPackageManager(context.workspaceRoot); const pmc = getPackageManagerCommand(packageManager); const lockFileName = getLockFileName(packageManager); const normalizedOptions = normalizeOptions(options); const typecheckTargetName = isTsSolutionSetup ? getTypecheckTargetName(context.nxJsonConfiguration) : false; try { const { entries, preErrors } = await filterNextConfigs( configFiles, context ); const projectHashes = await calculateHashesForCreateNodes( entries.map((e) => e.projectRoot), { ...normalizedOptions, typecheckTargetName }, context, entries.map(() => [lockFileName]) ); let results: CreateNodesResultArray = []; let nodeErrors: Array<[string | null, Error]> = []; try { results = await createNodesFromFiles( (configFile, _, ctx, idx) => createNodesInternal( configFile, normalizedOptions, ctx, targetsCache, isTsSolutionSetup, typecheckTargetName, pmc, projectHashes[idx] ), entries.map((e) => e.configFile), options, context ); } catch (e) { if (e instanceof AggregateCreateNodesError) { results = e.partialResults ?? []; nodeErrors = e.errors; } else { throw e; } } const allErrors = [...preErrors, ...nodeErrors]; if (allErrors.length > 0) { throw new AggregateCreateNodesError(allErrors, results); } return results; } finally { targetsCache.writeToDisk(); } }, ]; export const createNodesV2 = createNodes; async function createNodesInternal( configFilePath: string, options: NextPluginOptions, context: CreateNodesContext, targetsCache: PluginCache, isTsSolutionSetup: boolean, typecheckTargetName: string | false, pmc: ReturnType, hash: string ) { const projectRoot = dirname(configFilePath); if (!targetsCache.has(hash)) { targetsCache.set( hash, await buildNextTargets( configFilePath, projectRoot, options, context, isTsSolutionSetup, typecheckTargetName, pmc ) ); } return { projects: { [projectRoot]: { root: projectRoot, targets: targetsCache.get(hash), }, }, }; } async function buildNextTargets( nextConfigPath: string, projectRoot: string, options: NextPluginOptions, context: CreateNodesContext, isTsSolutionSetup: boolean, typecheckTargetName: string | false, pmc: ReturnType ) { const nextConfig = await getNextConfig(nextConfigPath, context); const namedInputs = getNamedInputs(projectRoot, context); const targets: Record = {}; targets[options.buildTargetName] = await getBuildTargetConfig( namedInputs, projectRoot, nextConfig, isTsSolutionSetup, typecheckTargetName ); targets[options.devTargetName] = getDevTargetConfig( namedInputs, projectRoot, isTsSolutionSetup ); const startTarget = getStartTargetConfig(namedInputs, options, projectRoot); targets[options.startTargetName] = startTarget; targets[options.serveStaticTargetName] = startTarget; addBuildAndWatchDepsTargets( context.workspaceRoot, projectRoot, targets, options, pmc ); return targets; } async function getBuildTargetConfig( namedInputs: { [inputName: string]: any[] }, projectRoot: string, nextConfig: any, isTsSolutionSetup: boolean, typecheckTargetName: string | false ) { const nextOutputPath = await getOutputs(projectRoot, nextConfig); // Set output path here so that `withNx` can pick it up. const targetConfig: TargetConfiguration = { command: `next build`, options: { cwd: projectRoot, }, dependsOn: ['^build'], cache: true, inputs: getInputs(namedInputs), outputs: [`${nextOutputPath}/!(cache)/**/*`, `${nextOutputPath}/!(cache)`], }; // v14/v15/v16 all emit the same `next build` inferred target shape - no per-major branch needed. // The tty=false is not version-specific: Next.js exits 0 on SIGINT regardless of major // (https://github.com/vercel/next.js/issues/62906). Turbopack-default-on (v16+) and other // bundler differences live at the executor / runtime layer, not here. targetConfig.options.tty = false; if (isTsSolutionSetup) { // `next build` type-checks against dependency declarations, which the // dependencies' typecheck targets emit in TS solution setups. if (typecheckTargetName) { targetConfig.dependsOn.push(`^${typecheckTargetName}`); } targetConfig.syncGenerators = ['@nx/js:typescript-sync']; } return targetConfig; } function getTypecheckTargetName( nxJson: NxJsonConfiguration | undefined ): string | false { const registration = nxJson?.plugins?.find( (p) => (typeof p === 'string' ? p : p.plugin) === '@nx/js/typescript' ); const typecheck = typeof registration === 'object' ? ( registration.options as { typecheck?: boolean | { targetName?: string }; } )?.typecheck : undefined; if (typecheck === false) { return false; } return (typeof typecheck === 'object' && typecheck.targetName) || 'typecheck'; } function getDevTargetConfig( namedInputs: { [inputName: string]: any[] }, projectRoot: string, isTsSolutionSetup: boolean ) { const targetConfig: TargetConfiguration = { continuous: true, command: `next dev`, options: { cwd: projectRoot, }, inputs: getInputs(namedInputs), readyWhen, }; if (isTsSolutionSetup) { targetConfig.syncGenerators = ['@nx/js:typescript-sync']; } return targetConfig; } function getStartTargetConfig( namedInputs: { [inputName: string]: any[] }, options: NextPluginOptions, projectRoot: string ) { const targetConfig: TargetConfiguration = { continuous: true, command: `next start`, options: { cwd: projectRoot, }, dependsOn: [options.buildTargetName], inputs: getInputs(namedInputs), readyWhen, }; return targetConfig; } async function getOutputs(projectRoot, nextConfig) { const { PHASE_PRODUCTION_BUILD } = require('next/constants'); let resolvedConfig: Pick | undefined; if (typeof nextConfig === 'function') { // Works for both async and sync functions. resolvedConfig = await Promise.resolve( nextConfig(PHASE_PRODUCTION_BUILD, { defaultConfig: {} }) ); } else if (typeof nextConfig === 'object') { resolvedConfig = nextConfig; } // Mirrors Next.js' own `hasCustomExportOutput` check. With `output: 'export'`, // `next build` treats a custom `distDir` as the export directory and falls back // to `out` otherwise, reusing `.next` internally for intermediate artifacts. let dir: string; if (resolvedConfig?.output === 'export') { dir = resolvedConfig.distDir && resolvedConfig.distDir !== '.next' ? resolvedConfig.distDir : 'out'; } else { dir = resolvedConfig?.distDir || '.next'; } if (projectRoot === '.') { return `{projectRoot}/${dir}`; } else { return `{workspaceRoot}/${projectRoot}/${dir}`; } } function getNextConfig( configFilePath: string, context: CreateNodesContext ): Promise { const resolvedPath = join(context.workspaceRoot, configFilePath); return loadConfigFile(resolvedPath); } interface NextEntry { configFile: string; projectRoot: string; } async function filterNextConfigs( configFiles: readonly string[], context: CreateNodesContext ): Promise<{ entries: NextEntry[]; preErrors: Array<[string, Error]>; }> { const preErrors: Array<[string, Error]> = []; const candidates = await Promise.all( configFiles.map(async (configFile): Promise => { try { const projectRoot = dirname(configFile); const siblingFiles = readdirSync( join(context.workspaceRoot, projectRoot) ); if ( !siblingFiles.includes('package.json') && !siblingFiles.includes('project.json') ) { return null; } return { configFile, projectRoot }; } catch (e) { preErrors.push([configFile, e as Error]); return null; } }) ); return { entries: candidates.filter((c): c is NextEntry => c !== null), preErrors, }; } function normalizeOptions(options: NextPluginOptions): NextPluginOptions { options ??= {}; options.buildTargetName ??= 'build'; options.devTargetName ??= 'dev'; options.startTargetName ??= 'start'; options.serveStaticTargetName ??= 'serve-static'; return options; } function getInputs( namedInputs: NxJsonConfiguration['namedInputs'] ): TargetConfiguration['inputs'] { return [ ...('production' in namedInputs ? ['default', '^default'] : ['default', '^default']), { externalDependencies: ['next'], }, { dependentTasksOutputFiles: '**/*.d.ts', transitive: true, }, ]; }