-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathconfig.ts
89 lines (79 loc) · 3.01 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { readJSON } from 'fs-extra'
import type { NextConfigComplete } from 'next/dist/server/config-shared'
import { join, dirname, relative } from 'pathe'
import slash from 'slash'
import { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME } from '../constants'
export interface RequiredServerFiles {
version?: number
config?: NextConfigComplete
appDir?: string
files?: string[]
ignore?: string[]
}
export type NextConfig = Pick<RequiredServerFiles, 'appDir' | 'ignore'> & NextConfigComplete
const defaultFailBuild = (message: string, { error }): never => {
throw new Error(`${message}\n${error && error.stack}`)
}
export const getNextConfig = async function getNextConfig({
publish,
failBuild = defaultFailBuild,
}): Promise<NextConfig> {
try {
const { config, appDir, ignore }: RequiredServerFiles = await readJSON(join(publish, 'required-server-files.json'))
if (!config) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return failBuild('Error loading your Next config')
}
return { ...config, appDir, ignore }
} catch (error: unknown) {
return failBuild('Error loading your Next config', { error })
}
}
const resolveModuleRoot = (moduleName) => {
try {
return dirname(relative(process.cwd(), require.resolve(`${moduleName}/package.json`, { paths: [process.cwd()] })))
} catch {
return null
}
}
const DEFAULT_EXCLUDED_MODULES = ['sharp', 'electron']
export const configureHandlerFunctions = ({ netlifyConfig, publish, ignore = [] }) => {
/* eslint-disable no-underscore-dangle */
netlifyConfig.functions._ipx ||= {}
netlifyConfig.functions._ipx.node_bundler = 'nft'
/* eslint-enable no-underscore-dangle */
;[HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME].forEach((functionName) => {
netlifyConfig.functions[functionName] ||= { included_files: [], external_node_modules: [] }
netlifyConfig.functions[functionName].node_bundler = 'nft'
netlifyConfig.functions[functionName].included_files ||= []
netlifyConfig.functions[functionName].included_files.push(
'.env',
'.env.local',
'.env.production',
'.env.production.local',
`${publish}/server/**`,
`${publish}/serverless/**`,
`${publish}/*.json`,
`${publish}/BUILD_ID`,
`${publish}/static/chunks/webpack-middleware*.js`,
`!${publish}/server/**/*.js.nft.json`,
...ignore.map((path) => `!${slash(path)}`),
)
const nextRoot = resolveModuleRoot('next')
if (nextRoot) {
netlifyConfig.functions[functionName].included_files.push(
`!${nextRoot}/dist/server/lib/squoosh/**/*.wasm`,
`!${nextRoot}/dist/next-server/server/lib/squoosh/**/*.wasm`,
`!${nextRoot}/dist/compiled/webpack/bundle4.js`,
`!${nextRoot}/dist/compiled/webpack/bundle5.js`,
)
}
DEFAULT_EXCLUDED_MODULES.forEach((moduleName) => {
const moduleRoot = resolveModuleRoot(moduleName)
if (moduleRoot) {
netlifyConfig.functions[functionName].included_files.push(`!${moduleRoot}/**/*`)
}
})
})
}