Unverified Commit 194e94ed authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

codemod: migrate ImageResponse imports (#57074)

Adding codemod for #56662 for easier migration of `ImageResponse` from `"next/server"` to `"next/og"`
parent ed6b60c5
import { ImageResponse, NextResponse } from "next/server";
export {
ImageResponse,
NextResponse
}
\ No newline at end of file
import { ImageResponse } from "next/og";
import { NextResponse } from "next/server";
export {
ImageResponse,
NextResponse
}
\ No newline at end of file
import { NextResponse } from "next/server";
export {
NextResponse
}
\ No newline at end of file
import { NextResponse } from "next/server";
export {
NextResponse
}
\ No newline at end of file
import { ImageResponse } from "next/server";
export {
ImageResponse
}
\ No newline at end of file
import { ImageResponse } from "next/og";
export {
ImageResponse
}
\ 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 = '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' });
}
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
}
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