Skip to content

Commit 1c1fef9

Browse files
committed
fix: use default publish dir
1 parent 13d3a7e commit 1c1fef9

File tree

6 files changed

+34
-12
lines changed

6 files changed

+34
-12
lines changed

helpers/checkNxConfig.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { existsSync } = require('fs')
22
const { EOL } = require('os')
33
const path = require('path')
44

5-
const checkNxConfig = ({ netlifyConfig, nextConfig, failBuild, constants: { PUBLISH_DIR } }) => {
5+
const checkNxConfig = ({ netlifyConfig, nextConfig, failBuild, constants: { PUBLISH_DIR = 'out' } }) => {
66
const errors = []
77
if (nextConfig.distDir === '.next') {
88
errors.push(
@@ -15,8 +15,9 @@ const checkNxConfig = ({ netlifyConfig, nextConfig, failBuild, constants: { PUBL
1515
"Please set the 'publish' value in your Netlify build config to a folder inside your app directory. e.g. 'apps/myapp/out'",
1616
)
1717
}
18+
1819
// Look for the config file as a sibling of the publish dir
19-
const expectedConfigFile = path.resolve(netlifyConfig.build.publish, '..', 'next.config.js')
20+
const expectedConfigFile = path.resolve(netlifyConfig.build.publish || PUBLISH_DIR, '..', 'next.config.js')
2021

2122
if (expectedConfigFile !== nextConfig.configFile) {
2223
const confName = path.relative(process.cwd(), nextConfig.configFile)

helpers/getNextRoot.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ const path = require('path')
66
* If there's no next.config.js in the root, we instead look for it as a sibling of the publish dir
77
*/
88
const getNextRoot = ({ netlifyConfig }) => {
9+
console.log('cwd', process.cwd(), 'publish', netlifyConfig.build.publish)
910
let nextRoot = process.cwd()
10-
if (!existsSync(path.join(nextRoot, 'next.config.js')) && netlifyConfig.build.publish) {
11+
if (
12+
!existsSync(path.join(nextRoot, 'next.config.js')) &&
13+
netlifyConfig.build.publish &&
14+
netlifyConfig.build.publish !== nextRoot
15+
) {
1116
nextRoot = path.dirname(netlifyConfig.build.publish)
1217
}
18+
console.log({ nextRoot })
1319
return nextRoot
1420
}
1521

helpers/verifyBuildTarget.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ const verifyBuildTarget = async ({ failBuild, netlifyConfig }) => {
4141

4242
// Creating a config file, because otherwise Next won't reload the config and pick up the new target
4343

44-
if (!configFile) {
44+
if (configFile) {
45+
console.log(`Using ${configFile}`)
46+
} else {
47+
console.log(`Writing config to ${path.resolve(nextRoot, 'next.config.js')}`)
4548
await writeFile(
4649
path.resolve(nextRoot, 'next.config.js'),
4750
`
@@ -52,7 +55,8 @@ module.exports = {
5255
)
5356
}
5457
// Force the new config to be generated
55-
await getNextConfig(failBuild, nextRoot)
58+
const newConf = await getNextConfig(failBuild, nextRoot)
59+
console.log(`Set target to ${newConf.target}`)
5660
// Reset the value in case something else is looking for it
5761
process.env.NOW_BUILDER = false
5862
}

index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { readdirSync } = require('fs')
12
const path = require('path')
23

34
const makeDir = require('make-dir')
@@ -17,6 +18,7 @@ const nextOnNetlify = require('./src')
1718

1819
module.exports = {
1920
async onPreBuild({ netlifyConfig, packageJson, utils, constants }) {
21+
console.log(process.env, { constants }, process.cwd())
2022
const { failBuild } = utils.build
2123

2224
validateNextUsage({ failBuild, netlifyConfig })
@@ -60,24 +62,26 @@ module.exports = {
6062
async onBuild({
6163
netlifyConfig,
6264
packageJson,
63-
constants: { PUBLISH_DIR, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC },
65+
constants: { PUBLISH_DIR = DEFAULT_PUBLISH_DIR, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC },
6466
utils,
6567
}) {
6668
const { failBuild } = utils.build
6769

6870
const nextRoot = getNextRoot({ netlifyConfig })
6971

7072
if (doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })) {
73+
console.log('Does not need plugin')
7174
return
7275
}
7376

74-
console.log(`** Running Next on Netlify package **`)
77+
console.log(`** Running Next on Netlify package **`, netlifyConfig.build.publish)
7578

7679
await makeDir(PUBLISH_DIR)
77-
80+
const { target, distDir } = await getNextConfig(utils.failBuild, nextRoot)
81+
console.log(`Building with target ${target} distDir ${distDir}`)
7882
await nextOnNetlify({
7983
functionsDir: path.resolve(FUNCTIONS_SRC),
80-
publishDir: netlifyConfig.build.publish,
84+
publishDir: netlifyConfig.build.publish || PUBLISH_DIR,
8185
nextRoot,
8286
})
8387
},
@@ -89,10 +93,12 @@ module.exports = {
8993
const nextRoot = getNextRoot({ netlifyConfig })
9094

9195
const nextConfig = await getNextConfig(utils.failBuild, nextRoot)
96+
console.log(`Finished building with target ${nextConfig.target}`)
9297
await saveCache({ cache: utils.cache, distDir: nextConfig.distDir })
9398
copyUnstableIncludedDirs({ nextConfig, functionsDist: path.resolve(FUNCTIONS_DIST) })
9499
},
95100
}
96101

97102
const DEFAULT_FUNCTIONS_SRC = 'netlify/functions'
98103
const DEFAULT_FUNCTIONS_DIST = '.netlify/functions/'
104+
const DEFAULT_PUBLISH_DIR = 'out'

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const build = async (functionsPath, publishPath, nextRoot) => {
2323
// If we're in a monorepo we need to move into the Next site root
2424
const oldCwd = process.cwd()
2525
if (nextRoot !== oldCwd) {
26+
console.log(`Changing dir from ${oldCwd} to ${nextRoot}`)
2627
process.chdir(nextRoot)
2728
}
2829
copyPublicFiles(publishPath)
@@ -79,7 +80,7 @@ const nextOnNetlify = async (options = {}) => {
7980
const functionsPath = normalize(options.functionsDir || NETLIFY_FUNCTIONS_PATH)
8081
const publishPath = normalize(options.publishDir || NETLIFY_PUBLISH_PATH)
8182
const nextRoot = normalize(options.nextRoot || process.cwd())
82-
83+
console.log('nextRoot', nextRoot)
8384
if (options.watch) {
8485
watch(functionsPath, publishPath, nextRoot)
8586
} else {

src/lib/steps/copyNextAssets.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const { join } = require('path')
1+
const { readdirSync } = require('fs')
2+
const { join, resolve } = require('path')
23

34
const { copySync, existsSync } = require('fs-extra')
45

@@ -15,8 +16,11 @@ const copyNextAssets = async (publishPath) => {
1516
const nextDistDir = await getNextDistDir()
1617
const staticAssetsPath = join(nextDistDir, 'static')
1718
if (!existsSync(staticAssetsPath)) {
19+
console.log(nextDistDir, readdirSync(nextDistDir))
1820
throw new Error(
19-
`No static assets found in your distDir (missing /static in ${nextDistDir}). Please check your project configuration. Your next.config.js must be one of \`serverless\` or \`experimental-serverless-trace\`. Your build command should include \`next build\`.`,
21+
`No static assets found in your distDir (missing /static in ${resolve(
22+
nextDistDir,
23+
)}). Please check your project configuration. Your next.config.js must be one of \`serverless\` or \`experimental-serverless-trace\`. Your build command should include \`next build\`.`,
2024
)
2125
}
2226
logTitle('💼 Copying static NextJS assets to', publishPath)

0 commit comments

Comments
 (0)