/
module-federation-dev-ssr.impl.ts
141 lines (128 loc) · 4.12 KB
/
module-federation-dev-ssr.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import type { Schema } from './schema';
import type { BuilderContext } from '@angular-devkit/architect';
import { createBuilder } from '@angular-devkit/architect';
import type { JsonObject } from '@angular-devkit/core';
import { executeSSRDevServerBuilder } from '@nguniversal/builders';
import { readProjectsConfigurationFromProjectGraph } from 'nx/src/project-graph/project-graph';
import {
readCachedProjectGraph,
workspaceRoot,
Workspaces,
} from '@nrwl/devkit';
import {
getDynamicRemotes,
getStaticRemotes,
validateDevRemotes,
} from '../utilities/module-federation';
import { switchMap, tap } from 'rxjs/operators';
import { from } from 'rxjs';
import { join } from 'path';
import { execSync, fork } from 'child_process';
import { scheduleTarget } from 'nx/src/adapter/ngcli-adapter';
export function executeModuleFederationDevSSRBuilder(
schema: Schema,
context: BuilderContext
) {
const { ...options } = schema;
const projectGraph = readCachedProjectGraph();
const { projects: workspaceProjects } =
readProjectsConfigurationFromProjectGraph(projectGraph);
const ws = new Workspaces(workspaceRoot);
const project = workspaceProjects[context.target.project];
validateDevRemotes(options, workspaceProjects);
const remotesToSkip = new Set(options.skipRemotes ?? []);
const staticRemotes = getStaticRemotes(
project,
context,
workspaceProjects,
remotesToSkip
);
const dynamicRemotes = getDynamicRemotes(
project,
context,
workspaceProjects,
remotesToSkip
);
const remotes = [...staticRemotes, ...dynamicRemotes];
const devServeRemotes = !options.devRemotes
? []
: Array.isArray(options.devRemotes)
? options.devRemotes
: [options.devRemotes];
const remoteProcessPromises = [];
for (const remote of remotes) {
const isDev = devServeRemotes.includes(remote);
const target = isDev ? 'serve-ssr' : 'static-server';
if (!workspaceProjects[remote].targets?.[target]) {
throw new Error(
`Could not find "${target}" target in "${remote}" project.`
);
} else if (!workspaceProjects[remote].targets?.[target].executor) {
throw new Error(
`Could not find executor for "${target}" target in "${remote}" project.`
);
}
const runOptions: { verbose?: boolean } = {};
if (options.verbose) {
const [collection, executor] =
workspaceProjects[remote].targets[target].executor.split(':');
const { schema } = ws.readExecutor(collection, executor);
if (schema.additionalProperties || 'verbose' in schema.properties) {
runOptions.verbose = options.verbose;
}
}
const remotePromise = new Promise<void>((res, rej) => {
if (target === 'static-server') {
const remoteProject = workspaceProjects[remote];
const remoteServerOutput = join(
workspaceRoot,
remoteProject.targets.server.options.outputPath,
'main.js'
);
execSync(
`npx nx run ${remote}:server${
context.target.configuration
? `:${context.target.configuration}`
: ''
}`,
{ stdio: 'inherit' }
);
const child = fork(remoteServerOutput, {
env: remoteProject.targets['serve-ssr'].options.port,
});
child.on('message', (msg) => {
if (msg === 'nx.server.ready') {
res();
}
});
}
if (target === 'serve-ssr') {
scheduleTarget(
context.workspaceRoot,
{
project: remote,
target,
configuration: context.target.configuration,
runOptions,
},
options.verbose
).then((obs) =>
obs
.pipe(
tap((result) => {
result.success && res();
})
)
.toPromise()
);
}
});
remoteProcessPromises.push(remotePromise);
}
return from(Promise.all(remoteProcessPromises)).pipe(
switchMap(() => executeSSRDevServerBuilder(options, context))
);
}
export default createBuilder<JsonObject & Schema>(
executeModuleFederationDevSSRBuilder
);