-
Notifications
You must be signed in to change notification settings - Fork 87
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5876ab5
fix: force serverless target
ascorbic edcc653
fix: fix tests
ascorbic d9451e8
fix: extract target checks
ascorbic 0456d6c
fix: changes from review
ascorbic af78226
fix: create temporary config file
ascorbic 625a2c0
fix: changes from review
ascorbic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'))) { | ||
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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