|
12 | 12 | // See the License for the specific language governing permissions and
|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
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'; |
17 | 17 | import type { Header, Rewrite, Redirect } from 'next/dist/lib/load-custom-routes.js';
|
18 | 18 | import type { NextConfig } from 'next';
|
19 | 19 | import { copy } from 'fs-extra';
|
20 | 20 | import { pathToFileURL } from 'url';
|
21 | 21 |
|
22 | 22 | import { Commands, DeployConfig, PathFactory, spawn } from '../../utils.js';
|
| 23 | +import { existsSync } from 'fs'; |
23 | 24 |
|
24 | 25 | export const build = async (config: DeployConfig | Required<DeployConfig>, getProjectPath: PathFactory) => {
|
25 | 26 |
|
26 | 27 | const { default: { default: nextBuild } } = await import('next/dist/build/index.js');
|
27 | 28 |
|
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) { } |
37 | 43 |
|
| 44 | + if (!nextConfig) throw new Error('Unable to load next.config.js.'); |
| 45 | + |
| 46 | + } else { |
| 47 | + |
| 48 | + nextConfig = {}; |
| 49 | + |
| 50 | + } |
38 | 51 |
|
39 | 52 | // SEMVER these defaults are only needed for Next 11
|
40 | 53 | const { distDir='.next', basePath='' } = nextConfig;
|
41 | 54 |
|
42 | 55 | const deployPath = (...args: string[]) => config.dist ? join(config.dist, ...args) : getProjectPath('.deploy', ...args);
|
43 | 56 | const getHostingPath = (...args: string[]) => deployPath('hosting', ...basePath.split('/'), ...args);
|
44 | 57 |
|
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 | + }); |
46 | 62 |
|
47 | 63 | try {
|
48 | 64 | // Using spawn here, rather than their programatic API because I can't silence it
|
|
0 commit comments