From eaba4bb388f97f21d7a6317e88006670915ece0a Mon Sep 17 00:00:00 2001 From: devjiwonchoi Date: Wed, 9 Oct 2024 02:07:41 +0900 Subject: [PATCH 1/5] feat(next-codemod): add app-dir-runtime-config-experimental-edge --- ...pp-dir-runtime-config-experimental-edge.ts | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 packages/next-codemod/transforms/app-dir-runtime-config-experimental-edge.ts diff --git a/packages/next-codemod/transforms/app-dir-runtime-config-experimental-edge.ts b/packages/next-codemod/transforms/app-dir-runtime-config-experimental-edge.ts new file mode 100644 index 0000000000000..862ca1f0ec67b --- /dev/null +++ b/packages/next-codemod/transforms/app-dir-runtime-config-experimental-edge.ts @@ -0,0 +1,40 @@ +import type { API, FileInfo } from 'jscodeshift' + +export default function transformer(file: FileInfo, api: API) { + if ( + process.env.NODE_ENV !== 'test' && + !/[/\\]app[/\\].*?(page|layout|route)\.[^/\\]+$/.test(file.path) + ) { + return file.source + } + + const j = api.jscodeshift + const root = j(file.source) + + const runtimeExport = root.find(j.ExportNamedDeclaration, { + declaration: { + type: 'VariableDeclaration', + declarations: [ + { + id: { name: 'runtime' }, + }, + ], + }, + }) + + if (runtimeExport.size() !== 1) { + return file.source + } + + const runtimeValue = runtimeExport.find(j.StringLiteral, { + value: 'experimental-edge', + }) + + if (runtimeValue.size() !== 1) { + return file.source + } + + runtimeValue.replaceWith(j.stringLiteral('edge')) + + return root.toSource() +} From 983d290120836850763f16931e637d1cdb934c18 Mon Sep 17 00:00:00 2001 From: devjiwonchoi Date: Wed, 9 Oct 2024 02:44:00 +0900 Subject: [PATCH 2/5] chore: add to choices --- packages/next-codemod/lib/utils.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/next-codemod/lib/utils.ts b/packages/next-codemod/lib/utils.ts index 17e4083973286..6308a45a1d5f5 100644 --- a/packages/next-codemod/lib/utils.ts +++ b/packages/next-codemod/lib/utils.ts @@ -116,4 +116,10 @@ export const TRANSFORMER_INQUIRER_CHOICES = [ value: 'next-async-request-api', version: '15.0.0-canary.171', }, + { + title: + 'Transforms `experimental-edge` to `edge` in the `runtime` route segment configuration within the App Router', + value: 'app-dir-runtime-config-experimental-edge', + version: '15.0.0-canary.179', + }, ] From b8f054d05c013ccff485d176a046aa2a84a181a0 Mon Sep 17 00:00:00 2001 From: devjiwonchoi Date: Wed, 9 Oct 2024 02:54:52 +0900 Subject: [PATCH 3/5] add tests --- .../already-edge.input.tsx | 4 ++++ .../already-edge.output.tsx | 4 ++++ .../basic.input.tsx | 4 ++++ .../basic.output.tsx | 4 ++++ .../no-runtime.input.tsx | 3 +++ .../no-runtime.output.tsx | 3 +++ ...pp-dir-runtime-config-experimental-edge.test.js | 14 ++++++++++++++ 7 files changed, 36 insertions(+) create mode 100644 packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/already-edge.input.tsx create mode 100644 packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/already-edge.output.tsx create mode 100644 packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/basic.input.tsx create mode 100644 packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/basic.output.tsx create mode 100644 packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/no-runtime.input.tsx create mode 100644 packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/no-runtime.output.tsx create mode 100644 packages/next-codemod/transforms/__tests__/app-dir-runtime-config-experimental-edge.test.js diff --git a/packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/already-edge.input.tsx b/packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/already-edge.input.tsx new file mode 100644 index 0000000000000..1087abc543cbb --- /dev/null +++ b/packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/already-edge.input.tsx @@ -0,0 +1,4 @@ +export const runtime = "edge"; +export default function Page() { + return
hello world
; +} \ No newline at end of file diff --git a/packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/already-edge.output.tsx b/packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/already-edge.output.tsx new file mode 100644 index 0000000000000..1087abc543cbb --- /dev/null +++ b/packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/already-edge.output.tsx @@ -0,0 +1,4 @@ +export const runtime = "edge"; +export default function Page() { + return
hello world
; +} \ No newline at end of file diff --git a/packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/basic.input.tsx b/packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/basic.input.tsx new file mode 100644 index 0000000000000..4a82859b7b39a --- /dev/null +++ b/packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/basic.input.tsx @@ -0,0 +1,4 @@ +export const runtime = "experimental-edge"; +export default function Page() { + return
hello world
; +} \ No newline at end of file diff --git a/packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/basic.output.tsx b/packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/basic.output.tsx new file mode 100644 index 0000000000000..1087abc543cbb --- /dev/null +++ b/packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/basic.output.tsx @@ -0,0 +1,4 @@ +export const runtime = "edge"; +export default function Page() { + return
hello world
; +} \ No newline at end of file diff --git a/packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/no-runtime.input.tsx b/packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/no-runtime.input.tsx new file mode 100644 index 0000000000000..f5cdf6d2885e4 --- /dev/null +++ b/packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/no-runtime.input.tsx @@ -0,0 +1,3 @@ +export default function Page() { + return
hello world
; +} \ No newline at end of file diff --git a/packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/no-runtime.output.tsx b/packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/no-runtime.output.tsx new file mode 100644 index 0000000000000..f5cdf6d2885e4 --- /dev/null +++ b/packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/no-runtime.output.tsx @@ -0,0 +1,3 @@ +export default function Page() { + return
hello world
; +} \ No newline at end of file diff --git a/packages/next-codemod/transforms/__tests__/app-dir-runtime-config-experimental-edge.test.js b/packages/next-codemod/transforms/__tests__/app-dir-runtime-config-experimental-edge.test.js new file mode 100644 index 0000000000000..ab00d7678adef --- /dev/null +++ b/packages/next-codemod/transforms/__tests__/app-dir-runtime-config-experimental-edge.test.js @@ -0,0 +1,14 @@ +/* global jest */ +jest.autoMockOff() +const defineTest = require('jscodeshift/dist/testUtils').defineTest +const { readdirSync } = require('fs') +const { join } = require('path') +const fixtureDir = 'app-dir-runtime-config-experimental-edge' +const fixtureDirPath = join(__dirname, '..', '__testfixtures__', fixtureDir) +const fixtures = readdirSync(fixtureDirPath) + .filter(file => file.endsWith('.input.tsx')) + .map(file => file.replace('.input.tsx', '')) +for (const fixture of fixtures) { + const prefix = `${fixtureDir}/${fixture}`; + defineTest(__dirname, fixtureDir, null, prefix, { parser: 'tsx' }); +} \ No newline at end of file From 42b678e23802da987bf215d54d21ee3c91c9df16 Mon Sep 17 00:00:00 2001 From: devjiwonchoi Date: Wed, 9 Oct 2024 03:02:36 +0900 Subject: [PATCH 4/5] fix: use withParser tsx --- .../transforms/app-dir-runtime-config-experimental-edge.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/next-codemod/transforms/app-dir-runtime-config-experimental-edge.ts b/packages/next-codemod/transforms/app-dir-runtime-config-experimental-edge.ts index 862ca1f0ec67b..0cc45cc55a209 100644 --- a/packages/next-codemod/transforms/app-dir-runtime-config-experimental-edge.ts +++ b/packages/next-codemod/transforms/app-dir-runtime-config-experimental-edge.ts @@ -8,7 +8,7 @@ export default function transformer(file: FileInfo, api: API) { return file.source } - const j = api.jscodeshift + const j = api.jscodeshift.withParser('tsx') const root = j(file.source) const runtimeExport = root.find(j.ExportNamedDeclaration, { From 17602594cef6f88df06e5bc09adc2cd62bbde425 Mon Sep 17 00:00:00 2001 From: devjiwonchoi Date: Wed, 9 Oct 2024 03:30:51 +0900 Subject: [PATCH 5/5] test: add nodejs --- .../app-dir-runtime-config-experimental-edge/nodejs.input.tsx | 4 ++++ .../nodejs.output.tsx | 4 ++++ 2 files changed, 8 insertions(+) create mode 100644 packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/nodejs.input.tsx create mode 100644 packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/nodejs.output.tsx diff --git a/packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/nodejs.input.tsx b/packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/nodejs.input.tsx new file mode 100644 index 0000000000000..a890002940286 --- /dev/null +++ b/packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/nodejs.input.tsx @@ -0,0 +1,4 @@ +export const runtime = "nodejs"; +export default function Page() { + return
hello world
; +} \ No newline at end of file diff --git a/packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/nodejs.output.tsx b/packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/nodejs.output.tsx new file mode 100644 index 0000000000000..a890002940286 --- /dev/null +++ b/packages/next-codemod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/nodejs.output.tsx @@ -0,0 +1,4 @@ +export const runtime = "nodejs"; +export default function Page() { + return
hello world
; +} \ No newline at end of file