-
Notifications
You must be signed in to change notification settings - Fork 92
fix: force serverless target #343
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| const getNextConfig = require('./getNextConfig') | ||
|
|
||
| // Checks if site has the correct next.config.js | ||
| const hasCorrectNextConfig = async ({ nextConfigPath, failBuild }) => { | ||
| // In the plugin's case, no config is valid because we'll make it ourselves | ||
|
|
@@ -12,9 +11,27 @@ const hasCorrectNextConfig = async ({ nextConfigPath, failBuild }) => { | |
| const isValidTarget = acceptableTargets.includes(target) | ||
| if (!isValidTarget) { | ||
| console.log( | ||
| `Your next.config.js must set the "target" property to one of: ${acceptableTargets.join(', ')}. Update the | ||
| target property to allow this plugin to run.`, | ||
| `The "target" config property must be one of "${acceptableTargets.join('", "')}". Setting it to "serverless".`, | ||
| ) | ||
|
|
||
| /* 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() | ||
|
||
|
|
||
| /* eslint-enable fp/no-delete, node/no-unpublished-require */ | ||
| } | ||
|
|
||
| return isValidTarget | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -28,21 +28,8 @@ module.exports = { | |||
| if (hasNoPackageJson) { | ||||
| 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) | ||||
| } | ||||
| } | ||||
| // Populates the correct config if needed | ||||
| await doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild }) | ||||
|
|
||||
| // Because we memoize nextConfig, we need to do this after the write file | ||||
|
||||
| // Because we memoize nextConfig, we need to do this after the write file |
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.
"Settingit to serverless" might imply to some that we're actually mutating/updating/changing their config file (which i don't think we're doing? 🤔 ). maybe "forcing the build to use target serverless" or something?
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.
Good point