-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathindex.ts
192 lines (162 loc) · 5.57 KB
/
index.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* eslint-disable max-lines */
import { join, relative } from 'path'
import type { NetlifyPlugin } from '@netlify/build'
import { greenBright } from 'chalk'
import { existsSync, readFileSync } from 'fs-extra'
import { outdent } from 'outdent'
import { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME } from './constants'
import { restoreCache, saveCache } from './helpers/cache'
import {
getNextConfig,
getRequiredServerFiles,
updateRequiredServerFiles,
configureHandlerFunctions,
generateCustomHeaders,
} from './helpers/config'
import { updateConfig, writeMiddleware } from './helpers/edge'
import { moveStaticPages, movePublicFiles, patchNextFiles, unpatchNextFiles } from './helpers/files'
import { generateFunctions, setupImageFunction, generatePagesResolver } from './helpers/functions'
import { generateRedirects, generateStaticRedirects } from './helpers/redirects'
import { shouldSkip, isNextAuthInstalled } from './helpers/utils'
import {
verifyNetlifyBuildVersion,
checkNextSiteHasBuilt,
checkForRootPublish,
checkZipSize,
checkForOldFunctions,
warnForProblematicUserRewrites,
warnForRootRedirects,
} from './helpers/verification'
const plugin: NetlifyPlugin = {
async onPreBuild({
constants,
netlifyConfig,
utils: {
build: { failBuild },
cache,
},
}) {
const { publish } = netlifyConfig.build
if (shouldSkip()) {
await restoreCache({ cache, publish })
console.log('Not running Essential Next.js plugin')
if (existsSync(join(constants.INTERNAL_FUNCTIONS_SRC, HANDLER_FUNCTION_NAME))) {
console.log(`Please ensure you remove any generated functions from ${constants.INTERNAL_FUNCTIONS_SRC}`)
}
return
}
checkForRootPublish({ publish, failBuild })
verifyNetlifyBuildVersion({ failBuild, ...constants })
await restoreCache({ cache, publish })
netlifyConfig.build.environment ||= {}
// eslint-disable-next-line unicorn/consistent-destructuring
netlifyConfig.build.environment.NEXT_PRIVATE_TARGET = 'server'
},
async onBuild({
constants,
netlifyConfig,
utils: {
build: { failBuild },
},
}) {
if (shouldSkip()) {
return
}
const { publish } = netlifyConfig.build
checkNextSiteHasBuilt({ publish, failBuild })
let experimentalRemotePatterns = []
const { appDir, basePath, i18n, images, target, ignore, trailingSlash, outdir, experimental } = await getNextConfig(
{
publish,
failBuild,
},
)
if (experimental.images) {
experimentalRemotePatterns = experimental.images.remotePatterns || []
}
if (isNextAuthInstalled()) {
const config = await getRequiredServerFiles(publish)
const userDefinedNextAuthUrl = config.config.env.NEXTAUTH_URL
if (userDefinedNextAuthUrl) {
console.log(
`NextAuth package detected, NEXTAUTH_URL environment variable set by user to ${userDefinedNextAuthUrl}`,
)
} else {
const nextAuthUrl = `${process.env.URL}${basePath}`
console.log(`NextAuth package detected, setting NEXTAUTH_URL environment variable to ${nextAuthUrl}`)
config.config.env.NEXTAUTH_URL = nextAuthUrl
await updateRequiredServerFiles(publish, config)
}
}
const buildId = readFileSync(join(publish, 'BUILD_ID'), 'utf8').trim()
configureHandlerFunctions({ netlifyConfig, ignore, publish: relative(process.cwd(), publish) })
await generateFunctions(constants, appDir)
await generatePagesResolver({ target, constants })
await movePublicFiles({ appDir, outdir, publish })
await patchNextFiles(basePath)
if (!process.env.SERVE_STATIC_FILES_FROM_ORIGIN) {
await moveStaticPages({ target, netlifyConfig, i18n, basePath })
}
await generateStaticRedirects({
netlifyConfig,
nextConfig: { basePath, i18n },
})
await setupImageFunction({
constants,
imageconfig: images,
netlifyConfig,
basePath,
remotePatterns: experimentalRemotePatterns,
})
await generateRedirects({
netlifyConfig,
nextConfig: { basePath, i18n, trailingSlash, appDir },
buildId,
})
if (process.env.NEXT_USE_NETLIFY_EDGE) {
console.log(outdent`
✨ Deploying to ${greenBright`Netlify Edge Functions`} ✨
This feature is in beta. Please share your feedback here: https://ntl.fyi/next-netlify-edge
`)
await writeMiddleware(netlifyConfig)
await updateConfig(publish)
}
},
async onPostBuild({
netlifyConfig: {
build: { publish },
redirects,
headers,
},
utils: {
status,
cache,
functions,
build: { failBuild },
},
constants: { FUNCTIONS_DIST },
}) {
await saveCache({ cache, publish })
if (shouldSkip()) {
status.show({
title: 'Essential Next.js plugin did not run',
summary: `Next cache was stored, but all other functions were skipped because ${
process.env.NETLIFY_NEXT_PLUGIN_SKIP
? `NETLIFY_NEXT_PLUGIN_SKIP is set`
: `NEXT_PLUGIN_FORCE_RUN is set to ${process.env.NEXT_PLUGIN_FORCE_RUN}`
}`,
})
return
}
await checkForOldFunctions({ functions })
await checkZipSize(join(FUNCTIONS_DIST, `${ODB_FUNCTION_NAME}.zip`))
const nextConfig = await getNextConfig({ publish, failBuild })
const { basePath, appDir } = nextConfig
generateCustomHeaders(nextConfig, headers)
warnForProblematicUserRewrites({ basePath, redirects })
warnForRootRedirects({ appDir })
await unpatchNextFiles(basePath)
},
}
module.exports = plugin
/* eslint-enable max-lines */