Skip to content

Commit

Permalink
feat(cli): add flags to handle native deps
Browse files Browse the repository at this point in the history
This change introduces two new flags to the CLI to handle transitive
dependencies on packages containing native modules.
`--allow-native-package [package-name...]` to selectively allow native
packages when exporting a dynamic plugin and allowing it's installation
into the exported dynamic plugin and `--suppress-native-package
[package-name..]` which replaces the native package with an empty
package during export, preventing the native package's inclusion into
the exported dynamic plugin's private dependencies.

Signed-off-by: Stan Lewis <[email protected]>
  • Loading branch information
gashcrumb committed Nov 26, 2024
1 parent aa9b240 commit 8be7a10
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
9 changes: 9 additions & 0 deletions .changeset/long-pants-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@janus-idp/cli": patch
---

This change adds two new flags to handle native module dependencies.

- `--allow-native-package [package-name...]`: flag to selectively allow native packages when exporting a dynamic plugin and allowing it's installation into the exported dynamic plugin.

- `--suppress-native-package [package-name..]`: flag which replaces the native package with an empty package during export, preventing the native package's inclusion into the exported dynamic plugin's private dependencies.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export async function backend(opts: OptionValues): Promise<string> {
}

const packagesToEmbed = (opts.embedPackage || []) as string[];
const allowNative = (opts.allowNativePackage || []) as string[];
const suppressNative = (opts.suppressNativePackage || []) as string[];
const monoRepoPackages = await getPackages(paths.targetDir);
const embeddedResolvedPackages = await searchEmbedded(
pkg,
Expand Down Expand Up @@ -119,6 +121,11 @@ ${
}`,
);

if (suppressNative.length > 0) {
await fs.mkdirs(path.join(target, '.empty'));
await fs.writeFile(path.join(target, '.empty', 'package.json'), '{}');
}

const embeddedPeerDependencies: {
[key: string]: string;
} = {};
Expand Down Expand Up @@ -256,7 +263,12 @@ ${
// which are related to the packaging of the original static package.
scripts: {},
},
additionalResolutions: embeddedDependenciesResolutions,
additionalResolutions: {
...embeddedDependenciesResolutions,
...suppressNative
.map((nativePkg: string) => ({ [nativePkg]: 'file:./.empty' }))
.reduce((prev, curr) => ({ ...prev, ...curr }), {}),
},
after(mainPkg) {
if (Object.keys(embeddedPeerDependencies).length === 0) {
return;
Expand Down Expand Up @@ -407,9 +419,12 @@ ${

// Check whether private dependencies contain native modules, and fail for now (not supported).
const nativePackages: string[] = [];
for await (const n of gatherNativeModules(target)) {
nativePackages.push(n);
for await (const nativePkg of gatherNativeModules(target)) {
if (!allowNative.includes(nativePkg)) {
nativePackages.push(nativePkg);
}
}

if (nativePackages.length > 0) {
throw new Error(
`Dynamic plugins do not support native plugins. the following native modules have been transitively detected:${chalk.cyan(
Expand Down
8 changes: 8 additions & 0 deletions packages/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ export function registerScriptCommand(program: Command) {
},
{},
)
.option(
'--allow-native-package [package-name...]',
'Optional list of native packages names that can be included in the exported plugin',
)
.option(
'--suppress-native-package [package-name...]',
'Optional list of native package names to be excluded from the exported plugin',
)
.option(
'--no-install',
'Do not run `yarn install` to fill the dynamic plugin `node_modules` folder (backend plugin only).',
Expand Down

0 comments on commit 8be7a10

Please sign in to comment.