From 0af208f85fe9f3e7e907ea6fca53c29401f8893f Mon Sep 17 00:00:00 2001 From: Philipp Hugenroth Date: Wed, 19 Jul 2023 15:27:13 +0200 Subject: [PATCH] Abstract detection logic in seperate file Co-authored-by: Vincenzo Scamporlino Signed-off-by: Philipp Hugenroth --- packages/cli/src/lib/bundler/bundle.ts | 7 +++ packages/cli/src/lib/bundler/config.ts | 63 +-------------------- packages/cli/src/lib/bundler/discover.ts | 72 ++++++++++++++++++++++++ packages/cli/src/lib/bundler/server.ts | 7 +++ 4 files changed, 88 insertions(+), 61 deletions(-) create mode 100644 packages/cli/src/lib/bundler/discover.ts diff --git a/packages/cli/src/lib/bundler/bundle.ts b/packages/cli/src/lib/bundler/bundle.ts index 8129f38102..ec0c360c59 100644 --- a/packages/cli/src/lib/bundler/bundle.ts +++ b/packages/cli/src/lib/bundler/bundle.ts @@ -27,6 +27,7 @@ import { createConfig, resolveBaseUrl } from './config'; import { BuildOptions } from './types'; import { resolveBundlingPaths } from './paths'; import chalk from 'chalk'; +import { writeDetectedPluginsModule } from './discover'; // TODO(Rugvip): Limits from CRA, we might want to tweak these though. const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024; @@ -49,6 +50,12 @@ export async function buildBundle(options: BuildOptions) { getFrontendAppConfigs: () => options.frontendAppConfigs, }); + await writeDetectedPluginsModule({ + config: options.fullConfig, + entry: options.entry, + targetDir: options.targetDir, + }); + const isCi = yn(process.env.CI, { default: false }); const previousFileSizes = await measureFileSizesBeforeBuild(paths.targetDist); diff --git a/packages/cli/src/lib/bundler/config.ts b/packages/cli/src/lib/bundler/config.ts index be5a87b76a..25a3db1823 100644 --- a/packages/cli/src/lib/bundler/config.ts +++ b/packages/cli/src/lib/bundler/config.ts @@ -26,23 +26,18 @@ import { isChildPath } from '@backstage/cli-common'; import { getPackages } from '@manypkg/get-packages'; import { optimization } from './optimization'; import { Config } from '@backstage/config'; -import { - BundlingPaths, - BundlingPathsOptions, - resolveBundlingPaths, -} from './paths'; +import { BundlingPaths } from './paths'; import { transforms } from './transforms'; import { LinkedPackageResolvePlugin } from './LinkedPackageResolvePlugin'; import { BundlingOptions, BackendBundlingOptions } from './types'; import { version } from '../../lib/version'; import { paths as cliPaths } from '../../lib/paths'; -import { BackstagePackage, BackstagePackageJson } from '@backstage/cli-node'; +import { BackstagePackage } from '@backstage/cli-node'; import { runPlain } from '../run'; import ESLintPlugin from 'eslint-webpack-plugin'; import pickBy from 'lodash/pickBy'; import yn from 'yn'; import { readEntryPoints } from '../entryPoints'; -import path from 'path'; const BUILD_CACHE_ENV_VAR = 'BACKSTAGE_CLI_EXPERIMENTAL_BUILD_CACHE'; @@ -132,24 +127,6 @@ export async function createConfig( }), ); - const detectedPlugins = await detectPlugins({ - config: options.fullConfig, - entry: options.entry, - targetDir: options.targetDir, - }); - const requirePackageScript = detectedPlugins - ?.map(pkg => `{name: '${pkg}', module: require('${pkg}')}`) - .join(','); - - await fs.writeFile( - path.join( - cliPaths.targetRoot, - 'node_modules', - 'backstage-autodetected-plugins.js', - ), - `module.exports = { modules: [${requirePackageScript}] };`, - ); - const buildInfo = await readBuildInfo(); plugins.push( new webpack.DefinePlugin({ @@ -388,39 +365,3 @@ function nodeExternalsWithResolve( return externals(context, request, callback); }; } - -export async function detectPlugins({ - config, - entry, - targetDir, -}: { config: Config } & BundlingPathsOptions) { - const paths = resolveBundlingPaths({ entry, targetDir }); - const pkg: BackstagePackageJson = await fs.readJson(paths.targetPackageJson); - // TODO: proper - // Assumption for config string based on https://github.com/backstage/backstage/issues/18372 ^ - - const packageDetectionMode = - config.getOptional('app.experimental.packages') || 'all'; - - const allowedPackages = - packageDetectionMode === 'all' - ? Object.keys(pkg.dependencies ?? {}) - : config.getStringArray('app.experimental.packages'); - - return allowedPackages - .map(depName => { - const depPackageJson: BackstagePackageJson = require(require.resolve( - `${depName}/package.json`, - { paths: [paths.targetPath] }, - )); - if ( - ['frontend-plugin', 'frontend-plugin-module'].includes( - depPackageJson.backstage?.role || '', - ) - ) { - return depName; - } - return undefined; - }) - .filter((d): d is string => !!d); -} diff --git a/packages/cli/src/lib/bundler/discover.ts b/packages/cli/src/lib/bundler/discover.ts new file mode 100644 index 0000000000..5f3d3e290b --- /dev/null +++ b/packages/cli/src/lib/bundler/discover.ts @@ -0,0 +1,72 @@ +/* + * Copyright 2020 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { BackstagePackageJson } from '@backstage/cli-node'; +import { Config } from '@backstage/config'; +import fs from 'fs-extra'; +import path from 'path'; + +import { BundlingPathsOptions, resolveBundlingPaths } from './paths'; +import { paths as cliPaths } from '../../lib/paths'; + +type Options = { config: Config } & BundlingPathsOptions; + +async function detectPlugins({ config, entry, targetDir }: Options) { + const paths = resolveBundlingPaths({ entry, targetDir }); + const pkg: BackstagePackageJson = await fs.readJson(paths.targetPackageJson); + // TODO: proper + // Assumption for config string based on https://github.com/backstage/backstage/issues/18372 ^ + + const packageDetectionMode = + config.getOptional('app.experimental.packages') || 'all'; + + const allowedPackages = + packageDetectionMode === 'all' + ? Object.keys(pkg.dependencies ?? {}) + : config.getStringArray('app.experimental.packages'); + + return allowedPackages + .map(depName => { + const depPackageJson: BackstagePackageJson = require(require.resolve( + `${depName}/package.json`, + { paths: [paths.targetPath] }, + )); + if ( + ['frontend-plugin', 'frontend-plugin-module'].includes( + depPackageJson.backstage?.role || '', + ) + ) { + return depName; + } + return undefined; + }) + .filter((d): d is string => !!d); +} + +export async function writeDetectedPluginsModule(options: Options) { + const requirePackageScript = (await detectPlugins(options)) + ?.map(pkg => `{name: '${pkg}', module: require('${pkg}')}`) + .join(','); + + await fs.writeFile( + path.join( + cliPaths.targetRoot, + 'node_modules', + 'backstage-autodetected-plugins.js', + ), + `module.exports = { modules: [${requirePackageScript}] };`, + ); +} diff --git a/packages/cli/src/lib/bundler/server.ts b/packages/cli/src/lib/bundler/server.ts index 96eee2a387..fd9c2f92fd 100644 --- a/packages/cli/src/lib/bundler/server.ts +++ b/packages/cli/src/lib/bundler/server.ts @@ -34,6 +34,7 @@ import { includedFilter, } from '../../commands/versions/lint'; import { BackstagePackageJson } from '@backstage/cli-node'; +import { writeDetectedPluginsModule } from './discover'; export async function serveBundle(options: ServeOptions) { const paths = resolveBundlingPaths(options); @@ -171,6 +172,12 @@ export async function serveBundle(options: ServeOptions) { }, }); + await writeDetectedPluginsModule({ + config: options.fullConfig, + entry: options.entry, + targetDir: options.targetDir, + }); + const compiler = webpack(config); server = new WebpackDevServer(