Skip to content

Commit

Permalink
feat(nx-plugin): add "optional" flag to plugin configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
janschab committed May 2, 2024
1 parent 5ded713 commit 803c297
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/nx/src/config/nx-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ export type ExpandedPluginConfiguration<T = unknown> = {
options?: T;
include?: string[];
exclude?: string[];
optional?: boolean;
};

export function readNxJson(root: string = workspaceRoot): NxJsonConfiguration {
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/daemon/server/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function getPlugins() {
workspaceRoot
);
cleanup = cleanupFn;
return result;
return result.filter(Boolean);
}

export function cleanupPlugins() {
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/project-graph/plugins/internal-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export const nxPluginCache: Map<
export async function loadNxPlugins(
plugins: PluginConfiguration[],
root = workspaceRoot
): Promise<[LoadedNxPlugin[], () => void]> {
): Promise<[Array<LoadedNxPlugin | undefined>, () => void]> {
const result: Promise<LoadedNxPlugin>[] = [];

const loadingMethod =
Expand Down
7 changes: 5 additions & 2 deletions packages/nx/src/project-graph/plugins/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { extname } from 'node:path';
import { NxPlugin } from './public-api';
import { PluginConfiguration } from '../../config/nx-json';
import { retrieveProjectConfigurationsWithoutPluginInference } from '../utils/retrieve-workspace-files';
import { normalizeNxPlugin } from './utils';
import { isNxPluginOptional, normalizeNxPlugin } from './utils';
import { LoadedNxPlugin } from './internal-api';
import { LoadPluginError } from '../error-types';
import path = require('node:path/posix');
Expand Down Expand Up @@ -251,7 +251,7 @@ export async function loadNxPluginAsync(
pluginConfiguration: PluginConfiguration,
paths: string[],
root: string
): Promise<LoadedNxPlugin> {
): Promise<LoadedNxPlugin | undefined> {
const moduleName =
typeof pluginConfiguration === 'string'
? pluginConfiguration
Expand Down Expand Up @@ -282,6 +282,9 @@ export async function loadNxPluginAsync(
);
return new LoadedNxPlugin(plugin, pluginConfiguration);
} catch (e) {
if (isNxPluginOptional(pluginConfiguration)) {
return undefined;
}
throw new LoadPluginError(moduleName, e);
}
}
Expand Down
7 changes: 7 additions & 0 deletions packages/nx/src/project-graph/plugins/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { dirname } from 'node:path';

import { toProjectName } from '../../config/to-project-name';
import { combineGlobPatterns } from '../../utils/globs';
import { PluginConfiguration } from '../../config/nx-json';

import type { NxPluginV1 } from '../../utils/nx-plugin.deprecated';
import type {
Expand Down Expand Up @@ -100,3 +101,9 @@ export async function runCreateNodesInParallel(
}
return results;
}

export function isNxPluginOptional(pluginConfiguration: PluginConfiguration): boolean {
return typeof pluginConfiguration === 'string'
? false
: pluginConfiguration.optional;
}

0 comments on commit 803c297

Please sign in to comment.