Codemods

Codemodsは、コードベースに対してプログラム的に変換を実行するツールです。これにより、大量の変更を手動で各ファイルを確認することなく、プログラム的に適用できます。

Next.jsは、APIが更新または非推奨になった際にNext.jsコードベースをアップグレードするためのCodemod変換を提供しています。

使用方法

ターミナルでプロジェクトフォルダに移動(cd)した後、以下を実行します:

Terminal
npx @next/codemod <transform> <path>

<transform><path>を適切な値に置き換えてください。

  • transform - 変換名
  • path - 変換対象のファイルまたはディレクトリ
  • --dry ドライランを実行(コードは編集されません)
  • --print 変更後の出力を比較用に表示

Next.js Codemods

13.2

組み込みフォントの使用

built-in-next-font
Terminal
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
Terminal
npx @next/codemod@latest next-image-to-legacy-image .

Next.js 10、11、12アプリケーションのnext/imageインポートをNext.js 13で安全にnext/legacy/imageにリネームします。また、next/future/imagenext/imageにリネームします。

例:

pages/index.js
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>
  )
}

以下のように変換されます:

pages/index.js
// '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
Terminal
npx @next/codemod@latest next-image-experimental .

next/legacy/imageから新しいnext/imageへ危険を伴う移行を行い、インラインスタイルを追加して未使用のプロップを削除します。

  • layoutプロップを削除しstyleを追加
  • objectFitプロップを削除しstyleを追加
  • objectPositionプロップを削除しstyleを追加
  • lazyBoundaryプロップを削除
  • lazyRootプロップを削除

Linkコンポーネントから<a>タグを削除

Terminal
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
Terminal
npx @next/codemod cra-to-next

Create React AppプロジェクトをNext.jsに移行します。Pages Routerを作成し、動作を一致させるために必要な設定を行います。初期段階ではSSR時のwindow使用による互換性問題を防ぐため、クライアントサイドのみのレンダリングを活用し、Next.js固有の機能を段階的に採用できるようにします。

この変換に関するフィードバックはこちらのディスカッションで共有してください。

10

Reactインポートの追加

add-missing-react-import
Terminal
npx @next/codemod add-missing-react-import

新しいReact JSX transformが動作するように、Reactをインポートしていないファイルにインポートを追加します。

例:

my-component.js
export default class Home extends React.Component {
  render() {
    return <div>Hello World</div>
  }
}

以下のように変換されます:

my-component.js
import React from 'react'
export default class Home extends React.Component {
  render() {
    return <div>Hello World</div>
  }
}

9

匿名コンポーネントから名前付きコンポーネントへの変換

name-default-component
Terminal
npx @next/codemod name-default-component

バージョン9以上

匿名コンポーネントを名前付きコンポーネントに変換し、Fast Refreshが確実に動作するようにします。

例:

my-component.js
export default function () {
  return <div>Hello World</div>
}

以下のように変換されます:

my-component.js
export default function MyComponent() {
  return <div>Hello World</div>
}

コンポーネントにはファイル名に基づいたキャメルケースの名前が付けられ、アロー関数でも動作します。

8

AMP HOCからページ設定への変換

withamp-to-config
Terminal
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
Terminal
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__ディレクトリで確認できます。