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

feat(webpack): add convert-to-inferred generator #26621

Merged
merged 4 commits into from
Jun 25, 2024
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
8 changes: 8 additions & 0 deletions docs/generated/manifests/menus.json
Original file line number Diff line number Diff line change
Expand Up @@ -9891,6 +9891,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 @@ -3242,6 +3242,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 @@ -3207,6 +3207,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 @@ -712,6 +712,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 @@ -375,6 +402,7 @@ class ExecutorToPluginMigrator<T> {
return;
}

global.NX_GRAPH_CREATION = true;
for (const targetName of this.#targetAndProjectsToMigrate.keys()) {
const loadedPlugin = new LoadedNxPlugin(
{
Expand All @@ -398,12 +426,14 @@ class ExecutorToPluginMigrator<T> {
if (e instanceof ProjectConfigurationsError) {
projectConfigs = e.partialProjectConfigurationsResult;
} else {
global.NX_GRAPH_CREATION = false;
throw e;
}
}

this.#createNodesResultsForTargets.set(targetName, projectConfigs);
}
global.NX_GRAPH_CREATION = false;
}
}

Expand All @@ -416,7 +446,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 +461,7 @@ export async function migrateExecutorToPlugin<T>(
undefined,
createNodes,
specificProjectToMigrate,
skipTargetFilter
filters
);
return await migrator.run();
}
Expand All @@ -442,7 +475,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 +490,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