-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(next-codemod): add
app-dir-runtime-config-experimental-edge
(#…
…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
1 parent
a1947e8
commit 07712a0
Showing
11 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
...ansforms/__testfixtures__/app-dir-runtime-config-experimental-edge/already-edge.input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
4 changes: 4 additions & 0 deletions
4
...nsforms/__testfixtures__/app-dir-runtime-config-experimental-edge/already-edge.output.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
4 changes: 4 additions & 0 deletions
4
...emod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/basic.input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
4 changes: 4 additions & 0 deletions
4
...mod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/basic.output.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
3 changes: 3 additions & 0 deletions
3
...transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/no-runtime.input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function Page() { | ||
return <div>hello world</div>; | ||
} |
3 changes: 3 additions & 0 deletions
3
...ransforms/__testfixtures__/app-dir-runtime-config-experimental-edge/no-runtime.output.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function Page() { | ||
return <div>hello world</div>; | ||
} |
4 changes: 4 additions & 0 deletions
4
...mod/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/nodejs.input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
4 changes: 4 additions & 0 deletions
4
...od/transforms/__testfixtures__/app-dir-runtime-config-experimental-edge/nodejs.output.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/next-codemod/transforms/__tests__/app-dir-runtime-config-experimental-edge.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }); | ||
} |
40 changes: 40 additions & 0 deletions
40
packages/next-codemod/transforms/app-dir-runtime-config-experimental-edge.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |