1
1
import childProcess from 'node:child_process'
2
- import fs from 'fs/promises'
2
+ import fsPromises from 'fs/promises'
3
+ import fs from 'node:fs'
3
4
import path from 'node:path'
4
5
import type { PrerenderManifest , RoutesManifest } from 'next/dist/build'
5
6
import { type ProjectPackager , type ProjectSettings } from '../common/project'
@@ -16,6 +17,22 @@ interface BuildAppOptions {
16
17
17
18
export const OUTPUT_FOLDER = 'serverless-next'
18
19
20
+ export const cleanOutputFolder = ( ) => {
21
+ const outputFolderPath = path . join ( process . cwd ( ) , OUTPUT_FOLDER )
22
+
23
+ fs . rmSync ( outputFolderPath , { recursive : true , force : true } )
24
+ }
25
+
26
+ export const createOutputFolder = ( ) => {
27
+ const outputFolderPath = path . join ( process . cwd ( ) , OUTPUT_FOLDER )
28
+ // clean folder before creating new build output.
29
+ cleanOutputFolder ( )
30
+
31
+ fs . mkdirSync ( outputFolderPath )
32
+
33
+ return outputFolderPath
34
+ }
35
+
19
36
const setNextEnvs = ( ) => {
20
37
process . env . NEXT_SERVERLESS_DEPLOYING_PHASE = 'true'
21
38
}
@@ -29,19 +46,23 @@ export const buildNext = async (options: BuildOptions) => {
29
46
30
47
const copyAssets = async ( outputPath : string , appPath : string , appRelativePath : string ) => {
31
48
// Copying static assets (like js, css, images, .etc)
32
- await fs . cp ( path . join ( appPath , '.next' ) , path . join ( outputPath , '.next' ) , {
49
+ await fsPromises . cp ( path . join ( appPath , '.next' ) , path . join ( outputPath , '.next' ) , {
33
50
recursive : true
34
51
} )
35
- await fs . cp (
52
+ await fsPromises . cp (
36
53
path . join ( appPath , '.next' , 'static' ) ,
37
54
path . join ( outputPath , '.next' , 'standalone' , appRelativePath , '.next' , 'static' ) ,
38
55
{
39
56
recursive : true
40
57
}
41
58
)
42
- await fs . cp ( path . join ( appPath , 'public' ) , path . join ( outputPath , '.next' , 'standalone' , appRelativePath , 'public' ) , {
43
- recursive : true
44
- } )
59
+ await fsPromises . cp (
60
+ path . join ( appPath , 'public' ) ,
61
+ path . join ( outputPath , '.next' , 'standalone' , appRelativePath , 'public' ) ,
62
+ {
63
+ recursive : true
64
+ }
65
+ )
45
66
}
46
67
47
68
const getRewritesConfig = ( manifestRules : RoutesManifest [ 'rewrites' ] ) : NextRewrites => {
@@ -86,11 +107,11 @@ export const getNextCachedRoutesConfig = async (
86
107
outputPath : string ,
87
108
appRelativePath : string
88
109
) : Promise < { cachedRoutesMatchers : string [ ] ; rewritesConfig : NextRewrites ; redirectsConfig : NextRedirects } > => {
89
- const prerenderManifestJSON = await fs . readFile (
110
+ const prerenderManifestJSON = await fsPromises . readFile (
90
111
path . join ( outputPath , '.next' , 'standalone' , appRelativePath , '.next' , 'prerender-manifest.json' ) ,
91
112
'utf-8'
92
113
)
93
- const routesManifestJSON = await fs . readFile (
114
+ const routesManifestJSON = await fsPromises . readFile (
94
115
path . join ( outputPath , '.next' , 'standalone' , appRelativePath , '.next' , 'routes-manifest.json' ) ,
95
116
'utf-8'
96
117
)
0 commit comments