@openrewrite/recipes-nodejs 0.60.0 → 0.62.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +70 -1
- package/dist/index.js.map +1 -1
- package/dist/migrate/config-object-support.d.ts +8 -0
- package/dist/migrate/config-object-support.d.ts.map +1 -0
- package/dist/migrate/config-object-support.js +46 -0
- package/dist/migrate/config-object-support.js.map +1 -0
- package/dist/migrate/cypress/cypress-13.d.ts +20 -0
- package/dist/migrate/cypress/cypress-13.d.ts.map +1 -0
- package/dist/migrate/cypress/cypress-13.js +108 -0
- package/dist/migrate/cypress/cypress-13.js.map +1 -0
- package/dist/migrate/cypress/cypress-14.d.ts +27 -0
- package/dist/migrate/cypress/cypress-14.d.ts.map +1 -0
- package/dist/migrate/cypress/cypress-14.js +203 -0
- package/dist/migrate/cypress/cypress-14.js.map +1 -0
- package/dist/migrate/cypress/cypress-15.d.ts +26 -0
- package/dist/migrate/cypress/cypress-15.d.ts.map +1 -0
- package/dist/migrate/cypress/cypress-15.js +151 -0
- package/dist/migrate/cypress/cypress-15.js.map +1 -0
- package/dist/migrate/cypress/cypress-support.d.ts +10 -0
- package/dist/migrate/cypress/cypress-support.d.ts.map +1 -0
- package/dist/migrate/cypress/cypress-support.js +115 -0
- package/dist/migrate/cypress/cypress-support.js.map +1 -0
- package/dist/migrate/express5/express-support.d.ts +6 -0
- package/dist/migrate/express5/express-support.d.ts.map +1 -0
- package/dist/migrate/express5/express-support.js +41 -0
- package/dist/migrate/express5/express-support.js.map +1 -0
- package/dist/migrate/express5/migrate-route-wildcards.d.ts +10 -0
- package/dist/migrate/express5/migrate-route-wildcards.d.ts.map +1 -0
- package/dist/migrate/express5/migrate-route-wildcards.js +146 -0
- package/dist/migrate/express5/migrate-route-wildcards.js.map +1 -0
- package/dist/migrate/express5/replace-removed-express-apis.d.ts +9 -0
- package/dist/migrate/express5/replace-removed-express-apis.d.ts.map +1 -0
- package/dist/migrate/express5/replace-removed-express-apis.js +105 -0
- package/dist/migrate/express5/replace-removed-express-apis.js.map +1 -0
- package/dist/migrate/fastify4/find-removed-fastify-4-options.d.ts +9 -0
- package/dist/migrate/fastify4/find-removed-fastify-4-options.d.ts.map +1 -0
- package/dist/migrate/fastify4/find-removed-fastify-4-options.js +60 -0
- package/dist/migrate/fastify4/find-removed-fastify-4-options.js.map +1 -0
- package/dist/migrate/fastify5/replace-removed-fastify-apis.d.ts +9 -0
- package/dist/migrate/fastify5/replace-removed-fastify-apis.d.ts.map +1 -0
- package/dist/migrate/fastify5/replace-removed-fastify-apis.js +107 -0
- package/dist/migrate/fastify5/replace-removed-fastify-apis.js.map +1 -0
- package/dist/migrate/handler-support.d.ts +10 -0
- package/dist/migrate/handler-support.d.ts.map +1 -0
- package/dist/migrate/handler-support.js +62 -0
- package/dist/migrate/handler-support.js.map +1 -0
- package/dist/migrate/storybook/migrate-storybook-9-addons.d.ts +8 -0
- package/dist/migrate/storybook/migrate-storybook-9-addons.d.ts.map +1 -0
- package/dist/migrate/storybook/migrate-storybook-9-addons.js +163 -0
- package/dist/migrate/storybook/migrate-storybook-9-addons.js.map +1 -0
- package/dist/migrate/storybook/migrate-storybook-9-imports.d.ts +14 -0
- package/dist/migrate/storybook/migrate-storybook-9-imports.d.ts.map +1 -0
- package/dist/migrate/storybook/migrate-storybook-9-imports.js +160 -0
- package/dist/migrate/storybook/migrate-storybook-9-imports.js.map +1 -0
- package/dist/migrate/storybook/storybook-10.d.ts +14 -0
- package/dist/migrate/storybook/storybook-10.d.ts.map +1 -0
- package/dist/migrate/storybook/storybook-10.js +242 -0
- package/dist/migrate/storybook/storybook-10.js.map +1 -0
- package/dist/migrate/storybook/storybook-7.d.ts +21 -0
- package/dist/migrate/storybook/storybook-7.d.ts.map +1 -0
- package/dist/migrate/storybook/storybook-7.js +217 -0
- package/dist/migrate/storybook/storybook-7.js.map +1 -0
- package/dist/migrate/storybook/storybook-9-packages.d.ts +5 -0
- package/dist/migrate/storybook/storybook-9-packages.d.ts.map +1 -0
- package/dist/migrate/storybook/storybook-9-packages.js +12 -0
- package/dist/migrate/storybook/storybook-9-packages.js.map +1 -0
- package/dist/migrate/vite/find-vite-changes.d.ts +42 -0
- package/dist/migrate/vite/find-vite-changes.d.ts.map +1 -0
- package/dist/migrate/vite/find-vite-changes.js +133 -0
- package/dist/migrate/vite/find-vite-changes.js.map +1 -0
- package/dist/migrate/vite/migrate-vite-config.d.ts +26 -0
- package/dist/migrate/vite/migrate-vite-config.d.ts.map +1 -0
- package/dist/migrate/vite/migrate-vite-config.js +165 -0
- package/dist/migrate/vite/migrate-vite-config.js.map +1 -0
- package/dist/migrate/vite/vite-config-support.d.ts +15 -0
- package/dist/migrate/vite/vite-config-support.d.ts.map +1 -0
- package/dist/migrate/vite/vite-config-support.js +78 -0
- package/dist/migrate/vite/vite-config-support.js.map +1 -0
- package/dist/migrate/vue/find-vue-3-removed-apis.d.ts +8 -0
- package/dist/migrate/vue/find-vue-3-removed-apis.d.ts.map +1 -0
- package/dist/migrate/vue/find-vue-3-removed-apis.js +90 -0
- package/dist/migrate/vue/find-vue-3-removed-apis.js.map +1 -0
- package/dist/migrate/vue/migrate-new-vue-to-create-app.d.ts +8 -0
- package/dist/migrate/vue/migrate-new-vue-to-create-app.d.ts.map +1 -0
- package/dist/migrate/vue/migrate-new-vue-to-create-app.js +167 -0
- package/dist/migrate/vue/migrate-new-vue-to-create-app.js.map +1 -0
- package/dist/migrate/vue/migrate-vue-router-catch-all-routes.d.ts +8 -0
- package/dist/migrate/vue/migrate-vue-router-catch-all-routes.d.ts.map +1 -0
- package/dist/migrate/vue/migrate-vue-router-catch-all-routes.js +65 -0
- package/dist/migrate/vue/migrate-vue-router-catch-all-routes.js.map +1 -0
- package/dist/migrate/vue/migrate-vue-router-constructor.d.ts +8 -0
- package/dist/migrate/vue/migrate-vue-router-constructor.d.ts.map +1 -0
- package/dist/migrate/vue/migrate-vue-router-constructor.js +144 -0
- package/dist/migrate/vue/migrate-vue-router-constructor.js.map +1 -0
- package/dist/migrate/vue/rename-vue-destroy-hooks.d.ts +8 -0
- package/dist/migrate/vue/rename-vue-destroy-hooks.d.ts.map +1 -0
- package/dist/migrate/vue/rename-vue-destroy-hooks.js +82 -0
- package/dist/migrate/vue/rename-vue-destroy-hooks.js.map +1 -0
- package/dist/migrate/vue/vue-support.d.ts +23 -0
- package/dist/migrate/vue/vue-support.d.ts.map +1 -0
- package/dist/migrate/vue/vue-support.js +97 -0
- package/dist/migrate/vue/vue-support.js.map +1 -0
- package/dist/migrate/webpack5/asset-modules.d.ts +10 -0
- package/dist/migrate/webpack5/asset-modules.d.ts.map +1 -0
- package/dist/migrate/webpack5/asset-modules.js +100 -0
- package/dist/migrate/webpack5/asset-modules.js.map +1 -0
- package/dist/migrate/webpack5/find-webpack-5-manual-migrations.d.ts +9 -0
- package/dist/migrate/webpack5/find-webpack-5-manual-migrations.d.ts.map +1 -0
- package/dist/migrate/webpack5/find-webpack-5-manual-migrations.js +107 -0
- package/dist/migrate/webpack5/find-webpack-5-manual-migrations.js.map +1 -0
- package/dist/migrate/webpack5/node-polyfills.d.ts +9 -0
- package/dist/migrate/webpack5/node-polyfills.d.ts.map +1 -0
- package/dist/migrate/webpack5/node-polyfills.js +85 -0
- package/dist/migrate/webpack5/node-polyfills.js.map +1 -0
- package/dist/migrate/webpack5/update-optimization-options.d.ts +9 -0
- package/dist/migrate/webpack5/update-optimization-options.d.ts.map +1 -0
- package/dist/migrate/webpack5/update-optimization-options.js +106 -0
- package/dist/migrate/webpack5/update-optimization-options.js.map +1 -0
- package/dist/migrate/webpack5/webpack-config-support.d.ts +13 -0
- package/dist/migrate/webpack5/webpack-config-support.d.ts.map +1 -0
- package/dist/migrate/webpack5/webpack-config-support.js +92 -0
- package/dist/migrate/webpack5/webpack-config-support.js.map +1 -0
- package/dist/resources/advisories-npm.csv +40 -26
- package/package.json +4 -3
- package/src/index.ts +69 -0
- package/src/migrate/config-object-support.ts +55 -0
- package/src/migrate/cypress/cypress-13.ts +70 -0
- package/src/migrate/cypress/cypress-14.ts +147 -0
- package/src/migrate/cypress/cypress-15.ts +98 -0
- package/src/migrate/cypress/cypress-support.ts +100 -0
- package/src/migrate/express5/express-support.ts +43 -0
- package/src/migrate/express5/migrate-route-wildcards.ts +111 -0
- package/src/migrate/express5/replace-removed-express-apis.ts +86 -0
- package/src/migrate/fastify4/find-removed-fastify-4-options.ts +39 -0
- package/src/migrate/fastify5/replace-removed-fastify-apis.ts +94 -0
- package/src/migrate/handler-support.ts +64 -0
- package/src/migrate/storybook/migrate-storybook-9-addons.ts +126 -0
- package/src/migrate/storybook/migrate-storybook-9-imports.ts +122 -0
- package/src/migrate/storybook/storybook-10.ts +180 -0
- package/src/migrate/storybook/storybook-7.ts +177 -0
- package/src/migrate/storybook/storybook-9-packages.ts +18 -0
- package/src/migrate/vite/find-vite-changes.ts +130 -0
- package/src/migrate/vite/migrate-vite-config.ts +123 -0
- package/src/migrate/vite/vite-config-support.ts +71 -0
- package/src/migrate/vue/find-vue-3-removed-apis.ts +63 -0
- package/src/migrate/vue/migrate-new-vue-to-create-app.ts +160 -0
- package/src/migrate/vue/migrate-vue-router-catch-all-routes.ts +50 -0
- package/src/migrate/vue/migrate-vue-router-constructor.ts +116 -0
- package/src/migrate/vue/rename-vue-destroy-hooks.ts +57 -0
- package/src/migrate/vue/vue-support.ts +115 -0
- package/src/migrate/webpack5/asset-modules.ts +77 -0
- package/src/migrate/webpack5/find-webpack-5-manual-migrations.ts +76 -0
- package/src/migrate/webpack5/node-polyfills.ts +73 -0
- package/src/migrate/webpack5/update-optimization-options.ts +84 -0
- package/src/migrate/webpack5/webpack-config-support.ts +98 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2026 the original author or authors.
|
|
3
|
+
*
|
|
4
|
+
* Moderne Proprietary. Only for use by Moderne customers under the terms of a commercial contract.
|
|
5
|
+
*/
|
|
6
|
+
import {ExecutionContext, foundSearchResult, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
7
|
+
import {JavaScriptVisitor, JS} from "@openrewrite/rewrite/javascript";
|
|
8
|
+
import {isIdentifier, J} from "@openrewrite/rewrite/java";
|
|
9
|
+
import {stringLiteral} from "../module-bindings";
|
|
10
|
+
import {ASSET_TYPES} from "./asset-modules";
|
|
11
|
+
import {isWebpackConfig, keyOf, objectOf, membersOf} from "./webpack-config-support";
|
|
12
|
+
|
|
13
|
+
/** The `node` options webpack 5 still supports; every other entry was a polyfill or mock of a core module. */
|
|
14
|
+
const STILL_VALID_NODE_OPTIONS = new Set(["global", "__filename", "__dirname"]);
|
|
15
|
+
|
|
16
|
+
const PLUGINS_FOLDED_INTO_OPTIMIZATION: Readonly<Record<string, string>> = {
|
|
17
|
+
NamedModulesPlugin: "`optimization.moduleIds: 'named'`",
|
|
18
|
+
NamedChunksPlugin: "`optimization.chunkIds: 'named'`",
|
|
19
|
+
HashedModuleIdsPlugin: "`optimization.moduleIds: 'deterministic'`"
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export class FindWebpack5ManualMigrations extends Recipe {
|
|
23
|
+
readonly name = "org.openrewrite.nodejs.migrate.webpack.FindWebpack5ManualMigrations";
|
|
24
|
+
readonly displayName: string = "Find webpack 4 configuration that webpack 5 needs migrated by hand";
|
|
25
|
+
readonly description: string = "Marks what the webpack 5 migration guide lists in a webpack configuration and the webpack 5 recipes do not change: " +
|
|
26
|
+
"`NamedModulesPlugin`, `NamedChunksPlugin` and `HashedModuleIdsPlugin` (now `optimization` options), `IgnorePlugin` called with a regular expression " +
|
|
27
|
+
"(now `{resourceRegExp}`), a `node` object with entries for core modules (now `resolve.fallback`, with a polyfill package where the module is needed; `node.global`, `node.__filename` and `node.__dirname` are still valid and not marked), " +
|
|
28
|
+
"and `file-loader`, `url-loader` and `raw-loader` rules that could not be converted to asset modules, such as those with options.";
|
|
29
|
+
readonly tags: string[] = ["webpack"];
|
|
30
|
+
|
|
31
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
32
|
+
return new class extends JavaScriptVisitor<ExecutionContext> {
|
|
33
|
+
private config = false;
|
|
34
|
+
|
|
35
|
+
protected async visitJsCompilationUnit(cu: JS.CompilationUnit, p: ExecutionContext): Promise<J | undefined> {
|
|
36
|
+
this.config = isWebpackConfig(cu);
|
|
37
|
+
return this.config ? super.visitJsCompilationUnit(cu, p) : cu;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
protected async visitNewClass(newClass: J.NewClass, p: ExecutionContext): Promise<J | undefined> {
|
|
41
|
+
const n = await super.visitNewClass(newClass, p) as J.NewClass;
|
|
42
|
+
const name = classNameOf(n);
|
|
43
|
+
const replacement = name && PLUGINS_FOLDED_INTO_OPTIMIZATION[name];
|
|
44
|
+
if (replacement) return foundSearchResult(n, `webpack 5 replaces \`${name}\` with ${replacement}.`);
|
|
45
|
+
const first = n.arguments?.elements[0]?.element;
|
|
46
|
+
if (name === "IgnorePlugin" && first?.kind === J.Kind.Literal && /^\/.*\/[a-z]*$/.test((first as J.Literal).valueSource ?? "")) {
|
|
47
|
+
return foundSearchResult(n, "webpack 5 takes an options object: `new IgnorePlugin({resourceRegExp: /regExp/})`.");
|
|
48
|
+
}
|
|
49
|
+
return n;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
protected async visitPropertyAssignment(assignment: JS.PropertyAssignment, p: ExecutionContext): Promise<J | undefined> {
|
|
53
|
+
const a = await super.visitPropertyAssignment(assignment, p) as JS.PropertyAssignment;
|
|
54
|
+
const polyfills = keyOf(a) === "node" ? objectOf(a.initializer) : undefined;
|
|
55
|
+
return polyfills && membersOf(polyfills).some(m => !STILL_VALID_NODE_OPTIONS.has(keyOf(m.element) ?? ""))
|
|
56
|
+
? foundSearchResult(a, "webpack 5 no longer polyfills Node.js core modules: use `resolve.fallback` (`false`, or a polyfill package such as `path-browserify`).")
|
|
57
|
+
: a;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
protected async visitLiteral(literal: J.Literal, p: ExecutionContext): Promise<J | undefined> {
|
|
61
|
+
const l = await super.visitLiteral(literal, p) as J.Literal;
|
|
62
|
+
const loader = stringLiteral(l);
|
|
63
|
+
return loader && ASSET_TYPES[loader]
|
|
64
|
+
? foundSearchResult(l, `webpack 5 asset modules replace \`${loader}\` (\`type: '${ASSET_TYPES[loader]}'\`); this rule has options or other loaders, so migrate it by hand.`)
|
|
65
|
+
: l;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function classNameOf(n: J.NewClass): string | undefined {
|
|
72
|
+
const clazz: any = n.class;
|
|
73
|
+
if (!clazz) return undefined;
|
|
74
|
+
if (isIdentifier(clazz)) return clazz.simpleName;
|
|
75
|
+
return clazz.kind === J.Kind.FieldAccess ? (clazz as J.FieldAccess).name.element.simpleName : undefined;
|
|
76
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2026 the original author or authors.
|
|
3
|
+
*
|
|
4
|
+
* Moderne Proprietary. Only for use by Moderne customers under the terms of a commercial contract.
|
|
5
|
+
*/
|
|
6
|
+
import {ExecutionContext, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
7
|
+
import {JavaScriptVisitor, JS} from "@openrewrite/rewrite/javascript";
|
|
8
|
+
import {J} from "@openrewrite/rewrite/java";
|
|
9
|
+
import {space} from "@openrewrite/rewrite/java";
|
|
10
|
+
import {stringLiteral} from "../module-bindings";
|
|
11
|
+
import {
|
|
12
|
+
booleanLike, hasOnlyNamedProperties, isWebpackConfig, keyOf, memberNamed, membersOf, objectOf, renameKey, withMembers
|
|
13
|
+
} from "./webpack-config-support";
|
|
14
|
+
|
|
15
|
+
/** Node.js core modules whose webpack 4 `node: {name: 'empty'}` setting is `resolve.fallback: {name: false}` in webpack 5. */
|
|
16
|
+
const CORE_MODULES = new Set(["fs", "net", "tls", "dns", "dgram", "child_process", "cluster", "module", "readline", "repl"]);
|
|
17
|
+
|
|
18
|
+
export class ReplaceWebpackNodeEmptyWithResolveFallback extends Recipe {
|
|
19
|
+
readonly name = "org.openrewrite.nodejs.migrate.webpack.ReplaceWebpackNodeEmptyWithResolveFallback";
|
|
20
|
+
readonly displayName: string = "Replace `node: {fs: 'empty'}` with `resolve.fallback`";
|
|
21
|
+
readonly description: string = "webpack 5 removes the automatic Node.js polyfills, so `node: {fs: 'empty'}` becomes `resolve: {fallback: {fs: false}}`. " +
|
|
22
|
+
"Applies to a `node` object in a webpack configuration whose every entry is `'empty'` for a Node.js core module that has no browser polyfill, " +
|
|
23
|
+
"when the configuration has no `resolve` of its own. Anything else in `node` is left for `FindWebpack5ManualMigrations`.";
|
|
24
|
+
readonly tags: string[] = ["webpack"];
|
|
25
|
+
|
|
26
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
27
|
+
return new class extends JavaScriptVisitor<ExecutionContext> {
|
|
28
|
+
private config = false;
|
|
29
|
+
|
|
30
|
+
protected async visitJsCompilationUnit(cu: JS.CompilationUnit, p: ExecutionContext): Promise<J | undefined> {
|
|
31
|
+
this.config = isWebpackConfig(cu);
|
|
32
|
+
return this.config ? super.visitJsCompilationUnit(cu, p) : cu;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
protected async visitNewClass(newClass: J.NewClass, p: ExecutionContext): Promise<J | undefined> {
|
|
36
|
+
const n = await super.visitNewClass(newClass, p) as J.NewClass;
|
|
37
|
+
const config = objectOf(n);
|
|
38
|
+
if (!config || !hasOnlyNamedProperties(config) || memberNamed(config, "resolve")) return n;
|
|
39
|
+
const node = memberNamed(config, "node");
|
|
40
|
+
const polyfills = objectOf(node?.initializer);
|
|
41
|
+
if (!node || !polyfills || !isAllEmpty(polyfills)) return n;
|
|
42
|
+
return withMembers(n, membersOf(n).map(m => m.element === node
|
|
43
|
+
? {...m, element: {...renameKey(node, "resolve"), initializer: fallbackOf(polyfills)}}
|
|
44
|
+
: m));
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function isAllEmpty(polyfills: J.NewClass): boolean {
|
|
51
|
+
return membersOf(polyfills).length > 0 && hasOnlyNamedProperties(polyfills) && membersOf(polyfills).every(m =>
|
|
52
|
+
CORE_MODULES.has(keyOf(m.element)!) && stringLiteral((m.element as JS.PropertyAssignment).initializer) === "empty");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* `{fallback: {fs: false, ...}}` from `{fs: 'empty', ...}`. When the entries are on their own lines the new
|
|
57
|
+
* level is indented one step further, using the step between the entries and the closing brace.
|
|
58
|
+
*/
|
|
59
|
+
function fallbackOf(polyfills: J.NewClass): J.NewClass {
|
|
60
|
+
const first = membersOf(polyfills)[0];
|
|
61
|
+
const entryIndent = first.element.prefix.whitespace;
|
|
62
|
+
const closeIndent = polyfills.body!.end.whitespace;
|
|
63
|
+
const multiline = entryIndent.includes("\n");
|
|
64
|
+
const step = multiline && entryIndent.startsWith(closeIndent) ? entryIndent.slice(closeIndent.length) : " ";
|
|
65
|
+
const falses = withMembers(polyfills, membersOf(polyfills).map(m => {
|
|
66
|
+
const property = m.element as JS.PropertyAssignment;
|
|
67
|
+
const prefix = multiline ? {...property.prefix, whitespace: property.prefix.whitespace + step} : property.prefix;
|
|
68
|
+
return {...m, element: {...property, prefix, initializer: booleanLike(property.initializer as J.Literal, false)}};
|
|
69
|
+
}));
|
|
70
|
+
const inner = {...falses, prefix: space(" "), body: {...falses.body!, end: multiline ? {...falses.body!.end, whitespace: entryIndent} : falses.body!.end}};
|
|
71
|
+
const fallback = {...(first.element as JS.PropertyAssignment), initializer: inner};
|
|
72
|
+
return withMembers(polyfills, [{...first, element: renameKey(fallback as JS.PropertyAssignment, "fallback")}]);
|
|
73
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2026 the original author or authors.
|
|
3
|
+
*
|
|
4
|
+
* Moderne Proprietary. Only for use by Moderne customers under the terms of a commercial contract.
|
|
5
|
+
*/
|
|
6
|
+
import {ExecutionContext, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
7
|
+
import {JavaScriptVisitor, JS} from "@openrewrite/rewrite/javascript";
|
|
8
|
+
import {J} from "@openrewrite/rewrite/java";
|
|
9
|
+
import {stringLiteral} from "../module-bindings";
|
|
10
|
+
import {
|
|
11
|
+
booleanLike, booleanOf, hasOnlyNamedProperties, isWebpackConfig, keyOf, memberNamed, membersOf, objectOf, renameKey,
|
|
12
|
+
stringLike, withMembers
|
|
13
|
+
} from "./webpack-config-support";
|
|
14
|
+
|
|
15
|
+
export class UpdateWebpackOptimizationOptions extends Recipe {
|
|
16
|
+
readonly name = "org.openrewrite.nodejs.migrate.webpack.UpdateWebpackOptimizationOptions";
|
|
17
|
+
readonly displayName: string = "Update outdated webpack `optimization` options";
|
|
18
|
+
readonly description: string = "Applies the `optimization` renames of the webpack 5 migration guide in a webpack configuration: " +
|
|
19
|
+
"`hashedModuleIds: true` and `moduleIds: 'hashed'` to `moduleIds: 'deterministic'`, `namedModules: true` to `moduleIds: 'named'`, " +
|
|
20
|
+
"`namedChunks: true` to `chunkIds: 'named'`, `noEmitOnErrors` to the inverted `emitOnErrors` and " +
|
|
21
|
+
"`splitChunks.cacheGroups.vendors` to `defaultVendors`. An option is left alone where the object already sets its new name.";
|
|
22
|
+
readonly tags: string[] = ["webpack"];
|
|
23
|
+
|
|
24
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
25
|
+
return new class extends JavaScriptVisitor<ExecutionContext> {
|
|
26
|
+
private config = false;
|
|
27
|
+
|
|
28
|
+
protected async visitJsCompilationUnit(cu: JS.CompilationUnit, p: ExecutionContext): Promise<J | undefined> {
|
|
29
|
+
this.config = isWebpackConfig(cu);
|
|
30
|
+
return this.config ? super.visitJsCompilationUnit(cu, p) : cu;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
protected async visitPropertyAssignment(assignment: JS.PropertyAssignment, p: ExecutionContext): Promise<J | undefined> {
|
|
34
|
+
const a = await super.visitPropertyAssignment(assignment, p) as JS.PropertyAssignment;
|
|
35
|
+
if (keyOf(a) !== "optimization") return a;
|
|
36
|
+
const optimization = objectOf(a.initializer);
|
|
37
|
+
const updated = optimization && updateOptimization(optimization);
|
|
38
|
+
return updated && updated !== optimization ? {...a, initializer: updated} as JS.PropertyAssignment : a;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function updateOptimization(optimization: J.NewClass): J.NewClass {
|
|
45
|
+
if (!hasOnlyNamedProperties(optimization)) return optimization;
|
|
46
|
+
const taken = new Set(membersOf(optimization).map(m => keyOf(m.element)!));
|
|
47
|
+
const statements = membersOf(optimization).map(m => {
|
|
48
|
+
const property = m.element as JS.PropertyAssignment;
|
|
49
|
+
const key = keyOf(property)!;
|
|
50
|
+
const value = property.initializer;
|
|
51
|
+
const set = (name: string, to: J.Literal) => {
|
|
52
|
+
taken.add(name);
|
|
53
|
+
return {...m, element: {...renameKey(property, name), initializer: to}};
|
|
54
|
+
};
|
|
55
|
+
const lit = (text: string) => stringLike(value as J.Literal, text);
|
|
56
|
+
if (key === "hashedModuleIds" && booleanOf(value) === true && !taken.has("moduleIds")) return set("moduleIds", lit("deterministic"));
|
|
57
|
+
if (key === "namedModules" && booleanOf(value) === true && !taken.has("moduleIds")) return set("moduleIds", lit("named"));
|
|
58
|
+
if (key === "namedChunks" && booleanOf(value) === true && !taken.has("chunkIds")) return set("chunkIds", lit("named"));
|
|
59
|
+
if (key === "noEmitOnErrors" && booleanOf(value) !== undefined && !taken.has("emitOnErrors")) {
|
|
60
|
+
const inverted = !booleanOf(value);
|
|
61
|
+
return set("emitOnErrors", booleanLike(value as J.Literal, inverted));
|
|
62
|
+
}
|
|
63
|
+
if (key === "moduleIds" && stringLiteral(value) === "hashed") return {...m, element: {...property, initializer: lit("deterministic")}};
|
|
64
|
+
if (key === "splitChunks") {
|
|
65
|
+
const splitChunks = objectOf(value);
|
|
66
|
+
const updated = splitChunks && renameVendors(splitChunks);
|
|
67
|
+
if (updated && updated !== splitChunks) return {...m, element: {...property, initializer: updated}};
|
|
68
|
+
}
|
|
69
|
+
return m;
|
|
70
|
+
});
|
|
71
|
+
return statements.some((m, i) => m !== membersOf(optimization)[i]) ? withMembers(optimization, statements) : optimization;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** `cacheGroups.vendors` under its webpack 5 name, unless `defaultVendors` is already there. */
|
|
75
|
+
function renameVendors(splitChunks: J.NewClass): J.NewClass {
|
|
76
|
+
const cacheGroups = objectOf(memberNamed(splitChunks, "cacheGroups")?.initializer);
|
|
77
|
+
if (!cacheGroups || !hasOnlyNamedProperties(cacheGroups) || !memberNamed(cacheGroups, "vendors") || memberNamed(cacheGroups, "defaultVendors")) {
|
|
78
|
+
return splitChunks;
|
|
79
|
+
}
|
|
80
|
+
const groups = withMembers(cacheGroups, membersOf(cacheGroups).map(m =>
|
|
81
|
+
keyOf(m.element) === "vendors" ? {...m, element: renameKey(m.element as JS.PropertyAssignment, "defaultVendors")} : m));
|
|
82
|
+
return withMembers(splitChunks, membersOf(splitChunks).map(m =>
|
|
83
|
+
keyOf(m.element) === "cacheGroups" ? {...m, element: {...(m.element as JS.PropertyAssignment), initializer: groups}} : m));
|
|
84
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2026 the original author or authors.
|
|
3
|
+
*
|
|
4
|
+
* Moderne Proprietary. Only for use by Moderne customers under the terms of a commercial contract.
|
|
5
|
+
*/
|
|
6
|
+
import {isIdentifier, J, Type} from "@openrewrite/rewrite/java";
|
|
7
|
+
import {JS, walk, withStringValue} from "@openrewrite/rewrite/javascript";
|
|
8
|
+
import {matchesConfigFile, memberNameOf} from "../config-object-support";
|
|
9
|
+
import {mentionsModule} from "../handler-support";
|
|
10
|
+
import {optionsLiteral} from "../options-literal";
|
|
11
|
+
|
|
12
|
+
export {memberNameOf as keyOf, renameKey} from "../config-object-support";
|
|
13
|
+
|
|
14
|
+
const CONFIG_FILE = /(^|[\\/])webpack[^\\/]*\.config\.[cm]?[jt]s$|(^|[\\/])webpack\.[^\\/]*\.[cm]?[jt]s$/;
|
|
15
|
+
const CONFIG_OPTIONS = new Set(["entry", "output"]);
|
|
16
|
+
|
|
17
|
+
const verdicts = new WeakMap<JS.CompilationUnit, boolean>();
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Whether the file is a webpack configuration: it is named like one, declares `module: {rules: [...]}`, or
|
|
21
|
+
* imports or requires `webpack` and either sets `entry` or `output` or exports an object literal. A file that only
|
|
22
|
+
* mentions `webpack`, such as a script that runs a compilation, is not one.
|
|
23
|
+
*/
|
|
24
|
+
export function isWebpackConfig(cu: JS.CompilationUnit): boolean {
|
|
25
|
+
if (matchesConfigFile(cu.sourcePath, CONFIG_FILE)) return true;
|
|
26
|
+
let verdict = verdicts.get(cu);
|
|
27
|
+
if (verdict === undefined) {
|
|
28
|
+
verdict = inspect(cu);
|
|
29
|
+
verdicts.set(cu, verdict);
|
|
30
|
+
}
|
|
31
|
+
return verdict;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function inspect(cu: JS.CompilationUnit): boolean {
|
|
35
|
+
let moduleRules = false;
|
|
36
|
+
let entryOrOutput = false;
|
|
37
|
+
let exportsObject = false;
|
|
38
|
+
walk(cu, (n: any) => {
|
|
39
|
+
if (n.kind === JS.Kind.PropertyAssignment) {
|
|
40
|
+
const name = memberNameOf(n);
|
|
41
|
+
if (name === "module") moduleRules ||= objectHas(n.initializer, "rules");
|
|
42
|
+
else if (name && CONFIG_OPTIONS.has(name)) entryOrOutput = true;
|
|
43
|
+
} else if (n.kind === JS.Kind.ExportAssignment) {
|
|
44
|
+
exportsObject ||= optionsLiteral(n.expression.element) !== undefined;
|
|
45
|
+
} else if (n.kind === J.Kind.Assignment && isModuleExports(n.variable)) {
|
|
46
|
+
exportsObject ||= optionsLiteral(n.assignment.element) !== undefined;
|
|
47
|
+
}
|
|
48
|
+
return !moduleRules;
|
|
49
|
+
});
|
|
50
|
+
return moduleRules || ((entryOrOutput || exportsObject) && mentionsModule(cu, m => m === "webpack"));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function isModuleExports(e: J): boolean {
|
|
54
|
+
if (e.kind !== J.Kind.FieldAccess) return false;
|
|
55
|
+
const f = e as J.FieldAccess;
|
|
56
|
+
return f.name.element.simpleName === "exports" && isIdentifier(f.target) && (f.target as J.Identifier).simpleName === "module";
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function objectHas(e: J | undefined, name: string): boolean {
|
|
60
|
+
const object = objectOf(e);
|
|
61
|
+
return object !== undefined && memberNamed(object, name) !== undefined;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function objectOf(e: J | undefined): J.NewClass | undefined {
|
|
65
|
+
return optionsLiteral(e);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function membersOf(object: J.NewClass): J.RightPadded<J>[] {
|
|
69
|
+
return object.body?.statements ?? [];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function memberNamed(object: J.NewClass, name: string): JS.PropertyAssignment | undefined {
|
|
73
|
+
return membersOf(object).map(m => m.element).find(m => memberNameOf(m) === name) as JS.PropertyAssignment | undefined;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Whether every member is a plain `key: value` property, so no spread or method can hide a name. */
|
|
77
|
+
export function hasOnlyNamedProperties(object: J.NewClass): boolean {
|
|
78
|
+
return membersOf(object).every(m => memberNameOf(m.element) !== undefined);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function withMembers(object: J.NewClass, statements: J.RightPadded<J>[]): J.NewClass {
|
|
82
|
+
return {...object, body: {...object.body!, statements}} as J.NewClass;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** A string literal with value `text`, in the quote style of `like` when that is a string, else single quotes. */
|
|
86
|
+
export function stringLike(like: J.Literal, text: string): J.Literal {
|
|
87
|
+
const base = typeof like.value === "string" ? like : {...like, valueSource: "''"};
|
|
88
|
+
return withStringValue({...base, type: Type.Primitive.String} as J.Literal, text);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** `like` as the boolean literal `value`. */
|
|
92
|
+
export function booleanLike(like: J.Literal, value: boolean): J.Literal {
|
|
93
|
+
return {...like, value, valueSource: String(value), type: Type.Primitive.Boolean, unicodeEscapes: undefined} as J.Literal;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function booleanOf(e: J | undefined): boolean | undefined {
|
|
97
|
+
return e?.kind === J.Kind.Literal && typeof (e as J.Literal).value === "boolean" ? (e as J.Literal).value as boolean : undefined;
|
|
98
|
+
}
|