Unverified Commit f9d40660 authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

feat(next-codemod): add `app-dir-runtime-config-experimental-edge` (#70968)

### Why?

We had breaking change in App Router at
https://github.com/vercel/next.js/pull/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`.
parent 3f393037
......@@ -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',
},
]
export const runtime = "edge";
export default function Page() {
return <div>hello world</div>;
}
\ No newline at end of file
export const runtime = "edge";
export default function Page() {
return <div>hello world</div>;
}
\ No newline at end of file
export const runtime = "experimental-edge";
export default function Page() {
return <div>hello world</div>;
}
\ No newline at end of file
export const runtime = "edge";
export default function Page() {
return <div>hello world</div>;
}
\ No newline at end of file
export default function Page() {
return <div>hello world</div>;
}
\ No newline at end of file
export default function Page() {
return <div>hello world</div>;
}
\ No newline at end of file
export const runtime = "nodejs";
export default function Page() {
return <div>hello world</div>;
}
\ No newline at end of file
export const runtime = "nodejs";
export default function Page() {
return <div>hello world</div>;
}
\ No newline at end of file
/* 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
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()
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment