1
1
/* eslint-disable max-lines */
2
2
3
- const { yellowBright } = require ( 'chalk' )
4
- const { readJSON, existsSync } = require ( 'fs-extra' )
5
- const { outdent } = require ( 'outdent' )
6
- const { join, dirname, relative } = require ( 'pathe' )
7
- const slash = require ( 'slash' )
3
+ import { NetlifyConfig } from '@netlify/build'
4
+ import { yellowBright } from 'chalk'
5
+ import { readJSON } from 'fs-extra'
6
+ import { PrerenderManifest } from 'next/dist/build'
7
+ import { outdent } from 'outdent'
8
+ import { join , dirname , relative } from 'pathe'
9
+ import slash from 'slash'
8
10
9
- const defaultFailBuild = ( message , { error } ) => {
11
+ import { HANDLER_FUNCTION_NAME , ODB_FUNCTION_NAME , HIDDEN_PATHS } from '../constants'
12
+
13
+ import { RequiredServerFiles } from './requiredServerFilesType'
14
+
15
+ const defaultFailBuild = ( message : string , { error } ) : never => {
10
16
throw new Error ( `${ message } \n${ error && error . stack } ` )
11
17
}
12
18
13
- const { HANDLER_FUNCTION_NAME , ODB_FUNCTION_NAME , HIDDEN_PATHS } = require ( '../constants' )
14
-
15
19
const ODB_FUNCTION_PATH = `/.netlify/builders/${ ODB_FUNCTION_NAME } `
16
20
const HANDLER_FUNCTION_PATH = `/.netlify/functions/${ HANDLER_FUNCTION_NAME } `
17
21
18
22
const CATCH_ALL_REGEX = / \/ \[ \. { 3 } ( .* ) ] ( .j s o n ) ? $ /
19
23
const OPTIONAL_CATCH_ALL_REGEX = / \/ \[ { 2 } \. { 3 } ( .* ) ] { 2 } ( .j s o n ) ? $ /
20
24
const DYNAMIC_PARAMETER_REGEX = / \/ \[ ( .* ?) ] / g
21
25
22
- const getNetlifyRoutes = ( nextRoute ) => {
26
+ const getNetlifyRoutes = ( nextRoute : string ) : Array < string > => {
23
27
let netlifyRoutes = [ nextRoute ]
24
28
25
29
// If the route is an optional catch-all route, we need to add a second
@@ -54,8 +58,12 @@ const getNetlifyRoutes = (nextRoute) => {
54
58
return netlifyRoutes
55
59
}
56
60
57
- exports . generateRedirects = async ( { netlifyConfig, basePath, i18n } ) => {
58
- const { dynamicRoutes, routes : staticRoutes } = await readJSON (
61
+ export const generateRedirects = async ( { netlifyConfig, basePath, i18n } : {
62
+ netlifyConfig : NetlifyConfig ,
63
+ basePath : string ,
64
+ i18n
65
+ } ) => {
66
+ const { dynamicRoutes, routes : staticRoutes } : PrerenderManifest = await readJSON (
59
67
join ( netlifyConfig . build . publish , 'prerender-manifest.json' ) ,
60
68
)
61
69
@@ -123,6 +131,8 @@ exports.generateRedirects = async ({ netlifyConfig, basePath, i18n }) => {
123
131
from : `${ basePath } /*` ,
124
132
to : HANDLER_FUNCTION_PATH ,
125
133
status : 200 ,
134
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
135
+ // @ts -ignore
126
136
conditions : { Cookie : [ '__prerender_bypass' , '__next_preview_data' ] } ,
127
137
force : true ,
128
138
} ,
@@ -158,14 +168,16 @@ exports.generateRedirects = async ({ netlifyConfig, basePath, i18n }) => {
158
168
}
159
169
}
160
170
161
- exports . getNextConfig = async function getNextConfig ( { publish, failBuild = defaultFailBuild } ) {
171
+ export const getNextConfig = async function getNextConfig ( { publish, failBuild = defaultFailBuild } ) {
162
172
try {
163
- const { config, appDir, ignore } = await readJSON ( join ( publish , 'required-server-files.json' ) )
173
+ const { config, appDir, ignore } : RequiredServerFiles = await readJSON ( join ( publish , 'required-server-files.json' ) )
164
174
if ( ! config ) {
175
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
176
+ // @ts -ignore
165
177
return failBuild ( 'Error loading your Next config' )
166
178
}
167
179
return { ...config , appDir, ignore }
168
- } catch ( error ) {
180
+ } catch ( error : unknown ) {
169
181
return failBuild ( 'Error loading your Next config' , { error } )
170
182
}
171
183
}
@@ -180,7 +192,7 @@ const resolveModuleRoot = (moduleName) => {
180
192
181
193
const DEFAULT_EXCLUDED_MODULES = [ 'sharp' , 'electron' ]
182
194
183
- exports . configureHandlerFunctions = ( { netlifyConfig, publish, ignore = [ ] } ) => {
195
+ export const configureHandlerFunctions = ( { netlifyConfig, publish, ignore = [ ] } ) => {
184
196
/* eslint-disable no-underscore-dangle */
185
197
netlifyConfig . functions . _ipx ||= { }
186
198
netlifyConfig . functions . _ipx . node_bundler = 'nft'
0 commit comments