Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): do not change interop mode by default #971

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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