From 8d4b5cf95fd8740902c593a43ff2c90ee49315fc Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 6 Sep 2023 16:32:40 +0200 Subject: [PATCH] cli,frontend-app-api: switch experimental discovery to use default export MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Fredrik Adelöw Co-authored-by: Johan Haals Co-authored-by: Camila Belo Co-authored-by: Philipp Hugenroth Signed-off-by: Patrik Oldsberg --- .../app-next-example-plugin/api-report.md | 3 +- packages/app-next-example-plugin/package.json | 1 + packages/app-next-example-plugin/src/index.ts | 2 +- .../cli/src/lib/bundler/packageDetection.ts | 2 +- .../src/wiring/discovery.test.ts | 73 +++++-------------- .../frontend-app-api/src/wiring/discovery.ts | 6 +- 6 files changed, 25 insertions(+), 62 deletions(-) diff --git a/packages/app-next-example-plugin/api-report.md b/packages/app-next-example-plugin/api-report.md index 08e283b879..68a0471b6b 100644 --- a/packages/app-next-example-plugin/api-report.md +++ b/packages/app-next-example-plugin/api-report.md @@ -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; diff --git a/packages/app-next-example-plugin/package.json b/packages/app-next-example-plugin/package.json index 62eeaba08b..08bff4e82a 100644 --- a/packages/app-next-example-plugin/package.json +++ b/packages/app-next-example-plugin/package.json @@ -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:^", diff --git a/packages/app-next-example-plugin/src/index.ts b/packages/app-next-example-plugin/src/index.ts index dae1eea379..89ce4180fd 100644 --- a/packages/app-next-example-plugin/src/index.ts +++ b/packages/app-next-example-plugin/src/index.ts @@ -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'; diff --git a/packages/cli/src/lib/bundler/packageDetection.ts b/packages/cli/src/lib/bundler/packageDetection.ts index 293ef74f81..46cffdceb0 100644 --- a/packages/cli/src/lib/bundler/packageDetection.ts +++ b/packages/cli/src/lib/bundler/packageDetection.ts @@ -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( diff --git a/packages/frontend-app-api/src/wiring/discovery.test.ts b/packages/frontend-app-api/src/wiring/discovery.test.ts index a93f853c1b..5f1c4df718 100644 --- a/packages/frontend-app-api/src/wiring/discovery.test.ts +++ b/packages/frontend-app-api/src/wiring/discovery.test.ts @@ -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([ diff --git a/packages/frontend-app-api/src/wiring/discovery.ts b/packages/frontend-app-api/src/wiring/discovery.ts index 7b0f4a50e4..d872ae4441 100644 --- a/packages/frontend-app-api/src/wiring/discovery.ts +++ b/packages/frontend-app-api/src/wiring/discovery.ts @@ -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) ?? [] ); }