cli: only consider PORT environment variable for module federation remotes

Signed-off-by: MT Lewis <mtlewis@users.noreply.github.com>
This commit is contained in:
MT Lewis
2024-07-19 14:28:13 +01:00
parent 0eedec304d
commit 9d661bb380
2 changed files with 37 additions and 13 deletions
+32 -11
View File
@@ -14,7 +14,11 @@
* limitations under the License.
*/
import { BackendBundlingOptions, BundlingOptions } from './types';
import {
BackendBundlingOptions,
BundlingOptions,
ModuleFederationOptions,
} from './types';
import { posix as posixPath, resolve as resolvePath, dirname } from 'path';
import chalk from 'chalk';
import webpack, { ProvidePlugin } from 'webpack';
@@ -46,24 +50,32 @@ import { hasReactDomClient } from './hasReactDomClient';
const BUILD_CACHE_ENV_VAR = 'BACKSTAGE_CLI_EXPERIMENTAL_BUILD_CACHE';
export function resolveBaseUrl(config: Config): URL {
export function resolveBaseUrl(
config: Config,
moduleFederation?: ModuleFederationOptions,
): URL {
const baseUrl = config.getOptionalString('app.baseUrl');
const defaultBaseUrl =
moduleFederation?.mode === 'remote'
? `http://localhost:${process.env.PORT ?? '3000'}`
: 'http://localhost:3000';
try {
return new URL(
baseUrl ?? '/',
`http://localhost:${process.env.PORT ?? '3000'}`,
);
return new URL(baseUrl ?? '/', defaultBaseUrl);
} catch (error) {
throw new Error(`Invalid app.baseUrl, ${error}`);
}
}
export function resolveEndpoint(config: Config): {
export function resolveEndpoint(
config: Config,
moduleFederation?: ModuleFederationOptions,
): {
host: string;
port: number;
} {
const url = resolveBaseUrl(config);
const url = resolveBaseUrl(config, moduleFederation);
return {
host: config.getOptionalString('app.listen.host') ?? url.hostname,
@@ -114,7 +126,13 @@ export async function createConfig(
paths: BundlingPaths,
options: BundlingOptions,
): Promise<webpack.Configuration> {
const { checksEnabled, isDev, frontendConfig, publicSubPath = '' } = options;
const {
checksEnabled,
isDev,
frontendConfig,
moduleFederation,
publicSubPath = '',
} = options;
const { plugins, loaders } = transforms(options);
// Any package that is part of the monorepo but outside the monorepo root dir need
@@ -122,14 +140,17 @@ export async function createConfig(
const { packages } = await getPackages(cliPaths.targetDir);
const externalPkgs = packages.filter(p => !isChildPath(paths.root, p.dir));
const validBaseUrl = resolveBaseUrl(frontendConfig);
const validBaseUrl = resolveBaseUrl(frontendConfig, moduleFederation);
let publicPath = validBaseUrl.pathname.replace(/\/$/, '');
if (publicSubPath) {
publicPath = `${publicPath}${publicSubPath}`.replace('//', '/');
}
if (isDev) {
const { host, port } = resolveEndpoint(options.frontendConfig);
const { host, port } = resolveEndpoint(
options.frontendConfig,
options.moduleFederation,
);
plugins.push(
new ReactRefreshPlugin({
+5 -2
View File
@@ -130,8 +130,11 @@ DEPRECATION WARNING: React Router Beta is deprecated and support for it will be
}
const { frontendConfig, fullConfig } = cliConfig;
const url = resolveBaseUrl(frontendConfig);
const { host, port } = resolveEndpoint(frontendConfig);
const url = resolveBaseUrl(frontendConfig, options.moduleFederation);
const { host, port } = resolveEndpoint(
frontendConfig,
options.moduleFederation,
);
const detectedModulesEntryPoint = await createDetectedModulesEntryPoint({
config: fullConfig,