@openrewrite/recipes-react 0.13.0 → 0.15.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 +12 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +70 -17
- package/dist/index.js.map +1 -1
- package/dist/migration/cra-to-vite/add-vite-config.d.ts +9 -0
- package/dist/migration/cra-to-vite/add-vite-config.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/add-vite-config.js +49 -0
- package/dist/migration/cra-to-vite/add-vite-config.js.map +1 -0
- package/dist/migration/cra-to-vite/cra-blockers-table.d.ts +10 -0
- package/dist/migration/cra-to-vite/cra-blockers-table.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/cra-blockers-table.js +31 -0
- package/dist/migration/cra-to-vite/cra-blockers-table.js.map +1 -0
- package/dist/migration/cra-to-vite/cra-to-vite.d.ts +14 -0
- package/dist/migration/cra-to-vite/cra-to-vite.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/cra-to-vite.js +73 -0
- package/dist/migration/cra-to-vite/cra-to-vite.js.map +1 -0
- package/dist/migration/cra-to-vite/find-cra-config-blockers.d.ts +14 -0
- package/dist/migration/cra-to-vite/find-cra-config-blockers.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/find-cra-config-blockers.js +185 -0
- package/dist/migration/cra-to-vite/find-cra-config-blockers.js.map +1 -0
- package/dist/migration/cra-to-vite/find-cra-env-file-blockers.d.ts +10 -0
- package/dist/migration/cra-to-vite/find-cra-env-file-blockers.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/find-cra-env-file-blockers.js +67 -0
- package/dist/migration/cra-to-vite/find-cra-env-file-blockers.js.map +1 -0
- package/dist/migration/cra-to-vite/find-cra-source-blockers.d.ts +10 -0
- package/dist/migration/cra-to-vite/find-cra-source-blockers.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/find-cra-source-blockers.js +168 -0
- package/dist/migration/cra-to-vite/find-cra-source-blockers.js.map +1 -0
- package/dist/migration/cra-to-vite/find-cra-to-vite-blockers.d.ts +8 -0
- package/dist/migration/cra-to-vite/find-cra-to-vite-blockers.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/find-cra-to-vite-blockers.js +31 -0
- package/dist/migration/cra-to-vite/find-cra-to-vite-blockers.js.map +1 -0
- package/dist/migration/cra-to-vite/index.d.ts +16 -0
- package/dist/migration/cra-to-vite/index.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/index.js +35 -0
- package/dist/migration/cra-to-vite/index.js.map +1 -0
- package/dist/migration/cra-to-vite/move-public-index-html.d.ts +11 -0
- package/dist/migration/cra-to-vite/move-public-index-html.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/move-public-index-html.js +124 -0
- package/dist/migration/cra-to-vite/move-public-index-html.js.map +1 -0
- package/dist/migration/cra-to-vite/rename-process-env-react-app.d.ts +9 -0
- package/dist/migration/cra-to-vite/rename-process-env-react-app.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/rename-process-env-react-app.js +92 -0
- package/dist/migration/cra-to-vite/rename-process-env-react-app.js.map +1 -0
- package/dist/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.d.ts +9 -0
- package/dist/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.js +65 -0
- package/dist/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.js.map +1 -0
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.d.ts +14 -0
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.js +98 -0
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.js.map +1 -0
- package/dist/migration/cra-to-vite/scan-cra-projects.d.ts +21 -0
- package/dist/migration/cra-to-vite/scan-cra-projects.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/scan-cra-projects.js +87 -0
- package/dist/migration/cra-to-vite/scan-cra-projects.js.map +1 -0
- package/dist/migration/cra-to-vite/shared.d.ts +37 -0
- package/dist/migration/cra-to-vite/shared.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/shared.js +218 -0
- package/dist/migration/cra-to-vite/shared.js.map +1 -0
- package/dist/migration/cra-to-vite/update-react-app-env-types.d.ts +9 -0
- package/dist/migration/cra-to-vite/update-react-app-env-types.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/update-react-app-env-types.js +47 -0
- package/dist/migration/cra-to-vite/update-react-app-env-types.js.map +1 -0
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.d.ts +9 -0
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.js +67 -0
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.js.map +1 -0
- package/dist/migration/cra-to-vite/update-tsconfig-for-vite.d.ts +10 -0
- package/dist/migration/cra-to-vite/update-tsconfig-for-vite.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/update-tsconfig-for-vite.js +53 -0
- package/dist/migration/cra-to-vite/update-tsconfig-for-vite.js.map +1 -0
- package/dist/react17/migrate-deprecated-lifecycles.d.ts +8 -0
- package/dist/react17/migrate-deprecated-lifecycles.d.ts.map +1 -0
- package/dist/react17/migrate-deprecated-lifecycles.js +258 -0
- package/dist/react17/migrate-deprecated-lifecycles.js.map +1 -0
- package/dist/react17/use-new-jsx-transform.d.ts +8 -0
- package/dist/react17/use-new-jsx-transform.d.ts.map +1 -0
- package/dist/react17/use-new-jsx-transform.js +169 -0
- package/dist/react17/use-new-jsx-transform.js.map +1 -0
- package/dist/react18/add-children-to-props.d.ts +8 -0
- package/dist/react18/add-children-to-props.d.ts.map +1 -0
- package/dist/react18/add-children-to-props.js +277 -0
- package/dist/react18/add-children-to-props.js.map +1 -0
- package/dist/react18/replace-reactdom-render.d.ts.map +1 -1
- package/dist/react18/replace-reactdom-render.js +109 -12
- package/dist/react18/replace-reactdom-render.js.map +1 -1
- package/dist/refactoring/class-to-functional.d.ts.map +1 -1
- package/dist/refactoring/class-to-functional.js +81 -7
- package/dist/refactoring/class-to-functional.js.map +1 -1
- package/dist/refactoring/fix-async-effect.d.ts +8 -0
- package/dist/refactoring/fix-async-effect.d.ts.map +1 -0
- package/dist/refactoring/fix-async-effect.js +71 -0
- package/dist/refactoring/fix-async-effect.js.map +1 -0
- package/dist/refactoring/prop-types-to-typescript.d.ts +8 -0
- package/dist/refactoring/prop-types-to-typescript.d.ts.map +1 -0
- package/dist/refactoring/prop-types-to-typescript.js +342 -0
- package/dist/refactoring/prop-types-to-typescript.js.map +1 -0
- package/dist/search/find-deprecated-lifecycles.d.ts +12 -0
- package/dist/search/find-deprecated-lifecycles.d.ts.map +1 -0
- package/dist/search/find-deprecated-lifecycles.js +86 -0
- package/dist/search/find-deprecated-lifecycles.js.map +1 -0
- package/dist/search/find-effects-without-cleanup.d.ts +9 -0
- package/dist/search/find-effects-without-cleanup.d.ts.map +1 -0
- package/dist/search/find-effects-without-cleanup.js +133 -0
- package/dist/search/find-effects-without-cleanup.js.map +1 -0
- package/dist/search/find-enzyme-usage.d.ts +9 -0
- package/dist/search/find-enzyme-usage.d.ts.map +1 -0
- package/dist/search/find-enzyme-usage.js +155 -0
- package/dist/search/find-enzyme-usage.js.map +1 -0
- package/dist/search/find-function-component-default-props.d.ts +11 -0
- package/dist/search/find-function-component-default-props.d.ts.map +1 -0
- package/dist/search/find-function-component-default-props.js +96 -0
- package/dist/search/find-function-component-default-props.js.map +1 -0
- package/dist/search/find-legacy-react-apis.d.ts +8 -0
- package/dist/search/find-legacy-react-apis.d.ts.map +1 -0
- package/dist/search/find-legacy-react-apis.js +41 -0
- package/dist/search/find-legacy-react-apis.js.map +1 -0
- package/dist/search/find-missing-use-client-directive.d.ts +9 -0
- package/dist/search/find-missing-use-client-directive.d.ts.map +1 -0
- package/dist/search/find-missing-use-client-directive.js +127 -0
- package/dist/search/find-missing-use-client-directive.js.map +1 -0
- package/dist/search/find-reactdom-root-apis.d.ts +14 -0
- package/dist/search/find-reactdom-root-apis.d.ts.map +1 -0
- package/dist/search/find-reactdom-root-apis.js +122 -0
- package/dist/search/find-reactdom-root-apis.js.map +1 -0
- package/dist/testing/enzyme-to-react-testing-library.d.ts +36 -0
- package/dist/testing/enzyme-to-react-testing-library.d.ts.map +1 -0
- package/dist/testing/enzyme-to-react-testing-library.js +547 -0
- package/dist/testing/enzyme-to-react-testing-library.js.map +1 -0
- package/dist/testing/enzyme-usage.d.ts +22 -0
- package/dist/testing/enzyme-usage.d.ts.map +1 -0
- package/dist/testing/enzyme-usage.js +107 -0
- package/dist/testing/enzyme-usage.js.map +1 -0
- package/dist/testing/enzyme-wrapper-scope.d.ts +42 -0
- package/dist/testing/enzyme-wrapper-scope.d.ts.map +1 -0
- package/dist/testing/enzyme-wrapper-scope.js +422 -0
- package/dist/testing/enzyme-wrapper-scope.js.map +1 -0
- package/dist/util/react-class.d.ts +6 -0
- package/dist/util/react-class.d.ts.map +1 -0
- package/dist/util/react-class.js +41 -0
- package/dist/util/react-class.js.map +1 -0
- package/dist/util/tree-search.d.ts +6 -0
- package/dist/util/tree-search.d.ts.map +1 -0
- package/dist/util/tree-search.js +51 -0
- package/dist/util/tree-search.js.map +1 -0
- package/package.json +7 -4
- package/src/index.ts +53 -12
- package/src/migration/cra-to-vite/add-vite-config.ts +47 -0
- package/src/migration/cra-to-vite/cra-blockers-table.ts +27 -0
- package/src/migration/cra-to-vite/cra-to-vite.ts +76 -0
- package/src/migration/cra-to-vite/find-cra-config-blockers.ts +179 -0
- package/src/migration/cra-to-vite/find-cra-env-file-blockers.ts +60 -0
- package/src/migration/cra-to-vite/find-cra-source-blockers.ts +138 -0
- package/src/migration/cra-to-vite/find-cra-to-vite-blockers.ts +21 -0
- package/src/migration/cra-to-vite/index.ts +26 -0
- package/src/migration/cra-to-vite/move-public-index-html.ts +132 -0
- package/src/migration/cra-to-vite/rename-process-env-react-app.ts +93 -0
- package/src/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.ts +61 -0
- package/src/migration/cra-to-vite/replace-react-scripts-dependency.ts +96 -0
- package/src/migration/cra-to-vite/scan-cra-projects.ts +86 -0
- package/src/migration/cra-to-vite/shared.ts +244 -0
- package/src/migration/cra-to-vite/update-react-app-env-types.ts +40 -0
- package/src/migration/cra-to-vite/update-react-scripts-scripts.ts +69 -0
- package/src/migration/cra-to-vite/update-tsconfig-for-vite.ts +47 -0
- package/src/react17/migrate-deprecated-lifecycles.ts +265 -0
- package/src/react17/use-new-jsx-transform.ts +150 -0
- package/src/react18/add-children-to-props.ts +235 -0
- package/src/react18/replace-reactdom-render.ts +109 -20
- package/src/refactoring/class-to-functional.ts +101 -13
- package/src/refactoring/fix-async-effect.ts +88 -0
- package/src/refactoring/prop-types-to-typescript.ts +341 -0
- package/src/search/find-deprecated-lifecycles.ts +64 -0
- package/src/search/find-effects-without-cleanup.ts +109 -0
- package/src/search/find-enzyme-usage.ts +117 -0
- package/src/search/find-function-component-default-props.ts +68 -0
- package/src/search/find-legacy-react-apis.ts +41 -0
- package/src/search/find-missing-use-client-directive.ts +99 -0
- package/src/search/find-reactdom-root-apis.ts +99 -0
- package/src/testing/enzyme-to-react-testing-library.ts +553 -0
- package/src/testing/enzyme-usage.ts +94 -0
- package/src/testing/enzyme-wrapper-scope.ts +449 -0
- package/src/util/react-class.ts +34 -0
- package/src/util/tree-search.ts +32 -0
- package/dist/react16/upgrade-to-react-16.d.ts +0 -12
- package/dist/react16/upgrade-to-react-16.d.ts.map +0 -1
- package/dist/react16/upgrade-to-react-16.js +0 -66
- package/dist/react16/upgrade-to-react-16.js.map +0 -1
- package/dist/react17/upgrade-to-react-17.d.ts +0 -12
- package/dist/react17/upgrade-to-react-17.d.ts.map +0 -1
- package/dist/react17/upgrade-to-react-17.js +0 -62
- package/dist/react17/upgrade-to-react-17.js.map +0 -1
- package/dist/react18/upgrade-to-react-18.d.ts +0 -12
- package/dist/react18/upgrade-to-react-18.d.ts.map +0 -1
- package/dist/react18/upgrade-to-react-18.js +0 -64
- package/dist/react18/upgrade-to-react-18.js.map +0 -1
- package/dist/react19/upgrade-to-react-19.d.ts +0 -8
- package/dist/react19/upgrade-to-react-19.d.ts.map +0 -1
- package/dist/react19/upgrade-to-react-19.js +0 -68
- package/dist/react19/upgrade-to-react-19.js.map +0 -1
- package/src/react16/upgrade-to-react-16.ts +0 -59
- package/src/react17/upgrade-to-react-17.ts +0 -52
- package/src/react18/upgrade-to-react-18.ts +0 -55
- package/src/react19/upgrade-to-react-19.ts +0 -75
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import {ExecutionContext, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
+
import {PlainText, PlainTextVisitor} from "@openrewrite/rewrite/text";
|
|
3
|
+
import {craToViteBlockersTable} from "./cra-blockers-table";
|
|
4
|
+
import {CraProjects, CraScanningRecipe} from "./scan-cra-projects";
|
|
5
|
+
import {CRA_ENV_PREFIX, isEnvFile, VITE_ENV_PREFIX} from "./shared";
|
|
6
|
+
|
|
7
|
+
const ASSIGNMENT = new RegExp(`^(\\s*(?:export\\s+)?)${CRA_ENV_PREFIX}([A-Za-z0-9_]+)(\\s*=)`);
|
|
8
|
+
const EXPANSION = new RegExp(`(\\$\\{?)${CRA_ENV_PREFIX}([A-Za-z0-9_]+)`, "g");
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Renames `REACT_APP_*` variables to `VITE_*` in `.env` files, including `${REACT_APP_*}` expansions.
|
|
12
|
+
*
|
|
13
|
+
* ```
|
|
14
|
+
* REACT_APP_API_URL=https://example.com -> VITE_API_URL=https://example.com
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* A variable is left alone and recorded in the blockers data table when the same file already defines the
|
|
18
|
+
* `VITE_` name, so no value is overwritten.
|
|
19
|
+
* Only `.env` files of Create React App packages change. Vite only reads `.env` files in the project root, and they are not moved.
|
|
20
|
+
*
|
|
21
|
+
* @see https://vite.dev/guide/env-and-mode
|
|
22
|
+
*/
|
|
23
|
+
export class RenameReactAppEnvVarsInEnvFiles extends CraScanningRecipe {
|
|
24
|
+
readonly name = "org.openrewrite.react.cra-to-vite.rename-env-file-variables";
|
|
25
|
+
readonly displayName = "Rename `REACT_APP_` variables in `.env` files to `VITE_`";
|
|
26
|
+
readonly description = "Renames `REACT_APP_*` variables in `.env` files to the `VITE_*` prefix that Vite exposes to client code, including `${REACT_APP_*}` expansions. Variables whose `VITE_` name is already defined in the same file are left unchanged and recorded in a data table.";
|
|
27
|
+
|
|
28
|
+
async editorWithData(acc: CraProjects): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
29
|
+
const table = craToViteBlockersTable();
|
|
30
|
+
return new class extends PlainTextVisitor<ExecutionContext> {
|
|
31
|
+
protected override async visitText(text: PlainText, ctx: ExecutionContext): Promise<PlainText | undefined> {
|
|
32
|
+
if (!isEnvFile(text.sourcePath) || !acc.isViteFile(text.sourcePath)) return text;
|
|
33
|
+
// Without the prefix there is nothing to rename or to collide with.
|
|
34
|
+
if (!text.text.includes(CRA_ENV_PREFIX)) return text;
|
|
35
|
+
const lines = text.text.split("\n");
|
|
36
|
+
const taken = new Set<string>();
|
|
37
|
+
for (const line of lines) {
|
|
38
|
+
const m = line.match(/^\s*(?:export\s+)?(VITE_[A-Za-z0-9_]+)\s*=/);
|
|
39
|
+
if (m) taken.add(m[1]);
|
|
40
|
+
}
|
|
41
|
+
const reported = new Set<string>();
|
|
42
|
+
const renamable = (name: string) => {
|
|
43
|
+
if (!taken.has(VITE_ENV_PREFIX + name)) return true;
|
|
44
|
+
if (!reported.has(name)) table.insertRow(ctx, {
|
|
45
|
+
filePath: text.sourcePath, category: "env-collision", detail: `${CRA_ENV_PREFIX}${name} was not renamed`,
|
|
46
|
+
nextStep: `${VITE_ENV_PREFIX}${name} is already defined in this file; merge the two by hand.`
|
|
47
|
+
});
|
|
48
|
+
reported.add(name);
|
|
49
|
+
return false;
|
|
50
|
+
};
|
|
51
|
+
const updated = lines.map(line =>
|
|
52
|
+
line.replace(ASSIGNMENT, (whole, lead, name, eq) =>
|
|
53
|
+
renamable(name) ? `${lead}${VITE_ENV_PREFIX}${name}${eq}` : whole)
|
|
54
|
+
.replace(EXPANSION, (whole, lead, name) =>
|
|
55
|
+
renamable(name) ? `${lead}${VITE_ENV_PREFIX}${name}` : whole)
|
|
56
|
+
).join("\n");
|
|
57
|
+
return updated === text.text ? text : {...text, text: updated};
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import {emptyMarkers, ExecutionContext, Option, randomId, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
+
import {emptySpace, Json, JsonVisitor} from "@openrewrite/rewrite/json";
|
|
3
|
+
import {
|
|
4
|
+
getMember, hasDependency, insertMember, nestedLayout, runsOnVite, isPackageJson, memberObject, newMember,
|
|
5
|
+
newStringMember, removeMember, replaceMember, rootObject, scriptsKeepReactScripts,
|
|
6
|
+
REACT_SCRIPTS, VITE, VITE_PLUGIN_REACT
|
|
7
|
+
} from "./shared";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Adds `vite` and `@vitejs/plugin-react` to `devDependencies` of a package that depends on `react-scripts`,
|
|
11
|
+
* and removes `react-scripts` once no script invokes it any more.
|
|
12
|
+
*
|
|
13
|
+
* ```json
|
|
14
|
+
* "dependencies": {"react-scripts": "5.0.1"} -> "devDependencies": {"@vitejs/plugin-react": "^5.2.0", "vite": "^7.0.0"}
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* The default versions are a peer-consistent pair that both need Node 20.19 or later. `react-scripts` stays
|
|
18
|
+
* while a script such as `test` still runs it, because it provides Jest. The `eject` script does not count, and
|
|
19
|
+
* `UpdateReactScriptsScripts` removes it. A package that `craco`, `react-app-rewired` or a wrapped `start` or
|
|
20
|
+
* `build` script still depends on `react-scripts` is left to the finders.
|
|
21
|
+
*
|
|
22
|
+
* @see https://react.dev/blog/2025/02/14/sunsetting-create-react-app#how-to-migrate-to-a-build-tool
|
|
23
|
+
*/
|
|
24
|
+
export class ReplaceReactScriptsDependency extends Recipe {
|
|
25
|
+
readonly name = "org.openrewrite.react.cra-to-vite.replace-react-scripts-dependency";
|
|
26
|
+
readonly displayName = "Replace `react-scripts` dependency with Vite";
|
|
27
|
+
readonly description = "Adds `vite` and `@vitejs/plugin-react` as dev dependencies of projects that depend on `react-scripts`, and removes `react-scripts` unless a script other than `eject` still invokes it (for example `react-scripts test`, which the stock Create React App template keeps, so the dependency usually stays until the tests move to Vitest). Packages that use `craco`, `react-app-rewired` or wrapped `start` or `build` scripts are left unchanged. The lock file is not regenerated; run your package manager afterwards.";
|
|
28
|
+
|
|
29
|
+
@Option({
|
|
30
|
+
displayName: "Vite version",
|
|
31
|
+
description: "The version range of `vite` to add. Vite 7 needs Node 20.19 or later.",
|
|
32
|
+
example: "^7.0.0",
|
|
33
|
+
required: false
|
|
34
|
+
})
|
|
35
|
+
viteVersion?: string;
|
|
36
|
+
|
|
37
|
+
@Option({
|
|
38
|
+
displayName: "Vite React plugin version",
|
|
39
|
+
description: "The version range of `@vitejs/plugin-react` to add. It must accept the `vite` range above.",
|
|
40
|
+
example: "^5.2.0",
|
|
41
|
+
required: false
|
|
42
|
+
})
|
|
43
|
+
pluginReactVersion?: string;
|
|
44
|
+
|
|
45
|
+
constructor(options?: { viteVersion?: string; pluginReactVersion?: string }) {
|
|
46
|
+
super(options);
|
|
47
|
+
this.viteVersion ??= "^7.0.0";
|
|
48
|
+
this.pluginReactVersion ??= "^5.2.0";
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
52
|
+
const viteVersion = this.viteVersion!;
|
|
53
|
+
const pluginVersion = this.pluginReactVersion!;
|
|
54
|
+
return new class extends JsonVisitor<ExecutionContext> {
|
|
55
|
+
protected override async visitDocument(doc: Json.Document): Promise<Json | undefined> {
|
|
56
|
+
const pkg = rootObject(doc);
|
|
57
|
+
if (!pkg || !isPackageJson(doc.sourcePath) || !runsOnVite(pkg)) return doc;
|
|
58
|
+
|
|
59
|
+
const additions: [string, string][] = [];
|
|
60
|
+
if (!hasDependency(pkg, VITE)) additions.push([VITE, viteVersion]);
|
|
61
|
+
if (!hasDependency(pkg, VITE_PLUGIN_REACT)) additions.push([VITE_PLUGIN_REACT, pluginVersion]);
|
|
62
|
+
|
|
63
|
+
let updated = pkg;
|
|
64
|
+
if (additions.length > 0) updated = addToDevDependencies(updated, additions);
|
|
65
|
+
if (!scriptsKeepReactScripts(pkg)) updated = removeReactScripts(updated);
|
|
66
|
+
return updated === pkg ? doc : {...doc, value: updated} as Json.Document;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function addToDevDependencies(pkg: Json.Object, additions: [string, string][]): Json.Object {
|
|
73
|
+
const dev = memberObject(pkg, "devDependencies");
|
|
74
|
+
if (dev) {
|
|
75
|
+
return replaceMember(pkg, "devDependencies", m => ({
|
|
76
|
+
...m,
|
|
77
|
+
value: additions.reduce((obj, [name, version]) =>
|
|
78
|
+
insertMember(obj, newStringMember(name, version, emptySpace), nestedLayout(pkg)), dev)
|
|
79
|
+
}));
|
|
80
|
+
}
|
|
81
|
+
const layout = nestedLayout(pkg);
|
|
82
|
+
let block: Json.Object = {kind: Json.Kind.Object, id: randomId(), prefix: emptySpace, markers: emptyMarkers, members: []};
|
|
83
|
+
for (const [name, version] of additions) block = insertMember(block, newStringMember(name, version, emptySpace), layout);
|
|
84
|
+
return insertMember(pkg, newMember("devDependencies", block, pkg.members[0]?.element.prefix ?? emptySpace));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function removeReactScripts(pkg: Json.Object): Json.Object {
|
|
88
|
+
let result = pkg;
|
|
89
|
+
for (const section of ["dependencies", "devDependencies"]) {
|
|
90
|
+
const obj = memberObject(result, section);
|
|
91
|
+
if (obj && getMember(obj, REACT_SCRIPTS)) {
|
|
92
|
+
result = replaceMember(result, section, m => ({...m, value: removeMember(obj, REACT_SCRIPTS)}));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import {ExecutionContext, ScanningRecipe, Tree, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
+
import {Json} from "@openrewrite/rewrite/json";
|
|
3
|
+
import {PlainText} from "@openrewrite/rewrite/text";
|
|
4
|
+
import {baseName, dirName, isCraPackage, runsOnVite, nearestRoot, isPackageJson, joinPath, rootObject} from "./shared";
|
|
5
|
+
|
|
6
|
+
/** What the recipes that add or move files need to know about the whole repository before they edit it. */
|
|
7
|
+
export class CraProjects {
|
|
8
|
+
/** Directories of the `package.json` files that Create React App built; `''` is the repository root. */
|
|
9
|
+
readonly roots = new Set<string>();
|
|
10
|
+
/** The subset of `roots` whose `start` and `build` will run on Vite, the only ones the edit recipes change. */
|
|
11
|
+
readonly viteRoots = new Set<string>();
|
|
12
|
+
/** Directories of every `package.json`, CRA or not, to tell which package a file belongs to. */
|
|
13
|
+
readonly packageDirs = new Set<string>();
|
|
14
|
+
/** Every source path in the repository, to tell whether a file already exists. */
|
|
15
|
+
readonly paths = new Set<string>();
|
|
16
|
+
/** The `public/index.html` text of each directory. */
|
|
17
|
+
readonly indexHtml = new Map<string, string>();
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* True when the file belongs to a Create React App package: the nearest `package.json` above it, not any
|
|
21
|
+
* package higher up, depends on `react-scripts`. A Node server in `server/` of a CRA repository does not.
|
|
22
|
+
*/
|
|
23
|
+
isCraFile(sourcePath: string): boolean {
|
|
24
|
+
const owner = nearestRoot(this.packageDirs, sourcePath);
|
|
25
|
+
return owner !== undefined && this.roots.has(owner);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** True when the file belongs to a package whose `start` and `build` will run on Vite, so the edit recipes may change it. */
|
|
29
|
+
isViteFile(sourcePath: string): boolean {
|
|
30
|
+
const owner = nearestRoot(this.packageDirs, sourcePath);
|
|
31
|
+
return owner !== undefined && this.viteRoots.has(owner);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** True when the file is client code that CRA bundled: below the `src/` of a package that runs on Vite. */
|
|
35
|
+
isViteClientSource(sourcePath: string): boolean {
|
|
36
|
+
const owner = nearestRoot(this.packageDirs, sourcePath);
|
|
37
|
+
return owner !== undefined && this.viteRoots.has(owner) && sourcePath.startsWith(joinPath(owner, "src/"));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
has(root: string, name: string): boolean {
|
|
41
|
+
return this.paths.has(joinPath(root, name));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The first of `names` that exists below `root`. */
|
|
45
|
+
firstExisting(root: string, names: string[]): string | undefined {
|
|
46
|
+
return names.find(name => this.has(root, name));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The package root a `public/index.html` belongs to, or `undefined` when the path is not one. */
|
|
51
|
+
export function indexHtmlRoot(sourcePath: string): string | undefined {
|
|
52
|
+
return baseName(sourcePath) === "index.html" && baseName(dirName(sourcePath)) === "public" ?
|
|
53
|
+
dirName(dirName(sourcePath)) : undefined;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export const VITE_CONFIG_NAMES = ["vite.config.ts", "vite.config.js", "vite.config.mts", "vite.config.mjs", "vite.config.cts", "vite.config.cjs"];
|
|
57
|
+
|
|
58
|
+
export function scanCraProjects(acc: CraProjects): TreeVisitor<any, ExecutionContext> {
|
|
59
|
+
return new class extends TreeVisitor<Tree, ExecutionContext> {
|
|
60
|
+
override async visit<R extends Tree>(tree: Tree): Promise<R | undefined> {
|
|
61
|
+
const sourcePath = (tree as { sourcePath?: string }).sourcePath;
|
|
62
|
+
if (sourcePath === undefined) return tree as R;
|
|
63
|
+
acc.paths.add(sourcePath);
|
|
64
|
+
if (tree.kind === Json.Kind.Document && isPackageJson(sourcePath)) {
|
|
65
|
+
acc.packageDirs.add(dirName(sourcePath));
|
|
66
|
+
const pkg = rootObject(tree as Json.Document);
|
|
67
|
+
if (pkg && isCraPackage(pkg)) acc.roots.add(dirName(sourcePath));
|
|
68
|
+
if (pkg && runsOnVite(pkg)) acc.viteRoots.add(dirName(sourcePath));
|
|
69
|
+
} else if (tree.kind === PlainText.Kind.PlainText && indexHtmlRoot(sourcePath) !== undefined) {
|
|
70
|
+
acc.indexHtml.set(indexHtmlRoot(sourcePath)!, (tree as PlainText).text);
|
|
71
|
+
}
|
|
72
|
+
return tree as R;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** A recipe that scans the whole repository for Create React App packages. Finders use `isCraFile`, edits `isViteFile`. */
|
|
78
|
+
export abstract class CraScanningRecipe extends ScanningRecipe<CraProjects> {
|
|
79
|
+
initialValue(): CraProjects {
|
|
80
|
+
return new CraProjects();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async scanner(acc: CraProjects): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
84
|
+
return scanCraProjects(acc);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import {emptySpace, getMemberKeyName, isLiteral, isMember, isObject, Json, rightPadded as paddedElement, space} from "@openrewrite/rewrite/json";
|
|
2
|
+
import {emptyMarkers, randomId} from "@openrewrite/rewrite";
|
|
3
|
+
|
|
4
|
+
export const REACT_SCRIPTS = "react-scripts";
|
|
5
|
+
export const VITE = "vite";
|
|
6
|
+
export const VITE_PLUGIN_REACT = "@vitejs/plugin-react";
|
|
7
|
+
|
|
8
|
+
/** `REACT_APP_NAME`, the prefix Create React App exposes to client code. */
|
|
9
|
+
export const CRA_ENV_PREFIX = "REACT_APP_";
|
|
10
|
+
export const VITE_ENV_PREFIX = "VITE_";
|
|
11
|
+
|
|
12
|
+
const DEPENDENCY_SECTIONS = ["dependencies", "devDependencies", "optionalDependencies", "peerDependencies"];
|
|
13
|
+
|
|
14
|
+
/** The directory of a path with no trailing slash; `''` for a root-level file. */
|
|
15
|
+
export function dirName(path: string): string {
|
|
16
|
+
const i = path.lastIndexOf("/");
|
|
17
|
+
return i < 0 ? "" : path.substring(0, i);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function baseName(path: string): string {
|
|
21
|
+
return path.substring(path.lastIndexOf("/") + 1);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function joinPath(dir: string, name: string): string {
|
|
25
|
+
return dir === "" ? name : `${dir}/${name}`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The nearest of `roots` that is a directory above `path`, found by climbing its ancestors. */
|
|
29
|
+
export function nearestRoot(roots: ReadonlySet<string>, path: string): string | undefined {
|
|
30
|
+
for (let dir = dirName(path); ; dir = dirName(dir)) {
|
|
31
|
+
if (roots.has(dir)) return dir;
|
|
32
|
+
if (dir === "") return undefined;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function isPackageJson(sourcePath: string): boolean {
|
|
37
|
+
return baseName(sourcePath) === "package.json" && !sourcePath.split("/").includes("node_modules");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function rootObject(doc: Json.Document): Json.Object | undefined {
|
|
41
|
+
return isObject(doc.value) ? doc.value : undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function getMember(obj: Json.Object, name: string): Json.Member | undefined {
|
|
45
|
+
for (const rp of obj.members) {
|
|
46
|
+
if (isMember(rp.element) && getMemberKeyName(rp.element) === name) return rp.element;
|
|
47
|
+
}
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function memberObject(obj: Json.Object, name: string): Json.Object | undefined {
|
|
52
|
+
const value = getMember(obj, name)?.value;
|
|
53
|
+
return value && isObject(value) ? value : undefined;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The string a JSON value holds, or `undefined` when it is not a string literal. */
|
|
57
|
+
export function stringValue(value: Json | undefined): string | undefined {
|
|
58
|
+
return value && isLiteral(value) && typeof value.value === "string" ? value.value : undefined;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The string value of a member, or `undefined` when it is absent or not a string literal. */
|
|
62
|
+
export function stringMember(obj: Json.Object, name: string): string | undefined {
|
|
63
|
+
return stringValue(getMember(obj, name)?.value);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function hasDependency(pkg: Json.Object, name: string): boolean {
|
|
67
|
+
return DEPENDENCY_SECTIONS.some(section => {
|
|
68
|
+
const dependencies = memberObject(pkg, section);
|
|
69
|
+
return !!dependencies && !!getMember(dependencies, name);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** True when a package.json script invokes `react-scripts`. */
|
|
74
|
+
export function scriptsUseReactScripts(pkg: Json.Object): boolean {
|
|
75
|
+
const scripts = memberObject(pkg, "scripts");
|
|
76
|
+
return !!scripts && scripts.members.some(rp => {
|
|
77
|
+
const command = isMember(rp.element) ? stringValue(rp.element.value) : undefined;
|
|
78
|
+
return command !== undefined && /(^|[\s&;|])react-scripts(\s|$)/.test(command);
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** A `package.json` that Create React App built: `react-scripts` is a dependency. */
|
|
83
|
+
export function isCraPackage(pkg: Json.Object): boolean {
|
|
84
|
+
return hasDependency(pkg, REACT_SCRIPTS);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Tools that wrap or replace `react-scripts`, and which Vite does not replace. */
|
|
88
|
+
const WRAPPER_DEPENDENCIES = ["@craco/craco", "craco", "react-app-rewired"];
|
|
89
|
+
const WRAPPER_COMMAND = /(^|[\s&;|])(craco|react-app-rewired)(\s|$)/;
|
|
90
|
+
const START_OR_BUILD = /(^|[\s&;|])react-scripts\s+(start|build)(\s|$)/;
|
|
91
|
+
/** The only commands the migration rewrites. */
|
|
92
|
+
export const EXACT_START_OR_BUILD = /^react-scripts (start|build)$/;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* True when the package's `start` and `build` will run on Vite once the migration has edited it: it is a Create
|
|
96
|
+
* React App package that neither depends on `craco` or `react-app-rewired` nor has a script that wraps those or
|
|
97
|
+
* `react-scripts start|build` (`cross-env PORT=4000 react-scripts start`, `react-scripts build --profile`).
|
|
98
|
+
* Such a package is only found by the finders; editing it would leave `start` or `build` without a working tool.
|
|
99
|
+
*/
|
|
100
|
+
export function runsOnVite(pkg: Json.Object): boolean {
|
|
101
|
+
if (!isCraPackage(pkg) || WRAPPER_DEPENDENCIES.some(name => hasDependency(pkg, name))) return false;
|
|
102
|
+
const scripts = memberObject(pkg, "scripts");
|
|
103
|
+
return !scripts || scripts.members.every(rp => {
|
|
104
|
+
const command = isMember(rp.element) ? stringValue(rp.element.value)?.trim() : undefined;
|
|
105
|
+
return command === undefined || (!WRAPPER_COMMAND.test(command) &&
|
|
106
|
+
(!START_OR_BUILD.test(command) || EXACT_START_OR_BUILD.test(command)));
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* True when a script still needs `react-scripts` once the migration is done, that is any script that runs it
|
|
112
|
+
* other than `eject`, which the migration removes.
|
|
113
|
+
*/
|
|
114
|
+
export function scriptsKeepReactScripts(pkg: Json.Object): boolean {
|
|
115
|
+
const scripts = memberObject(pkg, "scripts");
|
|
116
|
+
return !!scripts && scripts.members.some(rp => {
|
|
117
|
+
const command = isMember(rp.element) ? stringValue(rp.element.value) : undefined;
|
|
118
|
+
return command !== undefined && /(^|[\s&;|])react-scripts(\s|$)/.test(command) &&
|
|
119
|
+
!/(^|[\s&;|])react-scripts\s+eject\s*$/.test(command);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function stringLiteral(value: string, prefix = emptySpace): Json.Literal {
|
|
124
|
+
return {
|
|
125
|
+
kind: Json.Kind.Literal, id: randomId(), prefix, markers: emptyMarkers,
|
|
126
|
+
source: JSON.stringify(value), value
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function newMember(name: string, value: Json.Value, prefix: Json.Space): Json.Member {
|
|
131
|
+
return {
|
|
132
|
+
kind: Json.Kind.Member, id: randomId(), prefix, markers: emptyMarkers,
|
|
133
|
+
key: {kind: Json.Kind.RightPadded, element: stringLiteral(name), after: emptySpace, markers: emptyMarkers},
|
|
134
|
+
value: {...value, prefix: space(" ")} as Json.Value
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function newStringMember(name: string, value: string, prefix: Json.Space): Json.Member {
|
|
139
|
+
return newMember(name, stringLiteral(value), prefix);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function rightPadded<T extends Json>(element: T, after: Json.Space = emptySpace): Json.RightPadded<T> {
|
|
143
|
+
return paddedElement(element, after);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** The whitespace of a prefix without its comments, which belong to the member that carries them. */
|
|
147
|
+
function layoutOf(prefix: Json.Space): Json.Space {
|
|
148
|
+
// After a comment, the line break that follows it is the comment's suffix.
|
|
149
|
+
const ws = prefix.comments.length > 0 ? prefix.comments[prefix.comments.length - 1].suffix : prefix.whitespace;
|
|
150
|
+
return space(ws.includes("\n") ? ws : " ");
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** How to lay out the first member of an empty object: the space before it and before the closing brace. */
|
|
154
|
+
export interface EmptyObjectLayout {
|
|
155
|
+
readonly inner: Json.Space;
|
|
156
|
+
readonly closing: Json.Space;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** The layout for an object nested one level inside `parent`, which is indented like its own members. */
|
|
160
|
+
export function nestedLayout(parent: Json.Object): EmptyObjectLayout {
|
|
161
|
+
const ws = parent.members[0]?.element.prefix.whitespace ?? "\n ";
|
|
162
|
+
if (!ws.includes("\n")) return {inner: emptySpace, closing: emptySpace};
|
|
163
|
+
const unit = ws.substring(ws.lastIndexOf("\n") + 1) || " ";
|
|
164
|
+
return {inner: space(ws + unit), closing: space(ws)};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Inserts `member` in alphabetical order among the object's members, or last when they are not sorted.
|
|
169
|
+
* Copies the whitespace the object already uses between members.
|
|
170
|
+
*/
|
|
171
|
+
export function insertMember(obj: Json.Object, member: Json.Member, whenEmpty?: EmptyObjectLayout): Json.Object {
|
|
172
|
+
// An empty object `{}` holds a single `Json.Empty`, not zero members.
|
|
173
|
+
const members = obj.members.filter(rp => rp.element.kind !== Json.Kind.Empty);
|
|
174
|
+
if (members.length === 0) {
|
|
175
|
+
const layout = whenEmpty ?? {inner: emptySpace, closing: emptySpace};
|
|
176
|
+
return {...obj, members: [rightPadded({...member, prefix: layout.inner} as Json.Member, layout.closing)]};
|
|
177
|
+
}
|
|
178
|
+
const names = members.map(rp => isMember(rp.element) ? getMemberKeyName(rp.element) ?? "" : "");
|
|
179
|
+
const sorted = names.every((n, i) => i === 0 || names[i - 1] <= n);
|
|
180
|
+
let index = sorted ? names.findIndex(n => n > getMemberKeyName(member)!) : -1;
|
|
181
|
+
if (index < 0) index = members.length;
|
|
182
|
+
const first = members[0].element.prefix;
|
|
183
|
+
const separator = layoutOf(members.length > 1 ? members[1].element.prefix : first);
|
|
184
|
+
if (index === 0) {
|
|
185
|
+
// The old first member keeps the comments written above it.
|
|
186
|
+
members[0] = {...members[0], element: {...members[0].element, prefix: first.comments.length > 0 ? first : {...first, whitespace: separator.whitespace}}};
|
|
187
|
+
members.unshift(rightPadded({...member, prefix: space(first.whitespace)} as Json.Member));
|
|
188
|
+
} else if (index === members.length) {
|
|
189
|
+
const last = members[members.length - 1];
|
|
190
|
+
members[members.length - 1] = {...last, after: emptySpace};
|
|
191
|
+
members.push(rightPadded({...member, prefix: separator} as Json.Member, last.after));
|
|
192
|
+
} else {
|
|
193
|
+
members.splice(index, 0, rightPadded({...member, prefix: separator} as Json.Member));
|
|
194
|
+
}
|
|
195
|
+
return {...obj, members};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Removes a member by name, handing the last member's trailing space to the one before it. */
|
|
199
|
+
export function removeMember(obj: Json.Object, name: string): Json.Object {
|
|
200
|
+
const index = obj.members.findIndex(rp => isMember(rp.element) && getMemberKeyName(rp.element) === name);
|
|
201
|
+
if (index < 0) return obj;
|
|
202
|
+
const members = [...obj.members];
|
|
203
|
+
const [removed] = members.splice(index, 1);
|
|
204
|
+
if (index === 0 && members.length > 0) {
|
|
205
|
+
members[0] = {...members[0], element: {...members[0].element, prefix: removed.element.prefix}};
|
|
206
|
+
}
|
|
207
|
+
if (index === obj.members.length - 1 && members.length > 0) {
|
|
208
|
+
members[members.length - 1] = {...members[members.length - 1], after: removed.after};
|
|
209
|
+
}
|
|
210
|
+
return {...obj, members};
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export function replaceMember(obj: Json.Object, name: string, update: (m: Json.Member) => Json.Member): Json.Object {
|
|
214
|
+
let changed = false;
|
|
215
|
+
const members = obj.members.map(rp => {
|
|
216
|
+
if (!isMember(rp.element) || getMemberKeyName(rp.element) !== name) return rp;
|
|
217
|
+
const updated = update(rp.element);
|
|
218
|
+
if (updated === rp.element) return rp;
|
|
219
|
+
changed = true;
|
|
220
|
+
return {...rp, element: updated};
|
|
221
|
+
});
|
|
222
|
+
return changed ? {...obj, members} : obj;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** `.env`, `.env.local`, `.env.production.local` and the like. */
|
|
226
|
+
export function isEnvFile(sourcePath: string): boolean {
|
|
227
|
+
return /^\.env(\..+)?$/.test(baseName(sourcePath));
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** Appends a string to a JSON array, copying the whitespace the array already uses between values. */
|
|
231
|
+
export function appendString(array: Json.Array, value: string): Json.Array {
|
|
232
|
+
const values = array.values.filter(rp => rp.element.kind !== Json.Kind.Empty);
|
|
233
|
+
if (values.length === 0) return array;
|
|
234
|
+
const last = values[values.length - 1];
|
|
235
|
+
const separator = layoutOf(values.length > 1 ? values[1].element.prefix : values[0].element.prefix);
|
|
236
|
+
return {
|
|
237
|
+
...array,
|
|
238
|
+
values: [...values.slice(0, -1), {...last, after: emptySpace}, rightPadded<Json.Value>(stringLiteral(value, separator), last.after)]
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export function stringValues(array: Json.Array): string[] {
|
|
243
|
+
return array.values.flatMap(rp => stringValue(rp.element) ?? []);
|
|
244
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import {ExecutionContext, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
+
import {JavaScriptVisitor, JS} from "@openrewrite/rewrite/javascript";
|
|
3
|
+
import {J} from "@openrewrite/rewrite/java";
|
|
4
|
+
import {CraProjects, CraScanningRecipe} from "./scan-cra-projects";
|
|
5
|
+
import {baseName} from "./shared";
|
|
6
|
+
|
|
7
|
+
/** The `///` of a triple-slash directive is parsed as the comment's `//`, leaving `/ <reference ... />` as its text. */
|
|
8
|
+
const REACT_SCRIPTS_REFERENCE = /^\/ <reference types="react-scripts" \/>$/;
|
|
9
|
+
const VITE_CLIENT_REFERENCE = '/ <reference types="vite/client" />';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Points the triple-slash type reference that Create React App generates at Vite's client types.
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* /// <reference types="react-scripts" /> -> /// <reference types="vite/client" />
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* The `react-scripts` package types no longer resolve once `react-scripts` is removed. `vite/client` declares
|
|
19
|
+
* `import.meta.env`, asset imports and CSS modules.
|
|
20
|
+
*
|
|
21
|
+
* @see https://vite.dev/guide/features#client-types
|
|
22
|
+
*/
|
|
23
|
+
export class UpdateReactAppEnvTypes extends CraScanningRecipe {
|
|
24
|
+
readonly name = "org.openrewrite.react.cra-to-vite.update-react-app-env-types";
|
|
25
|
+
readonly displayName = "Reference `vite/client` instead of `react-scripts` types";
|
|
26
|
+
readonly description = "Replaces `/// <reference types=\"react-scripts\" />` in declaration files of Create React App packages, such as `src/react-app-env.d.ts`, with `/// <reference types=\"vite/client\" />`.";
|
|
27
|
+
|
|
28
|
+
async editorWithData(acc: CraProjects): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
29
|
+
return new class extends JavaScriptVisitor<ExecutionContext> {
|
|
30
|
+
override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
|
|
31
|
+
if (!baseName(cu.sourcePath).endsWith(".d.ts") || !acc.isViteFile(cu.sourcePath)) return cu;
|
|
32
|
+
const comments = cu.prefix.comments.map(c =>
|
|
33
|
+
c.kind === J.Kind.TextComment && REACT_SCRIPTS_REFERENCE.test((c as unknown as {text: string}).text) ?
|
|
34
|
+
{...c, text: VITE_CLIENT_REFERENCE} : c);
|
|
35
|
+
if (comments.every((c, i) => c === cu.prefix.comments[i])) return super.visitJsCompilationUnit(cu, ctx);
|
|
36
|
+
return {...cu, prefix: {...cu.prefix, comments}} as JS.CompilationUnit;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import {ExecutionContext, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
+
import {getMemberKeyName, isMember, isObject, Json, JsonVisitor} from "@openrewrite/rewrite/json";
|
|
3
|
+
import {CraProjects, CraScanningRecipe} from "./scan-cra-projects";
|
|
4
|
+
import {dirName, isPackageJson, memberObject, removeMember, replaceMember, runsOnVite, stringValue} from "./shared";
|
|
5
|
+
|
|
6
|
+
/** Only an exact match is rewritten: extra flags or environment prefixes need a human. */
|
|
7
|
+
const SCRIPT_REPLACEMENTS = new Map<string, string>([
|
|
8
|
+
["react-scripts start", "vite"],
|
|
9
|
+
["react-scripts build", "vite build"],
|
|
10
|
+
]);
|
|
11
|
+
|
|
12
|
+
/** `react-scripts build` fails the build on type errors; `vite build` does not type-check, so a project with a tsconfig keeps the check. */
|
|
13
|
+
const TYPE_CHECKED_BUILD = "tsc --noEmit && vite build";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Rewrites scripts whose command is exactly `react-scripts start` and `react-scripts build`.
|
|
17
|
+
*
|
|
18
|
+
* ```json
|
|
19
|
+
* "start": "react-scripts start" -> "start": "vite"
|
|
20
|
+
* "build": "react-scripts build" -> "build": "vite build"
|
|
21
|
+
* "build": "react-scripts build" -> "build": "tsc --noEmit && vite build" (when there is a tsconfig.json)
|
|
22
|
+
* "eject": "react-scripts eject" -> (removed)
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* `react-scripts build` failed on type errors and `vite build` does not type-check, so a package with a `tsconfig.json`
|
|
26
|
+
* keeps the check. `--noEmit` rather than `tsc -b`, because the CRA tsconfig sets `noEmit` and `tsc -b` rejects that before TypeScript 5.6.
|
|
27
|
+
*
|
|
28
|
+
* Only packages that depend on `react-scripts` change; in a workspace that hoists it the scripts are left alone. A package
|
|
29
|
+
* with a script that wraps `react-scripts start|build` or runs `craco` is not edited at all, because a half-migrated
|
|
30
|
+
* package has no working `start`. Scripts with extra flags (`cross-env PORT=4000 react-scripts start`) and `test` are left for
|
|
31
|
+
* `FindCraToViteBlockers` to flag.
|
|
32
|
+
*
|
|
33
|
+
* @see https://react.dev/blog/2025/02/14/sunsetting-create-react-app#how-to-migrate-to-a-build-tool
|
|
34
|
+
*/
|
|
35
|
+
export class UpdateReactScriptsScripts extends CraScanningRecipe {
|
|
36
|
+
readonly name = "org.openrewrite.react.cra-to-vite.update-package-scripts";
|
|
37
|
+
readonly displayName = "Replace `react-scripts` start and build scripts with Vite";
|
|
38
|
+
readonly description = "Rewrites `package.json` scripts that are exactly `react-scripts start` or `react-scripts build` to `vite` and `vite build` (`tsc --noEmit && vite build` when there is a `tsconfig.json`), and removes the `react-scripts eject` script, in packages that depend on `react-scripts`. Packages with `craco`, `react-app-rewired` or wrapped `start` or `build` scripts are left unchanged, as is the `test` script.";
|
|
39
|
+
|
|
40
|
+
async editorWithData(acc: CraProjects): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
41
|
+
return new class extends JsonVisitor<ExecutionContext> {
|
|
42
|
+
protected override async visitDocument(doc: Json.Document): Promise<Json | undefined> {
|
|
43
|
+
if (!isPackageJson(doc.sourcePath) || !isObject(doc.value) || !runsOnVite(doc.value)) return doc;
|
|
44
|
+
const scripts = memberObject(doc.value, "scripts");
|
|
45
|
+
if (!scripts) return doc;
|
|
46
|
+
const build = acc.has(dirName(doc.sourcePath), "tsconfig.json") ? TYPE_CHECKED_BUILD : "vite build";
|
|
47
|
+
// `eject` has no Vite equivalent and is the last thing that keeps react-scripts installed.
|
|
48
|
+
const ejects = scripts.members.flatMap(rp => isMember(rp.element) &&
|
|
49
|
+
/^react-scripts eject$/.test(stringValue(rp.element.value)?.trim() ?? "") ? [getMemberKeyName(rp.element)!] : []);
|
|
50
|
+
const kept = ejects.reduce((obj, name) => removeMember(obj, name), scripts);
|
|
51
|
+
const newScripts = {
|
|
52
|
+
...kept,
|
|
53
|
+
members: kept.members.map(rp => {
|
|
54
|
+
const m = rp.element;
|
|
55
|
+
if (!isMember(m)) return rp;
|
|
56
|
+
const literal = m.value as Json.Literal;
|
|
57
|
+
const replacement = typeof literal.value === "string" ? SCRIPT_REPLACEMENTS.get(literal.value.trim()) : undefined;
|
|
58
|
+
if (!replacement) return rp;
|
|
59
|
+
const value = replacement === "vite build" ? build : replacement;
|
|
60
|
+
return {...rp, element: {...m, value: {...literal, source: JSON.stringify(value), value}}};
|
|
61
|
+
})
|
|
62
|
+
};
|
|
63
|
+
const changed = ejects.length > 0 || newScripts.members.some((rp, i) => rp !== kept.members[i]);
|
|
64
|
+
if (!changed) return doc;
|
|
65
|
+
return {...doc, value: replaceMember(doc.value, "scripts", m => ({...m, value: newScripts}))} as Json.Document;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import {ExecutionContext, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
+
import {Json, JsonVisitor} from "@openrewrite/rewrite/json";
|
|
3
|
+
import {CraProjects, CraScanningRecipe} from "./scan-cra-projects";
|
|
4
|
+
import {appendString, baseName, getMember, memberObject, replaceMember, rootObject, stringValues} from "./shared";
|
|
5
|
+
|
|
6
|
+
const VITE_CLIENT = "vite/client";
|
|
7
|
+
|
|
8
|
+
export function isTsConfig(sourcePath: string): boolean {
|
|
9
|
+
return /^tsconfig(\..+)?\.json$/.test(baseName(sourcePath)) && !sourcePath.split("/").includes("node_modules");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Adds `vite/client` to an existing `compilerOptions.types` list in `tsconfig.json`, so `import.meta.env` and
|
|
14
|
+
* asset imports type-check.
|
|
15
|
+
*
|
|
16
|
+
* ```json
|
|
17
|
+
* "types": ["node"] -> "types": ["node", "vite/client"]
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* Only tsconfig files of Create React App packages change. A tsconfig without a `types` list is left alone, because adding one would stop TypeScript from including
|
|
21
|
+
* every `@types/*` package automatically; `UpdateReactAppEnvTypes` points `src/react-app-env.d.ts` at
|
|
22
|
+
* `vite/client` for those projects.
|
|
23
|
+
*
|
|
24
|
+
* @see https://vite.dev/guide/features#client-types
|
|
25
|
+
*/
|
|
26
|
+
export class UpdateTsconfigForVite extends CraScanningRecipe {
|
|
27
|
+
readonly name = "org.openrewrite.react.cra-to-vite.update-tsconfig";
|
|
28
|
+
readonly displayName = "Add `vite/client` types to `tsconfig.json`";
|
|
29
|
+
readonly description = "Appends `vite/client` to an existing `compilerOptions.types` list in `tsconfig.json` files. Files without a `types` list are left unchanged so that TypeScript keeps including every `@types/*` package.";
|
|
30
|
+
|
|
31
|
+
async editorWithData(acc: CraProjects): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
32
|
+
return new class extends JsonVisitor<ExecutionContext> {
|
|
33
|
+
protected override async visitDocument(doc: Json.Document): Promise<Json | undefined> {
|
|
34
|
+
const root = rootObject(doc);
|
|
35
|
+
const options = root && isTsConfig(doc.sourcePath) && acc.isViteFile(doc.sourcePath) ? memberObject(root, "compilerOptions") : undefined;
|
|
36
|
+
const types = options ? getMember(options, "types") : undefined;
|
|
37
|
+
if (!root || !options || !types || types.value.kind !== Json.Kind.Array) return doc;
|
|
38
|
+
const array = types.value as Json.Array;
|
|
39
|
+
if (stringValues(array).includes(VITE_CLIENT)) return doc;
|
|
40
|
+
const updated = appendString(array, VITE_CLIENT);
|
|
41
|
+
if (updated === array) return doc;
|
|
42
|
+
const newOptions = replaceMember(options, "types", m => ({...m, value: updated}));
|
|
43
|
+
return {...doc, value: replaceMember(root, "compilerOptions", m => ({...m, value: newOptions}))} as Json.Document;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|