-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathindex.js
76 lines (60 loc) · 2.59 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
const fs = require('fs')
const path = require('path')
const util = require('util')
const findUp = require('find-up')
const makeDir = require('make-dir')
const { restoreCache, saveCache } = require('./helpers/cacheBuild')
const copyUnstableIncludedDirs = require('./helpers/copyUnstableIncludedDirs')
const doesNotNeedPlugin = require('./helpers/doesNotNeedPlugin')
const getNextConfig = require('./helpers/getNextConfig')
const validateNextUsage = require('./helpers/validateNextUsage')
const nextOnNetlify = require('./src/index.js')
const pWriteFile = util.promisify(fs.writeFile)
// * 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 }) {
const { failBuild } = utils.build
validateNextUsage(failBuild)
const hasNoPackageJson = Object.keys(packageJson).length === 0
if (hasNoPackageJson) {
return failBuild('Could not find a package.json for this project')
}
// Populates the correct config if needed
await doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })
// Because we memoize nextConfig, we need to do this after the write file
const nextConfig = await getNextConfig(utils.failBuild)
if (nextConfig.images.domains.length !== 0 && !process.env.NEXT_IMAGE_ALLOWED_DOMAINS) {
console.log(
`Image domains set in next.config.js are ignored.\nPlease set the env variable NEXT_IMAGE_ALLOWED_DOMAINS to "${nextConfig.images.domains.join(
',',
)}" instead`,
)
}
await restoreCache({ cache: utils.cache, distDir: nextConfig.distDir })
},
async onBuild({
netlifyConfig,
packageJson,
constants: { PUBLISH_DIR, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC },
utils,
}) {
const { failBuild } = utils.build
if (await doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })) {
return
}
console.log(`** Running Next on Netlify package **`)
await makeDir(PUBLISH_DIR)
await nextOnNetlify({ functionsDir: FUNCTIONS_SRC, publishDir: PUBLISH_DIR })
},
async onPostBuild({ netlifyConfig, packageJson, constants: { FUNCTIONS_DIST }, utils }) {
if (await doesNotNeedPlugin({ netlifyConfig, packageJson, utils })) {
return
}
const nextConfig = await getNextConfig(utils.failBuild)
await saveCache({ cache: utils.cache, distDir: nextConfig.distDir })
copyUnstableIncludedDirs({ nextConfig, functionsDist: FUNCTIONS_DIST })
},
}
const DEFAULT_FUNCTIONS_SRC = 'netlify/functions'