Skip to content

Commit

Permalink
feat(core): add ability to scope plugins (#22379)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored Mar 22, 2024
1 parent 5a28158 commit 777fbd1
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docs/generated/devkit/PluginConfiguration.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Type alias: PluginConfiguration

Ƭ **PluginConfiguration**: `string` \| \{ `options?`: `unknown` ; `plugin`: `string` }
Ƭ **PluginConfiguration**: `string` \| \{ `exclude?`: `string`[] ; `include?`: `string`[] ; `options?`: `unknown` ; `plugin`: `string` }
38 changes: 32 additions & 6 deletions packages/nx/schemas/nx-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,9 @@
{
"type": "array",
"description": "The projects that the targets belong to.",
"items": { "type": "string" }
"items": {
"type": "string"
}
}
]
},
Expand All @@ -294,8 +296,12 @@
"required": ["input"],
"not": {
"anyOf": [
{ "required": ["projects"] },
{ "required": ["dependencies"] }
{
"required": ["projects"]
},
{
"required": ["dependencies"]
}
]
}
}
Expand Down Expand Up @@ -327,7 +333,9 @@
"properties": {
"externalDependencies": {
"type": "array",
"items": { "type": "string" },
"items": {
"type": "string"
},
"description": "The list of external dependencies that our target depends on for `nx:run-commands` and community plugins."
}
},
Expand Down Expand Up @@ -495,8 +503,12 @@
"required": ["target"],
"not": {
"anyOf": [
{ "required": ["projects"] },
{ "required": ["dependencies"] }
{
"required": ["projects"]
},
{
"required": ["dependencies"]
}
]
}
}
Expand Down Expand Up @@ -529,6 +541,20 @@
"options": {
"type": "object",
"description": "The options passed to the plugin when creating nodes and dependencies"
},
"include": {
"type": "array",
"description": "File patterns which are included by the plugin",
"items": {
"type": "string"
}
},
"exclude": {
"type": "array",
"description": "File patterns which are excluded by the plugin",
"items": {
"type": "string"
}
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion packages/nx/src/config/nx-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,12 @@ export interface NxJsonConfiguration<T = '*' | string[]> {

export type PluginConfiguration =
| string
| { plugin: string; options?: unknown };
| {
plugin: string;
options?: unknown;
include?: string[];
exclude?: string[];
};

export function readNxJson(root: string = workspaceRoot): NxJsonConfiguration {
const nxJson = join(root, 'nx.json');
Expand Down
29 changes: 25 additions & 4 deletions packages/nx/src/project-graph/utils/project-configuration-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ export function createProjectConfigurations(
const errors: Array<CreateNodesError | MergeNodesError> = [];

// We iterate over plugins first - this ensures that plugins specified first take precedence.
for (const { plugin, options } of plugins) {
for (const { plugin, options, include, exclude } of plugins) {
const [pattern, createNodes] = plugin.createNodes ?? [];
const pluginResults: Array<
CreateNodesResultWithContext | Promise<CreateNodesResultWithContext>
Expand All @@ -318,10 +318,31 @@ export function createProjectConfigurations(
continue;
}

const matchingConfigFiles: string[] = workspaceFiles.filter(
minimatch.filter(pattern, { dot: true })
);
const matchingConfigFiles: string[] = [];

for (const file of workspaceFiles) {
if (minimatch(file, pattern, { dot: true })) {
if (include) {
const included = include.some((includedPattern) =>
minimatch(file, includedPattern, { dot: true })
);
if (!included) {
continue;
}
}

if (exclude) {
const excluded = include.some((excludedPattern) =>
minimatch(file, excludedPattern, { dot: true })
);
if (excluded) {
continue;
}
}

matchingConfigFiles.push(file);
}
}
for (const file of matchingConfigFiles) {
performance.mark(`${plugin.name}:createNodes:${file} - start`);
try {
Expand Down
25 changes: 23 additions & 2 deletions packages/nx/src/utils/nx-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ export type NxPlugin = NxPluginV1 | NxPluginV2;
export type LoadedNxPlugin = {
plugin: NxPluginV2 & Pick<NxPluginV1, 'processProjectGraph'>;
options?: unknown;
include?: string[];
exclude?: string[];
};

// Short lived cache (cleared between cmd runs)
Expand Down Expand Up @@ -225,8 +227,22 @@ export async function loadNxPluginAsync(
? pluginConfiguration
: { plugin: pluginConfiguration, options: undefined };
let pluginModule = nxPluginCache.get(moduleName);

const include =
typeof pluginConfiguration === 'object'
? pluginConfiguration.include
: undefined;
const exclude =
typeof pluginConfiguration === 'object'
? pluginConfiguration.exclude
: undefined;
if (pluginModule) {
return { plugin: pluginModule, options };
return {
plugin: pluginModule,
options,
include,
exclude,
};
}
performance.mark(`Load Nx Plugin: ${moduleName} - start`);
let { pluginPath, name } = await getPluginPathAndName(
Expand All @@ -246,7 +262,12 @@ export async function loadNxPluginAsync(
`Load Nx Plugin: ${moduleName} - start`,
`Load Nx Plugin: ${moduleName} - end`
);
return { plugin, options };
return {
plugin,
options,
include,
exclude,
};
}

export async function loadNxPlugins(
Expand Down

0 comments on commit 777fbd1

Please sign in to comment.