frontend-app-api: remove plugin duplicates

Co-authored-by: Camila Belo <camilaibs@gmail.com>
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-10-17 16:44:39 +02:00
parent c5e8be56c3
commit 685a4c8901
3 changed files with 33 additions and 8 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/frontend-app-api': patch
---
Installed features are now deduplicated both by reference and ID when available. Features passed to `createApp` now override both discovered and loaded features.
@@ -104,6 +104,5 @@ export function collectLegacyRoutes(
},
);
// TODO: For every legacy plugin that we find, make sure any matching plugin is disabled in the new system
return results;
}
@@ -267,6 +267,29 @@ export function createInstances(options: {
return { coreInstance, instances };
}
function deduplicateFeatures(
allFeatures: (BackstagePlugin | ExtensionOverrides)[],
): (BackstagePlugin | ExtensionOverrides)[] {
// Start by removing duplicates by reference
const features = Array.from(new Set(allFeatures));
// Plugins are deduplicated by ID, last one wins
const seenIds = new Set<string>();
return features
.reverse()
.filter(feature => {
if (feature.$$type !== '@backstage/BackstagePlugin') {
return true;
}
if (seenIds.has(feature.id)) {
return false;
}
seenIds.add(feature.id);
return true;
})
.reverse();
}
/** @public */
export function createApp(options: {
features?: (BackstagePlugin | ExtensionOverrides)[];
@@ -287,13 +310,11 @@ export function createApp(options: {
const discoveredFeatures = getAvailableFeatures(config);
const loadedFeatures = (await options.featureLoader?.({ config })) ?? [];
const allFeatures = Array.from(
new Set([
...discoveredFeatures,
...(options.features ?? []),
...loadedFeatures,
]),
);
const allFeatures = deduplicateFeatures([
...discoveredFeatures,
...loadedFeatures,
...(options.features ?? []),
]);
const { coreInstance } = createInstances({
features: allFeatures,