代码转换工具
代码转换工具是对你的代码库进行程序化转换的工具。它允许在不需要手动修改每个文件的情况下,对大量代码进行程序化的修改。
Next.js 提供了代码转换工具来帮助你在 API 更新或弃用时升级 Next.js 代码库。
使用方法
在终端中,进入 (cd
) 你的项目文件夹,然后运行:
将 <transform>
和 <path>
替换为适当的值。
transform
- 转换的名称path
- 要转换的文件或目录--dry
进行试运行,不会编辑任何代码--print
打印更改后的输出以进行比较
Next.js 代码转换工具
14.0
迁移 ImageResponse
导入
next-og-import
这个代码转换工具将 动态 OG 图像生成 的导入从 next/server
转移到 next/og
。
例如:
转换为:
使用 viewport
导出
metadata-to-viewport-export
这个代码转换工具将某些视口元数据迁移到 viewport
导出。
例如:
转换为:
13.2
使用内置字体
built-in-next-font
这个代码转换工具卸载 @next/font
包,并将 @next/font
导入转换为内置的 next/font
。
例如:
转换为:
13.0
重命名 Next Image 导入
next-image-to-legacy-image
安全地将现有 Next.js 10、11 或 12 应用程序中的 next/image
导入重命名为 Next.js 13 中的 next/legacy/image
。同时将 next/future/image
重命名为 next/image
。
例如:
转换为:
迁移到新的 Image 组件
next-image-experimental
通过添加内联样式和删除未使用的属性,将 next/legacy/image
危险地迁移到新的 next/image
。
- 删除
layout
属性并添加style
。 - 删除
objectFit
属性并添加style
。 - 删除
objectPosition
属性并添加style
。 - 删除
lazyBoundary
属性。 - 删除
lazyRoot
属性。
从 Link 组件中移除 <a>
标签
new-link
从 Link 组件 中移除 <a>
标签,或为无法自动修复的 Link 添加 legacyBehavior
属性。
例如:
在无法应用自动修复的情况下,会添加 legacyBehavior
属性。这允许你的应用程序对该特定链接继续使用旧的行为。
11
从 CRA 迁移
cra-to-next
将 Create React App 项目迁移到 Next.js;创建 Pages Router 和必要的配置以匹配行为。最初利用仅客户端渲染来防止由于 SSR 期间使用 window
而导致的兼容性问题,并且可以无缝启用以允许逐步采用 Next.js 特定功能。
请在 这个讨论 中分享与此转换相关的任何反馈。
10
添加 React 导入
add-missing-react-import
转换未导入 React
的文件,以包含导入,以便新的 React JSX 转换 能够正常工作。
例如:
转换为:
9
将匿名组件转换为命名组件
name-default-component
版本 9 及以上。
将匿名组件转换为命名组件,以确保它们与 Fast Refresh 一起工作。
例如:
转换为:
组件将根据文件名使用驼峰命名法命名,它也适用于箭头函数。
8
将 AMP HOC 转换为页面配置
withamp-to-config
将 withAmp
HOC 转换为 Next.js 9 页面配置。
例如:
6
使用 withRouter
url-to-withrouter
将顶级页面上已弃用的自动注入 url
属性转换为使用 withRouter
及其注入的 router
属性。阅读更多信息:https://nextjs.org/docs/messages/url-deprecated
例如:
这只是一种情况。所有被转换(和测试)的情况都可以在 __testfixtures__
目录 中找到。