backend-app-api: double default wrapping workaround

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-10-21 15:43:28 +02:00
parent 1df3c04025
commit bc9a18d5ec
2 changed files with 26 additions and 2 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/backend-app-api': patch
---
Added a workaround for double `default` wrapping when dynamically importing CommonJS modules with default exports.
@@ -57,7 +57,26 @@ function isPromise<T>(value: unknown | Promise<T>): value is Promise<T> {
}
function unwrapFeature(
feature: BackendFeature | (() => BackendFeature),
feature:
| BackendFeature
| (() => BackendFeature)
| { default: BackendFeature | (() => BackendFeature) },
): BackendFeature {
return typeof feature === 'function' ? feature() : feature;
if (typeof feature === 'function') {
return feature();
}
if ('$$type' in feature) {
return feature;
}
// This is a workaround where default exports get transpiled to `exports['default'] = ...`
// in CommonJS modules, which in turn results in a double `{ default: { default: ... } }` nesting
// when importing using a dynamic import.
// TODO: This is a broader issue than just this piece of code, and should move away from CommonJS.
if ('default' in feature) {
const defaultFeature = feature.default;
return typeof defaultFeature === 'function'
? defaultFeature()
: defaultFeature;
}
return feature;
}