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 <poldsberg@gmail.com> Co-authored-by: Cursor <cursoragent@cursor.com> Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com> Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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": [
|
||||
|
||||
Reference in New Issue
Block a user