diff --git a/packages/cli/src/commands/export-dynamic-plugin/backend.ts b/packages/cli/src/commands/export-dynamic-plugin/backend.ts index 5a4470faec..d1f8149e12 100644 --- a/packages/cli/src/commands/export-dynamic-plugin/backend.ts +++ b/packages/cli/src/commands/export-dynamic-plugin/backend.ts @@ -21,7 +21,7 @@ import { loadConfig } from '@backstage/config-loader'; import { getPackages } from '@manypkg/get-packages'; import { OptionValues } from 'commander'; import fs from 'fs-extra'; -import { rollup } from 'rollup'; +import { InteropType, rollup } from 'rollup'; import * as semver from 'semver'; import { execSync } from 'child_process'; @@ -102,6 +102,8 @@ export async function backend( if (relatedCommonPackage !== pkgToEmbed) { mergeWithOutput.push(relatedCommonPackage); } + const relatedAlphaPackage = pkgToEmbed.concat('/alpha'); + mergeWithOutput.push(relatedAlphaPackage); } } @@ -125,6 +127,21 @@ export async function backend( .map(p => p.slice(1)) .map(stringOrRegexp); + let interopForAll: InteropType | undefined = undefined; + const interopForPackage: { [key: string]: InteropType } = {}; + for (const mode in opts.overrideInterop) { + if (!Object.prototype.hasOwnProperty.call(opts.overrideInterop, mode)) { + continue; + } + + if (!opts.overrideInterop[mode]?.length) { + interopForAll = mode as InteropType; + } + for (const interopPkg of opts.overrideInterop[mode]) { + interopForPackage[interopPkg] = mode as InteropType; + } + } + const rollupConfigs = await makeRollupConfigs({ outputs, minify: Boolean(opts.minify), @@ -189,7 +206,20 @@ export async function backend( if (Array.isArray(rollupConfig.output)) { rollupConfig.output.forEach(output => { if (output.format === 'commonjs') { - output.interop = 'default'; + if (interopForAll) { + console.log( + `Overriding Interop to '${interopForAll}' for all imports`, + ); + } + output.interop = (id: string | null) => { + if (id && interopForPackage[id]) { + console.log( + `Overriding Interop to '${interopForPackage[id]}' for '${id}'`, + ); + return interopForPackage[id]; + } + return interopForAll || true; // true is the default value in Rollup. + }; } }); } diff --git a/packages/cli/src/commands/index.ts b/packages/cli/src/commands/index.ts index d98e4c7511..fe3b50b9af 100644 --- a/packages/cli/src/commands/index.ts +++ b/packages/cli/src/commands/index.ts @@ -16,7 +16,7 @@ import { assertError } from '@backstage/errors'; -import { Command } from 'commander'; +import { Command, InvalidArgumentError } from 'commander'; import { exitWithError } from '../lib/errors'; @@ -87,6 +87,20 @@ export function registerScriptCommand(program: Command) { '--shared-package [package-name...]', 'Optional list of packages that should be considered shared by all dynamic plugins, and will be moved to peer dependencies of the dynamic plugin. The `@backstage` packages are by default considered shared dependencies.', ) + .option( + '--override-interop ', + 'Optional list of packages for which the CommonJS Rollup output interop mode should be overridden to `mode` when building the dynamic plugin assets (backend plugin only).', + (value, previous) => { + const [key, val] = value.split(':'); + if (!['auto', 'esModule', 'default', 'defaultOnly'].includes(key)) { + throw new InvalidArgumentError( + `Invalid interop mode '${key}'. Possible values are: auto, esModule, default, defaultOnly (see https://rollupjs.org/configuration-options/#output-interop).`, + ); + } + return { ...previous, [key]: val?.split(',') || [] }; + }, + {}, + ) .option( '--no-install', 'Do not run `yarn install` to fill the dynamic plugin `node_modules` folder (backend plugin only).', diff --git a/packages/cli/src/lib/builder/config.ts b/packages/cli/src/lib/builder/config.ts index 6672b0c4d3..934d924fb6 100644 --- a/packages/cli/src/lib/builder/config.ts +++ b/packages/cli/src/lib/builder/config.ts @@ -92,6 +92,7 @@ export async function makeRollupConfigs( chunkFileNames: `cjs/[name]-[hash].cjs.js`, format: 'commonjs', sourcemap: true, + exports: 'named', }); } if (options.outputs.has(Output.esm)) { diff --git a/packages/cli/src/lib/builder/embedPlugin.ts b/packages/cli/src/lib/builder/embedPlugin.ts index 1e3e33bd17..c7880b35b6 100644 --- a/packages/cli/src/lib/builder/embedPlugin.ts +++ b/packages/cli/src/lib/builder/embedPlugin.ts @@ -54,6 +54,9 @@ export function embedModules(options: EmbedModulesOptions): Plugin { return; } for (const e of embedded) { + if (e.endsWith('/alpha')) { + continue; + } const mod = await this.resolve( path.join(e, 'package.json'), undefined,