1
1
/* eslint-disable max-lines */
2
2
import { cpus } from 'os'
3
3
4
- import type { NetlifyConfig } from '@netlify/build'
5
4
import { yellowBright } from 'chalk'
6
5
import { existsSync , readJson , move , copy , writeJson , readFile , writeFile , ensureDir , readFileSync } from 'fs-extra'
7
6
import globby from 'globby'
@@ -60,11 +59,11 @@ export const matchesRewrite = (file: string, rewrites: Rewrites): boolean => {
60
59
return matchesRedirect ( file , rewrites . beforeFiles )
61
60
}
62
61
63
- export const getMiddleware = async ( publish : string ) : Promise < Array < string > > => {
62
+ export const getMiddleware = async ( distDir : string ) : Promise < Array < string > > => {
64
63
if ( process . env . NEXT_DISABLE_NETLIFY_EDGE !== 'true' && process . env . NEXT_DISABLE_NETLIFY_EDGE !== '1' ) {
65
64
return [ ]
66
65
}
67
- const manifestPath = join ( publish , 'server' , 'middleware-manifest.json' )
66
+ const manifestPath = join ( distDir , 'server' , 'middleware-manifest.json' )
68
67
if ( existsSync ( manifestPath ) ) {
69
68
const manifest = await readJson ( manifestPath , { throws : false } )
70
69
return manifest ?. sortedMiddleware ?? [ ]
@@ -74,32 +73,28 @@ export const getMiddleware = async (publish: string): Promise<Array<string>> =>
74
73
75
74
// eslint-disable-next-line max-lines-per-function
76
75
export const moveStaticPages = async ( {
77
- netlifyConfig,
78
- target,
76
+ distDir,
79
77
i18n,
80
78
basePath,
79
+ publishDir,
81
80
} : {
82
- netlifyConfig : NetlifyConfig
83
- target : 'server' | 'serverless' | 'experimental-serverless-trace'
81
+ distDir : string
84
82
i18n : NextConfig [ 'i18n' ]
85
83
basePath ?: string
84
+ publishDir
86
85
} ) : Promise < void > => {
87
86
console . log ( 'Moving static page files to serve from CDN...' )
88
- const outputDir = join ( netlifyConfig . build . publish , target === 'server' ? 'server' : 'serverless ')
87
+ const outputDir = join ( distDir , 'server' )
89
88
const root = join ( outputDir , 'pages' )
90
- const buildId = readFileSync ( join ( netlifyConfig . build . publish , 'BUILD_ID' ) , 'utf8' ) . trim ( )
89
+ const buildId = readFileSync ( join ( distDir , 'BUILD_ID' ) , 'utf8' ) . trim ( )
91
90
const dataDir = join ( '_next' , 'data' , buildId )
92
- await ensureDir ( join ( netlifyConfig . build . publish , dataDir ) )
91
+ await ensureDir ( join ( publishDir , dataDir ) )
93
92
// Load the middleware manifest so we can check if a file matches it before moving
94
- const middlewarePaths = await getMiddleware ( netlifyConfig . build . publish )
93
+ const middlewarePaths = await getMiddleware ( distDir )
95
94
const middleware = middlewarePaths . map ( ( path ) => path . slice ( 1 ) )
96
95
97
- const prerenderManifest : PrerenderManifest = await readJson (
98
- join ( netlifyConfig . build . publish , 'prerender-manifest.json' ) ,
99
- )
100
- const { redirects, rewrites } : RoutesManifest = await readJson (
101
- join ( netlifyConfig . build . publish , 'routes-manifest.json' ) ,
102
- )
96
+ const prerenderManifest : PrerenderManifest = await readJson ( join ( distDir , 'prerender-manifest.json' ) )
97
+ const { redirects, rewrites } : RoutesManifest = await readJson ( join ( distDir , 'routes-manifest.json' ) )
103
98
104
99
const isrFiles = new Set < string > ( )
105
100
@@ -128,7 +123,7 @@ export const moveStaticPages = async ({
128
123
files . push ( file )
129
124
filesManifest [ file ] = targetPath
130
125
131
- const dest = join ( netlifyConfig . build . publish , targetPath )
126
+ const dest = join ( publishDir , targetPath )
132
127
133
128
try {
134
129
await move ( source , dest )
@@ -242,10 +237,10 @@ export const moveStaticPages = async ({
242
237
}
243
238
244
239
// Write the manifest for use in the serverless functions
245
- await writeJson ( join ( netlifyConfig . build . publish , 'static-manifest.json' ) , Object . entries ( filesManifest ) )
240
+ await writeJson ( join ( distDir , 'static-manifest.json' ) , Object . entries ( filesManifest ) )
246
241
247
242
if ( i18n ?. defaultLocale ) {
248
- const rootPath = basePath ? join ( netlifyConfig . build . publish , basePath ) : netlifyConfig . build . publish
243
+ const rootPath = basePath ? join ( publishDir , basePath ) : publishDir
249
244
// Copy the default locale into the root
250
245
const defaultLocaleDir = join ( rootPath , i18n . defaultLocale )
251
246
if ( existsSync ( defaultLocaleDir ) ) {
@@ -427,12 +422,13 @@ export const unpatchNextFiles = async (root: string): Promise<void> => {
427
422
export const movePublicFiles = async ( {
428
423
appDir,
429
424
outdir,
430
- publish ,
425
+ publishDir ,
431
426
} : {
432
427
appDir : string
433
428
outdir ?: string
434
- publish : string
429
+ publishDir : string
435
430
} ) : Promise < void > => {
431
+ await ensureDir ( publishDir )
436
432
// `outdir` is a config property added when using Next.js with Nx. It's typically
437
433
// a relative path outside of the appDir, e.g. '../../dist/apps/<app-name>', and
438
434
// the parent directory of the .next directory.
@@ -441,7 +437,7 @@ export const movePublicFiles = async ({
441
437
// directory from the original app directory.
442
438
const publicDir = outdir ? join ( appDir , outdir , 'public' ) : join ( appDir , 'public' )
443
439
if ( existsSync ( publicDir ) ) {
444
- await copy ( publicDir , `${ publish } /` )
440
+ await copy ( publicDir , `${ publishDir } /` )
445
441
}
446
442
}
447
443
/* eslint-enable max-lines */
0 commit comments