Skip to content

Commit

Permalink
feat(next-codemod): add app-dir-runtime-config-experimental-edge (#…
Browse files Browse the repository at this point in the history
…70968)

### Why?

We had breaking change in App Router at
#70480 where it strictly checks
`runtime` route segment config. If it is set to `experimental-edge`,
will throw. Therefore we support a codemod to replace it to `edge`.
  • Loading branch information
devjiwonchoi authored and kdy1 committed Oct 10, 2024
1 parent d70e31f commit 881bd33
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/next-codemod/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const runtime = "edge";
export default function Page() {
return <div>hello world</div>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const runtime = "edge";
export default function Page() {
return <div>hello world</div>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const runtime = "experimental-edge";
export default function Page() {
return <div>hello world</div>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const runtime = "edge";
export default function Page() {
return <div>hello world</div>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return <div>hello world</div>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return <div>hello world</div>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const runtime = "nodejs";
export default function Page() {
return <div>hello world</div>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const runtime = "nodejs";
export default function Page() {
return <div>hello world</div>;
}
Original file line number Diff line number Diff line change
@@ -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' });
}
Original file line number Diff line number Diff line change
@@ -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.withParser('tsx')
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()
}

0 comments on commit 881bd33

Please sign in to comment.