-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathverifyBuildTarget.js
60 lines (48 loc) · 2.1 KB
/
verifyBuildTarget.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
const path = require('path')
const { writeFile } = require('fs-extra')
const getNextConfig = require('./getNextConfig')
const getNextRoot = require('./getNextRoot')
const resolveNextModule = require('./resolveNextModule')
// Checks if site has the correct next.config.js
const verifyBuildTarget = async ({ failBuild, netlifyConfig }) => {
const nextRoot = getNextRoot({ netlifyConfig })
const { target, configFile } = await getNextConfig(failBuild, nextRoot)
// If the next config exists, log warning if target isnt in acceptableTargets
const acceptableTargets = ['serverless', 'experimental-serverless-trace']
const isValidTarget = acceptableTargets.includes(target)
if (isValidTarget) {
return
}
console.log(
`The "target" config property must be one of "${acceptableTargets.join(
'", "',
)}". Building with "serverless" target.`,
)
// We emulate Vercel so that we can set target to serverless if needed
process.env.NOW_BUILDER = true
// If no valid target is set, we use an internal Next env var to force it
process.env.NEXT_PRIVATE_TARGET = 'serverless'
// 🐉 We need Next to recalculate "isZeitNow" var so we can set the target, but it's
// set as an import side effect so we need to clear the require cache first. 🐲
// https://github.com/vercel/next.js/blob/canary/packages/next/telemetry/ci-info.ts
delete require.cache[resolveNextModule('next/dist/telemetry/ci-info', nextRoot)]
delete require.cache[resolveNextModule('next/dist/next-server/server/config', nextRoot)]
// Clear memoized cache
getNextConfig.clear()
// Creating a config file, because otherwise Next won't reload the config and pick up the new target
if (!configFile) {
await writeFile(
path.resolve(nextRoot, 'next.config.js'),
`
module.exports = {
// Supported targets are "serverless" and "experimental-serverless-trace"
target: "serverless"
}`,
)
}
// Force the new config to be generated
await getNextConfig(failBuild, nextRoot)
// Reset the value in case something else is looking for it
process.env.NOW_BUILDER = false
}
module.exports = verifyBuildTarget