-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
server.impl.ts
123 lines (109 loc) · 3.33 KB
/
server.impl.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import 'dotenv/config';
import {
ExecutorContext,
logger,
parseTargetString,
readTargetOptions,
} from '@nrwl/devkit';
import {
PHASE_DEVELOPMENT_SERVER,
PHASE_PRODUCTION_SERVER,
} from 'next/dist/next-server/lib/constants';
import * as chalk from 'chalk';
import { existsSync } from 'fs';
import { join, resolve } from 'path';
import { prepareConfig } from '../../utils/config';
import {
NextBuildBuilderOptions,
NextServeBuilderOptions,
NextServer,
NextServerOptions,
ProxyConfig,
} from '../../utils/types';
import { customServer } from './lib/custom-server';
import { defaultServer } from './lib/default-server';
import { readCachedProjectGraph } from '@nrwl/workspace/src/core/project-graph';
import {
calculateProjectDependencies,
DependentBuildableProjectNode,
} from '@nrwl/workspace/src/utilities/buildable-libs-utils';
import { assertDependentProjectsHaveBeenBuilt } from '../../utils/buildable-libs';
const infoPrefix = `[ ${chalk.dim(chalk.cyan('info'))} ] `;
const readyPrefix = `[ ${chalk.green('ready')} ]`;
export default async function* serveExecutor(
options: NextServeBuilderOptions,
context: ExecutorContext
) {
process.env.NODE_ENV = process.env.NODE_ENV
? process.env.NODE_ENV
: options.dev
? 'development'
: 'production';
let dependencies: DependentBuildableProjectNode[] = [];
const buildTarget = parseTargetString(options.buildTarget);
const baseUrl = `http://${options.hostname || 'localhost'}:${options.port}`;
const buildOptions = readTargetOptions<NextBuildBuilderOptions>(
buildTarget,
context
);
const root = resolve(context.root, buildOptions.root);
if (!options.buildLibsFromSource) {
const result = calculateProjectDependencies(
readCachedProjectGraph(),
context.root,
context.projectName,
'build', // should be generalized
context.configurationName
);
dependencies = result.dependencies;
assertDependentProjectsHaveBeenBuilt(dependencies, context);
}
const config = await prepareConfig(
options.dev ? PHASE_DEVELOPMENT_SERVER : PHASE_PRODUCTION_SERVER,
buildOptions,
context,
dependencies
);
const settings: NextServerOptions = {
dev: options.dev,
dir: root,
staticMarkup: options.staticMarkup,
quiet: options.quiet,
conf: config,
port: options.port,
path: options.customServerPath,
hostname: options.hostname,
};
const server: NextServer = options.customServerPath
? customServer
: defaultServer;
// look for the proxy.conf.json
let proxyConfig: ProxyConfig;
const proxyConfigPath = options.proxyConfig
? join(context.root, options.proxyConfig)
: join(root, 'proxy.conf.json');
if (existsSync(proxyConfigPath)) {
logger.info(
`${infoPrefix} found proxy configuration at ${proxyConfigPath}`
);
proxyConfig = require(proxyConfigPath);
}
try {
await server(settings, proxyConfig);
logger.info(`${readyPrefix} on ${baseUrl}`);
yield {
baseUrl,
success: true,
};
// This Promise intentionally never resolves, leaving the process running
await new Promise<{ success: boolean }>(() => {});
} catch (e) {
if (options.dev) {
throw e;
} else {
throw new Error(
`Could not start production server. Try building your app with \`nx build ${context.projectName}\`.`
);
}
}
}