Skip to main content

代码模组

Codemod 是以编程方式在代码库上运行的转换。这允许以编程方式应用大量更改,而无需手动检查每个文件。

¥Codemods are transformations that run on your codebase programmatically. This allows a large number of changes to be programmatically applied without having to manually go through every file.

Next.js 提供 Codemod 转换,以帮助在 API 更新或弃用时升级 Next.js 代码库。

¥Next.js provides Codemod transformations to help upgrade your Next.js codebase when an API is updated or deprecated.

用法

¥Usage

在终端中,导航 (cd) 到项目文件夹,然后运行:

¥In your terminal, navigate (cd) into your project's folder, then run:

npx @next/codemod <transform> <path>

<transform><path> 替换为适当的值。

¥Replacing <transform> and <path> with appropriate values.

  • transform - 变换名称

    ¥transform - name of transform

  • path - 要转换的文件或目录

    ¥path - files or directory to transform

  • --dry 进行试运行,不会编辑任何代码

    ¥--dry Do a dry-run, no code will be edited

  • --print 打印更改后的输出以进行比较

    ¥--print Prints the changed output for comparison

Next.js Codemods

14.0

迁移 ImageResponse 导入

¥Migrate ImageResponse imports

next-og-import
npx @next/codemod@latest next-og-import .

此 codemod 将转换导入从 next/server 移动到 next/og 以使用 动态 OG 图片生成

¥This codemod moves transforms imports from next/server to next/og for usage of Dynamic OG Image Generation.

例如:

¥For example:

import { ImageResponse } from 'next/server'

转变为:

¥Transforms into:

import { ImageResponse } from 'next/og'

使用 viewport 导出

¥Use viewport export

metadata-to-viewport-export
npx @next/codemod@latest metadata-to-viewport-export .

此 codemod 将某些视口元数据迁移到 viewport 导出。

¥This codemod migrates certain viewport metadata to viewport export.

例如:

¥For example:

export const metadata = {
title: 'My App',
themeColor: 'dark',
viewport: {
width: 1,
},
}

转变为:

¥Transforms into:

export const metadata = {
title: 'My App',
}

export const viewport = {
width: 1,
themeColor: 'dark',
}

13.2

使用内置字体

¥Use Built-in Font

built-in-next-font
npx @next/codemod@latest built-in-next-font .

此 codemod 卸载 @next/font 包并将 @next/font 导入转换为内置 next/font

¥This codemod uninstalls the @next/font package and transforms @next/font imports into the built-in next/font.

例如:

¥For example:

import { Inter } from '@next/font/google'

转变为:

¥Transforms into:

import { Inter } from 'next/font/google'

13.0

重命名下一个图片导入

¥Rename Next Image Imports

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

¥Safely renames next/image imports in existing Next.js 10, 11, or 12 applications to next/legacy/image in Next.js 13. Also renames next/future/image to next/image.

例如:

¥For example:

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>
)
}

转变为:

¥Transforms into:

// '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>
)
}

迁移到新的图片组件

¥Migrate to the New Image Component

next-image-experimental
npx @next/codemod@latest next-image-experimental .

通过添加内联样式并删除未使用的 props,危险地从 next/legacy/image 迁移到新的 next/image

¥Dangerously migrates from next/legacy/image to the new next/image by adding inline styles and removing unused props.

  • 删除 layout 属性并添加 style

    ¥Removes layout prop and adds style.

  • 删除 objectFit 属性并添加 style

    ¥Removes objectFit prop and adds style.

  • 删除 objectPosition 属性并添加 style

    ¥Removes objectPosition prop and adds style.

  • 移除 lazyBoundary 属性。

    ¥Removes lazyBoundary prop.

  • 移除 lazyRoot 属性。

    ¥Removes lazyRoot prop.

¥Remove <a> Tags From Link Components

npx @next/codemod@latest new-link .

