Skip to content

Commit

Permalink
feat(angular): support auto-serving known dynamic remotes (#10475)
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed May 27, 2022
1 parent 759a28d commit 0c241ee
Showing 1 changed file with 55 additions and 2 deletions.
@@ -1,10 +1,59 @@
import type { Schema } from './schema';
import { Workspaces } from '@nrwl/devkit';
import { ProjectConfiguration, 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 { executeWebpackServerBuilder } from '../webpack-server/webpack-server.impl';
import { existsSync, readFileSync } from 'fs';

function getDynamicRemotesIfExists(
context: BuilderContext,
p: ProjectConfiguration
) {
// check for dynamic remotes
// we should only check for dynamic based on what we generate
// and fallback to empty array

const standardPathToGeneratedMFManifestJson = join(
context.workspaceRoot,
p.sourceRoot,
'assets/module-federation.manifest.json'
);
if (!existsSync(standardPathToGeneratedMFManifestJson)) {
return [];
}

const moduleFederationManifestJson = readFileSync(
standardPathToGeneratedMFManifestJson,
'utf-8'
);

if (moduleFederationManifestJson) {
// This should have shape of
// {
// "remoteName": "remoteLocation",
// }
const parsedManifest = JSON.parse(moduleFederationManifestJson);
if (
!Object.keys(parsedManifest).every(
(key) =>
typeof key === 'string' && typeof parsedManifest[key] === 'string'
)
) {
return [];
}

return Object.entries(parsedManifest).reduce(
(remotesArray, [remoteName, remoteLocation]) => [
...remotesArray,
[remoteName, remoteLocation],
],
[]
);
}
return [];
}

export function executeModuleFederationDevServerBuilder(
schema: Schema,
Expand All @@ -31,7 +80,11 @@ export function executeModuleFederationDevServerBuilder(
}

const { ...options } = schema;
const unparsedRemotes = mfeConfig.remotes.length > 0 ? mfeConfig.remotes : [];

let unparsedRemotes = mfeConfig.remotes.length > 0 ? mfeConfig.remotes : [];
const dynamicRemotes = getDynamicRemotesIfExists(context, p);
unparsedRemotes = [...unparsedRemotes, ...dynamicRemotes];

const remotes = unparsedRemotes.map((a) => (Array.isArray(a) ? a[0] : a));

const devServeRemotes = !options.devRemotes
Expand Down

0 comments on commit 0c241ee

Please sign in to comment.