@@ -16,10 +16,12 @@ import { join as posixJoin, sep as posixSep } from 'node:path/posix'
1616import { trace } from '@opentelemetry/api'
1717import { wrapTracer } from '@opentelemetry/api/experimental'
1818import glob from 'fast-glob'
19+ import type { NextConfigComplete } from 'next/dist/server/config-shared.js'
1920import { prerelease , lt as semverLowerThan , lte as semverLowerThanOrEqual } from 'semver'
2021
21- import { RUN_CONFIG } from '../../run/constants.js'
22- import { PluginContext } from '../plugin-context.js'
22+ import type { RunConfig } from '../../run/config.js'
23+ import { RUN_CONFIG_FILE } from '../../run/constants.js'
24+ import type { PluginContext , RequiredServerFilesManifest } from '../plugin-context.js'
2325
2426const tracer = wrapTracer ( trace . getTracer ( 'Next runtime' ) )
2527
@@ -32,8 +34,8 @@ function isError(error: unknown): error is NodeJS.ErrnoException {
3234/**
3335 * Copy App/Pages Router Javascript needed by the server handler
3436 */
35- export const copyNextServerCode = async ( ctx : PluginContext ) : Promise < void > => {
36- await tracer . withActiveSpan ( 'copyNextServerCode' , async ( ) => {
37+ export const copyNextServerCode = async ( ctx : PluginContext ) : Promise < NextConfigComplete > => {
38+ return await tracer . withActiveSpan ( 'copyNextServerCode' , async ( ) => {
3739 // update the dist directory inside the required-server-files.json to work with
3840 // nx monorepos and other setups where the dist directory is modified
3941 const reqServerFilesPath = join (
@@ -54,7 +56,9 @@ export const copyNextServerCode = async (ctx: PluginContext): Promise<void> => {
5456 throw error
5557 }
5658 }
57- const reqServerFiles = JSON . parse ( await readFile ( reqServerFilesPath , 'utf-8' ) )
59+ const reqServerFiles = JSON . parse (
60+ await readFile ( reqServerFilesPath , 'utf-8' ) ,
61+ ) as RequiredServerFilesManifest
5862
5963 // if the resolved dist folder does not match the distDir of the required-server-files.json
6064 // this means the path got altered by a plugin like nx and contained ../../ parts so we have to reset it
@@ -70,13 +74,6 @@ export const copyNextServerCode = async (ctx: PluginContext): Promise<void> => {
7074
7175 // ensure the directory exists before writing to it
7276 await mkdir ( ctx . serverHandlerDir , { recursive : true } )
73- // write our run-config.json to the root dir so that we can easily get the runtime config of the required-server-files.json
74- // without the need to know about the monorepo or distDir configuration upfront.
75- await writeFile (
76- join ( ctx . serverHandlerDir , RUN_CONFIG ) ,
77- JSON . stringify ( reqServerFiles . config ) ,
78- 'utf-8' ,
79- )
8077
8178 const srcDir = join ( ctx . standaloneDir , ctx . nextDistDir )
8279 // if the distDir got resolved and altered use the nextDistDir instead
@@ -114,6 +111,8 @@ export const copyNextServerCode = async (ctx: PluginContext): Promise<void> => {
114111 await cp ( srcPath , destPath , { recursive : true , force : true } )
115112 } ) ,
116113 )
114+
115+ return reqServerFiles . config
117116 } )
118117}
119118
@@ -336,9 +335,11 @@ const replaceMiddlewareManifest = async (sourcePath: string, destPath: string) =
336335}
337336
338337export const verifyHandlerDirStructure = async ( ctx : PluginContext ) => {
339- const runConfig = JSON . parse ( await readFile ( join ( ctx . serverHandlerDir , RUN_CONFIG ) , 'utf-8' ) )
338+ const { nextConfig } = JSON . parse (
339+ await readFile ( join ( ctx . serverHandlerDir , RUN_CONFIG_FILE ) , 'utf-8' ) ,
340+ ) as RunConfig
340341
341- const expectedBuildIDPath = join ( ctx . serverHandlerDir , runConfig . distDir , 'BUILD_ID' )
342+ const expectedBuildIDPath = join ( ctx . serverHandlerDir , nextConfig . distDir , 'BUILD_ID' )
342343 if ( ! existsSync ( expectedBuildIDPath ) ) {
343344 ctx . failBuild (
344345 `Failed creating server handler. BUILD_ID file not found at expected location "${ expectedBuildIDPath } ".` ,
0 commit comments