Merge pull request #24300 from backstage/rugvip/app-mode

app-backend,cli: move backstage-app-mode injection to build time
This commit is contained in:
Patrik Oldsberg
2024-04-16 14:04:31 +02:00
committed by GitHub
4 changed files with 16 additions and 33 deletions
+11 -5
View File
@@ -41,6 +41,10 @@ export async function buildBundle(options: BuildOptions) {
const { statsJsonEnabled, schema: configSchema } = options;
const paths = resolveBundlingPaths(options);
const publicPaths = await resolveOptionalBundlingPaths({
entry: 'src/index-public-experimental',
dist: 'dist/public',
});
const detectedModulesEntryPoint = await createDetectedModulesEntryPoint({
config: options.fullConfig,
@@ -57,20 +61,22 @@ export async function buildBundle(options: BuildOptions) {
await createConfig(paths, {
...commonConfigOptions,
additionalEntryPoints: detectedModulesEntryPoint,
appMode: publicPaths ? 'protected' : 'public',
}),
];
const publicPaths = await resolveOptionalBundlingPaths({
entry: 'src/index-public-experimental',
dist: 'dist/public',
});
if (publicPaths) {
console.log(
chalk.yellow(
`⚠️ WARNING: The app /public entry point is an experimental feature that may receive immediate breaking changes.`,
),
);
configs.push(await createConfig(publicPaths, commonConfigOptions));
configs.push(
await createConfig(publicPaths, {
...commonConfigOptions,
appMode: 'public',
}),
);
}
const isCi = yn(process.env.CI, { default: false });
+3
View File
@@ -130,6 +130,9 @@ export async function createConfig(
plugins.push(
new HtmlWebpackPlugin({
meta: {
'backstage-app-mode': options?.appMode ?? 'public',
},
template: paths.targetHtml,
templateParameters: {
publicPath,
+2
View File
@@ -27,6 +27,8 @@ export type BundlingOptions = {
additionalEntryPoints?: string[];
// Path to append to the detected public path, e.g. '/public'
publicSubPath?: string;
// Mode that the app is running in, 'protected' or 'public', default is 'public'
appMode?: string;
};
export type ServeOptions = BundlingPathsOptions & {
-28
View File
@@ -218,7 +218,6 @@ export async function createRouter(
publicRouter.use(
await createEntryPointRouter({
appMode: 'public',
logger: logger.child({ entry: 'public' }),
rootDir: publicDistDir,
assetStore: assetStore?.withNamespace('public'),
@@ -231,7 +230,6 @@ export async function createRouter(
router.use(
await createEntryPointRouter({
appMode: enablePublicEntryPoint ? 'protected' : 'public',
logger: logger.child({ entry: 'main' }),
rootDir: appDistDir,
assetStore,
@@ -243,41 +241,17 @@ export async function createRouter(
return router;
}
async function injectAppMode(options: {
appMode: 'public' | 'protected';
rootDir: string;
}) {
const { appMode, rootDir } = options;
const content = await fs.readFile(resolvePath(rootDir, 'index.html'), 'utf8');
const metaTag = `<meta name="backstage-app-mode" content="${appMode}">`;
let newContent;
if (content.includes('backstage-app-mode')) {
newContent = content.replace(
/<meta name="backstage-app-mode" content="[^"]+">/,
metaTag,
);
} else {
newContent = content.replace(/<head>/, `<head>${metaTag}`);
}
await fs.writeFile(resolvePath(rootDir, 'index.html'), newContent, 'utf8');
}
async function createEntryPointRouter({
logger,
rootDir,
assetStore,
staticFallbackHandler,
appMode,
appConfigs,
}: {
logger: LoggerService;
rootDir: string;
assetStore?: StaticAssetsStore;
staticFallbackHandler?: express.Handler;
appMode: 'public' | 'protected';
appConfigs?: AppConfig[];
}) {
const staticDir = resolvePath(rootDir, 'static');
@@ -285,8 +259,6 @@ async function createEntryPointRouter({
const injectedConfigPath =
appConfigs && (await injectConfig({ appConfigs, logger, staticDir }));
await injectAppMode({ appMode, rootDir });
const router = Router();
// Use a separate router for static content so that a fallback can be provided by backend