@@ -2,14 +2,12 @@ const fs = require('fs')
2
2
const path = require ( 'path' )
3
3
const util = require ( 'util' )
4
4
5
- const nextOnNetlify = require ( 'next-on-netlify' )
6
- const { PHASE_PRODUCTION_BUILD } = require ( 'next/constants' )
7
- const { default : loadConfig } = require ( 'next/dist/next-server/server/config' )
8
5
const findUp = require ( 'find-up' )
9
6
const makeDir = require ( 'make-dir' )
10
7
const { copy } = require ( 'cpx' )
11
8
12
9
const isStaticExportProject = require ( './helpers/isStaticExportProject' )
10
+ const validateNextUsage = require ( './helpers/validateNextUsage' )
13
11
14
12
const pWriteFile = util . promisify ( fs . writeFile )
15
13
const pCopy = util . promisify ( copy )
@@ -22,6 +20,8 @@ module.exports = {
22
20
async onPreBuild ( { netlifyConfig, packageJson, utils } ) {
23
21
const { failBuild } = utils . build
24
22
23
+ validateNextUsage ( failBuild )
24
+
25
25
if ( Object . keys ( packageJson ) . length === 0 ) {
26
26
return failBuild ( `Could not find a package.json for this project` )
27
27
}
@@ -46,6 +46,11 @@ module.exports = {
46
46
47
47
const nextConfigPath = await findUp ( 'next.config.js' )
48
48
if ( nextConfigPath !== undefined ) {
49
+ // We cannot load `next` at the top-level because we validate whether the
50
+ // site is using `next` inside `onPreBuild`.
51
+ const { PHASE_PRODUCTION_BUILD } = require ( 'next/constants' )
52
+ const { default : loadConfig } = require ( 'next/dist/next-server/server/config' )
53
+
49
54
// If the next config exists, fail build if target isnt in acceptableTargets
50
55
const acceptableTargets = [ 'serverless' , 'experimental-serverless-trace' ]
51
56
const nextConfig = loadConfig ( PHASE_PRODUCTION_BUILD , path . resolve ( '.' ) )
@@ -66,6 +71,11 @@ module.exports = {
66
71
} ,
67
72
async onBuild ( { constants : { PUBLISH_DIR , FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC } } ) {
68
73
console . log ( `** Running Next on Netlify package **` )
74
+
75
+ // We cannot load `next-on-netlify` (which depends on `next`) at the
76
+ // top-level because we validate whether the site is using `next`
77
+ // inside `onPreBuild`.
78
+ const nextOnNetlify = require ( 'next-on-netlify' )
69
79
nextOnNetlify ( )
70
80
71
81
// Next-on-netlify puts its files into out_functions and out_publish
0 commit comments