From ae08712748fed5ff194ad335f73dfa7fd7a8744a Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Tue, 10 Feb 2026 01:15:02 +0100 Subject: [PATCH] Fix repo fix to not add typesVersions for non-script exports Only CSS and other non-script, non-JSON exports should be excluded from typesVersions. The package.json export still needs to be included. Also preserves field order when updating existing typesVersions. Signed-off-by: Patrik Oldsberg Co-authored-by: Cursor Signed-off-by: Patrik Oldsberg Co-authored-by: Cursor --- .changeset/css-exports-support.md | 2 + .../modules/maintenance/commands/repo/fix.ts | 46 +++++++++++++------ packages/ui/package.json | 7 +++ 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/.changeset/css-exports-support.md b/.changeset/css-exports-support.md index b4e047b367..17c1a0d8b7 100644 --- a/.changeset/css-exports-support.md +++ b/.changeset/css-exports-support.md @@ -3,3 +3,5 @@ --- Added support for CSS exports in package builds. When a package declares a CSS file in its `exports` field (e.g., `"./styles.css": "./src/styles.css"`), the CLI will automatically bundle it during `backstage-cli package build`, resolving any `@import` statements. The export path is rewritten from `src/` to `dist/` at publish time. + +Fixed `backstage-cli repo fix` to not add `typesVersions` entries for non-script exports like CSS files. diff --git a/packages/cli/src/modules/maintenance/commands/repo/fix.ts b/packages/cli/src/modules/maintenance/commands/repo/fix.ts index 6b31bed466..a5c6b519ed 100644 --- a/packages/cli/src/modules/maintenance/commands/repo/fix.ts +++ b/packages/cli/src/modules/maintenance/commands/repo/fix.ts @@ -27,10 +27,13 @@ import { resolve as resolvePath, posix, relative as relativePath, + extname, } from 'node:path'; import { paths } from '../../../../lib/paths'; import { publishPreflightCheck } from '../../lib/publishing'; +const SCRIPT_EXTS = ['.js', '.jsx', '.ts', '.tsx', '.json']; + /** * A mutable object representing a package.json file with potential fixes. */ @@ -122,29 +125,46 @@ export function fixPackageExports(pkg: FixablePackage) { } const newPath = trimRelative(path); + // Only script files and package.json should be added to typesVersions if (typeof value === 'string') { - typeEntries[newPath] = [trimRelative(value)]; + if (SCRIPT_EXTS.includes(extname(value))) { + typeEntries[newPath] = [trimRelative(value)]; + } } else if (value && typeof value === 'object' && !Array.isArray(value)) { if (typeof value.types === 'string') { typeEntries[newPath] = [trimRelative(value.types)]; - } else if (typeof value.default === 'string') { + } else if ( + typeof value.default === 'string' && + SCRIPT_EXTS.includes(extname(value.default)) + ) { typeEntries[newPath] = [trimRelative(value.default)]; } } } - const typesVersions = { '*': typeEntries }; - if (existingTypesVersions !== JSON.stringify(typesVersions)) { - const newPkgEntries = Object.entries(pkg.packageJson).filter( - ([name]) => name !== 'typesVersions', - ); - newPkgEntries.splice( - newPkgEntries.findIndex(([name]) => name === 'exports') + 1, - 0, - ['typesVersions', typesVersions], - ); + const hasTypeEntries = Object.keys(typeEntries).length > 0; + const typesVersions = hasTypeEntries ? { '*': typeEntries } : undefined; - pkg.packageJson = Object.fromEntries(newPkgEntries) as BackstagePackageJson; + if (existingTypesVersions !== JSON.stringify(typesVersions)) { + if (pkg.packageJson.typesVersions) { + // Update in place to preserve field order + if (typesVersions) { + pkg.packageJson.typesVersions = typesVersions; + } else { + delete pkg.packageJson.typesVersions; + } + } else if (typesVersions) { + // Insert after exports when adding for the first time + const newPkgEntries = Object.entries(pkg.packageJson); + newPkgEntries.splice( + newPkgEntries.findIndex(([name]) => name === 'exports') + 1, + 0, + ['typesVersions', typesVersions], + ); + pkg.packageJson = Object.fromEntries( + newPkgEntries, + ) as BackstagePackageJson; + } pkg.changed = true; } diff --git a/packages/ui/package.json b/packages/ui/package.json index 08f7d89631..a70a060dad 100644 --- a/packages/ui/package.json +++ b/packages/ui/package.json @@ -25,6 +25,13 @@ "./css/styles.css": "./src/css/styles.css", "./package.json": "./package.json" }, + "typesVersions": { + "*": { + "package.json": [ + "package.json" + ] + } + }, "main": "src/index.ts", "types": "src/index.ts", "files": [