-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
module-federation-dev-server.impl.ts
69 lines (59 loc) · 2.02 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
import type { Schema } from './schema';
import { Workspaces } from '@nrwl/devkit';
import { scheduleTarget } from 'nx/src/adapter/ngcli-adapter';
import { BuilderContext, createBuilder } from '@angular-devkit/architect';
import { JsonObject } from '@angular-devkit/core';
import { join } from 'path';
import { webpackServer } from '../webpack-server/webpack-server.impl';
export function moduleFederationDevServer(
schema: Schema,
context: BuilderContext
) {
const workspaces = new Workspaces(context.workspaceRoot);
const workspaceConfig = workspaces.readWorkspaceConfiguration();
const p = workspaceConfig.projects[context.target.project];
const mfConfigPath = join(
context.workspaceRoot,
p.root,
'module-federation.config.js'
);
let mfeConfig: { remotes: string[] };
try {
mfeConfig = require(mfConfigPath);
} catch {
throw new Error(
`Could not load ${mfConfigPath}. Was this project generated with "@nrwl/angular:host"?`
);
}
const { ...options } = schema;
const unparsedRemotes = mfeConfig.remotes.length > 0 ? mfeConfig.remotes : [];
const remotes = unparsedRemotes.map((a) => (Array.isArray(a) ? a[0] : a));
const devServeRemotes = !options.devRemotes
? []
: Array.isArray(options.devRemotes)
? options.devRemotes
: [options.devRemotes];
for (const remote of remotes) {
const isDev = devServeRemotes.includes(remote);
const target = isDev ? 'serve' : 'serve-static';
scheduleTarget(
context.workspaceRoot,
{
project: remote,
target,
configuration: context.target.configuration,
runOptions: {},
executor: context.builder.builderName,
},
options.verbose
).then((obs) => {
obs.toPromise().catch((err) => {
throw new Error(
`Remote '${remote}' failed to serve correctly due to the following: \r\n${err.toString()}`
);
});
});
}
return webpackServer(options, context);
}
export default createBuilder<JsonObject & Schema>(moduleFederationDevServer);