-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathindex.js
120 lines (100 loc) · 4.71 KB
/
index.js
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
const { readdirSync, existsSync } = require('fs')
const path = require('path')
const makeDir = require('make-dir')
const { restoreCache, saveCache } = require('./helpers/cacheBuild')
const checkNxConfig = require('./helpers/checkNxConfig')
const copyUnstableIncludedDirs = require('./helpers/copyUnstableIncludedDirs')
const doesNotNeedPlugin = require('./helpers/doesNotNeedPlugin')
const getNextConfig = require('./helpers/getNextConfig')
const getNextRoot = require('./helpers/getNextRoot')
const validateNextUsage = require('./helpers/validateNextUsage')
const verifyBuildTarget = require('./helpers/verifyBuildTarget')
const nextOnNetlify = require('./src')
// * Helpful Plugin Context *
// - Between the prebuild and build steps, the project's build command is run
// - Between the build and postbuild steps, any functions are bundled
module.exports = {
async onPreBuild({ netlifyConfig, packageJson, utils, constants }) {
const { failBuild } = utils.build
validateNextUsage({ failBuild, netlifyConfig })
const hasNoPackageJson = Object.keys(packageJson).length === 0
if (hasNoPackageJson) {
return failBuild('Could not find a package.json for this project')
}
if (doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })) {
return
}
// Populates the correct config if needed
await verifyBuildTarget({ netlifyConfig, packageJson, failBuild })
const nextRoot = getNextRoot({ netlifyConfig })
// Because we memoize nextConfig, we need to do this after the write file
const nextConfig = await getNextConfig(utils.failBuild, nextRoot)
// Nx needs special config handling, so check for it specifically
const isNx = Boolean(
(packageJson.devDependencies && packageJson.devDependencies['@nrwl/next']) ||
(packageJson.dependencies && packageJson.dependencies['@nrwl/next']),
)
if (isNx) {
console.log('Detected Nx site. Checking configuration...')
checkNxConfig({ netlifyConfig, packageJson, nextConfig, failBuild, constants })
}
if (process.env.NEXT_IMAGE_ALLOWED_DOMAINS) {
console.log(
`The Essential Next.js plugin now supports reading image domains from your Next config, so using NEXT_IMAGE_ALLOWED_DOMAINS is now deprecated. Please set images.domains in next.config.js instead, and remove the NEXT_IMAGE_ALLOWED_DOMAINS variable.`,
)
}
await restoreCache({ cache: utils.cache, distDir: nextConfig.distDir })
},
async onBuild({
netlifyConfig,
packageJson,
constants: { PUBLISH_DIR = DEFAULT_PUBLISH_DIR, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC },
utils,
}) {
const { failBuild } = utils.build
const nextRoot = getNextRoot({ netlifyConfig })
if (doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })) {
return
}
console.log('Detected Next.js site. Copying files...')
const { distDir } = await getNextConfig(failBuild, nextRoot)
const dist = path.resolve(nextRoot, distDir)
if (!existsSync(dist)) {
failBuild(`
Could not find "${distDir}" after building the site, which indicates that "next build" was not run.
Check that your build command includes "next build". If the site is a monorepo, see https://ntl.fyi/next-monorepo
for information on configuring the site. If this is not a Next.js site you should remove the Essential Next.js plugin.
See https://ntl.fyi/remove-plugin for instructions.
`)
}
console.log(`** Running Next on Netlify package **`)
await makeDir(PUBLISH_DIR)
await nextOnNetlify({
functionsDir: path.resolve(FUNCTIONS_SRC),
publishDir: netlifyConfig.build.publish || PUBLISH_DIR,
nextRoot,
})
},
async onPostBuild({ netlifyConfig, packageJson, constants: { FUNCTIONS_DIST = DEFAULT_FUNCTIONS_DIST }, utils }) {
if (doesNotNeedPlugin({ netlifyConfig, packageJson, utils })) {
utils.status.show({
title: 'Essential Next.js Build Plugin did not run',
summary: netlifyConfig.build.command
? 'The site either uses static export, manually runs next-on-netlify, or is not a Next.js site'
: 'The site config does not specify a build command',
})
return
}
const nextRoot = getNextRoot({ netlifyConfig })
const nextConfig = await getNextConfig(utils.failBuild, nextRoot)
await saveCache({ cache: utils.cache, distDir: nextConfig.distDir })
copyUnstableIncludedDirs({ nextConfig, functionsDist: path.resolve(FUNCTIONS_DIST) })
utils.status.show({
title: 'Essential Next.js Build Plugin ran successfully',
summary: 'Generated serverless functions and stored the Next.js cache',
})
},
}
const DEFAULT_FUNCTIONS_SRC = 'netlify/functions'
const DEFAULT_FUNCTIONS_DIST = '.netlify/functions/'
const DEFAULT_PUBLISH_DIR = 'out'