删除 链接组件 内的 <a> 标签,或向无法自动修复的链接添加 legacyBehavior 属性。

¥Remove <a> tags inside Link Components, or add a legacyBehavior prop to Links that cannot be auto-fixed.

例如:

¥For example:

<Link href="/about">
<a>About</a>
</Link>
// transforms into
<Link href="/about">
About
</Link>

<Link href="/about">
<a onClick={() => console.log('clicked')}>About</a>
</Link>
// transforms into
<Link href="/about" onClick={() => console.log('clicked')}>
About
</Link>

如果无法应用自动修复,则会添加 legacyBehavior 属性。这允许你的应用使用该特定链接的旧行为继续运行。

¥In cases where auto-fixing can't be applied, the legacyBehavior prop is added. This allows your app to keep functioning using the old behavior for that particular link.

const Component = () => <a>About</a>

<Link href="/about">
<Component />
</Link>
// becomes
<Link href="/about" legacyBehavior>
<Component />
</Link>

11

从 CRA 迁移

¥Migrate from CRA

cra-to-next
npx @next/codemod cra-to-next

将 Create React App 项目迁移到 Next.js;创建页面路由和必要的配置来匹配行为。最初仅利用客户端渲染来防止由于 SSR 期间使用 window 而破坏兼容性,并且可以无缝启用以允许逐步采用 Next.js 特定功能。

¥Migrates a Create React App project to Next.js; creating a Pages Router and necessary config to match behavior. Client-side only rendering is leveraged initially to prevent breaking compatibility due to window usage during SSR and can be enabled seamlessly to allow the gradual adoption of Next.js specific features.

请分享与此转换 在这次讨论中 相关的任何反馈。

¥Please share any feedback related to this transform in this discussion.

10

添加 React 导入

¥Add React imports

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

将不导入 React 的文件转换为包含导入内容,以便新的 React JSX 转换 能够工作。

¥Transforms files that do not import React to include the import in order for the new React JSX transform to work.

例如:

¥For example:

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

转变为:

¥Transforms into:

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

9

将匿名组件转换为命名组件

¥Transform Anonymous Components into Named Components

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

版本 9 及以上。

¥Versions 9 and above.

将匿名组件转换为命名组件,以确保它们与 快速刷新 一起工作。

¥Transforms anonymous components into named components to make sure they work with Fast Refresh.

例如:

¥For example:

export default function () {
return <div>Hello World</div>
}

转变为:

¥Transforms into:

export default function MyComponent() {
return <div>Hello World</div>
}

该组件将具有基于文件名的驼峰式名称,并且它还可以与箭头函数一起使用。

¥The component will have a camel-cased name based on the name of the file, and it also works with arrow functions.

8

将 AMP HOC 转换为页面配置

¥Transform AMP HOC into page config

withamp-to-config
npx @next/codemod withamp-to-config

withAmp HOC 转换为 Next.js 9 页面配置。

¥Transforms the withAmp HOC into Next.js 9 page configuration.

例如:

¥For example:

// Before
import { withAmp } from 'next/amp'

function Home() {
return <h1>My AMP Page</h1>
}

export default withAmp(Home)
// After
export default function Home() {
return <h1>My AMP Page</h1>
}

export const config = {
amp: true,
}

6

使用 withRouter

¥Use withRouter

url-to-withrouter
npx @next/codemod url-to-withrouter

将顶层页面上已弃用的自动注入 url 属性转换为使用 withRouter 及其注入的 router 属性。在这里阅读更多内容:[https://next.nodejs.cn/docs/messages/url-deprecated](/docs/messages/url-deprecated)

¥Transforms the deprecated automatically injected url property on top level pages to using withRouter and the router property it injects. Read more here: https://next.nodejs.cn/docs/messages/url-deprecated

例如:

¥For example:

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__ 目录 中找到。

¥This is one case. All the cases that are transformed (and tested) can be found in the __testfixtures__ directory.