Skip to content

Commit 4beff95

Browse files
committed
Step 2 - Add config params for dependencies: JSON path or array; implement function to unify build and serve configurations.
1 parent 62c04cb commit 4beff95

File tree

12 files changed

+271
-5
lines changed

12 files changed

+271
-5
lines changed

apps/host-application/project.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
"executor": "./dist/libs/nx-angular-mf:build",
1313
"outputs": ["{options.outputPath}"],
1414
"options": {
15+
"mf": {
16+
"externalList": "build-external-list.json",
17+
"skipList": "build-skip-list.json"
18+
},
1519
"outputPath": "dist/apps/host-application",
1620
"index": "apps/host-application/src/index.html",
1721
"browser": "apps/host-application/src/main.ts",

build-external-list.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[
2+
"@angular/animations",
3+
"@angular/animations/browser",
4+
"@angular/common",
5+
"@angular/common/http",
6+
"@angular/compiler",
7+
"@angular/core",
8+
"@angular/core/event-dispatch-contract.min.js",
9+
"@angular/core/primitives/event-dispatch",
10+
"@angular/core/primitives/signals",
11+
"@angular/core/rxjs-interop",
12+
"@angular/forms",
13+
"@angular/platform-browser",
14+
"@angular/platform-browser/animations",
15+
"@angular/platform-browser/animations/async",
16+
"@angular/platform-browser-dynamic",
17+
"@angular/platform-server",
18+
"@angular/router",
19+
"@angular/upgrade/static",
20+
"@angular/cdk",
21+
"@angular/ssr",
22+
"@klerick/native-federation-ssr/loadModule",
23+
"rxjs",
24+
"rxjs/fetch",
25+
"rxjs/operators"
26+
]

build-skip-list.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[
2+
"@angular/compiler-cli",
3+
"@angular-devkit/core",
4+
"@angular/cdk/testing",
5+
"@angular/cdk/testing/selenium-webdriver",
6+
"@angular/cdk/testing/testbed",
7+
"@angular/cdk/schematics",
8+
"@angular/cdk/text-field",
9+
"@schematics/angular",
10+
"@angular/platform-server/testing",
11+
"@angular/platform-browser-dynamic/testing",
12+
"@angular/platform-browser/testing",
13+
"@angular/router/testing",
14+
"@angular/animations/browser/testing",
15+
"@angular/common/http/testing",
16+
"@angular/common/testing",
17+
"@angular/core/testing",
18+
"@angular/ssr/node",
19+
"rxjs/testing",
20+
"@klerick/native-federation-ssr",
21+
"@angular/platform-server/init"
22+
]

libs/nx-angular-mf/src/builders/build/schema.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,38 @@
648648
},
649649
"mfe": {
650650
"type": "object",
651+
"properties": {
652+
"externalList": {
653+
"description": "List of package imports that should use as external for building the MFE.",
654+
"oneOf": [
655+
{
656+
"type": "array",
657+
"items": {
658+
"type": "string"
659+
}
660+
},
661+
{
662+
"description": "pat to file json",
663+
"type": "string"
664+
}
665+
]
666+
},
667+
"skipList": {
668+
"description": "List of package imports that should not use as external for building the MFE.",
669+
"oneOf": [
670+
{
671+
"type": "array",
672+
"items": {
673+
"type": "string"
674+
}
675+
},
676+
{
677+
"description": "pat to file json",
678+
"type": "string"
679+
}
680+
]
681+
}
682+
},
651683
"additionalProperties": false
652684
}
653685
},
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './utils';
2+
export * from './prepare-config'
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { BuilderContext } from '@angular-devkit/architect';
2+
import { join } from 'path';
3+
import fsPromise from 'fs/promises';
4+
5+
import { SchemaMf } from '../schema';
6+
import { BuildExecutorSchema } from '../build/schema';
7+
import { ConfigMf } from '../types';
8+
import { workspaceRootPath } from './utils';
9+
10+
export async function prepareConfig(
11+
defaultOptions: SchemaMf['mf'],
12+
buildOptions: BuildExecutorSchema,
13+
context: BuilderContext
14+
): Promise<ConfigMf> {
15+
const skipList: ConfigMf['skipList'] = [];
16+
const externalList: ConfigMf['externalList'] = [];
17+
18+
if (defaultOptions.skipList) {
19+
skipList.push(...(await checkIsFileOrArray(defaultOptions.skipList)));
20+
}
21+
22+
if (defaultOptions.externalList) {
23+
externalList.push(...(await checkIsFileOrArray(defaultOptions.externalList)));
24+
}
25+
26+
return {
27+
skipList: skipList,
28+
externalList: externalList,
29+
};
30+
}
31+
32+
async function checkIsFileOrArray(
33+
skipList: string[] | string
34+
): Promise<string[]> {
35+
36+
if (Array.isArray(skipList)) {
37+
return skipList;
38+
} else {
39+
try {
40+
const listListPath = join(workspaceRootPath, skipList);
41+
42+
const result = await fsPromise.lstat(listListPath);
43+
44+
if (result.isFile()) {
45+
const skipListFromFile = JSON.parse(
46+
await fsPromise.readFile(listListPath, 'utf8')
47+
) as string[];
48+
49+
return Array.isArray(skipListFromFile)
50+
? skipListFromFile
51+
: [skipListFromFile];
52+
}
53+
} catch (err) {
54+
throw new Error(`Invalid path: ${skipList}`);
55+
}
56+
}
57+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { getSystemPath, normalize } from '@angular-devkit/core';
2+
import { workspaceRoot } from 'nx/src/utils/workspace-root';
3+
4+
export function deepMergeObject(targetObject = {}, sourceObject = {}) {
5+
const copyTargetObject = JSON.parse(JSON.stringify(targetObject));
6+
const copySourceObject = JSON.parse(JSON.stringify(sourceObject));
7+
8+
Object.keys(copySourceObject).forEach((key) => {
9+
if (
10+
typeof copySourceObject[key] === 'object' &&
11+
!Array.isArray(copySourceObject[key])
12+
) {
13+
copyTargetObject[key] = deepMergeObject(
14+
copyTargetObject[key],
15+
copySourceObject[key]
16+
);
17+
} else if (
18+
Array.isArray(copyTargetObject[key]) &&
19+
Array.isArray(copySourceObject[key])
20+
) {
21+
copyTargetObject[key] = [
22+
...copyTargetObject[key],
23+
...copySourceObject[key],
24+
];
25+
} else {
26+
copyTargetObject[key] = copySourceObject[key];
27+
}
28+
});
29+
30+
return copyTargetObject;
31+
}
32+
33+
export const workspaceRootPath = getSystemPath(normalize(workspaceRoot));
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
export type SchemaMf = {
2-
mf?: Record<string, unknown>;
2+
mf?: {
3+
skipList?: string | string[];
4+
externalList?: string | string[];
5+
};
36
};

libs/nx-angular-mf/src/builders/schema/schema.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,38 @@
44
"properties": {
55
"mfe": {
66
"type": "object",
7+
"properties": {
8+
"externalList": {
9+
"description": "List of package imports that should use as external for building the MFE.",
10+
"oneOf": [
11+
{
12+
"type": "array",
13+
"items": {
14+
"type": "string"
15+
}
16+
},
17+
{
18+
"description": "pat to file json",
19+
"type": "string"
20+
}
21+
]
22+
},
23+
"skipList": {
24+
"description": "List of package imports that should not use as external for building the MFE.",
25+
"oneOf": [
26+
{
27+
"type": "array",
28+
"items": {
29+
"type": "string"
30+
}
31+
},
32+
{
33+
"description": "pat to file json",
34+
"type": "string"
35+
}
36+
]
37+
}
38+
},
739
"additionalProperties": false
840
}
941
}

libs/nx-angular-mf/src/builders/serve/index.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
import { BuilderContext, createBuilder } from '@angular-devkit/architect';
1+
import {
2+
BuilderContext,
3+
createBuilder,
4+
targetFromTargetString,
5+
} from '@angular-devkit/architect';
26
import { normalizeOptions } from '@angular-devkit/build-angular/src/builders/dev-server/options';
37
import {
48
serveWithVite,
59
buildApplicationInternal,
610
} from '@angular/build/private';
711

8-
912
import { ServeExecutorSchema } from './schema';
13+
import { BuildExecutorSchema } from '../build/schema';
14+
import { deepMergeObject, prepareConfig } from '../helpers';
1015

1116

1217
function getBuilderAction() {
1318
return async function* (options, context, extensions) {
14-
1519
for await (const result of buildApplicationInternal(
1620
options,
1721
context,
@@ -28,6 +32,21 @@ export async function* runBuilder(
2832
) {
2933
context.logger.info('Run serve mf');
3034

35+
const buildTarget = targetFromTargetString(options.buildTarget);
36+
const targetOptions = (await context.getTargetOptions(
37+
buildTarget
38+
)) as unknown as BuildExecutorSchema;
39+
40+
const resultMfeOptions = deepMergeObject(
41+
targetOptions['mf'] || {},
42+
options.mf || {}
43+
);
44+
45+
const optionsMfe = await prepareConfig(
46+
resultMfeOptions,
47+
targetOptions,
48+
context
49+
);
3150

3251
const normalizeOuterOptions = await normalizeOptions(
3352
context,
@@ -42,7 +61,7 @@ export async function* runBuilder(
4261

4362
const transforms = {
4463
indexHtml: async (input: string) => {
45-
return input
64+
return input;
4665
},
4766
};
4867

0 commit comments

Comments
 (0)