Skip to content

fix: force serverless target #408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions demo/next.config.js

This file was deleted.

14 changes: 4 additions & 10 deletions helpers/doesNotNeedPlugin.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
const findUp = require('find-up')

// Checks all the cases for which the plugin should do nothing
const isStaticExportProject = require('./isStaticExportProject')
const doesSiteUseNextOnNetlify = require('./doesSiteUseNextOnNetlify')
const hasCorrectNextConfig = require('./hasCorrectNextConfig')
const isStaticExportProject = require('./isStaticExportProject')

const doesNotNeedPlugin = async ({ netlifyConfig, packageJson, failBuild }) => {
const doesNotNeedPlugin = ({ netlifyConfig, packageJson }) => {
const { build } = netlifyConfig
const { name, scripts = {} } = packageJson
const nextConfigPath = await findUp('next.config.js')
const { scripts = {} } = packageJson

return (
isStaticExportProject({ build, scripts }) ||
doesSiteUseNextOnNetlify({ packageJson }) ||
!(await hasCorrectNextConfig({ nextConfigPath, failBuild }))
)
return isStaticExportProject({ build, scripts }) || doesSiteUseNextOnNetlify({ packageJson })
}

module.exports = doesNotNeedPlugin
1 change: 0 additions & 1 deletion helpers/getNextConfig.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const { cwd: getCwd } = require('process')
const { resolve } = require('path')

const moize = require('moize')

Expand Down
23 changes: 0 additions & 23 deletions helpers/hasCorrectNextConfig.js

This file was deleted.

58 changes: 58 additions & 0 deletions helpers/verifyBuildTarget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const getNextConfig = require('./getNextConfig')
const findUp = require('find-up')
const { writeFile, unlink } = require('fs-extra')
const path = require('path')

// Checks if site has the correct next.config.js
const verifyBuildTarget = async ({ failBuild }) => {
const { target } = await getNextConfig(failBuild)

// 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.`,
)

/* eslint-disable fp/no-delete, node/no-unpublished-require */

// 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[require.resolve('next/dist/telemetry/ci-info')]
delete require.cache[require.resolve('next/dist/next-server/server/config')]

// Clear memoized cache
getNextConfig.clear()

// Creating a config file, because otherwise Next won't reload the config and pick up the new target

if (!(await findUp('next.config.js'))) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fyi this is part of the monorepo problem. just an fyi we can address later

await writeFile(
path.resolve('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)

// Reset the value in case something else is looking for it
process.env.NOW_BUILDER = false
/* eslint-enable fp/no-delete, node/no-unpublished-require */
}

module.exports = verifyBuildTarget
30 changes: 8 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
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 verifyBuildTarget = require('./helpers/verifyBuildTarget')
const nextOnNetlify = require('./src')

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
Expand All @@ -29,21 +23,13 @@ module.exports = {
return failBuild('Could not find a package.json for this project')
}

const pluginNotNeeded = await doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })

if (!pluginNotNeeded) {
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)
}
if (doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })) {
return
}

// Populates the correct config if needed
await verifyBuildTarget({ netlifyConfig, packageJson, failBuild })

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

Expand All @@ -64,7 +50,7 @@ module.exports = {
}) {
const { failBuild } = utils.build

if (await doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })) {
if (doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })) {
return
}

Expand All @@ -76,7 +62,7 @@ module.exports = {
},

async onPostBuild({ netlifyConfig, packageJson, constants: { FUNCTIONS_DIST }, utils }) {
if (await doesNotNeedPlugin({ netlifyConfig, packageJson, utils })) {
if (doesNotNeedPlugin({ netlifyConfig, packageJson, utils })) {
return
}

Expand Down
Loading