UNPKG

@nx/module-federation

Version:

The Nx Plugin for Module Federation contains executors and utilities that support building applications using Module Federation.

50 lines (49 loc) 2.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.startRemoteProxies = startRemoteProxies; const fs_1 = require("fs"); function startRemoteProxies(staticRemotesConfig, mappedLocationsOfRemotes, sslOptions, isServer) { const { createProxyMiddleware } = require('http-proxy-middleware'); const express = require('express'); let sslCert; let sslKey; if (sslOptions && sslOptions.pathToCert && sslOptions.pathToKey) { if ((0, fs_1.existsSync)(sslOptions.pathToCert) && (0, fs_1.existsSync)(sslOptions.pathToKey)) { sslCert = (0, fs_1.readFileSync)(sslOptions.pathToCert); sslKey = (0, fs_1.readFileSync)(sslOptions.pathToKey); } else { console.warn(`Encountered SSL options in project.json, however, the certificate files do not exist in the filesystem. Using http.`); console.warn(`Attempted to find '${sslOptions.pathToCert}' and '${sslOptions.pathToKey}'.`); } } const http = require('http'); const https = require('https'); const remotes = Object.keys(staticRemotesConfig); console.log(`NX Starting static remotes proxies...`); for (const app of remotes) { const appConfig = staticRemotesConfig[app]; const expressProxy = express(); expressProxy.use(createProxyMiddleware({ target: mappedLocationsOfRemotes[app], changeOrigin: true, secure: sslCert ? false : undefined, pathRewrite: isServer ? (path) => { if (path.includes('/server')) { return path; } else { return `browser/${path}`; } } : undefined, })); const proxyServer = (sslCert ? https : http) .createServer({ cert: sslCert, key: sslKey }, expressProxy) .listen(appConfig.port); process.on('SIGTERM', () => proxyServer.close()); process.on('exit', () => proxyServer.close()); } console.info(`NX Static remotes proxies started successfully`); }