程式碼修改工具
程式碼修改器是一種以程式化方式在您的程式碼庫上執行的轉換工具。這允許以程式化的方式套用大量變更,而無需手動檢查每個檔案。
當 API 更新或棄用時,Next.js 提供程式碼修改器轉換來協助您升級 Next.js 程式碼庫。
用法
在您的終端機中,導覽 (cd
) 至您的專案資料夾,然後執行
npx @next/codemod <transform> <path>
將 <transform>
和 <path>
替換為適當的值。
transform
- 轉換的名稱path
- 要轉換的檔案或目錄--dry
進行測試執行,不會修改任何程式碼--print
印出修改後的輸出以便比較
程式碼修改器
15.0
將應用程式路由器路由區段設定檔的 runtime
值從 experimental-edge
轉換為 edge
app-dir-runtime-config-experimental-edge
npx @next/codemod@latest next-dynamic-access-named-export .
此程式碼修改器會轉換使用 next/dynamic
的動態導入,以確保在存取具名匯出時,它們會返回具有 default
屬性的物件。這與 React.lazy
的行為一致,並解決了從伺服器元件中的客戶端元件存取具名匯出的問題。
例如:
import dynamic from 'next/dynamic'
const ComponentA = dynamic(() =>
import('../components/a').then((mod) => mod.default)
)
const ComponentB = dynamic(() =>
import('../components/b').then((mod) => mod.ComponentB)
)
轉換成:
import dynamic from 'next/dynamic'
const ComponentA = dynamic(() =>
import('../components/a').then((mod) => ({ default: mod.default }))
)
const ComponentB = dynamic(() =>
import('../components/b').then((mod) => ({ default: mod.ComponentB }))
)
注意事項:此程式碼修改器僅影響使用
next/dynamic
並存取具名匯出的動態導入。
14.0
遷移 ImageResponse
導入
next-og-import
npx @next/codemod@latest next-og-import .
此程式碼修改器會將導入從 next/server
移至 next/og
,以使用動態 OG 圖片產生。
例如:
import { ImageResponse } from 'next/server'
轉換成:
import { ImageResponse } from 'next/og'
使用 viewport
匯出
metadata-to-viewport-export
終端機npx @next/codemod@latest metadata-to-viewport-export .
npx @next/codemod@latest metadata-to-viewport-export .
此程式碼修改器會將特定 viewport 中繼資料遷移到 viewport
匯出。
例如:
export const metadata = {
title: 'My App',
themeColor: 'dark',
viewport: {
width: 1,
},
}
轉換成:
export const metadata = {
title: 'My App',
}
export const viewport = {
width: 1,
themeColor: 'dark',
}
13.2
使用內建字體
built-in-next-font
npx @next/codemod@latest built-in-next-font .
此程式碼修改器會解除安裝 @next/font
套件,並將 @next/font
導入轉換為內建的 next/font
。
例如:
import { Inter } from '@next/font/google'
轉換成:
import { Inter } from 'next/font/google'
13.0
重新命名 Next Image 導入
next-image-to-legacy-image
npx @next/codemod@latest next-image-to-legacy-image .
在現有的 Next.js 10、11 或 12 應用程式中,安全地將 next/image
導入重新命名為 Next.js 13 中的 next/legacy/image
。同時也將 next/future/image
重新命名為 next/image
。
例如:
import Image1 from 'next/image'
import Image2 from 'next/future/image'
export default function Home() {
return (
<div>
<Image1 src="/test.jpg" width="200" height="300" />
<Image2 src="/test.png" width="500" height="400" />
</div>
)
}
轉換成:
// 'next/image' becomes 'next/legacy/image'
import Image1 from 'next/legacy/image'
// 'next/future/image' becomes 'next/image'
import Image2 from 'next/image'
export default function Home() {
return (
<div>
<Image1 src="/test.jpg" width="200" height="300" />
<Image2 src="/test.png" width="500" height="400" />
</div>
)
}
遷移至新的圖片元件
next-image-experimental
npx @next/codemod@latest next-image-experimental .
透過新增內嵌樣式和移除未使用的屬性,將 next/legacy/image
遷移至新的 next/image
(此操作具風險性)。
- 移除
layout
屬性並新增style
屬性。 - 移除
objectFit
屬性並新增style
屬性。 - 移除
objectPosition
屬性並新增style
屬性。 - 移除
lazyBoundary
屬性。 - 移除
lazyRoot
屬性。
從連結元件中移除 <a>
標籤
new-link
終端機npx @next/codemod@latest new-link .
npx @next/codemod@latest new-link .