Skip to content

Make next a peer dependency #45

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 1 commit into from
Nov 17, 2020
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
22 changes: 22 additions & 0 deletions helpers/validateNextUsage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Ensure Next.js is available.
// We use `peerDependencies` instead of `dependencies` so that users can choose
// the Next.js version. However, this requires them to install "next" in their
// site.
const validateNextUsage = function (failBuild) {
if (!hasPackage('next')) {
return failBuild(
'This site does not seem to be using Next.js. Please run "npm install next" or "yarn next" in the repository.',
)
}
}

const hasPackage = function (packageName) {
try {
require(packageName)
return true
} catch (error) {
return false
}
}

module.exports = validateNextUsage
16 changes: 13 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ const fs = require('fs')
const path = require('path')
const util = require('util')

const nextOnNetlify = require('next-on-netlify')
const { PHASE_PRODUCTION_BUILD } = require('next/constants')
const { default: loadConfig } = require('next/dist/next-server/server/config')
const findUp = require('find-up')
const makeDir = require('make-dir')
const { copy } = require('cpx')

const isStaticExportProject = require('./helpers/isStaticExportProject')
const validateNextUsage = require('./helpers/validateNextUsage')

const pWriteFile = util.promisify(fs.writeFile)
const pCopy = util.promisify(copy)
Expand All @@ -22,6 +20,8 @@ module.exports = {
async onPreBuild({ netlifyConfig, packageJson, utils }) {
const { failBuild } = utils.build

validateNextUsage(failBuild)

if (Object.keys(packageJson).length === 0) {
return failBuild(`Could not find a package.json for this project`)
}
Expand All @@ -46,6 +46,11 @@ module.exports = {

const nextConfigPath = await findUp('next.config.js')
if (nextConfigPath !== undefined) {
// We cannot load `next` at the top-level because we validate whether the
// site is using `next` inside `onPreBuild`.

Choose a reason for hiding this comment

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

🖖

const { PHASE_PRODUCTION_BUILD } = require('next/constants')
const { default: loadConfig } = require('next/dist/next-server/server/config')

// If the next config exists, fail build if target isnt in acceptableTargets
const acceptableTargets = ['serverless', 'experimental-serverless-trace']
const nextConfig = loadConfig(PHASE_PRODUCTION_BUILD, path.resolve('.'))
Expand All @@ -66,6 +71,11 @@ module.exports = {
},
async onBuild({ constants: { PUBLISH_DIR, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC } }) {
console.log(`** Running Next on Netlify package **`)

// We cannot load `next-on-netlify` (which depends on `next`) at the
// top-level because we validate whether the site is using `next`
// inside `onPreBuild`.
const nextOnNetlify = require('next-on-netlify')
nextOnNetlify()

// Next-on-netlify puts its files into out_functions and out_publish
Expand Down
Loading