11/* eslint-disable max-lines */
2- const { cpus } = require ( 'os' )
3-
4- const { yellowBright } = require ( 'chalk' )
5- const {
6- existsSync,
7- readJson,
8- move,
9- copy,
10- writeJson,
11- readFile,
12- writeFile,
13- ensureDir,
14- readFileSync,
15- } = require ( 'fs-extra' )
16- const globby = require ( 'globby' )
17- const { outdent } = require ( 'outdent' )
18- const pLimit = require ( 'p-limit' )
19- const { join } = require ( 'pathe' )
20- const slash = require ( 'slash' )
21-
22- const { MINIMUM_REVALIDATE_SECONDS , DIVIDER } = require ( '../constants' )
2+ import { cpus } from 'os'
3+
4+ import { NetlifyConfig } from '@netlify/build'
5+ import { yellowBright } from 'chalk'
6+ import { existsSync , readJson , move , copy , writeJson , readFile , writeFile , ensureDir , readFileSync } from 'fs-extra'
7+ import globby from 'globby'
8+ import { PrerenderManifest } from 'next/dist/build'
9+ import { Redirect as NextRedirect } from 'next/dist/lib/load-custom-routes'
10+ import { outdent } from 'outdent'
11+ import pLimit from 'p-limit'
12+ import { join } from 'pathe'
13+ import slash from 'slash'
14+
15+ import { MINIMUM_REVALIDATE_SECONDS , DIVIDER } from '../constants'
16+
17+ import { NextConfig } from './config'
18+
19+ interface Redirect extends NextRedirect {
20+ regex : string
21+ internal ?: boolean
22+ }
23+
24+ type Rewrites =
25+ | {
26+ fallback ?: Array < Redirect >
27+ afterFiles ?: Array < Redirect >
28+ beforeFiles ?: Array < Redirect >
29+ }
30+ | Array < Redirect >
2331
2432const TEST_ROUTE = / ( | \/ ) \[ [ ^ / ] + ?] ( \/ | \. h t m l | $ ) /
2533
26- const isDynamicRoute = ( route ) => TEST_ROUTE . test ( route )
34+ export const isDynamicRoute = ( route ) => TEST_ROUTE . test ( route )
2735
28- const stripLocale = ( rawPath , locales = [ ] ) => {
36+ export const stripLocale = ( rawPath : string , locales : Array < string > = [ ] ) => {
2937 const [ locale , ...segments ] = rawPath . split ( '/' )
3038 if ( locales . includes ( locale ) ) {
3139 return segments . join ( '/' )
3240 }
3341 return rawPath
3442}
3543
36- const matchMiddleware = ( middleware , filePath ) =>
44+ export const matchMiddleware = ( middleware : Array < string > , filePath : string ) : string | boolean =>
3745 middleware ?. includes ( '' ) ||
3846 middleware ?. find (
3947 ( middlewarePath ) =>
4048 filePath === middlewarePath || filePath === `${ middlewarePath } .html` || filePath . startsWith ( `${ middlewarePath } /` ) ,
4149 )
4250
43- const matchesRedirect = ( file , redirects ) => {
51+ export const matchesRedirect = ( file : string , redirects : Rewrites ) : boolean => {
4452 if ( ! Array . isArray ( redirects ) ) {
4553 return false
4654 }
@@ -53,7 +61,7 @@ const matchesRedirect = (file, redirects) => {
5361 } )
5462}
5563
56- const matchesRewrite = ( file , rewrites ) => {
64+ export const matchesRewrite = ( file : string , rewrites : Rewrites ) : boolean => {
5765 if ( Array . isArray ( rewrites ) ) {
5866 return matchesRedirect ( file , rewrites )
5967 }
@@ -63,14 +71,16 @@ const matchesRewrite = (file, rewrites) => {
6371 return matchesRedirect ( file , rewrites . beforeFiles )
6472}
6573
66- exports . matchesRedirect = matchesRedirect
67- exports . matchesRewrite = matchesRewrite
68- exports . matchMiddleware = matchMiddleware
69- exports . stripLocale = stripLocale
70- exports . isDynamicRoute = isDynamicRoute
71-
7274// eslint-disable-next-line max-lines-per-function
73- exports . moveStaticPages = async ( { netlifyConfig, target, i18n } ) => {
75+ export const moveStaticPages = async ( {
76+ netlifyConfig,
77+ target,
78+ i18n,
79+ } : {
80+ netlifyConfig : NetlifyConfig
81+ target : 'server' | 'serverless' | 'experimental-serverless-trace'
82+ i18n : NextConfig [ 'i18n' ]
83+ } ) : Promise < void > => {
7484 console . log ( 'Moving static page files to serve from CDN...' )
7585 const outputDir = join ( netlifyConfig . build . publish , target === 'server' ? 'server' : 'serverless' )
7686 const root = join ( outputDir , 'pages' )
@@ -87,12 +97,16 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
8797 }
8898 }
8999
90- const prerenderManifest = await readJson ( join ( netlifyConfig . build . publish , 'prerender-manifest.json' ) )
91- const { redirects, rewrites } = await readJson ( join ( netlifyConfig . build . publish , 'routes-manifest.json' ) )
100+ const prerenderManifest : PrerenderManifest = await readJson (
101+ join ( netlifyConfig . build . publish , 'prerender-manifest.json' ) ,
102+ )
103+ const { redirects, rewrites } : { redirects : Array < Redirect > ; rewrites : Rewrites } = await readJson (
104+ join ( netlifyConfig . build . publish , 'routes-manifest.json' ) ,
105+ )
92106
93- const isrFiles = new Set ( )
107+ const isrFiles = new Set < string > ( )
94108
95- const shortRevalidateRoutes = [ ]
109+ const shortRevalidateRoutes : Array < { Route : string ; Revalidate : number } > = [ ]
96110
97111 Object . entries ( prerenderManifest . routes ) . forEach ( ( [ route , { initialRevalidateSeconds } ] ) => {
98112 if ( initialRevalidateSeconds ) {
@@ -106,8 +120,8 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
106120 }
107121 } )
108122
109- const files = [ ]
110- const filesManifest = { }
123+ const files : Array < string > = [ ]
124+ const filesManifest : Record < string , string > = { }
111125 const moveFile = async ( file ) => {
112126 const isData = file . endsWith ( '.json' )
113127 const source = join ( root , file )
@@ -268,7 +282,7 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
268282 }
269283}
270284
271- const patchFile = async ( { file, from, to } ) => {
285+ const patchFile = async ( { file, from, to } : { file : string ; from : string ; to : string } ) : Promise < void > => {
272286 if ( ! existsSync ( file ) ) {
273287 return
274288 }
@@ -299,7 +313,7 @@ const getServerFile = (root) => {
299313 return serverFile
300314}
301315
302- exports . patchNextFiles = async ( root ) => {
316+ export const patchNextFiles = async ( root : string ) : Promise < void > => {
303317 const serverFile = getServerFile ( root )
304318 console . log ( `Patching ${ serverFile } ` )
305319 if ( serverFile ) {
@@ -311,15 +325,23 @@ exports.patchNextFiles = async (root) => {
311325 }
312326}
313327
314- exports . unpatchNextFiles = async ( root ) => {
328+ export const unpatchNextFiles = async ( root : string ) : Promise < void > => {
315329 const serverFile = getServerFile ( root )
316330 const origFile = `${ serverFile } .orig`
317331 if ( existsSync ( origFile ) ) {
318332 await move ( origFile , serverFile , { overwrite : true } )
319333 }
320334}
321335
322- exports . movePublicFiles = async ( { appDir, outdir, publish } ) => {
336+ export const movePublicFiles = async ( {
337+ appDir,
338+ outdir,
339+ publish,
340+ } : {
341+ appDir : string
342+ outdir ?: string
343+ publish : string
344+ } ) : Promise < void > => {
323345 // `outdir` is a config property added when using Next.js with Nx. It's typically
324346 // a relative path outside of the appDir, e.g. '../../dist/apps/<app-name>', and
325347 // the parent directory of the .next directory.
0 commit comments