@openrewrite/recipes-react 0.31.0 → 0.33.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 +37 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +146 -2
- package/dist/index.js.map +1 -1
- package/dist/migration/cra-to-vite/shared.d.ts +2 -8
- package/dist/migration/cra-to-vite/shared.d.ts.map +1 -1
- package/dist/migration/cra-to-vite/shared.js +21 -53
- package/dist/migration/cra-to-vite/shared.js.map +1 -1
- package/dist/nextjs-config/config-recipes.d.ts +90 -0
- package/dist/nextjs-config/config-recipes.d.ts.map +1 -0
- package/dist/nextjs-config/config-recipes.js +222 -0
- package/dist/nextjs-config/config-recipes.js.map +1 -0
- package/dist/nextjs-config/next-config.d.ts +25 -0
- package/dist/nextjs-config/next-config.d.ts.map +1 -0
- package/dist/nextjs-config/next-config.js +199 -0
- package/dist/nextjs-config/next-config.js.map +1 -0
- package/dist/nextjs-search/find-nextjs-projects-below.d.ts +37 -0
- package/dist/nextjs-search/find-nextjs-projects-below.d.ts.map +1 -0
- package/dist/nextjs-search/find-nextjs-projects-below.js +196 -0
- package/dist/nextjs-search/find-nextjs-projects-below.js.map +1 -0
- package/dist/nextjs-search/find-nextjs-upgrade-issues.d.ts +18 -0
- package/dist/nextjs-search/find-nextjs-upgrade-issues.d.ts.map +1 -0
- package/dist/nextjs-search/find-nextjs-upgrade-issues.js +269 -0
- package/dist/nextjs-search/find-nextjs-upgrade-issues.js.map +1 -0
- package/dist/nextjs-search/find-pages-router-candidates.d.ts +14 -0
- package/dist/nextjs-search/find-pages-router-candidates.d.ts.map +1 -0
- package/dist/nextjs-search/find-pages-router-candidates.js +136 -0
- package/dist/nextjs-search/find-pages-router-candidates.js.map +1 -0
- package/dist/nextjs-search/mark-pending-nextjs-upgrade.d.ts +16 -0
- package/dist/nextjs-search/mark-pending-nextjs-upgrade.d.ts.map +1 -0
- package/dist/nextjs-search/mark-pending-nextjs-upgrade.js +125 -0
- package/dist/nextjs-search/mark-pending-nextjs-upgrade.js.map +1 -0
- package/dist/nextjs13/new-link.d.ts +8 -0
- package/dist/nextjs13/new-link.d.ts.map +1 -0
- package/dist/nextjs13/new-link.js +82 -0
- package/dist/nextjs13/new-link.js.map +1 -0
- package/dist/nextjs13/next-image-to-legacy-image.d.ts +8 -0
- package/dist/nextjs13/next-image-to-legacy-image.d.ts.map +1 -0
- package/dist/nextjs13/next-image-to-legacy-image.js +103 -0
- package/dist/nextjs13/next-image-to-legacy-image.js.map +1 -0
- package/dist/nextjs13/next-modules.d.ts +18 -0
- package/dist/nextjs13/next-modules.d.ts.map +1 -0
- package/dist/nextjs13/next-modules.js +127 -0
- package/dist/nextjs13/next-modules.js.map +1 -0
- package/dist/nextjs14/metadata-to-viewport-export.d.ts +8 -0
- package/dist/nextjs14/metadata-to-viewport-export.d.ts.map +1 -0
- package/dist/nextjs14/metadata-to-viewport-export.js +223 -0
- package/dist/nextjs14/metadata-to-viewport-export.js.map +1 -0
- package/dist/nextjs14/next-og-import.d.ts +8 -0
- package/dist/nextjs14/next-og-import.d.ts.map +1 -0
- package/dist/nextjs14/next-og-import.js +29 -0
- package/dist/nextjs14/next-og-import.js.map +1 -0
- package/dist/nextjs15/add-vercel-functions.d.ts +17 -0
- package/dist/nextjs15/add-vercel-functions.d.ts.map +1 -0
- package/dist/nextjs15/add-vercel-functions.js +86 -0
- package/dist/nextjs15/add-vercel-functions.js.map +1 -0
- package/dist/nextjs15/app-dir-runtime-config-experimental-edge.d.ts +11 -0
- package/dist/nextjs15/app-dir-runtime-config-experimental-edge.d.ts.map +1 -0
- package/dist/nextjs15/app-dir-runtime-config-experimental-edge.js +62 -0
- package/dist/nextjs15/app-dir-runtime-config-experimental-edge.js.map +1 -0
- package/dist/nextjs15/app-router.d.ts +26 -0
- package/dist/nextjs15/app-router.d.ts.map +1 -0
- package/dist/nextjs15/app-router.js +302 -0
- package/dist/nextjs15/app-router.js.map +1 -0
- package/dist/nextjs15/async-params.d.ts +8 -0
- package/dist/nextjs15/async-params.d.ts.map +1 -0
- package/dist/nextjs15/async-params.js +484 -0
- package/dist/nextjs15/async-params.js.map +1 -0
- package/dist/nextjs15/async-request-apis.d.ts +8 -0
- package/dist/nextjs15/async-request-apis.d.ts.map +1 -0
- package/dist/nextjs15/async-request-apis.js +204 -0
- package/dist/nextjs15/async-request-apis.js.map +1 -0
- package/dist/nextjs15/fetch-cache-defaults.d.ts +8 -0
- package/dist/nextjs15/fetch-cache-defaults.d.ts.map +1 -0
- package/dist/nextjs15/fetch-cache-defaults.js +160 -0
- package/dist/nextjs15/fetch-cache-defaults.js.map +1 -0
- package/dist/nextjs15/next-request-geo-ip.d.ts +10 -0
- package/dist/nextjs15/next-request-geo-ip.d.ts.map +1 -0
- package/dist/nextjs15/next-request-geo-ip.js +71 -0
- package/dist/nextjs15/next-request-geo-ip.js.map +1 -0
- package/dist/nextjs15/route-handler-cache-defaults.d.ts +8 -0
- package/dist/nextjs15/route-handler-cache-defaults.d.ts.map +1 -0
- package/dist/nextjs15/route-handler-cache-defaults.js +231 -0
- package/dist/nextjs15/route-handler-cache-defaults.js.map +1 -0
- package/dist/nextjs16/middleware-to-proxy.d.ts +16 -0
- package/dist/nextjs16/middleware-to-proxy.d.ts.map +1 -0
- package/dist/nextjs16/middleware-to-proxy.js +159 -0
- package/dist/nextjs16/middleware-to-proxy.js.map +1 -0
- package/dist/nextjs16/next-lint-to-eslint.d.ts +9 -0
- package/dist/nextjs16/next-lint-to-eslint.d.ts.map +1 -0
- package/dist/nextjs16/next-lint-to-eslint.js +60 -0
- package/dist/nextjs16/next-lint-to-eslint.js.map +1 -0
- package/dist/nextjs16/remove-experimental-ppr-config.d.ts +9 -0
- package/dist/nextjs16/remove-experimental-ppr-config.d.ts.map +1 -0
- package/dist/nextjs16/remove-experimental-ppr-config.js +17 -0
- package/dist/nextjs16/remove-experimental-ppr-config.js.map +1 -0
- package/dist/nextjs16/remove-experimental-ppr.d.ts +8 -0
- package/dist/nextjs16/remove-experimental-ppr.d.ts.map +1 -0
- package/dist/nextjs16/remove-experimental-ppr.js +67 -0
- package/dist/nextjs16/remove-experimental-ppr.js.map +1 -0
- package/dist/nextjs16/revalidate-tag-profile.d.ts +8 -0
- package/dist/nextjs16/revalidate-tag-profile.d.ts.map +1 -0
- package/dist/nextjs16/revalidate-tag-profile.js +46 -0
- package/dist/nextjs16/revalidate-tag-profile.js.map +1 -0
- package/dist/nextjs16/stable-cache-apis.d.ts +8 -0
- package/dist/nextjs16/stable-cache-apis.d.ts.map +1 -0
- package/dist/nextjs16/stable-cache-apis.js +29 -0
- package/dist/nextjs16/stable-cache-apis.js.map +1 -0
- package/dist/react19/replace-default-props.d.ts.map +1 -1
- package/dist/react19/replace-default-props.js +50 -13
- package/dist/react19/replace-default-props.js.map +1 -1
- package/dist/util/java-recipe-proxy.d.ts +1 -0
- package/dist/util/java-recipe-proxy.d.ts.map +1 -1
- package/dist/util/java-recipe-proxy.js +8 -5
- package/dist/util/java-recipe-proxy.js.map +1 -1
- package/dist/util/package-json.d.ts +11 -0
- package/dist/util/package-json.d.ts.map +1 -0
- package/dist/util/package-json.js +46 -0
- package/dist/util/package-json.js.map +1 -0
- package/package.json +2 -2
- package/src/index.ts +123 -0
- package/src/migration/cra-to-vite/shared.ts +3 -42
- package/src/nextjs-config/config-recipes.ts +219 -0
- package/src/nextjs-config/next-config.ts +190 -0
- package/src/nextjs-search/find-nextjs-projects-below.ts +216 -0
- package/src/nextjs-search/find-nextjs-upgrade-issues.ts +225 -0
- package/src/nextjs-search/find-pages-router-candidates.ts +110 -0
- package/src/nextjs-search/mark-pending-nextjs-upgrade.ts +131 -0
- package/src/nextjs13/new-link.ts +57 -0
- package/src/nextjs13/next-image-to-legacy-image.ts +71 -0
- package/src/nextjs13/next-modules.ts +93 -0
- package/src/nextjs14/metadata-to-viewport-export.ts +224 -0
- package/src/nextjs14/next-og-import.ts +19 -0
- package/src/nextjs15/add-vercel-functions.ts +78 -0
- package/src/nextjs15/app-dir-runtime-config-experimental-edge.ts +38 -0
- package/src/nextjs15/app-router.ts +316 -0
- package/src/nextjs15/async-params.ts +487 -0
- package/src/nextjs15/async-request-apis.ts +213 -0
- package/src/nextjs15/fetch-cache-defaults.ts +141 -0
- package/src/nextjs15/next-request-geo-ip.ts +49 -0
- package/src/nextjs15/route-handler-cache-defaults.ts +189 -0
- package/src/nextjs16/middleware-to-proxy.ts +146 -0
- package/src/nextjs16/next-lint-to-eslint.ts +43 -0
- package/src/nextjs16/remove-experimental-ppr-config.ts +15 -0
- package/src/nextjs16/remove-experimental-ppr.ts +39 -0
- package/src/nextjs16/revalidate-tag-profile.ts +28 -0
- package/src/nextjs16/stable-cache-apis.ts +20 -0
- package/src/react19/replace-default-props.ts +65 -27
- package/src/util/java-recipe-proxy.ts +9 -3
- package/src/util/package-json.ts +43 -0
package/src/index.ts
CHANGED
|
@@ -32,6 +32,34 @@ import {DeprecatedPropTypesTypes} from './react19/deprecated-prop-types-types';
|
|
|
32
32
|
import {RemoveUnstableBatchedUpdates} from './react19/remove-unstable-batched-updates';
|
|
33
33
|
import {ReplaceCreateFactory} from './react19/replace-create-factory';
|
|
34
34
|
|
|
35
|
+
// Next.js 14
|
|
36
|
+
import {NextOgImport} from "./nextjs14/next-og-import";
|
|
37
|
+
// Next.js 16
|
|
38
|
+
import {MiddlewareToProxy} from "./nextjs16/middleware-to-proxy";
|
|
39
|
+
import {StableCacheApis} from "./nextjs16/stable-cache-apis";
|
|
40
|
+
// Next.js 15
|
|
41
|
+
import {AsyncRequestApis} from './nextjs15/async-request-apis';
|
|
42
|
+
import {AsyncParams} from './nextjs15/async-params';
|
|
43
|
+
import {BuiltInNextFont, NextFutureImageToImage} from "./nextjs13/next-modules";
|
|
44
|
+
import {NextImageToLegacyImage} from "./nextjs13/next-image-to-legacy-image";
|
|
45
|
+
import {NewLink} from "./nextjs13/new-link";
|
|
46
|
+
import {RemoveExperimentalAppDir, OutputStandalone, RemoveExperimentalServerActions, ImagesDomainsToRemotePatterns, ServerExternalPackages, BundlePagesRouterDependencies, OutputFileTracingOptions, RemoveInstrumentationHook, RemoveSwcMinify, TurbopackConfig, CacheComponentsConfig, SkipProxyUrlNormalize, RemoveEslintConfig, KeepImageDefaults} from "./nextjs-config/config-recipes";
|
|
47
|
+
import {RevalidateTagProfile} from "./nextjs16/revalidate-tag-profile";
|
|
48
|
+
import {NextLintToEslint} from "./nextjs16/next-lint-to-eslint";
|
|
49
|
+
import {FindPagesRouterCandidates} from "./nextjs-search/find-pages-router-candidates";
|
|
50
|
+
import {FindNextjsUpgradeIssues} from "./nextjs-search/find-nextjs-upgrade-issues";
|
|
51
|
+
import {MarkPendingNextjsUpgrade} from "./nextjs-search/mark-pending-nextjs-upgrade";
|
|
52
|
+
import {FindNextjsProjectsAtLeast, FindNextjsProjectsBelow} from "./nextjs-search/find-nextjs-projects-below";
|
|
53
|
+
import {MetadataToViewportExport} from "./nextjs14/metadata-to-viewport-export";
|
|
54
|
+
import {NextRequestGeoIp} from "./nextjs15/next-request-geo-ip";
|
|
55
|
+
import {AddVercelFunctions} from "./nextjs15/add-vercel-functions";
|
|
56
|
+
import {AppDirRuntimeConfigExperimentalEdge} from "./nextjs15/app-dir-runtime-config-experimental-edge";
|
|
57
|
+
import {FetchCacheDefaults} from "./nextjs15/fetch-cache-defaults";
|
|
58
|
+
import {RouteHandlerCacheDefaults} from "./nextjs15/route-handler-cache-defaults";
|
|
59
|
+
import {RemoveExperimentalPpr} from "./nextjs16/remove-experimental-ppr";
|
|
60
|
+
import {RemoveExperimentalPprConfig} from "./nextjs16/remove-experimental-ppr-config";
|
|
61
|
+
|
|
62
|
+
|
|
35
63
|
// React 18
|
|
36
64
|
import {ReplaceReactDomRender} from './react18/replace-reactdom-render';
|
|
37
65
|
import {ReplaceUnmountComponentAtNode} from './react18/replace-unmount-component-at-node';
|
|
@@ -117,6 +145,13 @@ const ReactSearch: CategoryDescriptor[] = [...React, {displayName: "Search"}];
|
|
|
117
145
|
const MaterialUi: CategoryDescriptor[] = [...React, {displayName: "Material UI"}];
|
|
118
146
|
const ReactI18n: CategoryDescriptor[] = [...React, {displayName: "Internationalization"}];
|
|
119
147
|
const JavaScriptCleanup: CategoryDescriptor[] = [...JavaScript, {displayName: "Cleanup"}];
|
|
148
|
+
const Nextjs: CategoryDescriptor[] = [...JavaScript, {displayName: "Next.js"}];
|
|
149
|
+
const NextjsMigrate: CategoryDescriptor[] = [...Nextjs, {displayName: "Migrate"}];
|
|
150
|
+
const Nextjs14: CategoryDescriptor[] = [...NextjsMigrate, {displayName: "Next.js 14"}];
|
|
151
|
+
const Nextjs16: CategoryDescriptor[] = [...NextjsMigrate, {displayName: "Next.js 16"}];
|
|
152
|
+
const Nextjs15: CategoryDescriptor[] = [...NextjsMigrate, {displayName: "Next.js 15"}];
|
|
153
|
+
const Nextjs13: CategoryDescriptor[] = [...NextjsMigrate, {displayName: "Next.js 13"}];
|
|
154
|
+
const NextjsSearch: CategoryDescriptor[] = [...Nextjs, {displayName: "Search"}];
|
|
120
155
|
|
|
121
156
|
export async function activate(marketplace: RecipeMarketplace) {
|
|
122
157
|
// React 19
|
|
@@ -150,6 +185,51 @@ export async function activate(marketplace: RecipeMarketplace) {
|
|
|
150
185
|
await marketplace.install(ReplaceUseFormState, React19);
|
|
151
186
|
await marketplace.install(ReplaceReactShallowRenderer, React19);
|
|
152
187
|
|
|
188
|
+
// Next.js 14
|
|
189
|
+
await marketplace.install(NextOgImport, Nextjs14);
|
|
190
|
+
|
|
191
|
+
// Next.js 16
|
|
192
|
+
await marketplace.install(MiddlewareToProxy, Nextjs16);
|
|
193
|
+
await marketplace.install(StableCacheApis, Nextjs16);
|
|
194
|
+
|
|
195
|
+
// Next.js 15
|
|
196
|
+
await marketplace.install(AsyncRequestApis, Nextjs15);
|
|
197
|
+
await marketplace.install(AsyncParams, Nextjs15);
|
|
198
|
+
|
|
199
|
+
await marketplace.install(BuiltInNextFont, Nextjs13);
|
|
200
|
+
await marketplace.install(NextFutureImageToImage, Nextjs13);
|
|
201
|
+
await marketplace.install(NextImageToLegacyImage, Nextjs13);
|
|
202
|
+
await marketplace.install(NewLink, Nextjs13);
|
|
203
|
+
await marketplace.install(RemoveExperimentalAppDir, Nextjs13);
|
|
204
|
+
await marketplace.install(OutputStandalone, Nextjs13);
|
|
205
|
+
await marketplace.install(RemoveExperimentalServerActions, Nextjs14);
|
|
206
|
+
await marketplace.install(ImagesDomainsToRemotePatterns, Nextjs14);
|
|
207
|
+
await marketplace.install(ServerExternalPackages, Nextjs15);
|
|
208
|
+
await marketplace.install(BundlePagesRouterDependencies, Nextjs15);
|
|
209
|
+
await marketplace.install(OutputFileTracingOptions, Nextjs15);
|
|
210
|
+
await marketplace.install(RemoveInstrumentationHook, Nextjs15);
|
|
211
|
+
await marketplace.install(RemoveSwcMinify, Nextjs15);
|
|
212
|
+
await marketplace.install(TurbopackConfig, Nextjs16);
|
|
213
|
+
await marketplace.install(CacheComponentsConfig, Nextjs16);
|
|
214
|
+
await marketplace.install(SkipProxyUrlNormalize, Nextjs16);
|
|
215
|
+
await marketplace.install(RevalidateTagProfile, Nextjs16);
|
|
216
|
+
await marketplace.install(NextLintToEslint, Nextjs16);
|
|
217
|
+
await marketplace.install(FindPagesRouterCandidates, NextjsSearch);
|
|
218
|
+
await marketplace.install(FindNextjsUpgradeIssues, NextjsSearch);
|
|
219
|
+
await marketplace.install(FindNextjsProjectsBelow, NextjsSearch);
|
|
220
|
+
await marketplace.install(FindNextjsProjectsAtLeast, NextjsSearch);
|
|
221
|
+
await marketplace.install(MarkPendingNextjsUpgrade, NextjsSearch);
|
|
222
|
+
await marketplace.install(MetadataToViewportExport, Nextjs14);
|
|
223
|
+
await marketplace.install(NextRequestGeoIp, Nextjs15);
|
|
224
|
+
await marketplace.install(AddVercelFunctions, Nextjs15);
|
|
225
|
+
await marketplace.install(AppDirRuntimeConfigExperimentalEdge, Nextjs15);
|
|
226
|
+
await marketplace.install(FetchCacheDefaults, Nextjs15);
|
|
227
|
+
await marketplace.install(RouteHandlerCacheDefaults, Nextjs15);
|
|
228
|
+
await marketplace.install(RemoveExperimentalPpr, Nextjs16);
|
|
229
|
+
await marketplace.install(RemoveExperimentalPprConfig, Nextjs16);
|
|
230
|
+
await marketplace.install(RemoveEslintConfig, Nextjs16);
|
|
231
|
+
await marketplace.install(KeepImageDefaults, Nextjs16);
|
|
232
|
+
|
|
153
233
|
// React 18
|
|
154
234
|
await marketplace.install(ReplaceReactDomRender, React18);
|
|
155
235
|
await marketplace.install(ReplaceUnmountComponentAtNode, React18);
|
|
@@ -252,6 +332,49 @@ export {ReplaceActImport} from './react19/replace-act-import';
|
|
|
252
332
|
export {ReplaceUseFormState} from './react19/replace-use-form-state';
|
|
253
333
|
export {ReplaceReactShallowRenderer} from './react19/replace-react-shallow-renderer';
|
|
254
334
|
|
|
335
|
+
// Next.js 14
|
|
336
|
+
export {NextOgImport} from "./nextjs14/next-og-import";
|
|
337
|
+
|
|
338
|
+
// Next.js 16
|
|
339
|
+
export {MiddlewareToProxy} from "./nextjs16/middleware-to-proxy";
|
|
340
|
+
export {StableCacheApis} from "./nextjs16/stable-cache-apis";
|
|
341
|
+
|
|
342
|
+
// Next.js 15
|
|
343
|
+
export {AsyncRequestApis} from './nextjs15/async-request-apis';
|
|
344
|
+
export {AsyncParams} from './nextjs15/async-params';
|
|
345
|
+
export {BuiltInNextFont} from "./nextjs13/next-modules";
|
|
346
|
+
export {NextFutureImageToImage} from "./nextjs13/next-modules";
|
|
347
|
+
export {NextImageToLegacyImage} from "./nextjs13/next-image-to-legacy-image";
|
|
348
|
+
export {NewLink} from "./nextjs13/new-link";
|
|
349
|
+
export {RemoveExperimentalAppDir} from "./nextjs-config/config-recipes";
|
|
350
|
+
export {OutputStandalone} from "./nextjs-config/config-recipes";
|
|
351
|
+
export {RemoveExperimentalServerActions} from "./nextjs-config/config-recipes";
|
|
352
|
+
export {ImagesDomainsToRemotePatterns} from "./nextjs-config/config-recipes";
|
|
353
|
+
export {ServerExternalPackages} from "./nextjs-config/config-recipes";
|
|
354
|
+
export {BundlePagesRouterDependencies} from "./nextjs-config/config-recipes";
|
|
355
|
+
export {OutputFileTracingOptions} from "./nextjs-config/config-recipes";
|
|
356
|
+
export {RemoveInstrumentationHook} from "./nextjs-config/config-recipes";
|
|
357
|
+
export {RemoveSwcMinify} from "./nextjs-config/config-recipes";
|
|
358
|
+
export {TurbopackConfig} from "./nextjs-config/config-recipes";
|
|
359
|
+
export {CacheComponentsConfig} from "./nextjs-config/config-recipes";
|
|
360
|
+
export {SkipProxyUrlNormalize} from "./nextjs-config/config-recipes";
|
|
361
|
+
export {RevalidateTagProfile} from "./nextjs16/revalidate-tag-profile";
|
|
362
|
+
export {NextLintToEslint} from "./nextjs16/next-lint-to-eslint";
|
|
363
|
+
export {FindPagesRouterCandidates} from "./nextjs-search/find-pages-router-candidates";
|
|
364
|
+
export {FindNextjsUpgradeIssues} from "./nextjs-search/find-nextjs-upgrade-issues";
|
|
365
|
+
export {MarkPendingNextjsUpgrade} from "./nextjs-search/mark-pending-nextjs-upgrade";
|
|
366
|
+
export {FindNextjsProjectsAtLeast, FindNextjsProjectsBelow} from "./nextjs-search/find-nextjs-projects-below";
|
|
367
|
+
export {MetadataToViewportExport} from "./nextjs14/metadata-to-viewport-export";
|
|
368
|
+
export {NextRequestGeoIp} from "./nextjs15/next-request-geo-ip";
|
|
369
|
+
export {AddVercelFunctions} from "./nextjs15/add-vercel-functions";
|
|
370
|
+
export {AppDirRuntimeConfigExperimentalEdge} from "./nextjs15/app-dir-runtime-config-experimental-edge";
|
|
371
|
+
export {FetchCacheDefaults} from "./nextjs15/fetch-cache-defaults";
|
|
372
|
+
export {RouteHandlerCacheDefaults} from "./nextjs15/route-handler-cache-defaults";
|
|
373
|
+
export {RemoveExperimentalPpr} from "./nextjs16/remove-experimental-ppr";
|
|
374
|
+
export {RemoveExperimentalPprConfig} from "./nextjs16/remove-experimental-ppr-config";
|
|
375
|
+
export {RemoveEslintConfig} from "./nextjs-config/config-recipes";
|
|
376
|
+
export {KeepImageDefaults} from "./nextjs-config/config-recipes";
|
|
377
|
+
|
|
255
378
|
// React 18
|
|
256
379
|
export {ReplaceReactDomRender} from './react18/replace-reactdom-render';
|
|
257
380
|
export {ReplaceUnmountComponentAtNode} from './react18/replace-unmount-component-at-node';
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import {isStartOrBuildScript} from './cra-script';
|
|
2
2
|
import {elementsOf, findMember, getMemberKeyName, isLiteral, isObject, Json, membersOf, valueAt} from "@openrewrite/rewrite/json";
|
|
3
|
+
import {baseName, DEPENDENCY_SECTIONS, dirName, hasDependency, isPackageJson, memberObject, nearestRoot, rootObject, stringValue} from "../../util/package-json";
|
|
4
|
+
|
|
5
|
+
export {baseName, dirName, hasDependency, isPackageJson, memberObject, nearestRoot, rootObject, stringValue};
|
|
3
6
|
|
|
4
7
|
export const REACT_SCRIPTS = "react-scripts";
|
|
5
8
|
export const VITE = "vite";
|
|
@@ -9,52 +12,10 @@ export const VITE_PLUGIN_REACT = "@vitejs/plugin-react";
|
|
|
9
12
|
export const CRA_ENV_PREFIX = "REACT_APP_";
|
|
10
13
|
export const VITE_ENV_PREFIX = "VITE_";
|
|
11
14
|
|
|
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
15
|
export function joinPath(dir: string, name: string): string {
|
|
25
16
|
return dir === "" ? name : `${dir}/${name}`;
|
|
26
17
|
}
|
|
27
18
|
|
|
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 memberObject(obj: Json.Object, name: string): Json.Object | undefined {
|
|
45
|
-
const value = findMember(obj, name)?.value;
|
|
46
|
-
return value && isObject(value) ? value : undefined;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** The string a JSON value holds, or `undefined` when it is not a string literal. */
|
|
50
|
-
export function stringValue(value: Json | undefined): string | undefined {
|
|
51
|
-
return value && isLiteral(value) && typeof value.value === "string" ? value.value : undefined;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export function hasDependency(pkg: Json.Object, name: string): boolean {
|
|
55
|
-
return DEPENDENCY_SECTIONS.some(section => valueAt(pkg, [section, name]) !== undefined);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
19
|
/** True when a package.json script invokes `react-scripts`. */
|
|
59
20
|
export function scriptsUseReactScripts(pkg: Json.Object): boolean {
|
|
60
21
|
const scripts = memberObject(pkg, "scripts");
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import {ExecutionContext} from "@openrewrite/rewrite";
|
|
2
|
+
import {J} from "@openrewrite/rewrite/java";
|
|
3
|
+
import {
|
|
4
|
+
appendProp, expressionFrom, findProp, isTrue, moveToTop, NextConfigRecipe, objectValue, removeNested,
|
|
5
|
+
propName, removeProp, renameProp, replaceProp, setNested
|
|
6
|
+
} from "./next-config";
|
|
7
|
+
|
|
8
|
+
type Obj = J.NewClass;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @see https://nextjs.org/blog/next-13-4#nextjs-app-router
|
|
12
|
+
*/
|
|
13
|
+
export class RemoveExperimentalAppDir extends NextConfigRecipe {
|
|
14
|
+
readonly name = "io.moderne.nextjs.13.remove-experimental-app-dir";
|
|
15
|
+
readonly displayName = "Remove `experimental.appDir`";
|
|
16
|
+
readonly description = "The App Router is stable since Next.js 13.4 and `experimental.appDir` is no longer read. Removes the option, and `experimental` when it ends up empty.";
|
|
17
|
+
|
|
18
|
+
protected edit(root: Obj): Obj {
|
|
19
|
+
return removeNested(root, 'experimental', 'appDir');
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @see https://nextjs.org/blog/next-12-2
|
|
25
|
+
*/
|
|
26
|
+
export class OutputStandalone extends NextConfigRecipe {
|
|
27
|
+
readonly name = "io.moderne.nextjs.13.output-standalone";
|
|
28
|
+
readonly displayName = "Use `output: 'standalone'`";
|
|
29
|
+
readonly description = "Next.js 12.2 made the standalone output stable: `experimental.outputStandalone: true` became the top-level `output: 'standalone'`, and the experimental flag is no longer read.";
|
|
30
|
+
|
|
31
|
+
protected async edit(root: Obj, _ctx: ExecutionContext, cursor: any): Promise<Obj> {
|
|
32
|
+
const exp = objectValue(findProp(root, 'experimental'));
|
|
33
|
+
const flag = exp && findProp(exp, 'outputStandalone');
|
|
34
|
+
if (!flag || !isTrue(flag) || findProp(root, 'output')) return root;
|
|
35
|
+
const moved = moveToTop(root, 'experimental', 'outputStandalone', 'output');
|
|
36
|
+
const literal = await expressionFrom("'standalone'", flag.initializer as any, cursor);
|
|
37
|
+
return replaceProp(moved, 'output', p => ({...p, initializer: {...literal, prefix: (p.initializer as any).prefix}}) as any);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @see https://nextjs.org/blog/next-14#server-actions-stable
|
|
43
|
+
*/
|
|
44
|
+
export class RemoveExperimentalServerActions extends NextConfigRecipe {
|
|
45
|
+
readonly name = "io.moderne.nextjs.14.remove-experimental-server-actions";
|
|
46
|
+
readonly displayName = "Remove `experimental.serverActions: true`";
|
|
47
|
+
readonly description = "Server Actions are stable in Next.js 14, so the `experimental.serverActions: true` flag is not needed. A `serverActions` object with options is kept.";
|
|
48
|
+
|
|
49
|
+
protected edit(root: Obj): Obj {
|
|
50
|
+
return removeNested(root, 'experimental', 'serverActions', true);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @see https://nextjs.org/docs/app/guides/upgrading/version-16#imagesdomains-configuration-deprecated
|
|
56
|
+
*/
|
|
57
|
+
export class ImagesDomainsToRemotePatterns extends NextConfigRecipe {
|
|
58
|
+
readonly name = "io.moderne.nextjs.14.images-domains-to-remote-patterns";
|
|
59
|
+
readonly displayName = "Replace `images.domains` with `images.remotePatterns`";
|
|
60
|
+
readonly description = "`images.domains` is deprecated since Next.js 14. Converts a list of host names to `remotePatterns` entries that match the host over both `http` and `https`, as `domains` did. Lists with anything but string literals are left alone.";
|
|
61
|
+
|
|
62
|
+
protected async edit(root: Obj, _ctx: ExecutionContext, cursor: any): Promise<Obj> {
|
|
63
|
+
const images = objectValue(findProp(root, 'images'));
|
|
64
|
+
const domains = images && findProp(images, 'domains');
|
|
65
|
+
const list = domains?.initializer as any;
|
|
66
|
+
if (!images || !domains || list?.kind !== J.Kind.NewArray || findProp(images, 'remotePatterns')) return root;
|
|
67
|
+
const hosts = (list.initializer?.elements ?? []).map((e: any) => e.element);
|
|
68
|
+
if (hosts.length === 0 || !hosts.every((h: any) => h.kind === J.Kind.Literal && typeof h.value === 'string' && /^[A-Za-z0-9.-]+$/.test(h.value))) return root;
|
|
69
|
+
const text = `[${hosts.map((h: any) => `{ hostname: '${h.value}' }`).join(', ')}]`;
|
|
70
|
+
const expression = await expressionFrom(text, list, cursor);
|
|
71
|
+
if (!expression) return root;
|
|
72
|
+
const updated = replaceProp(images, 'domains', p => ({...renameProp(p, 'remotePatterns'), initializer: {...expression, prefix: list.prefix}}) as any);
|
|
73
|
+
return setNested(root, 'images', updated);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @see https://nextjs.org/docs/app/guides/upgrading/version-15#serverexternalpackages
|
|
79
|
+
*/
|
|
80
|
+
export class ServerExternalPackages extends NextConfigRecipe {
|
|
81
|
+
readonly name = "io.moderne.nextjs.15.server-external-packages";
|
|
82
|
+
readonly displayName = "Use `serverExternalPackages`";
|
|
83
|
+
readonly description = "Next.js 15 made `experimental.serverComponentsExternalPackages` stable as the top-level `serverExternalPackages`.";
|
|
84
|
+
|
|
85
|
+
protected edit(root: Obj): Obj {
|
|
86
|
+
return moveToTop(root, 'experimental', 'serverComponentsExternalPackages', 'serverExternalPackages');
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @see https://nextjs.org/docs/app/guides/upgrading/version-15#bundlepagesrouterdependencies
|
|
92
|
+
*/
|
|
93
|
+
export class BundlePagesRouterDependencies extends NextConfigRecipe {
|
|
94
|
+
readonly name = "io.moderne.nextjs.15.bundle-pages-router-dependencies";
|
|
95
|
+
readonly displayName = "Use `bundlePagesRouterDependencies`";
|
|
96
|
+
readonly description = "Next.js 15 made `experimental.bundlePagesExternals` stable as the top-level `bundlePagesRouterDependencies`.";
|
|
97
|
+
|
|
98
|
+
protected edit(root: Obj): Obj {
|
|
99
|
+
return moveToTop(root, 'experimental', 'bundlePagesExternals', 'bundlePagesRouterDependencies');
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* @see https://nextjs.org/blog/next-15#other-changes
|
|
105
|
+
*/
|
|
106
|
+
export class OutputFileTracingOptions extends NextConfigRecipe {
|
|
107
|
+
readonly name = "io.moderne.nextjs.15.output-file-tracing-options";
|
|
108
|
+
readonly displayName = "Move `experimental.outputFileTracing*` options to the top level";
|
|
109
|
+
readonly description = "Next.js 15 made `outputFileTracingRoot`, `outputFileTracingExcludes` and `outputFileTracingIncludes` stable, so they are top-level options and no longer part of `experimental`.";
|
|
110
|
+
|
|
111
|
+
protected edit(root: Obj): Obj {
|
|
112
|
+
return ['outputFileTracingRoot', 'outputFileTracingExcludes', 'outputFileTracingIncludes']
|
|
113
|
+
.reduce((r, name) => moveToTop(r, 'experimental', name, name), root);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* @see https://nextjs.org/blog/next-15#instrumentationjs-stable
|
|
119
|
+
*/
|
|
120
|
+
export class RemoveInstrumentationHook extends NextConfigRecipe {
|
|
121
|
+
readonly name = "io.moderne.nextjs.15.remove-instrumentation-hook";
|
|
122
|
+
readonly displayName = "Remove `experimental.instrumentationHook`";
|
|
123
|
+
readonly description = "`instrumentation.ts` is stable in Next.js 15 and `experimental.instrumentationHook: true` is no longer needed.";
|
|
124
|
+
|
|
125
|
+
protected edit(root: Obj): Obj {
|
|
126
|
+
return removeNested(root, 'experimental', 'instrumentationHook', true);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* @see https://nextjs.org/blog/next-15#other-changes
|
|
132
|
+
*/
|
|
133
|
+
export class RemoveSwcMinify extends NextConfigRecipe {
|
|
134
|
+
readonly name = "io.moderne.nextjs.15.remove-swc-minify";
|
|
135
|
+
readonly displayName = "Remove `swcMinify`";
|
|
136
|
+
readonly description = "SWC minification is the only minifier since Next.js 15, so the `swcMinify` option is unknown and only produces a warning.";
|
|
137
|
+
|
|
138
|
+
protected edit(root: Obj): Obj {
|
|
139
|
+
return removeProp(root, 'swcMinify');
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* @see https://nextjs.org/docs/app/guides/upgrading/version-16#turbopack-configuration-location
|
|
145
|
+
*/
|
|
146
|
+
export class TurbopackConfig extends NextConfigRecipe {
|
|
147
|
+
readonly name = "io.moderne.nextjs.16.turbopack-config";
|
|
148
|
+
readonly displayName = "Use the top-level `turbopack` option";
|
|
149
|
+
readonly description = "Turbopack is stable in Next.js 16 and its options moved from `experimental.turbo` to the top-level `turbopack`.";
|
|
150
|
+
|
|
151
|
+
protected edit(root: Obj): Obj {
|
|
152
|
+
return moveToTop(root, 'experimental', 'turbo', 'turbopack');
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* @see https://nextjs.org/docs/app/guides/upgrading/version-16#experimentaldynamicio-and-experimentalusecache
|
|
158
|
+
*/
|
|
159
|
+
export class CacheComponentsConfig extends NextConfigRecipe {
|
|
160
|
+
readonly name = "io.moderne.nextjs.16.cache-components-config";
|
|
161
|
+
readonly displayName = "Use the top-level `cacheComponents` option";
|
|
162
|
+
readonly description = "Next.js 16 removed `experimental.dynamicIO`; Cache Components are enabled with the top-level `cacheComponents`. Moves the option there, which also enables the Cache Components caching model for the whole app. `experimental.ppr` has no replacement here and is found by the search recipe.";
|
|
163
|
+
|
|
164
|
+
protected edit(root: Obj): Obj {
|
|
165
|
+
return moveToTop(root, 'experimental', 'dynamicIO', 'cacheComponents');
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* @see https://nextjs.org/docs/app/guides/upgrading/version-16#middleware-to-proxy
|
|
171
|
+
*/
|
|
172
|
+
export class SkipProxyUrlNormalize extends NextConfigRecipe {
|
|
173
|
+
readonly name = "io.moderne.nextjs.16.skip-proxy-url-normalize";
|
|
174
|
+
readonly displayName = "Rename `skipMiddlewareUrlNormalize` to `skipProxyUrlNormalize`";
|
|
175
|
+
readonly description = "Next.js 16 renamed Middleware to Proxy and the `skipMiddlewareUrlNormalize` option with it.";
|
|
176
|
+
|
|
177
|
+
protected edit(root: Obj): Obj {
|
|
178
|
+
const p = findProp(root, 'skipMiddlewareUrlNormalize');
|
|
179
|
+
return p && !findProp(root, 'skipProxyUrlNormalize') ? replaceProp(root, 'skipMiddlewareUrlNormalize', q => renameProp(q, 'skipProxyUrlNormalize')) : root;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* @see https://nextjs.org/docs/app/guides/upgrading/version-16#next-lint-command
|
|
186
|
+
*/
|
|
187
|
+
export class RemoveEslintConfig extends NextConfigRecipe {
|
|
188
|
+
readonly name = "io.moderne.nextjs.16.remove-eslint-config";
|
|
189
|
+
readonly displayName = "Remove the `eslint` option";
|
|
190
|
+
readonly description = "Next.js 16 no longer runs ESLint during `next build`, so `eslint: { ignoreDuringBuilds, dirs }` is unknown. Removes the option. Run ESLint in CI or as a build step instead.";
|
|
191
|
+
|
|
192
|
+
protected edit(root: Obj): Obj {
|
|
193
|
+
return objectValue(findProp(root, 'eslint')) ? removeProp(root, 'eslint') : root;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** The `next/image` defaults that Next.js 16 changed, written out with their Next.js 15 values. */
|
|
198
|
+
const OLD_IMAGE_DEFAULTS = "{ minimumCacheTTL: 60, imageSizes: [16, 32, 48, 64, 96, 128, 256, 384] }";
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* @see https://nextjs.org/docs/app/guides/upgrading/version-16#minimumcachettl-default-breaking-change
|
|
202
|
+
*/
|
|
203
|
+
export class KeepImageDefaults extends NextConfigRecipe {
|
|
204
|
+
readonly name = "io.moderne.nextjs.16.keep-image-defaults";
|
|
205
|
+
readonly displayName = "Keep the Next.js 15 `images` defaults";
|
|
206
|
+
readonly description = "Next.js 16 raised the default `images.minimumCacheTTL` from 60 seconds to 4 hours and removed 16 from the default `images.imageSizes`. Adds both with their old values to an existing `images` option, unless they are set. A config without `images` is left alone.";
|
|
207
|
+
|
|
208
|
+
protected async edit(root: Obj, _ctx: ExecutionContext, cursor: any): Promise<Obj> {
|
|
209
|
+
const images = objectValue(findProp(root, 'images'));
|
|
210
|
+
// A spread or computed key may already set a default, and appending after it would override that
|
|
211
|
+
if (!images || images.body!.statements.some(s => propName(s.element) === undefined)) return root;
|
|
212
|
+
const missing = ['minimumCacheTTL', 'imageSizes'].filter(name => !findProp(images, name));
|
|
213
|
+
if (missing.length === 0) return root;
|
|
214
|
+
const defaults = await expressionFrom(OLD_IMAGE_DEFAULTS, images, cursor) as Obj;
|
|
215
|
+
if (!defaults) return root;
|
|
216
|
+
const updated = missing.reduce((obj, name) => appendProp(obj, findProp(defaults, name)!), images);
|
|
217
|
+
return setNested(root, 'images', updated);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import {and, check, ExecutionContext, findMarker, Marker, not, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
2
|
+
import {hasSourcePath, isVendoredOrBundled, JavaScriptVisitor, JS, Template, withStringValue} from "@openrewrite/rewrite/javascript";
|
|
3
|
+
import {isIdentifier, J, rightPadded, emptySpace} from "@openrewrite/rewrite/java";
|
|
4
|
+
|
|
5
|
+
/** `next.config.js`, `.cjs`, `.mjs`, `.ts` and `.mts`, in any folder (monorepo apps keep their own). */
|
|
6
|
+
export const NEXT_CONFIG_FILE = /(^|\/)next\.config\.[cm]?[jt]s$/;
|
|
7
|
+
|
|
8
|
+
type Obj = J.NewClass;
|
|
9
|
+
type Prop = JS.PropertyAssignment;
|
|
10
|
+
|
|
11
|
+
/** The name of a property written as an identifier or a string literal key. */
|
|
12
|
+
export function propName(p: any): string | undefined {
|
|
13
|
+
if (p?.kind !== JS.Kind.PropertyAssignment) return undefined;
|
|
14
|
+
const n = (p as Prop).name.element as any;
|
|
15
|
+
if (isIdentifier(n)) return n.simpleName;
|
|
16
|
+
return n?.kind === J.Kind.Literal && typeof n.value === 'string' ? n.value : undefined;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function isObject(e: any): e is Obj {
|
|
20
|
+
return e?.kind === J.Kind.NewClass && !e.class && !!e.body;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function props(obj: Obj): J.RightPadded<any>[] {
|
|
24
|
+
return obj.body!.statements;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function findProp(obj: Obj, name: string): Prop | undefined {
|
|
28
|
+
return props(obj).map(s => s.element).find(p => propName(p) === name) as Prop | undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function objectValue(prop: Prop | undefined): Obj | undefined {
|
|
32
|
+
return prop && isObject(prop.initializer) ? prop.initializer : undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function isTrue(prop: Prop | undefined): boolean {
|
|
36
|
+
const v = prop?.initializer as any;
|
|
37
|
+
return v?.kind === J.Kind.Literal && v.value === true;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function withStatements(obj: Obj, statements: J.RightPadded<any>[]): Obj {
|
|
41
|
+
return {...obj, body: {...obj.body!, statements}} as Obj;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Removes a property. Its comments move to the next property, or to the end of the object when it was the last one, so
|
|
46
|
+
* they are not lost; the first property's layout moves along too. */
|
|
47
|
+
export function removeProp(obj: Obj, name: string): Obj {
|
|
48
|
+
const list = props(obj);
|
|
49
|
+
const i = list.findIndex(s => propName(s.element) === name);
|
|
50
|
+
if (i < 0) return obj;
|
|
51
|
+
const rest = list.filter((_, j) => j !== i);
|
|
52
|
+
const removed = list[i].element.prefix as J.Space;
|
|
53
|
+
const next = rest[i];
|
|
54
|
+
if (next) {
|
|
55
|
+
if (i === 0 || removed.comments.length > 0) {
|
|
56
|
+
const prefix = {...removed, comments: [...removed.comments, ...(next.element.prefix as J.Space).comments]};
|
|
57
|
+
rest[i] = {...next, element: {...next.element, prefix}};
|
|
58
|
+
}
|
|
59
|
+
return withStatements(obj, rest);
|
|
60
|
+
}
|
|
61
|
+
const end = obj.body!.end;
|
|
62
|
+
const comments = removed.comments;
|
|
63
|
+
const kept = comments.length === 0 ? end :
|
|
64
|
+
{...end, whitespace: removed.whitespace, comments: [...comments.slice(0, -1), {...comments[comments.length - 1], suffix: end.whitespace}, ...end.comments]};
|
|
65
|
+
return {...obj, body: {...obj.body!, statements: rest, end: kept}} as Obj;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function replaceProp(obj: Obj, name: string, update: (p: Prop) => Prop): Obj {
|
|
69
|
+
return withStatements(obj, props(obj).map(s => propName(s.element) === name ? {...s, element: update(s.element)} : s));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function setNested(obj: Obj, name: string, inner: Obj): Obj {
|
|
73
|
+
return replaceProp(obj, name, p => ({...p, initializer: inner}) as Prop);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function renameProp(p: Prop, newName: string): Prop {
|
|
77
|
+
const n = p.name.element as any;
|
|
78
|
+
const renamed = isIdentifier(n) ? {...n, simpleName: newName} : withStringValue(n, newName);
|
|
79
|
+
return {...p, name: {...p.name, element: renamed}} as Prop;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Appends a property at the end of the object, laid out like the first one. */
|
|
83
|
+
export function appendProp(obj: Obj, prop: Prop, fallbackPrefix?: J.Space): Obj {
|
|
84
|
+
const list = props(obj);
|
|
85
|
+
const prefix = list.length > 0 ? list[0].element.prefix : fallbackPrefix ?? prop.prefix;
|
|
86
|
+
const appended = rightPadded({...prop, prefix} as any, emptySpace);
|
|
87
|
+
const last = list[list.length - 1];
|
|
88
|
+
const trailingComma = last && findMarker(last, J.Markers.TrailingComma);
|
|
89
|
+
if (!trailingComma) return withStatements(obj, [...list, appended]);
|
|
90
|
+
// A trailing comma belongs after the property that is last now, not after the old one: the comma we add separates them
|
|
91
|
+
const stripped = {...last, markers: {...last.markers, markers: last.markers.markers.filter((m: Marker) => m.kind !== J.Markers.TrailingComma)}};
|
|
92
|
+
const withComma = {...appended, markers: {...appended.markers, markers: [...appended.markers.markers, trailingComma]}};
|
|
93
|
+
return withStatements(obj, [...list.slice(0, -1), stripped, withComma]);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Moves `from[0].from[1]` to a top-level property, dropping the parent when it ends up empty. */
|
|
97
|
+
export function moveToTop(root: Obj, parent: string, from: string, to: string): Obj {
|
|
98
|
+
const parentObj = objectValue(findProp(root, parent));
|
|
99
|
+
const prop = parentObj && findProp(parentObj, from);
|
|
100
|
+
if (!parentObj || !prop || findProp(root, to)) return root;
|
|
101
|
+
const left = removeProp(parentObj, from);
|
|
102
|
+
const withoutParent = props(left).length === 0 ? removeProp(root, parent) : setNested(root, parent, left);
|
|
103
|
+
return appendProp(withoutParent, renameProp(prop, to), findProp(root, parent)!.prefix);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Removes `parent.name`, and the parent when nothing else is left in it. */
|
|
107
|
+
export function removeNested(root: Obj, parent: string, name: string, onlyTrue = false): Obj {
|
|
108
|
+
const parentObj = objectValue(findProp(root, parent));
|
|
109
|
+
const prop = parentObj && findProp(parentObj, name);
|
|
110
|
+
if (!parentObj || !prop || onlyTrue && !isTrue(prop)) return root;
|
|
111
|
+
const left = removeProp(parentObj, name);
|
|
112
|
+
return props(left).length === 0 ? removeProp(root, parent) : setNested(root, parent, left);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Builds an expression, such as an array literal, from source text. */
|
|
116
|
+
export async function expressionFrom(text: string, anchor: J, cursor: any): Promise<any> {
|
|
117
|
+
return Template.builder().code(text).build().apply(anchor, cursor);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Names the identifiers a statement other than a declaration exports. Of a call such as `withSentryConfig(nextConfig, options)`
|
|
122
|
+
* only the first argument counts: the plugin wrappers take the config first and their own options after it.
|
|
123
|
+
*/
|
|
124
|
+
function exportedNames(cu: JS.CompilationUnit): Set<string> {
|
|
125
|
+
const names = new Set<string>();
|
|
126
|
+
const seen = new Set<object>();
|
|
127
|
+
const visit = (node: any): void => {
|
|
128
|
+
if (!node || typeof node !== 'object' || seen.has(node)) return;
|
|
129
|
+
seen.add(node);
|
|
130
|
+
if (isIdentifier(node)) names.add(node.simpleName);
|
|
131
|
+
if (node.kind === J.Kind.MethodInvocation) {
|
|
132
|
+
visit(node.select);
|
|
133
|
+
visit(node.arguments?.elements?.[0]);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
for (const [key, value] of Object.entries(node)) if (key !== 'type') visit(value);
|
|
137
|
+
};
|
|
138
|
+
for (const s of cu.statements) {
|
|
139
|
+
const kind = (s.element as J).kind;
|
|
140
|
+
if (kind !== J.Kind.VariableDeclarations && kind !== JS.Kind.Import && kind !== J.Kind.MethodDeclaration && kind !== J.Kind.ClassDeclaration) visit(s.element);
|
|
141
|
+
}
|
|
142
|
+
return names;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Base for recipes that edit the object literal `next.config.*` exports. Only object literals at the top level of the
|
|
147
|
+
* file are edited, so the option objects nested in them are left alone. A top-level object that initializes a variable
|
|
148
|
+
* is edited only when an export statement exports that variable, directly or as the first argument of a wrapper call, so
|
|
149
|
+
* a `const sentryOptions = {...}` that is passed after the config stays as it is. An object written inside the export
|
|
150
|
+
* statement itself, such as the second argument of `withSentryConfig(nextConfig, {...})`, is not told apart from the
|
|
151
|
+
* config.
|
|
152
|
+
*/
|
|
153
|
+
export abstract class NextConfigRecipe extends Recipe {
|
|
154
|
+
/** Returns the changed root config object, or the same object when there is nothing to do. */
|
|
155
|
+
protected abstract edit(root: Obj, ctx: ExecutionContext, cursor: any): Obj | Promise<Obj>;
|
|
156
|
+
|
|
157
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
158
|
+
const recipe = this;
|
|
159
|
+
return check(and(hasSourcePath("**/next.config.{js,cjs,mjs,ts,cts,mts}"), not(isVendoredOrBundled())), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
160
|
+
private active = false;
|
|
161
|
+
private depth = 0;
|
|
162
|
+
private exported = new Set<string>();
|
|
163
|
+
|
|
164
|
+
protected override async visitJsCompilationUnit(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<J | undefined> {
|
|
165
|
+
this.active = NEXT_CONFIG_FILE.test(cu.sourcePath.replace(/\\/g, '/'));
|
|
166
|
+
if (this.active) this.exported = exportedNames(cu);
|
|
167
|
+
return this.active ? super.visitJsCompilationUnit(cu, ctx) : cu;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
override async visitNewClass(newClass: J.NewClass, ctx: ExecutionContext): Promise<J | undefined> {
|
|
171
|
+
const root = this.depth === 0;
|
|
172
|
+
this.depth++;
|
|
173
|
+
const visited = await super.visitNewClass(newClass, ctx) as J.NewClass;
|
|
174
|
+
this.depth--;
|
|
175
|
+
if (!root || !isObject(visited) || !this.isConfigCandidate()) return visited;
|
|
176
|
+
const edited = await recipe.edit(visited, ctx, this.cursor);
|
|
177
|
+
return edited !== visited && edited.body!.statements.length === 0 && edited.body!.end.comments.length === 0 && edited.body!.end.whitespace.includes('\n') ?
|
|
178
|
+
{...edited, body: {...edited.body!, end: emptySpace}} as Obj : edited;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** False for an object that initializes a variable no export statement mentions. */
|
|
182
|
+
private isConfigCandidate(): boolean {
|
|
183
|
+
const owner = this.cursor.parentTree()?.value as J | undefined;
|
|
184
|
+
if (owner?.kind !== J.Kind.NamedVariable) return true;
|
|
185
|
+
const name = (owner as J.VariableDeclarations.NamedVariable).name;
|
|
186
|
+
return !isIdentifier(name) || this.exported.has(name.simpleName);
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
}
|