Skip to content

Commit a1ab7bf

Browse files
committed
fix: extract target checks
1 parent 5b71269 commit a1ab7bf

File tree

4 files changed

+51
-62
lines changed

4 files changed

+51
-62
lines changed

helpers/doesNotNeedPlugin.js

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
const findUp = require('find-up')
22

33
// Checks all the cases for which the plugin should do nothing
4-
const isStaticExportProject = require('./isStaticExportProject')
54
const doesSiteUseNextOnNetlify = require('./doesSiteUseNextOnNetlify')
6-
const hasCorrectNextConfig = require('./hasCorrectNextConfig')
5+
const isStaticExportProject = require('./isStaticExportProject')
76

8-
const doesNotNeedPlugin = async ({ netlifyConfig, packageJson, failBuild }) => {
7+
const doesNotNeedPlugin = ({ netlifyConfig, packageJson }) => {
98
const { build } = netlifyConfig
10-
const { name, scripts = {} } = packageJson
11-
const nextConfigPath = await findUp('next.config.js')
9+
const { scripts = {} } = packageJson
1210

13-
return (
14-
isStaticExportProject({ build, scripts }) ||
15-
doesSiteUseNextOnNetlify({ packageJson }) ||
16-
!(await hasCorrectNextConfig({ nextConfigPath, failBuild }))
17-
)
11+
return isStaticExportProject({ build, scripts }) || doesSiteUseNextOnNetlify({ packageJson })
1812
}
1913

2014
module.exports = doesNotNeedPlugin

helpers/hasCorrectNextConfig.js

-42
This file was deleted.

helpers/verifyBuildTarget.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const getNextConfig = require('./getNextConfig')
2+
// Checks if site has the correct next.config.js
3+
const verifyBuildTarget = async ({ failBuild }) => {
4+
const { target } = await getNextConfig(failBuild)
5+
6+
// If the next config exists, log warning if target isnt in acceptableTargets
7+
const acceptableTargets = ['serverless', 'experimental-serverless-trace']
8+
const isValidTarget = acceptableTargets.includes(target)
9+
if (isValidTarget) {
10+
return
11+
}
12+
console.log(
13+
`The "target" config property must be one of "${acceptableTargets.join(
14+
'", "',
15+
)}". Building with "serverless" target.`,
16+
)
17+
18+
/* eslint-disable fp/no-delete, node/no-unpublished-require */
19+
20+
// We emulate Vercel so that we can set target to serverless if needed
21+
process.env.NOW_BUILDER = true
22+
// If no valid target is set, we use an internal Next env var to force it
23+
process.env.NEXT_PRIVATE_TARGET = 'serverless'
24+
25+
// 🐉 We need Next to recalculate "isZeitNow" var so we can set the target, but it's
26+
// set as an import side effect so we need to clear the require cache first. 🐲
27+
// https://github.com/vercel/next.js/blob/canary/packages/next/telemetry/ci-info.ts
28+
29+
delete require.cache[require.resolve('next/dist/telemetry/ci-info')]
30+
delete require.cache[require.resolve('next/dist/next-server/server/config')]
31+
32+
// Clear memoized cache
33+
getNextConfig.clear()
34+
35+
/* eslint-enable fp/no-delete, node/no-unpublished-require */
36+
}
37+
38+
module.exports = verifyBuildTarget

index.js

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1-
const fs = require('fs')
2-
const path = require('path')
3-
const util = require('util')
4-
5-
const findUp = require('find-up')
61
const makeDir = require('make-dir')
72

83
const { restoreCache, saveCache } = require('./helpers/cacheBuild')
94
const copyUnstableIncludedDirs = require('./helpers/copyUnstableIncludedDirs')
105
const doesNotNeedPlugin = require('./helpers/doesNotNeedPlugin')
116
const getNextConfig = require('./helpers/getNextConfig')
127
const validateNextUsage = require('./helpers/validateNextUsage')
8+
const verifyBuildTarget = require('./helpers/verifyBuildTarget')
139
const nextOnNetlify = require('./src/index.js')
1410

15-
const pWriteFile = util.promisify(fs.writeFile)
16-
1711
// * Helpful Plugin Context *
1812
// - Between the prebuild and build steps, the project's build command is run
1913
// - Between the build and postbuild steps, any functions are bundled
@@ -28,8 +22,13 @@ module.exports = {
2822
if (hasNoPackageJson) {
2923
return failBuild('Could not find a package.json for this project')
3024
}
25+
26+
if (doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })) {
27+
return
28+
}
29+
3130
// Populates the correct config if needed
32-
await doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })
31+
await verifyBuildTarget({ netlifyConfig, packageJson, failBuild })
3332

3433
// Because we memoize nextConfig, we need to do this after the write file
3534
const nextConfig = await getNextConfig(utils.failBuild)
@@ -51,7 +50,7 @@ module.exports = {
5150
}) {
5251
const { failBuild } = utils.build
5352

54-
if (await doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })) {
53+
if (doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })) {
5554
return
5655
}
5756

@@ -63,7 +62,7 @@ module.exports = {
6362
},
6463

6564
async onPostBuild({ netlifyConfig, packageJson, constants: { FUNCTIONS_DIST }, utils }) {
66-
if (await doesNotNeedPlugin({ netlifyConfig, packageJson, utils })) {
65+
if (doesNotNeedPlugin({ netlifyConfig, packageJson, utils })) {
6766
return
6867
}
6968

0 commit comments

Comments
 (0)