Skip to content

Commit

Permalink
feat(webpack): add convert-to-inferred generator
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez committed Jun 21, 2024
1 parent efd0994 commit 9948f95
Show file tree
Hide file tree
Showing 17 changed files with 2,592 additions and 13 deletions.
8 changes: 8 additions & 0 deletions docs/generated/manifests/menus.json
Original file line number Diff line number Diff line change
Expand Up @@ -9883,6 +9883,14 @@
"children": [],
"isExternal": false,
"disableCollapsible": false
},
{
"id": "convert-to-inferred",
"path": "/nx-api/webpack/generators/convert-to-inferred",
"name": "convert-to-inferred",
"children": [],
"isExternal": false,
"disableCollapsible": false
}
],
"isExternal": false,
Expand Down
9 changes: 9 additions & 0 deletions docs/generated/manifests/nx-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -3233,6 +3233,15 @@
"originalFilePath": "/packages/webpack/src/generators/convert-config-to-webpack-plugin/schema.json",
"path": "/nx-api/webpack/generators/convert-config-to-webpack-plugin",
"type": "generator"
},
"/nx-api/webpack/generators/convert-to-inferred": {
"description": "Convert existing Webpack project(s) using `@nx/webpack:wepack` executor to use `@nx/webpack/plugin`.",
"file": "generated/packages/webpack/generators/convert-to-inferred.json",
"hidden": false,
"name": "convert-to-inferred",
"originalFilePath": "/packages/webpack/src/generators/convert-to-inferred/schema.json",
"path": "/nx-api/webpack/generators/convert-to-inferred",
"type": "generator"
}
},
"path": "/nx-api/webpack"
Expand Down
9 changes: 9 additions & 0 deletions docs/generated/packages-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -3198,6 +3198,15 @@
"originalFilePath": "/packages/webpack/src/generators/convert-config-to-webpack-plugin/schema.json",
"path": "webpack/generators/convert-config-to-webpack-plugin",
"type": "generator"
},
{
"description": "Convert existing Webpack project(s) using `@nx/webpack:wepack` executor to use `@nx/webpack/plugin`.",
"file": "generated/packages/webpack/generators/convert-to-inferred.json",
"hidden": false,
"name": "convert-to-inferred",
"originalFilePath": "/packages/webpack/src/generators/convert-to-inferred/schema.json",
"path": "webpack/generators/convert-to-inferred",
"type": "generator"
}
],
"githubRoot": "https://github.com/nrwl/nx/blob/master",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "convert-to-inferred",
"factory": "./src/generators/convert-to-inferred/convert-to-inferred#convertToInferred",
"schema": {
"$schema": "https://json-schema.org/schema",
"$id": "NxWebpackConvertToInferred",
"description": "Convert existing Webpack project(s) using `@nx/webpack:wepack` executor to use `@nx/webpack/plugin`.",
"title": "Convert a Webpack project from executor to plugin",
"type": "object",
"properties": {
"project": {
"type": "string",
"description": "The project to convert from using the `@nx/webpack:webpack` executor to use `@nx/webpack/plugin`. If not provided, all projects using the `@nx/webpack:webpack` executor will be converted.",
"x-priority": "important"
},
"skipFormat": {
"type": "boolean",
"description": "Whether to format files.",
"default": false
}
},
"presets": []
},
"description": "Convert existing Webpack project(s) using `@nx/webpack:wepack` executor to use `@nx/webpack/plugin`.",
"implementation": "/packages/webpack/src/generators/convert-to-inferred/convert-to-inferred#convertToInferred.ts",
"aliases": [],
"hidden": false,
"path": "/packages/webpack/src/generators/convert-to-inferred/schema.json",
"type": "generator"
}
1 change: 1 addition & 0 deletions docs/shared/reference/sitemap.md
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,7 @@
- [init](/nx-api/webpack/generators/init)
- [configuration](/nx-api/webpack/generators/configuration)
- [convert-config-to-webpack-plugin](/nx-api/webpack/generators/convert-config-to-webpack-plugin)
- [convert-to-inferred](/nx-api/webpack/generators/convert-to-inferred)
- [workspace](/nx-api/workspace)
- [documents](/nx-api/workspace/documents)
- [Overview](/nx-api/workspace/documents/overview)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export class AggregatedLog {
}

