@nx/module-federation
Version:
75 lines (74 loc) • 4.24 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFunctionDeterminateRemoteUrl = getFunctionDeterminateRemoteUrl;
exports.getModuleFederationConfig = getModuleFederationConfig;
const devkit_1 = require("@nx/devkit");
const project_graph_1 = require("nx/src/project-graph/project-graph");
const utils_1 = require("../../utils");
const utils_2 = require("../react/utils");
const framework_detection_1 = require("../../utils/framework-detection");
function getFunctionDeterminateRemoteUrl(isServer = false) {
const target = 'serve';
const remoteEntry = isServer ? 'server/remoteEntry.js' : 'remoteEntry.js';
return function (remote) {
const mappedStaticRemotesFromEnv = process.env
.NX_MF_DEV_SERVER_STATIC_REMOTES
? JSON.parse(process.env.NX_MF_DEV_SERVER_STATIC_REMOTES)
: undefined;
if (mappedStaticRemotesFromEnv && mappedStaticRemotesFromEnv[remote]) {
return `${mappedStaticRemotesFromEnv[remote]}/${remoteEntry}`;
}
let remoteConfiguration = null;
try {
remoteConfiguration = (0, project_graph_1.readCachedProjectConfiguration)(remote);
}
catch (e) {
throw new Error(`Cannot find remote: "${remote}". Check that the remote name is correct in your module federation config file.\n`);
}
const serveTarget = remoteConfiguration?.targets?.[target];
if (!serveTarget) {
throw new Error(`Cannot automatically determine URL of remote (${remote}). Looked for property "host" in the project's "${serveTarget}" target.\n
You can also use the tuple syntax in your rspack config to configure your remotes. e.g. \`remotes: [['remote1', 'http://localhost:4201']]\``);
}
const host = serveTarget.options?.host ??
`http${serveTarget.options.ssl ? 's' : ''}://localhost`;
const port = serveTarget.options?.port ?? 4201;
return `${host.endsWith('/') ? host.slice(0, -1) : host}:${port}/${remoteEntry}`;
};
}
function getModuleFederationConfig(mfConfig, options = { isServer: false }) {
const projectGraph = (0, devkit_1.readCachedProjectGraph)();
const project = projectGraph.nodes[mfConfig.name]?.data;
if (!project) {
throw Error(`Cannot find project "${mfConfig.name}". Check that the name is correct in module-federation.config.js`);
}
const dependencies = (0, utils_1.getDependentPackagesForProject)(projectGraph, mfConfig.name);
if (mfConfig.shared) {
dependencies.workspaceLibraries = dependencies.workspaceLibraries.filter((lib) => mfConfig.shared(lib.importKey, {}) !== false);
dependencies.npmPackages = dependencies.npmPackages.filter((pkg) => mfConfig.shared(pkg, {}) !== false);
}
const sharedLibraries = (0, utils_1.shareWorkspaceLibraries)(dependencies.workspaceLibraries);
const npmPackages = (0, utils_1.sharePackages)(dependencies.npmPackages);
const sharedDependencies = {
...sharedLibraries.getLibraries(project.root),
...npmPackages,
};
// Apply framework-specific eager packages
if ((0, framework_detection_1.isReactProject)(mfConfig.name, projectGraph)) {
(0, utils_2.applyDefaultEagerPackages)(sharedDependencies);
}
(0, utils_1.applySharedFunction)(sharedDependencies, mfConfig.shared);
(0, utils_1.applyAdditionalShared)(sharedDependencies, mfConfig.additionalShared, projectGraph);
// Choose the correct mapRemotes function based on the server state.
const mapRemotesFunction = options.isServer ? utils_1.mapRemotesForSSR : utils_1.mapRemotes;
// Determine the URL function, either from provided options or by using a default.
const determineRemoteUrlFunction = options.determineRemoteUrl
? options.determineRemoteUrl
: getFunctionDeterminateRemoteUrl(options.isServer);
// Map the remotes if they exist, otherwise default to an empty object.
let mappedRemotes = {};
if (mfConfig.remotes && mfConfig.remotes.length > 0) {
mappedRemotes = mapRemotesFunction(mfConfig.remotes, 'js', determineRemoteUrlFunction, true);
}
return { sharedLibraries, sharedDependencies, mappedRemotes };
}
;