From dadc7fa2ad8d7df4c95af8fe8d6e4ffb6324f87a Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Tue, 29 Jun 2021 11:03:33 +0100 Subject: [PATCH] fix: use default publish dir --- helpers/checkNxConfig.js | 5 +++-- helpers/getNextRoot.js | 6 +++++- index.js | 7 ++++--- src/lib/steps/copyNextAssets.js | 6 ++++-- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/helpers/checkNxConfig.js b/helpers/checkNxConfig.js index e68f0376fa..e18981af34 100644 --- a/helpers/checkNxConfig.js +++ b/helpers/checkNxConfig.js @@ -2,7 +2,7 @@ const { existsSync } = require('fs') const { EOL } = require('os') const path = require('path') -const checkNxConfig = ({ netlifyConfig, nextConfig, failBuild, constants: { PUBLISH_DIR } }) => { +const checkNxConfig = ({ netlifyConfig, nextConfig, failBuild, constants: { PUBLISH_DIR = 'out' } }) => { const errors = [] if (nextConfig.distDir === '.next') { errors.push( @@ -15,8 +15,9 @@ const checkNxConfig = ({ netlifyConfig, nextConfig, failBuild, constants: { PUBL "Please set the 'publish' value in your Netlify build config to a folder inside your app directory. e.g. 'apps/myapp/out'", ) } + // Look for the config file as a sibling of the publish dir - const expectedConfigFile = path.resolve(netlifyConfig.build.publish, '..', 'next.config.js') + const expectedConfigFile = path.resolve(netlifyConfig.build.publish || PUBLISH_DIR, '..', 'next.config.js') if (expectedConfigFile !== nextConfig.configFile) { const confName = path.relative(process.cwd(), nextConfig.configFile) diff --git a/helpers/getNextRoot.js b/helpers/getNextRoot.js index 181d422495..459140ac00 100644 --- a/helpers/getNextRoot.js +++ b/helpers/getNextRoot.js @@ -7,7 +7,11 @@ const path = require('path') */ const getNextRoot = ({ netlifyConfig }) => { let nextRoot = process.cwd() - if (!existsSync(path.join(nextRoot, 'next.config.js')) && netlifyConfig.build.publish) { + if ( + !existsSync(path.join(nextRoot, 'next.config.js')) && + netlifyConfig.build.publish && + netlifyConfig.build.publish !== nextRoot + ) { nextRoot = path.dirname(netlifyConfig.build.publish) } return nextRoot diff --git a/index.js b/index.js index ed9ba0e78a..d41210bc5f 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ +const { readdirSync } = require('fs') const path = require('path') const makeDir = require('make-dir') @@ -60,7 +61,7 @@ module.exports = { async onBuild({ netlifyConfig, packageJson, - constants: { PUBLISH_DIR, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC }, + constants: { PUBLISH_DIR = DEFAULT_PUBLISH_DIR, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC }, utils, }) { const { failBuild } = utils.build @@ -74,10 +75,9 @@ module.exports = { console.log(`** Running Next on Netlify package **`) await makeDir(PUBLISH_DIR) - await nextOnNetlify({ functionsDir: path.resolve(FUNCTIONS_SRC), - publishDir: netlifyConfig.build.publish, + publishDir: netlifyConfig.build.publish || PUBLISH_DIR, nextRoot, }) }, @@ -96,3 +96,4 @@ module.exports = { const DEFAULT_FUNCTIONS_SRC = 'netlify/functions' const DEFAULT_FUNCTIONS_DIST = '.netlify/functions/' +const DEFAULT_PUBLISH_DIR = 'out' diff --git a/src/lib/steps/copyNextAssets.js b/src/lib/steps/copyNextAssets.js index c3794572ea..1e1d240a55 100644 --- a/src/lib/steps/copyNextAssets.js +++ b/src/lib/steps/copyNextAssets.js @@ -1,4 +1,4 @@ -const { join } = require('path') +const { join, resolve } = require('path') const { copySync, existsSync } = require('fs-extra') @@ -16,7 +16,9 @@ const copyNextAssets = async (publishPath) => { const staticAssetsPath = join(nextDistDir, 'static') if (!existsSync(staticAssetsPath)) { throw new Error( - `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\`.`, + `No static assets found in your distDir (missing /static in ${resolve( + 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\`.`, ) } logTitle('💼 Copying static NextJS assets to', publishPath)