Skip to content

Commit 8dc7ca1

Browse files
committed
fix: force serverless target
1 parent 27dd4fb commit 8dc7ca1

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

helpers/getNextConfig.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const { cwd: getCwd } = require('process')
4-
const { resolve } = require('path')
54

65
const moize = require('moize')
76

helpers/hasCorrectNextConfig.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const getNextConfig = require('./getNextConfig')
2-
32
// Checks if site has the correct next.config.js
43
const hasCorrectNextConfig = async ({ nextConfigPath, failBuild }) => {
54
// In the plugin's case, no config is valid because we'll make it ourselves
@@ -12,9 +11,27 @@ const hasCorrectNextConfig = async ({ nextConfigPath, failBuild }) => {
1211
const isValidTarget = acceptableTargets.includes(target)
1312
if (!isValidTarget) {
1413
console.log(
15-
`Your next.config.js must set the "target" property to one of: ${acceptableTargets.join(', ')}. Update the
16-
target property to allow this plugin to run.`,
14+
`The "target" config property must be one of "${acceptableTargets.join('", "')}". Setting it to "serverless".`,
1715
)
16+
17+
/* eslint-disable fp/no-delete, node/no-unpublished-require */
18+
19+
// We emulate Vercel so that we can set target to serverless if needed
20+
process.env.NOW_BUILDER = true
21+
// If no valid target is set, we use an internal Next env var to force it
22+
process.env.NEXT_PRIVATE_TARGET = 'serverless'
23+
24+
// 🐉 We need Next to recalculate "isZeitNow" var so we can set the target, but it's
25+
// set as an import side effect so we need to clear the require cache first. 🐲
26+
// https://github.com/vercel/next.js/blob/canary/packages/next/telemetry/ci-info.ts
27+
28+
delete require.cache[require.resolve('next/dist/telemetry/ci-info')]
29+
delete require.cache[require.resolve('next/dist/next-server/server/config')]
30+
31+
// Clear memoized cache
32+
getNextConfig.clear()
33+
34+
/* eslint-enable fp/no-delete, node/no-unpublished-require */
1835
}
1936

2037
return isValidTarget

index.js

+2-15
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,8 @@ module.exports = {
2828
if (hasNoPackageJson) {
2929
return failBuild('Could not find a package.json for this project')
3030
}
31-
32-
const pluginNotNeeded = await doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })
33-
34-
if (!pluginNotNeeded) {
35-
const nextConfigPath = await findUp('next.config.js')
36-
if (nextConfigPath === undefined) {
37-
// Create the next config file with target set to serverless by default
38-
const nextConfig = `
39-
module.exports = {
40-
target: 'serverless'
41-
}
42-
`
43-
await pWriteFile('next.config.js', nextConfig)
44-
}
45-
}
31+
// Populates the correct config if needed
32+
await doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })
4633

4734
// Because we memoize nextConfig, we need to do this after the write file
4835
const nextConfig = await getNextConfig(utils.failBuild)

0 commit comments

Comments
 (0)