Skip to content

Commit a1eb7c9

Browse files
committed
Better error on next build failure
1 parent bae9ce9 commit a1eb7c9

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

src/frameworks/next.js/index.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,53 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import { readFile, mkdir, copyFile, stat, readdir } from 'fs/promises';
16-
import { basename, dirname, extname, join } from 'path';
15+
import { readFile, mkdir, copyFile, stat } from 'fs/promises';
16+
import { dirname, extname, join } from 'path';
1717
import type { Header, Rewrite, Redirect } from 'next/dist/lib/load-custom-routes.js';
1818
import type { NextConfig } from 'next';
1919
import { copy } from 'fs-extra';
2020
import { pathToFileURL } from 'url';
2121

2222
import { Commands, DeployConfig, PathFactory, spawn } from '../../utils.js';
23+
import { existsSync } from 'fs';
2324

2425
export const build = async (config: DeployConfig | Required<DeployConfig>, getProjectPath: PathFactory) => {
2526

2627
const { default: { default: nextBuild } } = await import('next/dist/build/index.js');
2728

28-
let nextConfig: NextConfig;
29-
try {
30-
const { default: { default: loadConfig } } = await import('next/dist/server/config.js');
31-
const { PHASE_PRODUCTION_BUILD } = await import('next/constants.js');
32-
nextConfig = await loadConfig(PHASE_PRODUCTION_BUILD, getProjectPath(), null);
33-
} catch(e) {
34-
// Must be Next 11, just import it
35-
nextConfig = await import(pathToFileURL(getProjectPath('next.config.js')).toString());
36-
}
29+
let nextConfig: NextConfig|undefined = undefined;
30+
31+
if (existsSync(getProjectPath('next.config.js'))) {
32+
33+
try {
34+
const { default: { default: loadConfig } } = await import('next/dist/server/config.js');
35+
const { PHASE_PRODUCTION_BUILD } = await import('next/constants.js');
36+
nextConfig = await loadConfig(PHASE_PRODUCTION_BUILD, getProjectPath(), null);
37+
} catch(e) { }
38+
39+
// Try just importing it, incase of Next 11
40+
try {
41+
nextConfig = await import(pathToFileURL(getProjectPath('next.config.js')).toString());
42+
} catch(e) { }
3743

44+
if (!nextConfig) throw new Error('Unable to load next.config.js.');
45+
46+
} else {
47+
48+
nextConfig = {};
49+
50+
}
3851

3952
// SEMVER these defaults are only needed for Next 11
4053
const { distDir='.next', basePath='' } = nextConfig;
4154

4255
const deployPath = (...args: string[]) => config.dist ? join(config.dist, ...args) : getProjectPath('.deploy', ...args);
4356
const getHostingPath = (...args: string[]) => deployPath('hosting', ...basePath.split('/'), ...args);
4457

45-
await nextBuild(getProjectPath(), null, false, false, true);
58+
await nextBuild(getProjectPath(), null, false, false, true).catch(e => {
59+
console.error(e.message);
60+
throw e;
61+
});
4662

4763
try {
4864
// Using spawn here, rather than their programatic API because I can't silence it

0 commit comments

Comments
 (0)