-
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.
codemod: migrate ImageResponse imports (#57074)
Adding codemod for #56662 for easier migration of `ImageResponse` from `"next/server"` to `"next/og"`
- Loading branch information
Showing
8 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
packages/next-codemod/transforms/__testfixtures__/next-og-import/mixed-import.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,6 @@ | ||
import { ImageResponse, NextResponse } from "next/server"; | ||
|
||
export { | ||
ImageResponse, | ||
NextResponse | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/next-codemod/transforms/__testfixtures__/next-og-import/mixed-import.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,7 @@ | ||
import { ImageResponse } from "next/og"; | ||
import { NextResponse } from "next/server"; | ||
|
||
export { | ||
ImageResponse, | ||
NextResponse | ||
} |
5 changes: 5 additions & 0 deletions
5
...next-codemod/transforms/__testfixtures__/next-og-import/next-server-only-import.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,5 @@ | ||
import { NextResponse } from "next/server"; | ||
|
||
export { | ||
NextResponse | ||
} |
5 changes: 5 additions & 0 deletions
5
...ext-codemod/transforms/__testfixtures__/next-og-import/next-server-only-import.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,5 @@ | ||
import { NextResponse } from "next/server"; | ||
|
||
export { | ||
NextResponse | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/next-codemod/transforms/__testfixtures__/next-og-import/og-only-import.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,5 @@ | ||
import { ImageResponse } from "next/server"; | ||
|
||
export { | ||
ImageResponse | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/next-codemod/transforms/__testfixtures__/next-og-import/og-only-import.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,5 @@ | ||
import { ImageResponse } from "next/og"; | ||
|
||
export { | ||
ImageResponse | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/next-codemod/transforms/__tests__/next-og-import.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,16 @@ | ||
/* global jest */ | ||
jest.autoMockOff() | ||
const defineTest = require('jscodeshift/dist/testUtils').defineTest | ||
const { readdirSync } = require('fs') | ||
const { join } = require('path') | ||
|
||
const fixtureDir = 'next-og-import' | ||
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' }); | ||
} |
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,51 @@ | ||
import type { API, FileInfo } from 'jscodeshift' | ||
|
||
const importToChange = 'ImageResponse' | ||
|
||
export default function transformer(file: FileInfo, api: API) { | ||
const j = api.jscodeshift | ||
|
||
// Find import declarations that match the pattern | ||
file.source = j(file.source) | ||
.find(j.ImportDeclaration, { | ||
source: { | ||
value: 'next/server', | ||
}, | ||
}) | ||
.forEach((path) => { | ||
const importSpecifiers = path.node.specifiers | ||
const importNamesToChange = importSpecifiers.filter( | ||
(specifier) => specifier.local.name === importToChange | ||
) | ||
const importsNamesRemained = importSpecifiers.filter( | ||
(specifier) => specifier.local.name !== importToChange | ||
) | ||
|
||
// If the import includes the specified import name, create a new import for it from 'next/og' | ||
|
||
if (importNamesToChange.length > 0) { | ||
// Replace the original import with the remaining specifiers | ||
// path.node.specifiers = remainingSpecifiers | ||
const newImportStatement = j.importDeclaration( | ||
importNamesToChange, | ||
j.stringLiteral('next/og') | ||
) | ||
path.insertBefore(newImportStatement) | ||
} | ||
if (importsNamesRemained.length > 0) { | ||
const remainingSpecifiers = importSpecifiers.filter( | ||
(specifier) => specifier.local.name !== importToChange | ||
) | ||
|
||
const nextServerRemainImportsStatement = j.importDeclaration( | ||
remainingSpecifiers, | ||
j.stringLiteral('next/server') | ||
) | ||
path.insertBefore(nextServerRemainImportsStatement) | ||
} | ||
j(path).remove() | ||
}) | ||
.toSource() | ||
|
||
return file.source | ||
} |