Skip to content

feat: use internal functions dir #549

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 4 commits into from
Aug 3, 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
25 changes: 21 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const { readdirSync, existsSync } = require('fs')
const path = require('path')

const { yellowBright, greenBright } = require('chalk')
const makeDir = require('make-dir')
const { satisfies } = require('semver')
const glob = require('tiny-glob')

const { restoreCache, saveCache } = require('./helpers/cacheBuild')
const checkNxConfig = require('./helpers/checkNxConfig')
Expand All @@ -23,6 +25,8 @@ const REQUIRED_BUILD_VERSION = '>=15.11.5'

module.exports = {
async onPreBuild({ netlifyConfig, packageJson, utils, constants = {} }) {
const { FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC, INTERNAL_FUNCTIONS_SRC, IS_LOCAL, NETLIFY_BUILD_VERSION } = constants

const { failBuild } = utils.build

validateNextUsage({ failBuild, netlifyConfig })
Expand All @@ -36,7 +40,7 @@ module.exports = {
return
}
// We check for build version because that's what's available to us, but prompt about the cli because that's what they can upgrade
if (constants.IS_LOCAL && !satisfies(constants.NETLIFY_BUILD_VERSION, REQUIRED_BUILD_VERSION)) {
if (IS_LOCAL && !satisfies(NETLIFY_BUILD_VERSION, REQUIRED_BUILD_VERSION, { includePrerelease: true })) {
return failBuild(
`This version of the Essential Next.js plugin requires netlify-cli@4.4.2 or higher. Please upgrade and try again.
You can do this by running: "npm install -g netlify-cli@latest" or "yarn global add netlify-cli@latest"`,
Expand Down Expand Up @@ -66,6 +70,19 @@ You can do this by running: "npm install -g netlify-cli@latest" or "yarn global
`The Essential Next.js plugin now supports reading image domains from your Next config, so using NEXT_IMAGE_ALLOWED_DOMAINS is now deprecated. Please set images.domains in next.config.js instead, and remove the NEXT_IMAGE_ALLOWED_DOMAINS variable.`,
)
}

if (INTERNAL_FUNCTIONS_SRC && existsSync(FUNCTIONS_SRC)) {
const oldFunctions = await glob('next_*', { cwd: FUNCTIONS_SRC })

if (oldFunctions.length !== 0) {
console.log(yellowBright`
Detected the following functions that seem to have been generated by an old version of the Essential Next.js plugin.
The plugin no longer uses these and they should be deleted to avoid conflicts.\n`)
console.log(greenBright`• ${oldFunctions.join('\n• ')}
`)
}
}

Choose a reason for hiding this comment

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

nice


await restoreCache({ cache: utils.cache, distDir: nextConfig.distDir, nextRoot })

// Exit the build process on unhandled promise rejection. This is the default in Node 15+, but earlier versions just warn.
Expand All @@ -75,7 +92,7 @@ You can do this by running: "npm install -g netlify-cli@latest" or "yarn global
async onBuild({
netlifyConfig,
packageJson,
constants: { PUBLISH_DIR = DEFAULT_PUBLISH_DIR, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC },
constants: { PUBLISH_DIR = DEFAULT_PUBLISH_DIR, INTERNAL_FUNCTIONS_SRC, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC },
utils,
}) {
const { failBuild } = utils.build
Expand All @@ -95,7 +112,7 @@ You can do this by running: "npm install -g netlify-cli@latest" or "yarn global
Could not find "${distDir}" after building the site, which indicates that "next build" was not run or that we're looking in the wrong place.
We're using the config file ${configFile}, and looking for the dist directory at ${dist}. If this is incorrect, try deleting the config file, or
moving it to the correct place. Check that your build command includes "next build". If the site is a monorepo, see https://ntl.fyi/next-monorepo
for information on configuring the site. If this is not a Next.js site, or if it uses static export, you should remove the Essential Next.js plugin.
for information on configuring the site. If this is not a Next.js site or if it uses static export, you should remove the Essential Next.js plugin.
See https://ntl.fyi/remove-plugin for instructions.
`)
}
Expand All @@ -104,7 +121,7 @@ See https://ntl.fyi/remove-plugin for instructions.

await makeDir(PUBLISH_DIR)
await nextOnNetlify({
functionsDir: path.resolve(FUNCTIONS_SRC),
functionsDir: path.resolve(INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC),
publishDir: netlifyConfig.build.publish || PUBLISH_DIR,
nextRoot,
})
Expand Down
Loading