1
1
/* 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 >
23
31
24
32
const TEST_ROUTE = / ( | \/ ) \[ [ ^ / ] + ?] ( \/ | \. h t m l | $ ) /
25
33
26
- const isDynamicRoute = ( route ) => TEST_ROUTE . test ( route )
34
+ export const isDynamicRoute = ( route ) => TEST_ROUTE . test ( route )
27
35
28
- const stripLocale = ( rawPath , locales = [ ] ) => {
36
+ export const stripLocale = ( rawPath : string , locales : Array < string > = [ ] ) => {
29
37
const [ locale , ...segments ] = rawPath . split ( '/' )
30
38
if ( locales . includes ( locale ) ) {
31
39
return segments . join ( '/' )
32
40
}
33
41
return rawPath
34
42
}
35
43
36
- const matchMiddleware = ( middleware , filePath ) =>
44
+ export const matchMiddleware = ( middleware : Array < string > , filePath : string ) : string | boolean =>
37
45
middleware ?. includes ( '' ) ||
38
46
middleware ?. find (
39
47
( middlewarePath ) =>
40
48
filePath === middlewarePath || filePath === `${ middlewarePath } .html` || filePath . startsWith ( `${ middlewarePath } /` ) ,
41
49
)
42
50
43
- const matchesRedirect = ( file , redirects ) => {
51
+ export const matchesRedirect = ( file : string , redirects : Rewrites ) : boolean => {
44
52
if ( ! Array . isArray ( redirects ) ) {
45
53
return false
46
54
}
@@ -53,7 +61,7 @@ const matchesRedirect = (file, redirects) => {
53
61
} )
54
62
}
55
63
56
- const matchesRewrite = ( file , rewrites ) => {
64
+ export const matchesRewrite = ( file : string , rewrites : Rewrites ) : boolean => {
57
65
if ( Array . isArray ( rewrites ) ) {
58
66
return matchesRedirect ( file , rewrites )
59
67
}
@@ -63,14 +71,16 @@ const matchesRewrite = (file, rewrites) => {
63
71
return matchesRedirect ( file , rewrites . beforeFiles )
64
72
}
65
73
66
- exports . matchesRedirect = matchesRedirect
67
- exports . matchesRewrite = matchesRewrite
68
- exports . matchMiddleware = matchMiddleware
69
- exports . stripLocale = stripLocale
70
- exports . isDynamicRoute = isDynamicRoute
71
-
72
74
// 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 > => {
74
84
console . log ( 'Moving static page files to serve from CDN...' )
75
85
const outputDir = join ( netlifyConfig . build . publish , target === 'server' ? 'server' : 'serverless' )
76
86
const root = join ( outputDir , 'pages' )
@@ -87,12 +97,16 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
87
97
}
88
98
}
89
99
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
+ )
92
106
93
- const isrFiles = new Set ( )
107
+ const isrFiles = new Set < string > ( )
94
108
95
- const shortRevalidateRoutes = [ ]
109
+ const shortRevalidateRoutes : Array < { Route : string ; Revalidate : number } > = [ ]
96
110
97
111
Object . entries ( prerenderManifest . routes ) . forEach ( ( [ route , { initialRevalidateSeconds } ] ) => {
98
112
if ( initialRevalidateSeconds ) {
@@ -106,8 +120,8 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
106
120
}
107
121
} )
108
122
109
- const files = [ ]
110
- const filesManifest = { }
123
+ const files : Array < string > = [ ]
124
+ const filesManifest : Record < string , string > = { }
111
125
const moveFile = async ( file ) => {
112
126
const isData = file . endsWith ( '.json' )
113
127
const source = join ( root , file )
@@ -268,7 +282,7 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
268
282
}
269
283
}
270
284
271
- const patchFile = async ( { file, from, to } ) => {
285
+ const patchFile = async ( { file, from, to } : { file : string ; from : string ; to : string } ) : Promise < void > => {
272
286
if ( ! existsSync ( file ) ) {
273
287
return
274
288
}
@@ -299,7 +313,7 @@ const getServerFile = (root) => {
299
313
return serverFile
300
314
}
301
315
302
- exports . patchNextFiles = async ( root ) => {
316
+ export const patchNextFiles = async ( root : string ) : Promise < void > => {
303
317
const serverFile = getServerFile ( root )
304
318
console . log ( `Patching ${ serverFile } ` )
305
319
if ( serverFile ) {
@@ -311,15 +325,23 @@ exports.patchNextFiles = async (root) => {
311
325
}
312
326
}
313
327
314
- exports . unpatchNextFiles = async ( root ) => {
328
+ export const unpatchNextFiles = async ( root : string ) : Promise < void > => {
315
329
const serverFile = getServerFile ( root )
316
330
const origFile = `${ serverFile } .orig`
317
331
if ( existsSync ( origFile ) ) {
318
332
await move ( origFile , serverFile , { overwrite : true } )
319
333
}
320
334
}
321
335
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 > => {
323
345
// `outdir` is a config property added when using Next.js with Nx. It's typically
324
346
// a relative path outside of the appDir, e.g. '../../dist/apps/<app-name>', and
325
347
// the parent directory of the .next directory.
0 commit comments