diff --git a/packages/cli/src/lib/bundler/bundle.ts b/packages/cli/src/lib/bundler/bundle.ts index b9b826c247..232657a3f2 100644 --- a/packages/cli/src/lib/bundler/bundle.ts +++ b/packages/cli/src/lib/bundler/bundle.ts @@ -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 }); diff --git a/packages/cli/src/lib/bundler/config.ts b/packages/cli/src/lib/bundler/config.ts index 8245cf61b3..dc90bed1e7 100644 --- a/packages/cli/src/lib/bundler/config.ts +++ b/packages/cli/src/lib/bundler/config.ts @@ -130,6 +130,9 @@ export async function createConfig( plugins.push( new HtmlWebpackPlugin({ + meta: { + 'backstage-app-mode': options?.appMode ?? 'public', + }, template: paths.targetHtml, templateParameters: { publicPath, diff --git a/packages/cli/src/lib/bundler/types.ts b/packages/cli/src/lib/bundler/types.ts index eeb42ef6a3..8fe25e91c5 100644 --- a/packages/cli/src/lib/bundler/types.ts +++ b/packages/cli/src/lib/bundler/types.ts @@ -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 & { diff --git a/plugins/app-backend/src/service/router.ts b/plugins/app-backend/src/service/router.ts index b3242444aa..af88f0d9da 100644 --- a/plugins/app-backend/src/service/router.ts +++ b/plugins/app-backend/src/service/router.ts @@ -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 = ``; - - let newContent; - if (content.includes('backstage-app-mode')) { - newContent = content.replace( - //, - metaTag, - ); - } else { - newContent = content.replace(//, `${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