Unverified Commit 54266bb5 authored by John Daly's avatar John Daly Committed by GitHub

[next/codemod]: Preserve type-only imports when using `next-image-to-legacy-image` (#46460

<!--
Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:
-->

Fix for https://github.com/vercel/next.js/issues/46459

In order to test this, I've updated the `next-image-to-legacy-image`
tests to use fixtures that are written in TypeScript. I've disabled
type-checking and linting on those test fixtures, because they are
pretty noisy.

## Bug

- [x] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ]
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 586ae4e8
// @ts-nocheck
/* eslint-disable */
export async function Home() { export async function Home() {
const Image = await import("next/image"); const Image = await import("next/image");
const Named = await import("next/image"); const Named = await import("next/image");
...@@ -11,4 +13,4 @@ export async function Home() { ...@@ -11,4 +13,4 @@ export async function Home() {
<Future1 src="/test.webp" width="60" height="70" /> <Future1 src="/test.webp" width="60" height="70" />
<Future2 src="/test.avif" width="80" height="90" /> <Future2 src="/test.avif" width="80" height="90" />
</div>) </div>)
} }
\ No newline at end of file
// @ts-nocheck
/* eslint-disable */
export async function Home() { export async function Home() {
const Image = await import("next/legacy/image"); const Image = await import("next/legacy/image");
const Named = await import("next/legacy/image"); const Named = await import("next/legacy/image");
...@@ -11,4 +13,4 @@ export async function Home() { ...@@ -11,4 +13,4 @@ export async function Home() {
<Future1 src="/test.webp" width="60" height="70" /> <Future1 src="/test.webp" width="60" height="70" />
<Future2 src="/test.avif" width="80" height="90" /> <Future2 src="/test.avif" width="80" height="90" />
</div>) </div>)
} }
\ No newline at end of file
// @ts-nocheck
/* eslint-disable */
import Image from "next/image"; import Image from "next/image";
import Named from "next/image"; import Named from "next/image";
import type { ImageProps } from "next/image";
import { type ImageLoaderProps } from "next/image";
import Foo from "foo"; import Foo from "foo";
import Future1 from "next/future/image"; import Future1 from "next/future/image";
import Future2 from "next/future/image"; import Future2 from "next/future/image";
...@@ -12,4 +16,4 @@ export default function Home() { ...@@ -12,4 +16,4 @@ export default function Home() {
<Future1 src="/test.webp" width="60" height="70" /> <Future1 src="/test.webp" width="60" height="70" />
<Future2 src="/test.avif" width="80" height="90" /> <Future2 src="/test.avif" width="80" height="90" />
</div>) </div>)
} }
\ No newline at end of file
// @ts-nocheck
/* eslint-disable */
import Image from "next/legacy/image"; import Image from "next/legacy/image";
import Named from "next/legacy/image"; import Named from "next/legacy/image";
import type { ImageProps } from "next/legacy/image";
import { type ImageLoaderProps } from "next/legacy/image";
import Foo from "foo"; import Foo from "foo";
import Future1 from "next/image"; import Future1 from "next/image";
import Future2 from "next/image"; import Future2 from "next/image";
...@@ -12,4 +16,4 @@ export default function Home() { ...@@ -12,4 +16,4 @@ export default function Home() {
<Future1 src="/test.webp" width="60" height="70" /> <Future1 src="/test.webp" width="60" height="70" />
<Future2 src="/test.avif" width="80" height="90" /> <Future2 src="/test.avif" width="80" height="90" />
</div>) </div>)
} }
\ No newline at end of file
// @ts-nocheck
/* eslint-disable */
export async function Home() { export async function Home() {
const Image = require("next/image"); const Image = require("next/image");
const Named = require("next/image"); const Named = require("next/image");
...@@ -11,4 +13,4 @@ export async function Home() { ...@@ -11,4 +13,4 @@ export async function Home() {
<Future1 src="/test.webp" width="60" height="70" /> <Future1 src="/test.webp" width="60" height="70" />
<Future2 src="/test.avif" width="80" height="90" /> <Future2 src="/test.avif" width="80" height="90" />
</div>) </div>)
} }
\ No newline at end of file
// @ts-nocheck
/* eslint-disable */
export async function Home() { export async function Home() {
const Image = require("next/legacy/image"); const Image = require("next/legacy/image");
const Named = require("next/legacy/image"); const Named = require("next/legacy/image");
...@@ -11,4 +13,4 @@ export async function Home() { ...@@ -11,4 +13,4 @@ export async function Home() {
<Future1 src="/test.webp" width="60" height="70" /> <Future1 src="/test.webp" width="60" height="70" />
<Future2 src="/test.avif" width="80" height="90" /> <Future2 src="/test.avif" width="80" height="90" />
</div>) </div>)
} }
\ No newline at end of file
...@@ -7,10 +7,10 @@ const { join } = require('path') ...@@ -7,10 +7,10 @@ const { join } = require('path')
const fixtureDir = 'next-image-to-legacy-image' const fixtureDir = 'next-image-to-legacy-image'
const fixtureDirPath = join(__dirname, '..', '__testfixtures__', fixtureDir) const fixtureDirPath = join(__dirname, '..', '__testfixtures__', fixtureDir)
const fixtures = readdirSync(fixtureDirPath) const fixtures = readdirSync(fixtureDirPath)
.filter(file => file.endsWith('.input.js')) .filter(file => file.endsWith('.input.tsx'))
.map(file => file.replace('.input.js', '')) .map(file => file.replace('.input.tsx', ''))
for (const fixture of fixtures) { for (const fixture of fixtures) {
const prefix = `${fixtureDir}/${fixture}`; const prefix = `${fixtureDir}/${fixture}`;
defineTest(__dirname, fixtureDir, null, prefix) defineTest(__dirname, fixtureDir, null, prefix, { parser: 'tsx' });
} }
...@@ -15,12 +15,7 @@ export default function transformer( ...@@ -15,12 +15,7 @@ export default function transformer(
source: { value: 'next/image' }, source: { value: 'next/image' },
}) })
.forEach((imageImport) => { .forEach((imageImport) => {
j(imageImport).replaceWith( imageImport.node.source = j.stringLiteral('next/legacy/image')
j.importDeclaration(
imageImport.node.specifiers,
j.stringLiteral('next/legacy/image')
)
)
}) })
// Before: const Image = await import("next/image") // Before: const Image = await import("next/image")
// After: const Image = await import("next/legacy/image") // After: const Image = await import("next/legacy/image")
......
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