@@ -16,10 +16,12 @@ import { join as posixJoin, sep as posixSep } from 'node:path/posix'
16
16
import { trace } from '@opentelemetry/api'
17
17
import { wrapTracer } from '@opentelemetry/api/experimental'
18
18
import glob from 'fast-glob'
19
+ import type { NextConfigComplete } from 'next/dist/server/config-shared.js'
19
20
import { prerelease , lt as semverLowerThan , lte as semverLowerThanOrEqual } from 'semver'
20
21
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'
23
25
24
26
const tracer = wrapTracer ( trace . getTracer ( 'Next runtime' ) )
25
27
@@ -32,8 +34,8 @@ function isError(error: unknown): error is NodeJS.ErrnoException {
32
34
/**
33
35
* Copy App/Pages Router Javascript needed by the server handler
34
36
*/
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 ( ) => {
37
39
// update the dist directory inside the required-server-files.json to work with
38
40
// nx monorepos and other setups where the dist directory is modified
39
41
const reqServerFilesPath = join (
@@ -54,7 +56,9 @@ export const copyNextServerCode = async (ctx: PluginContext): Promise<void> => {
54
56
throw error
55
57
}
56
58
}
57
- const reqServerFiles = JSON . parse ( await readFile ( reqServerFilesPath , 'utf-8' ) )
59
+ const reqServerFiles = JSON . parse (
60
+ await readFile ( reqServerFilesPath , 'utf-8' ) ,
61
+ ) as RequiredServerFilesManifest
58
62
59
63
// if the resolved dist folder does not match the distDir of the required-server-files.json
60
64
// 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> => {
70
74
71
75
// ensure the directory exists before writing to it
72
76
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
- )
80
77
81
78
const srcDir = join ( ctx . standaloneDir , ctx . nextDistDir )
82
79
// if the distDir got resolved and altered use the nextDistDir instead
@@ -114,6 +111,8 @@ export const copyNextServerCode = async (ctx: PluginContext): Promise<void> => {
114
111
await cp ( srcPath , destPath , { recursive : true , force : true } )
115
112
} ) ,
116
113
)
114
+
115
+ return reqServerFiles . config
117
116
} )
118
117
}
119
118
@@ -336,9 +335,11 @@ const replaceMiddlewareManifest = async (sourcePath: string, destPath: string) =
336
335
}
337
336
338
337
export 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
340
341
341
- const expectedBuildIDPath = join ( ctx . serverHandlerDir , runConfig . distDir , 'BUILD_ID' )
342
+ const expectedBuildIDPath = join ( ctx . serverHandlerDir , nextConfig . distDir , 'BUILD_ID' )
342
343
if ( ! existsSync ( expectedBuildIDPath ) ) {
343
344
ctx . failBuild (
344
345
`Failed creating server handler. BUILD_ID file not found at expected location "${ expectedBuildIDPath } ".` ,
0 commit comments