Skip to content

Commit

Permalink
codemod: migrate ImageResponse imports (#57074)
Browse files Browse the repository at this point in the history
Adding codemod for #56662 for easier migration of `ImageResponse` from `"next/server"` to `"next/og"`
  • Loading branch information
huozhi authored Oct 19, 2023
1 parent ed6b60c commit 194e94e
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ImageResponse, NextResponse } from "next/server";

export {
ImageResponse,
NextResponse
}
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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { NextResponse } from "next/server";

export {
NextResponse
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { NextResponse } from "next/server";

export {
NextResponse
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ImageResponse } from "next/server";

export {
ImageResponse
}
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 packages/next-codemod/transforms/__tests__/next-og-import.test.js
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' });
}
51 changes: 51 additions & 0 deletions packages/next-codemod/transforms/next-og-import.ts
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
}

0 comments on commit 194e94e

Please sign in to comment.