cli,frontend-app-api: switch experimental discovery to use default export
Co-authored-by: Fredrik Adelöw <freben@gmail.com> Co-authored-by: Johan Haals <johan.haals@gmail.com> Co-authored-by: Camila Belo <camilaibs@gmail.com> Co-authored-by: Philipp Hugenroth <philipph@spotify.com> Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -7,7 +7,8 @@ import { BackstagePlugin } from '@backstage/frontend-plugin-api';
|
||||
import { default as React_2 } from 'react';
|
||||
|
||||
// @public (undocumented)
|
||||
export const examplePlugin: BackstagePlugin;
|
||||
const examplePlugin: BackstagePlugin;
|
||||
export default examplePlugin;
|
||||
|
||||
// @public (undocumented)
|
||||
export const ExampleSidebarItem: () => React_2.JSX.Element;
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
"postpack": "backstage-cli package postpack",
|
||||
"clean": "backstage-cli package clean"
|
||||
},
|
||||
"sideEffects": false,
|
||||
"dependencies": {
|
||||
"@backstage/core-components": "workspace:^",
|
||||
"@backstage/frontend-plugin-api": "workspace:^",
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export { examplePlugin } from './plugin';
|
||||
export { examplePlugin as default } from './plugin';
|
||||
|
||||
// TODO: This should be an extension created & exported in the `plugin.tsx`
|
||||
export { ExampleSidebarItem } from './ExampleSidebarItem';
|
||||
|
||||
@@ -87,7 +87,7 @@ async function detectPackages(
|
||||
|
||||
async function writeDetectedPackagesModule(packageNames: string[]) {
|
||||
const requirePackageScript = packageNames
|
||||
?.map(pkg => `{name: '${pkg}', module: require('${pkg}')}`)
|
||||
?.map(pkg => `{ name: '${pkg}', default: require('${pkg}').default }`)
|
||||
.join(',');
|
||||
|
||||
await fs.writeFile(
|
||||
|
||||
@@ -39,55 +39,26 @@ describe('getAvailablePlugins', () => {
|
||||
it('should discover a plugin', () => {
|
||||
const testPlugin = createPlugin({ id: 'test' });
|
||||
globalSpy.mockReturnValue({
|
||||
modules: [
|
||||
{
|
||||
module: {
|
||||
testPlugin,
|
||||
},
|
||||
},
|
||||
],
|
||||
modules: [{ default: testPlugin }],
|
||||
});
|
||||
expect(getAvailablePlugins()).toEqual([testPlugin]);
|
||||
});
|
||||
|
||||
it('should ignore garbage', () => {
|
||||
const testPlugin = createPlugin({ id: 'test' });
|
||||
globalSpy.mockReturnValue({
|
||||
modules: [
|
||||
{
|
||||
module: {
|
||||
testPlugin,
|
||||
a: 'a',
|
||||
b: null,
|
||||
c: undefined,
|
||||
d: Symbol('wat'),
|
||||
e: () => {},
|
||||
f: [],
|
||||
g: {},
|
||||
h: class {},
|
||||
i: NaN,
|
||||
j: Infinity,
|
||||
k: -Infinity,
|
||||
l: new Date(),
|
||||
m: new RegExp('wat'),
|
||||
n: new Error('wat'),
|
||||
o: new Map(),
|
||||
p: new Set(),
|
||||
q: new WeakMap(),
|
||||
r: new WeakSet(),
|
||||
s: new ArrayBuffer(1),
|
||||
t: new DataView(new ArrayBuffer(1)),
|
||||
u: false,
|
||||
v: true,
|
||||
w: 0,
|
||||
x: 1,
|
||||
y: -1,
|
||||
z: '',
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(getAvailablePlugins()).toEqual([testPlugin]);
|
||||
globalSpy.mockReturnValueOnce({ modules: [{ default: null }] });
|
||||
expect(getAvailablePlugins()).toEqual([]);
|
||||
globalSpy.mockReturnValueOnce({ modules: [{ default: undefined }] });
|
||||
expect(getAvailablePlugins()).toEqual([]);
|
||||
globalSpy.mockReturnValueOnce({ modules: [{ default: Symbol() }] });
|
||||
expect(getAvailablePlugins()).toEqual([]);
|
||||
globalSpy.mockReturnValueOnce({ modules: [{ default: () => {} }] });
|
||||
expect(getAvailablePlugins()).toEqual([]);
|
||||
globalSpy.mockReturnValueOnce({ modules: [{ default: 0 }] });
|
||||
expect(getAvailablePlugins()).toEqual([]);
|
||||
globalSpy.mockReturnValueOnce({ modules: [{ default: false }] });
|
||||
expect(getAvailablePlugins()).toEqual([]);
|
||||
globalSpy.mockReturnValueOnce({ modules: [{ default: true }] });
|
||||
expect(getAvailablePlugins()).toEqual([]);
|
||||
});
|
||||
|
||||
it('should discover multiple plugins', () => {
|
||||
@@ -96,17 +67,9 @@ describe('getAvailablePlugins', () => {
|
||||
const test3Plugin = createPlugin({ id: 'test3' });
|
||||
globalSpy.mockReturnValue({
|
||||
modules: [
|
||||
{
|
||||
module: {
|
||||
test1Plugin,
|
||||
test2Plugin,
|
||||
},
|
||||
},
|
||||
{
|
||||
module: {
|
||||
test3Plugin,
|
||||
},
|
||||
},
|
||||
{ default: test1Plugin },
|
||||
{ default: test2Plugin },
|
||||
{ default: test3Plugin },
|
||||
],
|
||||
});
|
||||
expect(getAvailablePlugins()).toEqual([
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
import { BackstagePlugin } from '@backstage/frontend-plugin-api';
|
||||
|
||||
interface DiscoveryGlobal {
|
||||
modules: Array<{ name: string; module: object }>;
|
||||
modules: Array<{ name: string; default: unknown }>;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -29,9 +29,7 @@ export function getAvailablePlugins(): BackstagePlugin[] {
|
||||
)['__@backstage/discovered__'];
|
||||
|
||||
return (
|
||||
discovered?.modules.flatMap(({ module: mod }) =>
|
||||
Object.values(mod).filter(isBackstagePlugin),
|
||||
) ?? []
|
||||
discovered?.modules.map(m => m.default).filter(isBackstagePlugin) ?? []
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user