diff --git a/packages/cli/src/lib/bundler/bundle.ts b/packages/cli/src/lib/bundler/bundle.ts index ec0c360c59..9c67eb9bc3 100644 --- a/packages/cli/src/lib/bundler/bundle.ts +++ b/packages/cli/src/lib/bundler/bundle.ts @@ -27,7 +27,7 @@ import { createConfig, resolveBaseUrl } from './config'; import { BuildOptions } from './types'; import { resolveBundlingPaths } from './paths'; import chalk from 'chalk'; -import { writeDetectedPluginsModule } from './discover'; +import { buildDetectedPlugins } from './discover'; // TODO(Rugvip): Limits from CRA, we might want to tweak these though. const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024; @@ -50,10 +50,9 @@ export async function buildBundle(options: BuildOptions) { getFrontendAppConfigs: () => options.frontendAppConfigs, }); - await writeDetectedPluginsModule({ + await buildDetectedPlugins({ config: options.fullConfig, - entry: options.entry, - targetDir: options.targetDir, + targetPath: paths.targetPath, }); const isCi = yn(process.env.CI, { default: false }); diff --git a/packages/cli/src/lib/bundler/discover.ts b/packages/cli/src/lib/bundler/discover.ts index 5db8517f32..9325c9bcd8 100644 --- a/packages/cli/src/lib/bundler/discover.ts +++ b/packages/cli/src/lib/bundler/discover.ts @@ -16,38 +16,39 @@ import { BackstagePackageJson } from '@backstage/cli-node'; import { Config } from '@backstage/config'; -import fs from 'fs-extra'; -import path from 'path'; import chokidar from 'chokidar'; - +import fs from 'fs-extra'; +import { join as joinPath, resolve as resolvePath } from 'path'; import { paths as cliPaths } from '../../lib/paths'; -import { BundlingPathsOptions, resolveBundlingPaths } from './paths'; -type Options = { config: Config; watch: () => void } & BundlingPathsOptions; - -export async function buildDetectedPlugins(options: Options) { - const { entry, targetDir } = options; - const { targetPackageJson } = resolveBundlingPaths({ entry, targetDir }); - - if (!!options.watch) { - const watcher = chokidar.watch(targetPackageJson); +export async function buildDetectedPlugins(options: { + config: Config; + targetPath: string; + watch?: () => void; +}) { + const { watch, targetPath } = options; + if (watch) { + const watcher = chokidar.watch(resolvePath(targetPath, 'package.json')); watcher.on('change', async () => { await writeDetectedPluginsModule(options); - options.watch(); + watch(); }); } await writeDetectedPluginsModule(options); } -async function writeDetectedPluginsModule(options: Options) { +async function writeDetectedPluginsModule(options: { + config: Config; + targetPath: string; +}) { const requirePackageScript = (await detectPlugins(options)) ?.map(pkg => `{name: '${pkg}', module: require('${pkg}')}`) .join(','); await fs.writeFile( - path.join( + joinPath( cliPaths.targetRoot, 'node_modules', '__backstage-autodetected-plugins__.js', @@ -56,9 +57,16 @@ async function writeDetectedPluginsModule(options: Options) { ); } -async function detectPlugins({ config, entry, targetDir }: Options) { - const paths = resolveBundlingPaths({ entry, targetDir }); - const pkg: BackstagePackageJson = await fs.readJson(paths.targetPackageJson); +async function detectPlugins({ + config, + targetPath, +}: { + config: Config; + targetPath: string; +}) { + const pkg: BackstagePackageJson = await fs.readJson( + resolvePath(targetPath, 'package.json'), + ); // TODO: proper // Assumption for config string based on https://github.com/backstage/backstage/issues/18372 ^ @@ -74,7 +82,7 @@ async function detectPlugins({ config, entry, targetDir }: Options) { .map(depName => { const depPackageJson: BackstagePackageJson = require(require.resolve( `${depName}/package.json`, - { paths: [paths.targetPath] }, + { paths: [targetPath] }, )); if ( ['frontend-plugin', 'frontend-plugin-module'].includes( diff --git a/packages/cli/src/lib/bundler/server.ts b/packages/cli/src/lib/bundler/server.ts index 038d27ed56..43a87db684 100644 --- a/packages/cli/src/lib/bundler/server.ts +++ b/packages/cli/src/lib/bundler/server.ts @@ -133,8 +133,7 @@ export async function serveBundle(options: ServeOptions) { await buildDetectedPlugins({ config: fullConfig, - entry: options.entry, - targetDir: options.targetDir, + targetPath: paths.targetPath, watch() { server?.invalidate(); },