Codemods
Codemodsは、コードベースに対してプログラム的に変換を実行するツールです。これにより、大量の変更を手動で各ファイルを確認することなく、プログラム的に適用できます。
Next.jsは、APIが更新または非推奨になった際にNext.jsコードベースをアップグレードするためのCodemod変換を提供しています。
使用方法
ターミナルでプロジェクトフォルダに移動(cd
)した後、以下を実行します:
npx @next/codemod <transform> <path>
<transform>
と<path>
を適切な値に置き換えてください。
transform
- 変換名path
- 変換対象のファイルまたはディレクトリ--dry
ドライランを実行(コードは編集されません)--print
変更後の出力を比較用に表示
Next.js Codemods
13.2
組み込みフォントの使用
built-in-next-font
npx @next/codemod@latest built-in-next-font .
このcodemodは@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' が 'next/legacy/image' に
import Image1 from 'next/legacy/image'
// 'next/future/image' が '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>
)
}
新しいImageコンポーネントへの移行
next-image-experimental
npx @next/codemod@latest next-image-experimental .
next/legacy/image
から新しいnext/image
へ危険を伴う移行を行い、インラインスタイルを追加して未使用のプロップを削除します。
layout
プロップを削除しstyle
を追加objectFit
プロップを削除しstyle
を追加objectPosition
プロップを削除しstyle
を追加lazyBoundary
プロップを削除lazyRoot
プロップを削除
Linkコンポーネントから<a>
タグを削除
new-link
npx @next/codemod@latest new-link .
Linkコンポーネント内の<a>
タグを削除するか、自動修正できないLinkにlegacyBehavior
プロップを追加します。
例:
<Link href="/about">
<a>About</a>
</Link>
// 以下のように変換
<Link href="/about">
About
</Link>
<Link href="/about">
<a onClick={() => console.log('clicked')}>About</a>
</Link>
// 以下のように変換
<Link href="/about" onClick={() => console.log('clicked')}>
About
</Link>
自動修正が適用できない場合、legacyBehavior
プロップが追加されます。これにより、特定のリンクに対して古い動作を維持できます。
const Component = () => <a>About</a>
<Link href="/about">
<Component />
</Link>
// 以下のように変換
<Link href="/about" legacyBehavior>
<Component />
</Link>
11
CRAからの移行
cra-to-next
npx @next/codemod cra-to-next
Create React AppプロジェクトをNext.jsに移行します。Pages Routerを作成し、動作を一致させるために必要な設定を行います。初期段階ではSSR時のwindow
使用による互換性問題を防ぐため、クライアントサイドのみのレンダリングを活用し、Next.js固有の機能を段階的に採用できるようにします。
この変換に関するフィードバックはこちらのディスカッションで共有してください。
10
Reactインポートの追加
add-missing-react-import
npx @next/codemod add-missing-react-import
新しいReact JSX transformが動作するように、React
をインポートしていないファイルにインポートを追加します。
例:
export default class Home extends React.Component {
render() {
return <div>Hello World</div>
}
}
以下のように変換されます:
import React from 'react'
export default class Home extends React.Component {
render() {
return <div>Hello World</div>
}
}
9
匿名コンポーネントから名前付きコンポーネントへの変換
name-default-component
npx @next/codemod name-default-component
バージョン9以上
匿名コンポーネントを名前付きコンポーネントに変換し、Fast Refreshが確実に動作するようにします。
例:
export default function () {
return <div>Hello World</div>
}
以下のように変換されます:
export default function MyComponent() {
return <div>Hello World</div>
}
コンポーネントにはファイル名に基づいたキャメルケースの名前が付けられ、アロー関数でも動作します。
8
AMP HOCからページ設定への変換
withamp-to-config
npx @next/codemod withamp-to-config
withAmp
HOCをNext.js 9のページ設定に変換します。
例:
// 変換前
import { withAmp } from 'next/amp'
function Home() {
return <h1>My AMP Page</h1>
}
export default withAmp(Home)
// 変換後
export default function Home() {
return <h1>My AMP Page</h1>
}
export const config = {
amp: true,
}
6
withRouter
の使用
url-to-withrouter
npx @next/codemod url-to-withrouter
非推奨となったトップレベルページに自動注入されるurl
プロパティを、withRouter
とそれが注入するrouter
プロパティを使用するように変換します。詳細はこちら: https://nextjs.org/docs/messages/url-deprecated
例:
import React from 'react'
export default class extends React.Component {
render() {
const { pathname } = this.props.url
return <div>Current pathname: {pathname}</div>
}
}
import React from 'react'
import { withRouter } from 'next/router'
export default withRouter(
class extends React.Component {
render() {
const { pathname } = this.props.router
return <div>Current pathname: {pathname}</div>
}
}
)
これは一例です。変換されるすべてのケース(およびテスト済み)は__testfixtures__
ディレクトリで確認できます。