Unverified Commit 5d7d7391 authored by Steven's avatar Steven Committed by GitHub

Update `next/image` codemod to handle `require()` (#41345)

Follow up to a comment here:

-
https://github.com/vercel/next.js/pull/41004#pullrequestreview-1137492624
parent e1e64e07
const Image = require("next/legacy/image");
const Named = require("next/legacy/image");
const Foo = require("foo");
export default function Home() {
return (
<div>
<h1>Upgrade</h1>
<Image src="/test.jpg" width="200" height="300" layout="fixed" />
<Named src="/test.png" width="400" height="500" layout="fixed" />
<Foo />
</div>
);
}
\ No newline at end of file
const Image = require("next/image");
const Named = require("next/image");
const Foo = require("foo");
export default function Home() {
return (
<div>
<h1>Upgrade</h1>
<Image src="/test.jpg" width="200" height="300" />
<Named src="/test.png" width="400" height="500" />
<Foo />
</div>
);
}
\ No newline at end of file
export async function Home() {
const Image = require("next/image");
const Named = require("next/image");
const Foo = require("foo");
const Future1 = require("next/future/image");
const Future2 = require("next/future/image");
return (<div>
<h1>Both</h1>
<Image src="/test.jpg" width="200" height="300" />
<Named src="/test.png" width="500" height="400" />
<Future1 src="/test.webp" width="60" height="70" />
<Future2 src="/test.avif" width="80" height="90" />
</div>)
}
\ No newline at end of file
export async function Home() {
const Image = require("next/legacy/image");
const Named = require("next/legacy/image");
const Foo = require("foo");
const Future1 = require("next/image");
const Future2 = require("next/image");
return (<div>
<h1>Both</h1>
<Image src="/test.jpg" width="200" height="300" />
<Named src="/test.png" width="500" height="400" />
<Future1 src="/test.webp" width="60" height="70" />
<Future2 src="/test.avif" width="80" height="90" />
</div>)
}
\ No newline at end of file
......@@ -61,6 +61,42 @@ export default function transformer(
)
})
// Before: const Image = require("next/image")
// After: const Image = require("next/legacy/image")
root.find(j.CallExpression).forEach((requireExp) => {
if (
requireExp?.value?.callee?.type === 'Identifier' &&
requireExp.value.callee.name === 'require'
) {
let firstArg = requireExp.value.arguments[0]
if (
firstArg &&
firstArg.type === 'Literal' &&
firstArg.value === 'next/image'
) {
requireExp.value.arguments[0] = j.literal('next/legacy/image')
}
}
})
// Before: const Image = require("next/future/image")
// After: const Image = require("next/image")
root.find(j.CallExpression).forEach((requireExp) => {
if (
requireExp?.value?.callee?.type === 'Identifier' &&
requireExp.value.callee.name === 'require'
) {
let firstArg = requireExp.value.arguments[0]
if (
firstArg &&
firstArg.type === 'Literal' &&
firstArg.value === 'next/future/image'
) {
requireExp.value.arguments[0] = j.literal('next/image')
}
}
})
// Learn more about renaming an import declaration here:
// https://www.codeshiftcommunity.com/docs/import-manipulation/#replacerename-an-import-declaration
......
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