@openrewrite/recipes-react 0.15.0 → 0.17.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.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/migration/cra-to-vite/add-vite-config.d.ts +7 -0
- package/dist/migration/cra-to-vite/add-vite-config.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/add-vite-config.js +58 -1
- package/dist/migration/cra-to-vite/add-vite-config.js.map +1 -1
- package/dist/migration/cra-to-vite/cra-file-gate.d.ts +3 -0
- package/dist/migration/cra-to-vite/cra-file-gate.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/cra-file-gate.js +10 -0
- package/dist/migration/cra-to-vite/cra-file-gate.js.map +1 -0
- package/dist/migration/cra-to-vite/cra-runtime.d.ts +2 -0
- package/dist/migration/cra-to-vite/cra-runtime.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/cra-runtime.js +24 -0
- package/dist/migration/cra-to-vite/cra-runtime.js.map +1 -0
- package/dist/migration/cra-to-vite/cra-script.d.ts +8 -0
- package/dist/migration/cra-to-vite/cra-script.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/cra-script.js +82 -0
- package/dist/migration/cra-to-vite/cra-script.js.map +1 -0
- package/dist/migration/cra-to-vite/cra-to-vite.d.ts +2 -1
- package/dist/migration/cra-to-vite/cra-to-vite.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/cra-to-vite.js +23 -16
- package/dist/migration/cra-to-vite/cra-to-vite.js.map +1 -1
- package/dist/migration/cra-to-vite/cra-typecheck.d.ts +4 -0
- package/dist/migration/cra-to-vite/cra-typecheck.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/cra-typecheck.js +35 -0
- package/dist/migration/cra-to-vite/cra-typecheck.js.map +1 -0
- package/dist/migration/cra-to-vite/find-cra-config-blockers.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/find-cra-config-blockers.js +22 -29
- package/dist/migration/cra-to-vite/find-cra-config-blockers.js.map +1 -1
- package/dist/migration/cra-to-vite/index.d.ts +8 -2
- package/dist/migration/cra-to-vite/index.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/index.js +17 -2
- package/dist/migration/cra-to-vite/index.js.map +1 -1
- package/dist/migration/cra-to-vite/migrate-cra-package.d.ts +17 -0
- package/dist/migration/cra-to-vite/migrate-cra-package.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/migrate-cra-package.js +181 -0
- package/dist/migration/cra-to-vite/migrate-cra-package.js.map +1 -0
- package/dist/migration/cra-to-vite/migrate-cra-svg-imports.d.ts +9 -0
- package/dist/migration/cra-to-vite/migrate-cra-svg-imports.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/migrate-cra-svg-imports.js +66 -0
- package/dist/migration/cra-to-vite/migrate-cra-svg-imports.js.map +1 -0
- package/dist/migration/cra-to-vite/migrate-cra-tests-to-vitest.d.ts +49 -0
- package/dist/migration/cra-to-vite/migrate-cra-tests-to-vitest.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/migrate-cra-tests-to-vitest.js +506 -0
- package/dist/migration/cra-to-vite/migrate-cra-tests-to-vitest.js.map +1 -0
- package/dist/migration/cra-to-vite/move-public-index-html.d.ts +2 -0
- package/dist/migration/cra-to-vite/move-public-index-html.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/move-public-index-html.js +12 -7
- package/dist/migration/cra-to-vite/move-public-index-html.js.map +1 -1
- package/dist/migration/cra-to-vite/plan-cra-to-vite.d.ts +30 -0
- package/dist/migration/cra-to-vite/plan-cra-to-vite.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/plan-cra-to-vite.js +428 -0
- package/dist/migration/cra-to-vite/plan-cra-to-vite.js.map +1 -0
- package/dist/migration/cra-to-vite/prepare-cra-package.d.ts +15 -0
- package/dist/migration/cra-to-vite/prepare-cra-package.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/prepare-cra-package.js +100 -0
- package/dist/migration/cra-to-vite/prepare-cra-package.js.map +1 -0
- package/dist/migration/cra-to-vite/remove-cra-configuration.d.ts +12 -0
- package/dist/migration/cra-to-vite/remove-cra-configuration.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/remove-cra-configuration.js +85 -0
- package/dist/migration/cra-to-vite/remove-cra-configuration.js.map +1 -0
- package/dist/migration/cra-to-vite/rename-jsx-files.d.ts +20 -0
- package/dist/migration/cra-to-vite/rename-jsx-files.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/rename-jsx-files.js +220 -0
- package/dist/migration/cra-to-vite/rename-jsx-files.js.map +1 -0
- package/dist/migration/cra-to-vite/rename-process-env-react-app.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/rename-process-env-react-app.js +6 -3
- package/dist/migration/cra-to-vite/rename-process-env-react-app.js.map +1 -1
- package/dist/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.js +1 -1
- package/dist/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.js.map +1 -1
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.d.ts +11 -4
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.js +47 -50
- package/dist/migration/cra-to-vite/replace-react-scripts-dependency.js.map +1 -1
- package/dist/migration/cra-to-vite/resolved-cra-dependency.d.ts +3 -0
- package/dist/migration/cra-to-vite/resolved-cra-dependency.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/resolved-cra-dependency.js +42 -0
- package/dist/migration/cra-to-vite/resolved-cra-dependency.js.map +1 -0
- package/dist/migration/cra-to-vite/scan-cra-projects.d.ts +13 -0
- package/dist/migration/cra-to-vite/scan-cra-projects.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/scan-cra-projects.js +52 -4
- package/dist/migration/cra-to-vite/scan-cra-projects.js.map +1 -1
- package/dist/migration/cra-to-vite/shared.d.ts +2 -15
- package/dist/migration/cra-to-vite/shared.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/shared.js +32 -130
- package/dist/migration/cra-to-vite/shared.js.map +1 -1
- package/dist/migration/cra-to-vite/svg-component.d.ts +5 -0
- package/dist/migration/cra-to-vite/svg-component.d.ts.map +1 -0
- package/dist/migration/cra-to-vite/svg-component.js +60 -0
- package/dist/migration/cra-to-vite/svg-component.js.map +1 -0
- package/dist/migration/cra-to-vite/update-react-app-env-types.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/update-react-app-env-types.js +8 -2
- package/dist/migration/cra-to-vite/update-react-app-env-types.js.map +1 -1
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.d.ts +3 -2
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.js +28 -29
- package/dist/migration/cra-to-vite/update-react-scripts-scripts.js.map +1 -1
- package/dist/migration/cra-to-vite/update-tsconfig-for-vite.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/update-tsconfig-for-vite.js +4 -13
- package/dist/migration/cra-to-vite/update-tsconfig-for-vite.js.map +1 -1
- package/dist/react17/migrate-deprecated-lifecycles.js +1 -1
- package/dist/react17/migrate-deprecated-lifecycles.js.map +1 -1
- package/dist/react17/use-new-jsx-transform.d.ts.map +1 -1
- package/dist/react17/use-new-jsx-transform.js +30 -78
- package/dist/react17/use-new-jsx-transform.js.map +1 -1
- package/dist/react18/replace-reactdom-render.d.ts.map +1 -1
- package/dist/react18/replace-reactdom-render.js +16 -8
- package/dist/react18/replace-reactdom-render.js.map +1 -1
- package/dist/react18/replace-unmount-component-at-node.d.ts.map +1 -1
- package/dist/react18/replace-unmount-component-at-node.js +62 -20
- package/dist/react18/replace-unmount-component-at-node.js.map +1 -1
- package/dist/react19/replace-default-props.d.ts.map +1 -1
- package/dist/react19/replace-default-props.js +10 -13
- package/dist/react19/replace-default-props.js.map +1 -1
- package/dist/react19/replace-react-shallow-renderer.d.ts.map +1 -1
- package/dist/react19/replace-react-shallow-renderer.js +1 -6
- package/dist/react19/replace-react-shallow-renderer.js.map +1 -1
- package/dist/refactoring/create-class-to-es6.d.ts.map +1 -1
- package/dist/refactoring/create-class-to-es6.js +4 -7
- package/dist/refactoring/create-class-to-es6.js.map +1 -1
- package/dist/refactoring/create-element-to-jsx.d.ts.map +1 -1
- package/dist/refactoring/create-element-to-jsx.js +3 -4
- package/dist/refactoring/create-element-to-jsx.js.map +1 -1
- package/dist/refactoring/prop-types-to-typescript.js +2 -3
- package/dist/refactoring/prop-types-to-typescript.js.map +1 -1
- package/dist/refactoring/pure-render-mixin.d.ts.map +1 -1
- package/dist/refactoring/pure-render-mixin.js +3 -6
- package/dist/refactoring/pure-render-mixin.js.map +1 -1
- package/dist/testing/enzyme-constant-strings.d.ts +12 -0
- package/dist/testing/enzyme-constant-strings.d.ts.map +1 -0
- package/dist/testing/enzyme-constant-strings.js +101 -0
- package/dist/testing/enzyme-constant-strings.js.map +1 -0
- package/dist/testing/enzyme-host-components.d.ts +14 -0
- package/dist/testing/enzyme-host-components.d.ts.map +1 -0
- package/dist/testing/enzyme-host-components.js +248 -0
- package/dist/testing/enzyme-host-components.js.map +1 -0
- package/dist/testing/enzyme-portals.d.ts +38 -0
- package/dist/testing/enzyme-portals.d.ts.map +1 -0
- package/dist/testing/enzyme-portals.js +226 -0
- package/dist/testing/enzyme-portals.js.map +1 -0
- package/dist/testing/enzyme-selector-names.d.ts +9 -0
- package/dist/testing/enzyme-selector-names.d.ts.map +1 -0
- package/dist/testing/enzyme-selector-names.js +50 -0
- package/dist/testing/enzyme-selector-names.js.map +1 -0
- package/dist/testing/enzyme-to-react-testing-library.d.ts +80 -12
- package/dist/testing/enzyme-to-react-testing-library.d.ts.map +1 -1
- package/dist/testing/enzyme-to-react-testing-library.js +447 -135
- package/dist/testing/enzyme-to-react-testing-library.js.map +1 -1
- package/dist/testing/enzyme-usage.d.ts.map +1 -1
- package/dist/testing/enzyme-usage.js +27 -4
- package/dist/testing/enzyme-usage.js.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.d.ts +23 -8
- package/dist/testing/enzyme-wrapper-scope.d.ts.map +1 -1
- package/dist/testing/enzyme-wrapper-scope.js +291 -169
- package/dist/testing/enzyme-wrapper-scope.js.map +1 -1
- package/dist/util/java-recipe-proxy.d.ts +16 -0
- package/dist/util/java-recipe-proxy.d.ts.map +1 -0
- package/dist/util/java-recipe-proxy.js +85 -0
- package/dist/util/java-recipe-proxy.js.map +1 -0
- package/dist/util/module-imports.d.ts +13 -0
- package/dist/util/module-imports.d.ts.map +1 -0
- package/dist/util/module-imports.js +60 -0
- package/dist/util/module-imports.js.map +1 -0
- package/dist/util/react-class.d.ts +1 -0
- package/dist/util/react-class.d.ts.map +1 -1
- package/dist/util/react-class.js +3 -2
- package/dist/util/react-class.js.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +2 -1
- package/src/migration/cra-to-vite/add-vite-config.ts +53 -1
- package/src/migration/cra-to-vite/cra-file-gate.ts +8 -0
- package/src/migration/cra-to-vite/cra-runtime.ts +16 -0
- package/src/migration/cra-to-vite/cra-script.ts +61 -0
- package/src/migration/cra-to-vite/cra-to-vite.ts +25 -36
- package/src/migration/cra-to-vite/cra-typecheck.ts +33 -0
- package/src/migration/cra-to-vite/find-cra-config-blockers.ts +23 -28
- package/src/migration/cra-to-vite/index.ts +10 -2
- package/src/migration/cra-to-vite/migrate-cra-package.ts +127 -0
- package/src/migration/cra-to-vite/migrate-cra-svg-imports.ts +41 -0
- package/src/migration/cra-to-vite/migrate-cra-tests-to-vitest.ts +376 -0
- package/src/migration/cra-to-vite/move-public-index-html.ts +12 -7
- package/src/migration/cra-to-vite/plan-cra-to-vite.ts +283 -0
- package/src/migration/cra-to-vite/prepare-cra-package.ts +86 -0
- package/src/migration/cra-to-vite/remove-cra-configuration.ts +64 -0
- package/src/migration/cra-to-vite/rename-jsx-files.ts +155 -0
- package/src/migration/cra-to-vite/rename-process-env-react-app.ts +6 -3
- package/src/migration/cra-to-vite/rename-react-app-env-vars-in-env-files.ts +1 -1
- package/src/migration/cra-to-vite/replace-react-scripts-dependency.ts +48 -54
- package/src/migration/cra-to-vite/resolved-cra-dependency.ts +41 -0
- package/src/migration/cra-to-vite/scan-cra-projects.ts +57 -6
- package/src/migration/cra-to-vite/shared.ts +35 -146
- package/src/migration/cra-to-vite/svg-component.ts +39 -0
- package/src/migration/cra-to-vite/update-react-app-env-types.ts +11 -4
- package/src/migration/cra-to-vite/update-react-scripts-scripts.ts +30 -41
- package/src/migration/cra-to-vite/update-tsconfig-for-vite.ts +6 -13
- package/src/react17/migrate-deprecated-lifecycles.ts +2 -2
- package/src/react17/use-new-jsx-transform.ts +30 -72
- package/src/react18/replace-reactdom-render.ts +17 -8
- package/src/react18/replace-unmount-component-at-node.ts +78 -32
- package/src/react19/replace-default-props.ts +11 -15
- package/src/react19/replace-react-shallow-renderer.ts +2 -7
- package/src/refactoring/create-class-to-es6.ts +6 -9
- package/src/refactoring/create-element-to-jsx.ts +4 -6
- package/src/refactoring/prop-types-to-typescript.ts +4 -4
- package/src/refactoring/pure-render-mixin.ts +4 -7
- package/src/testing/enzyme-constant-strings.ts +95 -0
- package/src/testing/enzyme-host-components.ts +287 -0
- package/src/testing/enzyme-portals.ts +279 -0
- package/src/testing/enzyme-selector-names.ts +59 -0
- package/src/testing/enzyme-to-react-testing-library.ts +584 -140
- package/src/testing/enzyme-usage.ts +29 -4
- package/src/testing/enzyme-wrapper-scope.ts +309 -151
- package/src/util/java-recipe-proxy.ts +81 -0
- package/src/util/module-imports.ts +60 -0
- package/src/util/react-class.ts +2 -1
|
@@ -1,40 +1,29 @@
|
|
|
1
|
-
import {Option, Recipe} from "@openrewrite/rewrite";
|
|
1
|
+
import {noopVisitor, Option, Recipe} from "@openrewrite/rewrite";
|
|
2
|
+
import {craFilesOnly} from './cra-file-gate';
|
|
3
|
+
import {MigrateCraPackage} from "./migrate-cra-package";
|
|
4
|
+
import {MigrateCraTestsToVitestSources} from "./migrate-cra-tests-to-vitest";
|
|
5
|
+
import {RenameJsxFiles} from "./rename-jsx-files";
|
|
2
6
|
import {AddViteConfig} from "./add-vite-config";
|
|
3
|
-
import {FindCraConfigBlockers} from "./find-cra-config-blockers";
|
|
4
|
-
import {FindCraEnvFileBlockers} from "./find-cra-env-file-blockers";
|
|
5
|
-
import {FindCraSourceBlockers} from "./find-cra-source-blockers";
|
|
6
7
|
import {MovePublicIndexHtml} from "./move-public-index-html";
|
|
8
|
+
import {MigrateCraSvgImports} from "./migrate-cra-svg-imports";
|
|
9
|
+
import {RemoveCraConfiguration} from "./remove-cra-configuration";
|
|
7
10
|
import {RenameProcessEnvReactApp} from "./rename-process-env-react-app";
|
|
8
11
|
import {RenameReactAppEnvVarsInEnvFiles} from "./rename-react-app-env-vars-in-env-files";
|
|
9
|
-
import {ReplaceReactScriptsDependency} from "./replace-react-scripts-dependency";
|
|
10
12
|
import {UpdateReactAppEnvTypes} from "./update-react-app-env-types";
|
|
11
13
|
import {UpdateReactScriptsScripts} from "./update-react-scripts-scripts";
|
|
12
14
|
import {UpdateTsconfigForVite} from "./update-tsconfig-for-vite";
|
|
13
15
|
|
|
14
16
|
/**
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* - Add `vite` and `@vitejs/plugin-react`, rewrite the `start` and `build` scripts, remove `eject`, and remove
|
|
19
|
-
* `react-scripts` unless a script such as `react-scripts test` still runs it
|
|
20
|
-
* - Add a `vite.config.ts` or `vite.config.js`
|
|
21
|
-
* - Move `public/index.html` to the project root with a module script
|
|
22
|
-
* - Rename `REACT_APP_*` to `VITE_*` in `.env` files and `process.env.REACT_APP_*` to `import.meta.env.VITE_*`
|
|
23
|
-
* - Point `src/react-app-env.d.ts` and `tsconfig.json` at `vite/client`
|
|
24
|
-
* - Record in the `CraToViteBlockers` data table what needs a manual step: `craco`, Jest, SVG `ReactComponent`
|
|
25
|
-
* imports, `REACT_APP_*` values set outside `.env` files and the like
|
|
26
|
-
*
|
|
27
|
-
* A package that uses `craco` or `react-app-rewired`, or whose `start` or `build` script wraps `react-scripts`, is
|
|
28
|
-
* only reported in the data table and otherwise left unchanged.
|
|
29
|
-
*
|
|
30
|
-
* Run your package manager afterwards to update the lock file.
|
|
31
|
-
*
|
|
17
|
+
* Preflights each CRA package, commits its complete manifest with the native lock engine, then migrates
|
|
18
|
+
* only accepted packages. Vitest processes the same import.meta environment and Vite plugins as the app.
|
|
19
|
+
* Unsupported configurations and native lock refusals leave the package unchanged and are recorded.
|
|
32
20
|
* @see https://react.dev/blog/2025/02/14/sunsetting-create-react-app#how-to-migrate-to-a-build-tool
|
|
33
21
|
*/
|
|
34
22
|
export class CraToVite extends Recipe {
|
|
23
|
+
override async editor() { return craFilesOnly(noopVisitor()); }
|
|
35
24
|
readonly name = "org.openrewrite.react.migrate.cra-to-vite";
|
|
36
25
|
readonly displayName = "Migrate Create React App to Vite";
|
|
37
|
-
readonly description = "
|
|
26
|
+
readonly description = "Migrates supported Create React App packages and scoped react-scripts forks to Vite and Vitest. Commits dependencies and scripts with the repository's native npm/Yarn/pnpm lock engine, adds Vite/SVGR configuration, moves HTML, renames JSX files and imports, migrates environment variables and TypeScript client types, and configures jsdom test setup. Unsupported projects and native lock refusals are recorded and left unchanged.";
|
|
38
27
|
|
|
39
28
|
@Option({
|
|
40
29
|
displayName: "Vite version",
|
|
@@ -57,20 +46,20 @@ export class CraToVite extends Recipe {
|
|
|
57
46
|
}
|
|
58
47
|
|
|
59
48
|
async recipeList(): Promise<Recipe[]> {
|
|
49
|
+
const pkg = new MigrateCraPackage({viteVersion: this.viteVersion, pluginReactVersion: this.pluginReactVersion});
|
|
60
50
|
return [
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
new
|
|
64
|
-
new
|
|
65
|
-
new
|
|
66
|
-
new
|
|
67
|
-
new
|
|
68
|
-
new
|
|
69
|
-
new
|
|
70
|
-
new
|
|
71
|
-
new
|
|
72
|
-
new
|
|
73
|
-
new ReplaceReactScriptsDependency({viteVersion: this.viteVersion, pluginReactVersion: this.pluginReactVersion}),
|
|
51
|
+
pkg,
|
|
52
|
+
new AddViteConfig().restrictTo(pkg.landed),
|
|
53
|
+
new RenameJsxFiles().restrictTo(pkg.landed),
|
|
54
|
+
new MigrateCraSvgImports().restrictTo(pkg.landed),
|
|
55
|
+
new MovePublicIndexHtml().withRenamedEntries(pkg.renamedFiles).restrictTo(pkg.landed),
|
|
56
|
+
new RemoveCraConfiguration().restrictTo(pkg.landed),
|
|
57
|
+
new UpdateReactScriptsScripts().restrictTo(pkg.landed),
|
|
58
|
+
new RenameReactAppEnvVarsInEnvFiles().restrictTo(pkg.landed),
|
|
59
|
+
new RenameProcessEnvReactApp().restrictTo(pkg.landed),
|
|
60
|
+
new UpdateReactAppEnvTypes().restrictTo(pkg.landed),
|
|
61
|
+
new UpdateTsconfigForVite().restrictTo(pkg.landed),
|
|
62
|
+
new MigrateCraTestsToVitestSources(new Set(), true).withDependencyRoots(pkg.landed).withRenamedFiles(pkg.renamedFiles).restrictTo(pkg.landed),
|
|
74
63
|
];
|
|
75
64
|
}
|
|
76
65
|
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** CRA reports production diagnostics under src, even when tsconfig includes other files. */
|
|
2
|
+
export const CRA_TYPECHECK_PATH = 'scripts/vite-typecheck.cjs';
|
|
3
|
+
export const TYPE_CHECKED_BUILD = `node ${CRA_TYPECHECK_PATH} && vite build`;
|
|
4
|
+
|
|
5
|
+
export const CRA_TYPECHECK_SCRIPT = `// Preserve Create React App's production TypeScript diagnostic scope.
|
|
6
|
+
const ts = require('typescript');
|
|
7
|
+
const path = require('node:path');
|
|
8
|
+
const root = path.resolve(__dirname, '..');
|
|
9
|
+
const configPath = path.join(root, 'tsconfig.json');
|
|
10
|
+
const config = ts.readConfigFile(configPath, ts.sys.readFile);
|
|
11
|
+
const parsed = ts.parseJsonConfigFileContent(config.config || {}, ts.sys, root, undefined, configPath);
|
|
12
|
+
const options = {
|
|
13
|
+
...parsed.options,
|
|
14
|
+
noEmit: true,
|
|
15
|
+
module: ts.ModuleKind.ESNext,
|
|
16
|
+
moduleResolution: ts.ModuleResolutionKind.Bundler ?? ts.ModuleResolutionKind.NodeJs,
|
|
17
|
+
lib: parsed.options.lib ?? ['lib.dom.d.ts', 'lib.dom.iterable.d.ts', 'lib.esnext.d.ts'],
|
|
18
|
+
jsx: parsed.options.jsx ?? (ts.JsxEmit.ReactJSX ?? ts.JsxEmit.React),
|
|
19
|
+
};
|
|
20
|
+
const program = ts.createProgram({rootNames: parsed.fileNames, options, projectReferences: parsed.projectReferences});
|
|
21
|
+
const diagnostics = [config.error, ...parsed.errors, ...ts.getPreEmitDiagnostics(program)].filter(diagnostic => {
|
|
22
|
+
if (!diagnostic) return false;
|
|
23
|
+
if (!diagnostic.file) return true;
|
|
24
|
+
const file = path.relative(root, diagnostic.file.fileName).split(path.sep).join('/');
|
|
25
|
+
return file.startsWith('src/') && !/(?:^|\\/)__tests__\\/|\\.(?:test|spec)\\.[cm]?[jt]sx?$|^src\\/setupTests\\.[cm]?[jt]sx?$/.test(file);
|
|
26
|
+
});
|
|
27
|
+
if (diagnostics.length) {
|
|
28
|
+
process.stderr.write(ts.formatDiagnosticsWithColorAndContext(diagnostics, {
|
|
29
|
+
getCanonicalFileName: file => file, getCurrentDirectory: () => root, getNewLine: () => '\\n',
|
|
30
|
+
}));
|
|
31
|
+
process.exitCode = 1;
|
|
32
|
+
}
|
|
33
|
+
`;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import {isStartOrBuildScript} from './cra-script';
|
|
1
2
|
import {ExecutionContext, foundSearchResult, MarkersKind, Option, SearchResult, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
-
import {getMemberKeyName,
|
|
3
|
+
import {findMember, getMemberKeyName, isArray, isObject, Json, JsonVisitor, membersOf, valueAt} from "@openrewrite/rewrite/json";
|
|
3
4
|
import {CraProjects, CraScanningRecipe} from "./scan-cra-projects";
|
|
4
5
|
import {craToViteBlockersTable} from "./cra-blockers-table";
|
|
5
6
|
import {
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
hasDependency, isCraPackage, isPackageJson, rootObject, scriptsUseReactScripts,
|
|
8
|
+
stringValue, stringValues
|
|
8
9
|
} from "./shared";
|
|
9
10
|
import {isTsConfig} from "./update-tsconfig-for-vite";
|
|
10
11
|
|
|
@@ -86,14 +87,12 @@ function packageJsonFindings(pkg: Json.Object, findings: [Json.Member, Finding][
|
|
|
86
87
|
const isCra = isCraPackage(pkg) || scriptsUseReactScripts(pkg) || !!tool;
|
|
87
88
|
if (!isCra) return;
|
|
88
89
|
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
const
|
|
95
|
-
if (!command || /^react-scripts (start|build)$/.test(command.trim())) continue;
|
|
96
|
-
const name = getMemberKeyName(m as Json.Member);
|
|
90
|
+
const scriptsMember = findMember(pkg, "scripts");
|
|
91
|
+
if (scriptsMember && isObject(scriptsMember.value)) {
|
|
92
|
+
for (const m of membersOf(scriptsMember.value)) {
|
|
93
|
+
const command = stringValue(m.value);
|
|
94
|
+
if (!command || isStartOrBuildScript(command)) continue;
|
|
95
|
+
const name = getMemberKeyName(m);
|
|
97
96
|
const scriptTool = TOOL_SCRIPT.exec(command)?.[2];
|
|
98
97
|
const reactScripts = REACT_SCRIPTS_COMMAND.exec(command)?.[2];
|
|
99
98
|
if (scriptTool) {
|
|
@@ -118,8 +117,8 @@ function packageJsonFindings(pkg: Json.Object, findings: [Json.Member, Finding][
|
|
|
118
117
|
}
|
|
119
118
|
|
|
120
119
|
const dependenciesMember = tool ? ["devDependencies", "dependencies", "optionalDependencies", "peerDependencies"]
|
|
121
|
-
.map(section => (
|
|
122
|
-
.find(
|
|
120
|
+
.map(section => findMember(pkg, section))
|
|
121
|
+
.find(m => m && isObject(m.value) && findMember(m.value, tool)) : undefined;
|
|
123
122
|
if (tool && dependenciesMember) {
|
|
124
123
|
findings.push([dependenciesMember, {
|
|
125
124
|
category: "config-override-tool", detail: `depends on ${tool}`,
|
|
@@ -127,53 +126,49 @@ function packageJsonFindings(pkg: Json.Object, findings: [Json.Member, Finding][
|
|
|
127
126
|
}]);
|
|
128
127
|
}
|
|
129
128
|
|
|
130
|
-
const jest =
|
|
129
|
+
const jest = findMember(pkg, "jest");
|
|
131
130
|
if (jest) {
|
|
132
131
|
findings.push([jest, {
|
|
133
132
|
category: "jest", detail: "jest configuration in package.json",
|
|
134
133
|
nextStep: "Translate the CRA-supported jest keys to Vitest config; react-scripts validated this block."
|
|
135
134
|
}]);
|
|
136
135
|
}
|
|
137
|
-
const eslintConfig =
|
|
136
|
+
const eslintConfig = findMember(pkg, "eslintConfig");
|
|
138
137
|
if (eslintConfig && /react-app/.test(printedExtends(eslintConfig))) {
|
|
139
138
|
findings.push([eslintConfig, {
|
|
140
139
|
category: "eslint-config", detail: "eslintConfig extends react-app",
|
|
141
140
|
nextStep: "Keep eslint-config-react-app installed, or move to a standalone ESLint config; Vite does not lint."
|
|
142
141
|
}]);
|
|
143
142
|
}
|
|
144
|
-
const proxy =
|
|
143
|
+
const proxy = findMember(pkg, "proxy");
|
|
145
144
|
if (proxy) {
|
|
146
145
|
findings.push([proxy, {
|
|
147
146
|
category: "proxy", detail: "proxy in package.json",
|
|
148
147
|
nextStep: "Configure server.proxy in vite.config; CRA's package.json proxy is not read by Vite."
|
|
149
148
|
}]);
|
|
150
149
|
}
|
|
151
|
-
const homepage =
|
|
150
|
+
const homepage = findMember(pkg, "homepage");
|
|
152
151
|
if (homepage) {
|
|
153
152
|
findings.push([homepage, {
|
|
154
|
-
category: "homepage", detail: `homepage ${
|
|
153
|
+
category: "homepage", detail: `homepage ${stringValue(homepage.value) ?? ""}`.trim(),
|
|
155
154
|
nextStep: "Set `base` in vite.config; CRA derived PUBLIC_URL from homepage."
|
|
156
155
|
}]);
|
|
157
156
|
}
|
|
158
157
|
}
|
|
159
158
|
|
|
160
159
|
function tsConfigFindings(root: Json.Object, findings: [Json.Member, Finding][]): void {
|
|
161
|
-
const
|
|
162
|
-
const
|
|
163
|
-
const optionsMember = getMember(root, "compilerOptions");
|
|
160
|
+
const baseUrl = valueAt(root, ["compilerOptions", "baseUrl"]);
|
|
161
|
+
const optionsMember = findMember(root, "compilerOptions");
|
|
164
162
|
if (baseUrl && optionsMember) {
|
|
165
163
|
findings.push([optionsMember, {
|
|
166
|
-
category: "absolute-imports", detail: `compilerOptions.baseUrl ${
|
|
164
|
+
category: "absolute-imports", detail: `compilerOptions.baseUrl ${stringValue(baseUrl) ?? ""}`.trim(),
|
|
167
165
|
nextStep: "Add resolve.alias or vite-tsconfig-paths so imports relative to baseUrl resolve in Vite."
|
|
168
166
|
}]);
|
|
169
167
|
}
|
|
170
168
|
}
|
|
171
169
|
|
|
172
170
|
function printedExtends(eslintConfig: Json.Member): string {
|
|
173
|
-
const
|
|
174
|
-
if (
|
|
175
|
-
|
|
176
|
-
if (!extended) return "";
|
|
177
|
-
if (extended.kind === Json.Kind.Array) return stringValues(extended as Json.Array).join(",");
|
|
178
|
-
return typeof (extended as Json.Literal).value === "string" ? (extended as Json.Literal).value : "";
|
|
171
|
+
const extended = isObject(eslintConfig.value) ? findMember(eslintConfig.value, "extends")?.value : undefined;
|
|
172
|
+
if (extended && isArray(extended)) return stringValues(extended).join(",");
|
|
173
|
+
return stringValue(extended) ?? "";
|
|
179
174
|
}
|
|
@@ -1,9 +1,15 @@
|
|
|
1
|
+
import {MigrateCraPackage} from "./migrate-cra-package";
|
|
2
|
+
import {RenameJsxFiles} from "./rename-jsx-files";
|
|
3
|
+
import {MigrateCraTestsToVitest, MigrateCraTestsToVitestSources, PlanVitest} from "./migrate-cra-tests-to-vitest";
|
|
4
|
+
import {PlanCraToVite} from "./plan-cra-to-vite";
|
|
1
5
|
import {AddViteConfig} from "./add-vite-config";
|
|
2
6
|
import {FindCraConfigBlockers} from "./find-cra-config-blockers";
|
|
3
7
|
import {FindCraEnvFileBlockers} from "./find-cra-env-file-blockers";
|
|
4
8
|
import {FindCraSourceBlockers} from "./find-cra-source-blockers";
|
|
5
9
|
import {FindCraToViteBlockers} from "./find-cra-to-vite-blockers";
|
|
6
10
|
import {MovePublicIndexHtml} from "./move-public-index-html";
|
|
11
|
+
import {MigrateCraSvgImports} from "./migrate-cra-svg-imports";
|
|
12
|
+
import {RemoveCraConfiguration} from "./remove-cra-configuration";
|
|
7
13
|
import {RenameProcessEnvReactApp} from "./rename-process-env-react-app";
|
|
8
14
|
import {RenameReactAppEnvVarsInEnvFiles} from "./rename-react-app-env-vars-in-env-files";
|
|
9
15
|
import {ReplaceReactScriptsDependency} from "./replace-react-scripts-dependency";
|
|
@@ -13,14 +19,16 @@ import {UpdateTsconfigForVite} from "./update-tsconfig-for-vite";
|
|
|
13
19
|
|
|
14
20
|
export {CraToVite} from "./cra-to-vite";
|
|
15
21
|
export {
|
|
22
|
+
MigrateCraPackage, RenameJsxFiles, MigrateCraTestsToVitest, MigrateCraTestsToVitestSources, PlanVitest, PlanCraToVite,
|
|
16
23
|
AddViteConfig, FindCraConfigBlockers, FindCraEnvFileBlockers, FindCraSourceBlockers, FindCraToViteBlockers,
|
|
17
|
-
MovePublicIndexHtml, RenameProcessEnvReactApp, RenameReactAppEnvVarsInEnvFiles, ReplaceReactScriptsDependency,
|
|
24
|
+
MovePublicIndexHtml, MigrateCraSvgImports, RemoveCraConfiguration, RenameProcessEnvReactApp, RenameReactAppEnvVarsInEnvFiles, ReplaceReactScriptsDependency,
|
|
18
25
|
UpdateReactAppEnvTypes, UpdateReactScriptsScripts, UpdateTsconfigForVite,
|
|
19
26
|
};
|
|
20
27
|
|
|
21
28
|
/** The recipes `CraToVite` is made of, which are installed in the marketplace next to it. */
|
|
22
29
|
export const craToViteRecipes = [
|
|
30
|
+
MigrateCraPackage, RenameJsxFiles, MigrateCraTestsToVitest, MigrateCraTestsToVitestSources, PlanVitest, PlanCraToVite,
|
|
23
31
|
UpdateReactScriptsScripts, ReplaceReactScriptsDependency, AddViteConfig, MovePublicIndexHtml,
|
|
24
32
|
RenameReactAppEnvVarsInEnvFiles, RenameProcessEnvReactApp, UpdateReactAppEnvTypes, UpdateTsconfigForVite,
|
|
25
|
-
FindCraToViteBlockers, FindCraConfigBlockers, FindCraSourceBlockers, FindCraEnvFileBlockers,
|
|
33
|
+
FindCraToViteBlockers, FindCraConfigBlockers, FindCraSourceBlockers, FindCraEnvFileBlockers, MigrateCraSvgImports, RemoveCraConfiguration,
|
|
26
34
|
];
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import {Cursor, ExecutionContext, noopVisitor, Option, Recipe, SourceFile, Tree, TreePrinters, TreeVisitor} from '@openrewrite/rewrite';
|
|
2
|
+
import {DelegatingRecipe} from '@openrewrite/rewrite/rpc';
|
|
3
|
+
import {Json, valueAt} from '@openrewrite/rewrite/json';
|
|
4
|
+
import {isNodeDependencyInput} from '../../util/java-recipe-proxy';
|
|
5
|
+
import {PlanCraToVite} from './plan-cra-to-vite';
|
|
6
|
+
import {PlanVitest} from './migrate-cra-tests-to-vitest';
|
|
7
|
+
import {CraPackageOptions, prepareCraPackage} from './prepare-cra-package';
|
|
8
|
+
import {dirName, isPackageJson, joinPath, nearestRoot, rootObject, scriptsUseReactScripts, stringValue} from './shared';
|
|
9
|
+
import {craToViteBlockersTable} from './cra-blockers-table';
|
|
10
|
+
import {craViteNodeEngine} from './cra-runtime';
|
|
11
|
+
import {craEslintNeedsReview} from './remove-cra-configuration';
|
|
12
|
+
import {craFilesOnly} from './cra-file-gate';
|
|
13
|
+
|
|
14
|
+
type Projects = ReturnType<PlanCraToVite['initialValue']>;
|
|
15
|
+
|
|
16
|
+
/** Internal planner/transaction pair. Domain edits are prepared in TypeScript; Java owns native lock acceptance. */
|
|
17
|
+
class ApplyCraPackage extends PlanCraToVite {
|
|
18
|
+
override readonly name = 'org.openrewrite.react.cra-to-vite.apply-package';
|
|
19
|
+
override readonly displayName = 'Apply the complete CRA package migration with its native lock';
|
|
20
|
+
override readonly description = 'Commits scripts and all dependencies in one native lock transaction after project and test preflight.';
|
|
21
|
+
// Mutable holders survive the shallow copies the RPC host makes when preparing visitors.
|
|
22
|
+
private readonly state: {inputs: SourceFile[]; manifests: Json.Document[]; java?: DelegatingRecipe} = {inputs: [], manifests: []};
|
|
23
|
+
constructor(private readonly options: CraPackageOptions, private readonly testReady: ReadonlySet<string>,
|
|
24
|
+
private readonly landed: Set<string>, private readonly renames: Set<string>) { super(); }
|
|
25
|
+
override initialValue(): Projects {
|
|
26
|
+
this.state.inputs.length = 0;
|
|
27
|
+
this.state.manifests.length = 0;
|
|
28
|
+
this.state.java = undefined;
|
|
29
|
+
this.landed.clear(); this.renames.clear();
|
|
30
|
+
return super.initialValue();
|
|
31
|
+
}
|
|
32
|
+
override async scanner(acc: Projects): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
33
|
+
const scanner = await super.scanner(acc); const state = this.state;
|
|
34
|
+
return craFilesOnly(new class extends TreeVisitor<Tree, ExecutionContext> {
|
|
35
|
+
override async visit<R extends Tree>(tree: Tree, ctx: ExecutionContext, parent?: Cursor): Promise<R | undefined> {
|
|
36
|
+
await scanner.visit(tree, ctx, parent);
|
|
37
|
+
const source = tree as SourceFile;
|
|
38
|
+
if (source.sourcePath && !source.sourcePath.split('/').includes('node_modules') && isNodeDependencyInput(source.sourcePath, true)) {
|
|
39
|
+
state.inputs.push(source);
|
|
40
|
+
if (tree.kind === Json.Kind.Document && isPackageJson(source.sourcePath)) state.manifests.push(tree as Json.Document);
|
|
41
|
+
}
|
|
42
|
+
return tree as R;
|
|
43
|
+
}
|
|
44
|
+
}());
|
|
45
|
+
}
|
|
46
|
+
override async generate(acc: Projects, ctx: ExecutionContext): Promise<SourceFile[]> {
|
|
47
|
+
await super.generate(acc, ctx);
|
|
48
|
+
const manifests: Record<string, string> = {};
|
|
49
|
+
const table = craToViteBlockersTable();
|
|
50
|
+
for (const doc of this.state.manifests) {
|
|
51
|
+
const root = dirName(doc.sourcePath);
|
|
52
|
+
if (!this.ready.has(root)) continue;
|
|
53
|
+
const pkg = rootObject(doc)!;
|
|
54
|
+
if (craEslintNeedsReview(doc)) {
|
|
55
|
+
table.insertRow(ctx, {filePath: doc.sourcePath, category: 'migration-declined', detail: 'Retained ESLint configuration depends on CRA transitive dependencies', nextStep: 'Declare eslint-config-react-app explicitly or migrate the independent linter before removing CRA.'});
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
const test = stringValue(valueAt(pkg, ['scripts', 'test']));
|
|
59
|
+
const vitest = !!test && /react-scripts\s+test/.test(test);
|
|
60
|
+
const engine = stringValue(valueAt(pkg, ['engines', 'node']));
|
|
61
|
+
if (engine && !craViteNodeEngine(engine, this.options.viteVersion, this.options.pluginReactVersion, vitest)) {
|
|
62
|
+
table.insertRow(ctx, {filePath: doc.sourcePath, category: 'migration-declined', detail: 'Declared Node engine does not support the Vite toolchain', nextStep: 'Upgrade the supported Node runtime before migrating this package.'});
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (vitest && !this.testReady.has(root)) {
|
|
66
|
+
table.insertRow(ctx, {filePath: doc.sourcePath, category: 'migration-declined', detail: 'CRA tests require manual Vitest migration',
|
|
67
|
+
nextStep: 'Review unsupported Jest configuration, APIs, dependencies and TypeScript settings before migrating this package.'});
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
const svgr = acc.usesSvgr(root);
|
|
71
|
+
const prepared = prepareCraPackage(doc, {...this.options, vitest, svgr, typeCheck: acc.has(root, 'tsconfig.json'),
|
|
72
|
+
preservedDependencies: acc.preservedDependencies.get(root)});
|
|
73
|
+
if (prepared === doc || scriptsUseReactScripts(rootObject(prepared)!)) {
|
|
74
|
+
table.insertRow(ctx, {filePath: doc.sourcePath, category: 'migration-declined', detail: 'A custom script still needs react-scripts', nextStep: 'Rewrite all CRA commands before removing react-scripts.'});
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
manifests[doc.sourcePath] = await TreePrinters.print(prepared);
|
|
78
|
+
}
|
|
79
|
+
if (Object.keys(manifests).length === 0) return [];
|
|
80
|
+
const java = this.state.java = new DelegatingRecipe('org.openrewrite.react.migrate.ApplyCraManifestEdits', {manifests});
|
|
81
|
+
const scanner = await java.scanner(0);
|
|
82
|
+
for (const input of this.state.inputs) await scanner.visit(input, ctx);
|
|
83
|
+
const generated = await java.generate(0, ctx);
|
|
84
|
+
const editor = await java.editorWithData(0);
|
|
85
|
+
for (const doc of this.state.manifests) {
|
|
86
|
+
const desired = manifests[doc.sourcePath]; if (desired === undefined) continue;
|
|
87
|
+
const edited = await editor.visit<Json.Document>(doc, ctx);
|
|
88
|
+
if (edited && await TreePrinters.print(edited) === desired) this.landed.add(dirName(doc.sourcePath));
|
|
89
|
+
else table.insertRow(ctx, {filePath: doc.sourcePath, category: 'migration-declined', detail: 'The native dependency engine declined the complete manifest/lock transaction', nextStep: 'Review dependency resolution and package-manager configuration; all source steps remain unchanged.'});
|
|
90
|
+
}
|
|
91
|
+
for (const path of this.renamedFiles) if (this.landed.has(nearestRoot(acc.packageDirs, path) ?? '\0')) this.renames.add(path);
|
|
92
|
+
return generated;
|
|
93
|
+
}
|
|
94
|
+
override async editorWithData(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
95
|
+
const state = this.state;
|
|
96
|
+
return new class extends TreeVisitor<Tree, ExecutionContext> {
|
|
97
|
+
override async visit<R extends Tree>(tree: Tree, ctx: ExecutionContext, parent?: Cursor): Promise<R | undefined> {
|
|
98
|
+
const path = (tree as SourceFile).sourcePath;
|
|
99
|
+
if (!state.java || !path || !isNodeDependencyInput(path, true)) return tree as R;
|
|
100
|
+
return (await state.java.editorWithData(0)).visit<R>(tree, ctx, parent);
|
|
101
|
+
}
|
|
102
|
+
}();
|
|
103
|
+
}
|
|
104
|
+
override async onComplete(ctx: ExecutionContext): Promise<void> { await this.state.java?.onComplete(ctx); }
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* The complete package.json change is one decision with the repository's npm/Yarn/pnpm lock.
|
|
109
|
+
* @see https://react.dev/blog/2025/02/14/sunsetting-create-react-app#how-to-migrate-to-a-build-tool
|
|
110
|
+
*/
|
|
111
|
+
export class MigrateCraPackage extends Recipe {
|
|
112
|
+
override async editor() { return craFilesOnly(noopVisitor()); }
|
|
113
|
+
readonly name = 'org.openrewrite.react.cra-to-vite.migrate-package';
|
|
114
|
+
readonly displayName = 'Migrate CRA package configuration and native locks';
|
|
115
|
+
readonly description = 'Preflights CRA projects, prepares Vite/Vitest/SVGR dependencies and scripts, removes CRA-only defaults, and commits package.json with its native lock. Refused projects retain their scripts and dependencies.';
|
|
116
|
+
readonly landed = new Set<string>();
|
|
117
|
+
readonly renamedFiles = new Set<string>();
|
|
118
|
+
@Option({displayName: 'Vite version', description: 'Vite version range; defaults to ^7.0.0 (Node 20.19+).', required: false})
|
|
119
|
+
viteVersion?: string;
|
|
120
|
+
@Option({displayName: 'React plugin version', description: 'Compatible @vitejs/plugin-react version range; defaults to ^5.2.0.', required: false})
|
|
121
|
+
pluginReactVersion?: string;
|
|
122
|
+
constructor(options?: {viteVersion?: string; pluginReactVersion?: string}) { super(options); }
|
|
123
|
+
async recipeList(): Promise<Recipe[]> {
|
|
124
|
+
const tests = new Set<string>();
|
|
125
|
+
return [new PlanVitest(tests, true), new ApplyCraPackage({viteVersion: this.viteVersion, pluginReactVersion: this.pluginReactVersion}, tests, this.landed, this.renamedFiles)];
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import {check, ExecutionContext, not, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
+
import {isVendoredOrBundled, JavaScriptParser, JavaScriptVisitor, JS} from "@openrewrite/rewrite/javascript";
|
|
3
|
+
import {J} from "@openrewrite/rewrite/java";
|
|
4
|
+
import {svgComponentName, svgImportNeedsReview} from "./svg-component";
|
|
5
|
+
import {CraProjects, CraScanningRecipe} from "./scan-cra-projects";
|
|
6
|
+
|
|
7
|
+
/** Component imports use SVGR's explicit query; ordinary SVG imports remain asset URLs. */
|
|
8
|
+
export class MigrateCraSvgImports extends CraScanningRecipe {
|
|
9
|
+
readonly name = "org.openrewrite.react.cra-to-vite.migrate-svg-imports";
|
|
10
|
+
readonly displayName = "Migrate CRA SVG component imports to Vite SVGR";
|
|
11
|
+
readonly description = "Changes `import {ReactComponent as Logo} from './logo.svg'` to `import Logo from './logo.svg?react'` in CRA client source. A combined URL/component import becomes two imports so both uses retain their meaning. Requires vite-plugin-svgr in the Vite configuration.";
|
|
12
|
+
|
|
13
|
+
async editorWithData(acc: CraProjects): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
14
|
+
return check(not(isVendoredOrBundled()), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
15
|
+
override async visitJsCompilationUnit(cu: JS.CompilationUnit): Promise<J | undefined> {
|
|
16
|
+
if (!acc.isViteClientSource(cu.sourcePath)) return cu;
|
|
17
|
+
let changed = false;
|
|
18
|
+
const statements: typeof cu.statements = [];
|
|
19
|
+
for (const rp of cu.statements) {
|
|
20
|
+
const statement = rp.element;
|
|
21
|
+
if (statement.kind !== JS.Kind.Import) { statements.push(rp); continue; }
|
|
22
|
+
const imp = statement as JS.Import;
|
|
23
|
+
const name = svgComponentName(imp);
|
|
24
|
+
if (!name || await svgImportNeedsReview(imp)) { statements.push(rp); continue; }
|
|
25
|
+
const module = imp.moduleSpecifier!.element as J.Literal;
|
|
26
|
+
const source = module.valueSource ? module.valueSource.slice(0, -1) + "?react" + module.valueSource.slice(-1) : JSON.stringify(module.value + "?react");
|
|
27
|
+
const parsed = await new JavaScriptParser().parseOne({text: `import ${name} from ${source};`, sourcePath: "svgr-import.ts"}) as JS.CompilationUnit;
|
|
28
|
+
const newImport = parsed.statements[0].element;
|
|
29
|
+
if (imp.importClause!.name) {
|
|
30
|
+
statements.push({...rp, element: {...imp, importClause: {...imp.importClause!, namedBindings: undefined}} as JS.Import});
|
|
31
|
+
statements.push({...rp, element: {...newImport, prefix: {...newImport.prefix, whitespace: "\n"}}});
|
|
32
|
+
} else {
|
|
33
|
+
statements.push({...rp, element: {...newImport, prefix: imp.prefix}});
|
|
34
|
+
}
|
|
35
|
+
changed = true;
|
|
36
|
+
}
|
|
37
|
+
return changed ? {...cu, statements} as JS.CompilationUnit : cu;
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|