11/* eslint-disable max-lines */
22import { cpus } from 'os'
33
4- import type { NetlifyConfig } from '@netlify/build'
54import { yellowBright } from 'chalk'
65import { existsSync , readJson , move , copy , writeJson , readFile , writeFile , ensureDir , readFileSync } from 'fs-extra'
76import globby from 'globby'
@@ -60,11 +59,11 @@ export const matchesRewrite = (file: string, rewrites: Rewrites): boolean => {
6059 return matchesRedirect ( file , rewrites . beforeFiles )
6160}
6261
63- export const getMiddleware = async ( publish : string ) : Promise < Array < string > > => {
62+ export const getMiddleware = async ( distDir : string ) : Promise < Array < string > > => {
6463 if ( process . env . NEXT_DISABLE_NETLIFY_EDGE !== 'true' && process . env . NEXT_DISABLE_NETLIFY_EDGE !== '1' ) {
6564 return [ ]
6665 }
67- const manifestPath = join ( publish , 'server' , 'middleware-manifest.json' )
66+ const manifestPath = join ( distDir , 'server' , 'middleware-manifest.json' )
6867 if ( existsSync ( manifestPath ) ) {
6968 const manifest = await readJson ( manifestPath , { throws : false } )
7069 return manifest ?. sortedMiddleware ?? [ ]
@@ -74,32 +73,28 @@ export const getMiddleware = async (publish: string): Promise<Array<string>> =>
7473
7574// eslint-disable-next-line max-lines-per-function
7675export const moveStaticPages = async ( {
77- netlifyConfig,
78- target,
76+ distDir,
7977 i18n,
8078 basePath,
79+ publishDir,
8180} : {
82- netlifyConfig : NetlifyConfig
83- target : 'server' | 'serverless' | 'experimental-serverless-trace'
81+ distDir : string
8482 i18n : NextConfig [ 'i18n' ]
8583 basePath ?: string
84+ publishDir
8685} ) : Promise < void > => {
8786 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' )
8988 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 ( )
9190 const dataDir = join ( '_next' , 'data' , buildId )
92- await ensureDir ( join ( netlifyConfig . build . publish , dataDir ) )
91+ await ensureDir ( join ( publishDir , dataDir ) )
9392 // 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 )
9594 const middleware = middlewarePaths . map ( ( path ) => path . slice ( 1 ) )
9695
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' ) )
10398
10499 const isrFiles = new Set < string > ( )
105100
@@ -128,7 +123,7 @@ export const moveStaticPages = async ({
128123 files . push ( file )
129124 filesManifest [ file ] = targetPath
130125
131- const dest = join ( netlifyConfig . build . publish , targetPath )
126+ const dest = join ( publishDir , targetPath )
132127
133128 try {
134129 await move ( source , dest )
@@ -242,10 +237,10 @@ export const moveStaticPages = async ({
242237 }
243238
244239 // 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 ) )
246241
247242 if ( i18n ?. defaultLocale ) {
248- const rootPath = basePath ? join ( netlifyConfig . build . publish , basePath ) : netlifyConfig . build . publish
243+ const rootPath = basePath ? join ( publishDir , basePath ) : publishDir
249244 // Copy the default locale into the root
250245 const defaultLocaleDir = join ( rootPath , i18n . defaultLocale )
251246 if ( existsSync ( defaultLocaleDir ) ) {
@@ -427,12 +422,13 @@ export const unpatchNextFiles = async (root: string): Promise<void> => {
427422export const movePublicFiles = async ( {
428423 appDir,
429424 outdir,
430- publish ,
425+ publishDir ,
431426} : {
432427 appDir : string
433428 outdir ?: string
434- publish : string
429+ publishDir : string
435430} ) : Promise < void > => {
431+ await ensureDir ( publishDir )
436432 // `outdir` is a config property added when using Next.js with Nx. It's typically
437433 // a relative path outside of the appDir, e.g. '../../dist/apps/<app-name>', and
438434 // the parent directory of the .next directory.
@@ -441,7 +437,7 @@ export const movePublicFiles = async ({
441437 // directory from the original app directory.
442438 const publicDir = outdir ? join ( appDir , outdir , 'public' ) : join ( appDir , 'public' )
443439 if ( existsSync ( publicDir ) ) {
444- await copy ( publicDir , `${ publish } /` )
440+ await copy ( publicDir , `${ publishDir } /` )
445441 }
446442}
447443/* eslint-enable max-lines */
0 commit comments