Skip to content

Commit

Permalink
fix(cli): do not change interop mode by default (#971)
Browse files Browse the repository at this point in the history
* fix(cli): do not change interop mode by default

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 <[email protected]>

* fix(cli): make it up-to-date with upstream

to support default exports as in upstream.

Signed-off-by: David Festal <[email protected]>

---------

Signed-off-by: David Festal <[email protected]>
  • Loading branch information
davidfestal authored Dec 7, 2023
1 parent 011a153 commit 4159977
Show file tree
Hide file tree
Showing 4 changed files with 51 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
1 change: 1 addition & 0 deletions packages/cli/src/lib/builder/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
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 4159977

Please sign in to comment.