-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathindex.js
79 lines (63 loc) · 2.53 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
const fs = require('fs')
const path = require('path')
const util = require('util')
const makeDir = require('make-dir')
const findUp = require('find-up')
const nextOnNetlify = require('./src/index.js')
const validateNextUsage = require('./helpers/validateNextUsage')
const doesNotNeedPlugin = require('./helpers/doesNotNeedPlugin')
const getNextConfig = require('./helpers/getNextConfig')
const copyUnstableIncludedDirs = require('./helpers/copyUnstableIncludedDirs')
const { restoreCache, saveCache } = require('./helpers/cacheBuild')
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')
}
const nextConfig = await getNextConfig(utils.failBuild)
await restoreCache({ cache: utils.cache, distDir: nextConfig.distDir })
if (await doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })) {
return
}
const nextConfigPath = await findUp('next.config.js')
if (nextConfigPath === undefined) {
// Create the next config file with target set to serverless by default
const nextConfig = `
module.exports = {
target: 'serverless'
}
`
await pWriteFile('next.config.js', nextConfig)
}
},
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'