Skip to content

Commit

Permalink
fix(cli): do not change interop mode by default
Browse files Browse the repository at this point in the history
but provide an additional CLI argument to change it
per imported package.
Also include the related `./alpha` package when embedding a package.

Signed-off-by: David Festal <dfestal@redhat.com>
  • Loading branch information
davidfestal committed Nov 23, 2023
1 parent 80499eb commit 39053db
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
34 changes: 32 additions & 2 deletions packages/cli/src/commands/export-dynamic-plugin/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -102,6 +102,8 @@ export async function backend(
if (relatedCommonPackage !== pkgToEmbed) {
mergeWithOutput.push(relatedCommonPackage);
}
const relatedAlphaPackage = pkgToEmbed.concat('/alpha');
mergeWithOutput.push(relatedAlphaPackage);
}
}

Expand All @@ -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),
Expand Down Expand Up @@ -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.
};
}
});
}
Expand Down
16 changes: 15 additions & 1 deletion packages/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { assertError } from '@backstage/errors';

import { Command } from 'commander';
import { Command, InvalidArgumentError } from 'commander';

import { exitWithError } from '../lib/errors';

Expand Down Expand Up @@ -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 <mode:package-name,package-name...>',
'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).',
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/src/lib/builder/embedPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 39053db

Please sign in to comment.