flushLogs(): void {
if (this.logs.size === 0) {
return;
}

let fullLog = '';
for (const executorName of this.logs.keys()) {
fullLog = `${fullLog}${output.bold(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ import {
ProjectConfigurationsError,
} from 'nx/src/devkit-internals';
import type { ConfigurationResult } from 'nx/src/project-graph/utils/project-configuration-utils';
import type { InputDefinition } from 'nx/src/config/workspace-json-project-json';
import type {
InputDefinition,
ProjectConfiguration,
} from 'nx/src/config/workspace-json-project-json';

type PluginOptionsBuilder<T> = (targetName: string) => T;
type PostTargetTransformer = (
Expand All @@ -38,7 +41,10 @@ type PostTargetTransformer = (
) => TargetConfiguration | Promise<TargetConfiguration>;
type SkipTargetFilter = (
targetConfiguration: TargetConfiguration
) => [boolean, string];
) => false | string;
type SkipProjectFilter = (
projectConfiguration: ProjectConfiguration
) => false | string;

class ExecutorToPluginMigrator<T> {
readonly tree: Tree;
Expand All @@ -48,6 +54,7 @@ class ExecutorToPluginMigrator<T> {
readonly #pluginOptionsBuilder: PluginOptionsBuilder<T>;
readonly #postTargetTransformer: PostTargetTransformer;
readonly #skipTargetFilter: SkipTargetFilter;
readonly #skipProjectFilter: SkipProjectFilter;
readonly #specificProjectToMigrate: string;
#nxJson: NxJsonConfiguration;
#targetDefaultsForExecutor: Partial<TargetConfiguration>;
Expand All @@ -56,6 +63,7 @@ class ExecutorToPluginMigrator<T> {
#createNodes?: CreateNodes<T>;
#createNodesV2?: CreateNodesV2<T>;
#createNodesResultsForTargets: Map<string, ConfigurationResult>;
#skippedProjects: Set<string>;

constructor(
tree: Tree,
Expand All @@ -67,7 +75,10 @@ class ExecutorToPluginMigrator<T> {
createNodes?: CreateNodes<T>,
createNodesV2?: CreateNodesV2<T>,
specificProjectToMigrate?: string,
skipTargetFilter?: SkipTargetFilter
filters?: {
skipProjectFilter?: SkipProjectFilter;
skipTargetFilter?: SkipTargetFilter;
}
) {
this.tree = tree;
this.#projectGraph = projectGraph;
Expand All @@ -78,7 +89,9 @@ class ExecutorToPluginMigrator<T> {
this.#createNodes = createNodes;
this.#createNodesV2 = createNodesV2;
this.#specificProjectToMigrate = specificProjectToMigrate;
this.#skipTargetFilter = skipTargetFilter ?? ((...args) => [false, '']);
this.#skipProjectFilter =
filters?.skipProjectFilter ?? ((...args) => false);
this.#skipTargetFilter = filters?.skipTargetFilter ?? ((...args) => false);
}

async run(): Promise<Map<string, Set<string>>> {
Expand All @@ -99,6 +112,7 @@ class ExecutorToPluginMigrator<T> {
this.#targetAndProjectsToMigrate = new Map();
this.#pluginToAddForTarget = new Map();
this.#createNodesResultsForTargets = new Map();
this.#skippedProjects = new Set();

this.#getTargetDefaultsForExecutor();
this.#getTargetAndProjectsToMigrate();
Expand Down Expand Up @@ -311,7 +325,7 @@ class ExecutorToPluginMigrator<T> {
this.tree,
this.#executor,
(targetConfiguration, projectName, targetName, configurationName) => {
if (configurationName) {
if (this.#skippedProjects.has(projectName) || configurationName) {
return;
}

Expand All @@ -322,10 +336,23 @@ class ExecutorToPluginMigrator<T> {
return;
}

const [skipTarget, reasonTargetWasSkipped] =
this.#skipTargetFilter(targetConfiguration);
if (skipTarget) {
const errorMsg = `${targetName} target on project "${projectName}" cannot be migrated. ${reasonTargetWasSkipped}`;
const skipProjectReason = this.#skipProjectFilter(
this.#projectGraph.nodes[projectName].data
);
if (skipProjectReason) {
this.#skippedProjects.add(projectName);
const errorMsg = `The "${projectName}" project cannot be migrated. ${skipProjectReason}`;
if (this.#specificProjectToMigrate) {
throw new Error(errorMsg);
}

console.warn(errorMsg);
return;
}

const skipTargetReason = this.#skipTargetFilter(targetConfiguration);
if (skipTargetReason) {
const errorMsg = `${targetName} target on project "${projectName}" cannot be migrated. ${skipTargetReason}`;
if (this.#specificProjectToMigrate) {
throw new Error(errorMsg);
} else {
Expand Down Expand Up @@ -416,7 +443,10 @@ export async function migrateExecutorToPlugin<T>(
postTargetTransformer: PostTargetTransformer,
createNodes: CreateNodesV2<T>,
specificProjectToMigrate?: string,
skipTargetFilter?: SkipTargetFilter
filters?: {
skipProjectFilter?: SkipProjectFilter;
skipTargetFilter?: SkipTargetFilter;
}
): Promise<Map<string, Set<string>>> {
const migrator = new ExecutorToPluginMigrator<T>(
tree,
Expand All @@ -428,7 +458,7 @@ export async function migrateExecutorToPlugin<T>(
undefined,
createNodes,
specificProjectToMigrate,
skipTargetFilter
filters
);
return await migrator.run();
}
Expand All @@ -442,7 +472,10 @@ export async function migrateExecutorToPluginV1<T>(
postTargetTransformer: PostTargetTransformer,
createNodes: CreateNodes<T>,
specificProjectToMigrate?: string,
skipTargetFilter?: SkipTargetFilter
filters?: {
skipProjectFilter?: SkipProjectFilter;
skipTargetFilter?: SkipTargetFilter;
}
): Promise<Map<string, Set<string>>> {
const migrator = new ExecutorToPluginMigrator<T>(
tree,
Expand All @@ -454,7 +487,7 @@ export async function migrateExecutorToPluginV1<T>(
createNodes,
undefined,
specificProjectToMigrate,
skipTargetFilter
filters
);
return await migrator.run();
}
Expand Down
5 changes: 5 additions & 0 deletions packages/webpack/generators.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
"factory": "./src/generators/convert-config-to-webpack-plugin/convert-config-to-webpack-plugin",
"schema": "./src/generators/convert-config-to-webpack-plugin/schema.json",
"description": "Convert the project to use the `NxAppWebpackPlugin` and `NxReactWebpackPlugin`."
},
"convert-to-inferred": {
"factory": "./src/generators/convert-to-inferred/convert-to-inferred#convertToInferred",
"schema": "./src/generators/convert-to-inferred/schema.json",
"description": "Convert existing Webpack project(s) using `@nx/webpack:wepack` executor to use `@nx/webpack/plugin`."
}
}
}
Loading

0 comments on commit 9948f95

Please sign in to comment.