Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add codemods for
next/image
(#41004)
This adds two codemods for `next/image`. The first codemod a safe solution to upgrade from 12 to 13 by swapping to `next/legacy/image`. The second codemod is an experimental solution that attempts to automate the [migration guide](https://nextjs.org/docs/api-reference/next/future/image#migration).
- Loading branch information
Showing
12 changed files
with
520 additions
and
0 deletions.
There are no files selected for viewing
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
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
33 changes: 33 additions & 0 deletions
33
packages/next-codemod/transforms/__testfixtures__/next-image-experimental/general.input.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,33 @@ | ||
import Image from "next/legacy/image"; | ||
import Named, { Blarg } from "next/legacy/image"; | ||
import Foo from "foo"; | ||
import img from "../public/img.jpg"; | ||
|
||
export default function Home() { | ||
const myStyle = { color: 'black' }; | ||
return ( | ||
<div> | ||
<h1>Upgrade</h1> | ||
<Image src="/test.jpg" width="200" height="300" /> | ||
<Image src="/test.jpg" width="200" height="300" layout="intrinsic" /> | ||
<Image src="/test.jpg" width="200" height="300" layout="responsive" /> | ||
<Image src="/test.jpg" width="200" height="300" layout="fixed" /> | ||
<div style={{ position: 'relative', width: '300px', height: '500px' }}> | ||
<Image | ||
alt="example alt text" | ||
src={img} | ||
layout="fill" | ||
objectFit="contain" | ||
/> | ||
</div> | ||
<Named src="/test.png" width="500" height="400" layout="fixed" /> | ||
<Foo bar="1" /> | ||
<Image src="/test.jpg" width="200" height="300" layout="responsive" style={{color: "green"}} /> | ||
<Image src="/test.jpg" width="200" height="300" layout="responsive" style={{color: myStyle.color}} /> | ||
<Image src="/test.jpg" width="200" height="300" layout="responsive" sizes="50vw" style={{color: "#fff"}} /> | ||
<Image src="/test.jpg" width="200" height="300" layout="fixed" lazyBoundary="1500px" lazyRoot={img} /> | ||
<Image src="/test.lit" width="200" height="300" layout="responsive" style={myStyle} /> | ||
<Image src="/test.dot" width="200" height="300" layout="responsive" style={{...myStyle}} /> | ||
</div> | ||
); | ||
} |
95 changes: 95 additions & 0 deletions
95
packages/next-codemod/transforms/__testfixtures__/next-image-experimental/general.output.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,95 @@ | ||
import Image from "next/image"; | ||
import Named, { Blarg } from "next/image"; | ||
import Foo from "foo"; | ||
import img from "../public/img.jpg"; | ||
|
||
export default function Home() { | ||
const myStyle = { color: 'black' }; | ||
return ( | ||
<div> | ||
<h1>Upgrade</h1> | ||
<Image src="/test.jpg" width="200" height="300" /> | ||
<Image | ||
src="/test.jpg" | ||
width="200" | ||
height="300" | ||
style={{ | ||
maxWidth: "100%", | ||
height: "auto" | ||
}} /> | ||
<Image | ||
src="/test.jpg" | ||
width="200" | ||
height="300" | ||
sizes="100vw" | ||
style={{ | ||
width: "100%", | ||
height: "auto" | ||
}} /> | ||
<Image src="/test.jpg" width="200" height="300" /> | ||
<div style={{ position: 'relative', width: '300px', height: '500px' }}> | ||
<Image | ||
alt="example alt text" | ||
src={img} | ||
fill | ||
sizes="100vw" | ||
style={{ | ||
objectFit: "contain" | ||
}} /> | ||
</div> | ||
<Named src="/test.png" width="500" height="400" /> | ||
<Foo bar="1" /> | ||
<Image | ||
src="/test.jpg" | ||
width="200" | ||
height="300" | ||
sizes="100vw" | ||
style={{ | ||
color: "green", | ||
width: "100%", | ||
height: "auto" | ||
}} /> | ||
<Image | ||
src="/test.jpg" | ||
width="200" | ||
height="300" | ||
sizes="100vw" | ||
style={{ | ||
color: myStyle.color, | ||
width: "100%", | ||
height: "auto" | ||
}} /> | ||
<Image | ||
src="/test.jpg" | ||
width="200" | ||
height="300" | ||
sizes="50vw" | ||
style={{ | ||
color: "#fff", | ||
width: "100%", | ||
height: "auto" | ||
}} /> | ||
<Image src="/test.jpg" width="200" height="300" /> | ||
<Image | ||
src="/test.lit" | ||
width="200" | ||
height="300" | ||
sizes="100vw" | ||
style={{ | ||
...myStyle, | ||
width: "100%", | ||
height: "auto" | ||
}} /> | ||
<Image | ||
src="/test.dot" | ||
width="200" | ||
height="300" | ||
sizes="100vw" | ||
style={{ | ||
...myStyle, | ||
width: "100%", | ||
height: "auto" | ||
}} /> | ||
</div> | ||
); | ||
} |
14 changes: 14 additions & 0 deletions
14
...ages/next-codemod/transforms/__testfixtures__/next-image-to-legacy-image/dynamic.input.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,14 @@ | ||
export async function Home() { | ||
const Image = await import("next/image"); | ||
const Named = await import("next/image"); | ||
const Foo = await import("foo"); | ||
const Future1 = await import("next/future/image"); | ||
const Future2 = await import("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>) | ||
} |
14 changes: 14 additions & 0 deletions
14
...ges/next-codemod/transforms/__testfixtures__/next-image-to-legacy-image/dynamic.output.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,14 @@ | ||
export async function Home() { | ||
const Image = await import("next/legacy/image"); | ||
const Named = await import("next/legacy/image"); | ||
const Foo = await import("foo"); | ||
const Future1 = await import("next/image"); | ||
const Future2 = await import("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>) | ||
} |
15 changes: 15 additions & 0 deletions
15
...ages/next-codemod/transforms/__testfixtures__/next-image-to-legacy-image/general.input.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,15 @@ | ||
import Image from "next/image"; | ||
import Named from "next/image"; | ||
import Foo from "foo"; | ||
import Future1 from "next/future/image"; | ||
import Future2 from "next/future/image"; | ||
|
||
export default function Home() { | ||
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>) | ||
} |
15 changes: 15 additions & 0 deletions
15
...ges/next-codemod/transforms/__testfixtures__/next-image-to-legacy-image/general.output.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,15 @@ | ||
import Image from "next/legacy/image"; | ||
import Named from "next/legacy/image"; | ||
import Foo from "foo"; | ||
import Future1 from "next/image"; | ||
import Future2 from "next/image"; | ||
|
||
export default function Home() { | ||
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>) | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/next-codemod/transforms/__tests__/next-image-experimental.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-image-experimental' | ||
const fixtureDirPath = join(__dirname, '..', '__testfixtures__', fixtureDir) | ||
const fixtures = readdirSync(fixtureDirPath) | ||
.filter(file => file.endsWith('.input.js')) | ||
.map(file => file.replace('.input.js', '')) | ||
|
||
for (const fixture of fixtures) { | ||
const prefix = `${fixtureDir}/${fixture}`; | ||
defineTest(__dirname, fixtureDir, null, prefix) | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/next-codemod/transforms/__tests__/next-image-to-legacy-image.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-image-to-legacy-image' | ||
const fixtureDirPath = join(__dirname, '..', '__testfixtures__', fixtureDir) | ||
const fixtures = readdirSync(fixtureDirPath) | ||
.filter(file => file.endsWith('.input.js')) | ||
.map(file => file.replace('.input.js', '')) | ||
|
||
for (const fixture of fixtures) { | ||
const prefix = `${fixtureDir}/${fixture}`; | ||
defineTest(__dirname, fixtureDir, null, prefix) | ||
} |
Oops, something went wrong.