From 7955f9bbc0ba0886f75d209577f58503ca5202c3 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Tue, 8 Oct 2024 11:19:31 +0200 Subject: [PATCH] cli: disable feature detection for backend builds Signed-off-by: Patrik Oldsberg --- .changeset/neat-geckos-end.md | 5 +++++ packages/cli/src/commands/buildWorkspace.ts | 1 + packages/cli/src/commands/pack.ts | 6 +++++- .../src/lib/packager/createDistWorkspace.ts | 16 ++++++++++++--- .../cli/src/lib/packager/productionPack.ts | 20 ++++++++----------- 5 files changed, 32 insertions(+), 16 deletions(-) create mode 100644 .changeset/neat-geckos-end.md diff --git a/.changeset/neat-geckos-end.md b/.changeset/neat-geckos-end.md new file mode 100644 index 0000000000..02a281b447 --- /dev/null +++ b/.changeset/neat-geckos-end.md @@ -0,0 +1,5 @@ +--- +'@backstage/cli': patch +--- + +Tweaked the new package feature detection to not be active when building backend packages. diff --git a/packages/cli/src/commands/buildWorkspace.ts b/packages/cli/src/commands/buildWorkspace.ts index 685e4b1cb0..a20663fc32 100644 --- a/packages/cli/src/commands/buildWorkspace.ts +++ b/packages/cli/src/commands/buildWorkspace.ts @@ -29,5 +29,6 @@ export default async (dir: string, packages: string[], options: Options) => { await createDistWorkspace(packages, { targetDir: dir, alwaysYarnPack: options.alwaysYarnPack, + enableFeatureDetection: true, }); }; diff --git a/packages/cli/src/commands/pack.ts b/packages/cli/src/commands/pack.ts index 8fbbdb3d74..24f2a90c3f 100644 --- a/packages/cli/src/commands/pack.ts +++ b/packages/cli/src/commands/pack.ts @@ -21,6 +21,7 @@ import { import { paths } from '../lib/paths'; import fs from 'fs-extra'; import { publishPreflightCheck } from '../lib/publishing'; +import { createTypeDistProject } from '../lib/typeDistProject'; export const pre = async () => { publishPreflightCheck({ @@ -28,7 +29,10 @@ export const pre = async () => { packageJson: await fs.readJson(paths.resolveTarget('package.json')), }); - await productionPack({ packageDir: paths.targetDir }); + await productionPack({ + packageDir: paths.targetDir, + featureDetectionProject: await createTypeDistProject(), + }); }; export const post = async () => { diff --git a/packages/cli/src/lib/packager/createDistWorkspace.ts b/packages/cli/src/lib/packager/createDistWorkspace.ts index 7b7db43889..2363023ddd 100644 --- a/packages/cli/src/lib/packager/createDistWorkspace.ts +++ b/packages/cli/src/lib/packager/createDistWorkspace.ts @@ -105,6 +105,12 @@ type Options = { */ alwaysYarnPack?: boolean; + /** + * If set to true, the TypeScript feature detection will be enabled, which + * annotates the package exports field with the `backstage` export type. + */ + enableFeatureDetection?: boolean; + /** * If set to true, the generated code will be minified. */ @@ -237,6 +243,7 @@ export async function createDistWorkspace( targetDir, targets, Boolean(options.alwaysYarnPack), + Boolean(options.enableFeatureDetection), ); const files: FileEntry[] = options.files ?? ['yarn.lock', 'package.json']; @@ -280,6 +287,7 @@ async function moveToDistWorkspace( workspaceDir: string, localPackages: PackageGraphNode[], alwaysYarnPack: boolean, + enableFeatureDetection: boolean, ): Promise { const [fastPackPackages, slowPackPackages] = partition( localPackages, @@ -288,8 +296,10 @@ async function moveToDistWorkspace( FAST_PACK_SCRIPTS.includes(pkg.packageJson.scripts?.prepack), ); - const tsMorphProject = - fastPackPackages.length > 0 ? await createTypeDistProject() : undefined; + const featureDetectionProject = + fastPackPackages.length > 0 && enableFeatureDetection + ? await createTypeDistProject() + : undefined; // New an improved flow where we avoid calling `yarn pack` await Promise.all( @@ -301,7 +311,7 @@ async function moveToDistWorkspace( await productionPack({ packageDir: target.dir, targetDir: absoluteOutputPath, - project: tsMorphProject, + featureDetectionProject, }); }), ); diff --git a/packages/cli/src/lib/packager/productionPack.ts b/packages/cli/src/lib/packager/productionPack.ts index 85f50352cc..c15a7f8b9e 100644 --- a/packages/cli/src/lib/packager/productionPack.ts +++ b/packages/cli/src/lib/packager/productionPack.ts @@ -19,10 +19,7 @@ import npmPackList from 'npm-packlist'; import { resolve as resolvePath, posix as posixPath } from 'path'; import { BackstagePackageJson } from '@backstage/cli-node'; import { readEntryPoints } from '../entryPoints'; -import { - createTypeDistProject, - getEntryPointDefaultFeatureType, -} from '../typeDistProject'; +import { getEntryPointDefaultFeatureType } from '../typeDistProject'; import { Project } from 'ts-morph'; const PKG_PATH = 'package.json'; @@ -35,9 +32,9 @@ interface ProductionPackOptions { packageDir: string; targetDir?: string; /** - * A ts-morph project to share across packages + * Enables package feature detection using this TS-morph project. */ - project?: Project; + featureDetectionProject?: Project; } export async function productionPack(options: ProductionPackOptions) { @@ -55,7 +52,7 @@ export async function productionPack(options: ProductionPackOptions) { const writeCompatibilityEntryPoints = await prepareExportsEntryPoints( pkg, packageDir, - options.project, + options.featureDetectionProject, ); // TODO(Rugvip): Once exports are rolled out more broadly we should deprecate and remove this behavior @@ -144,7 +141,7 @@ const EXPORT_MAP = { async function prepareExportsEntryPoints( pkg: BackstagePackageJson, packageDir: string, - commonProject?: Project, + featureDetectionProject?: Project, ) { const distPath = resolvePath(packageDir, 'dist'); if (!(await fs.pathExists(distPath))) { @@ -158,7 +155,6 @@ async function prepareExportsEntryPoints( >(); const entryPoints = readEntryPoints(pkg); - const project = commonProject || (await createTypeDistProject()); for (const entryPoint of entryPoints) { if (!SCRIPT_EXTS.includes(entryPoint.ext)) { @@ -177,14 +173,14 @@ async function prepareExportsEntryPoints( exp.default = exp.require ?? exp.import; - // Find the default export type for the entry point - if (exp.types) { + // Find the default export type for the entry point, if feature detection is active + if (exp.types && featureDetectionProject) { const defaultFeatureType = pkg.backstage?.role && getEntryPointDefaultFeatureType( pkg.backstage?.role, packageDir, - project, + featureDetectionProject, exp.types, );