-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
module-federation-dev-server.impl.ts
74 lines (66 loc) · 1.95 KB
/
module-federation-dev-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
import { ExecutorContext, logger, runExecutor } from '@nrwl/devkit';
import devServerExecutor, {
WebDevServerOptions,
} from '@nrwl/web/src/executors/dev-server/dev-server.impl';
import { join } from 'path';
import {
combineAsyncIterators,
tapAsyncIterator,
} from '../../utils/async-iterator';
type ModuleFederationDevServerOptions = WebDevServerOptions & {
devRemotes?: string | string[];
};
export default async function* moduleFederationDevServer(
options: ModuleFederationDevServerOptions,
context: ExecutorContext
) {
let iter = devServerExecutor(options, context);
const p = context.workspace.projects[context.projectName];
const moduleFederationConfigPath = join(
context.root,
p.root,
'module-federation.config.js'
);
let moduleFederationConfig: any;
try {
moduleFederationConfig = require(moduleFederationConfigPath);
} catch {
// TODO(jack): Add a link to guide
throw new Error(
`Could not load ${moduleFederationConfigPath}. Was this project generated with "@nrwl/react:host"?`
);
}
const knownRemotes = moduleFederationConfig.remotes ?? [];
const devServeApps = !options.devRemotes
? []
: Array.isArray(options.devRemotes)
? options.devRemotes
: [options.devRemotes];
for (const app of knownRemotes) {
const [appName] = Array.isArray(app) ? app : [app];
const isDev = devServeApps.includes(appName);
iter = combineAsyncIterators(
iter,
await runExecutor(
{
project: appName,
target: isDev ? 'serve' : 'serve-static',
configuration: context.configurationName,
},
{
watch: isDev,
},
context
)
);
}
let numAwaiting = knownRemotes.length + 1; // remotes + host
return yield* tapAsyncIterator(iter, (x) => {
numAwaiting--;
if (numAwaiting === 0) {
logger.info(
`Host is ready: ${options.host ?? 'localhost'}:${options.port ?? 4200}`
);
}
});
}