Starting with Next.js and unable to prevent it from treating my test files as pages within the pages folder

huangapple go评论61阅读模式
英文:

Starting with Next.js and unable to prevent it from treating my test files as pages within the pages folder

问题

我已经习惯了在我的前端项目中,将测试文件直接存储在它们所测试的组件旁边。

话虽如此,我已经开始了一个使用 Next.js 的项目,而 Next.js 的路由是基于文件路径位置构建的,而不是像我以前使用的 react-router 那样。

我怎样明确告诉 Next.js 忽略我的 *.spec.ts 文件?

英文:

I have grown accustomed in my Frontend projects to storing my test files directly next to the components they are testing.

That being said I have started on a project with Next.js and Next.js has a router that is built on the premise of filepath location instead of something like react-router which i've used in the past.

how can i explicitly tell Next.js to ignore my *.spec.ts files?

Starting with Next.js and unable to prevent it from treating my test files as pages within the pages folder

答案1

得分: 0

在页面目录中包括非页面文件

您可以将测试文件或组件使用的其他文件与页面目录中的文件放在一起。在 next.config.js 中,添加 pageExtensions 配置:

module.exports = {
  pageExtensions: ['page.tsx', 'page.ts', 'page.jsx', 'page.js'],
}

然后,将您的页面重命名,以便文件扩展名包括 .page(例如,将 MyPage.tsx 重命名为 MyPage.page.tsx)。请确保重命名所有 Next.js 页面,包括上面提到的文件。

Next.js 文档

英文:

>## Including non-page files in the pages directory
>You can colocate test files or other files used by components in the pages directory. Inside next.config.js, add the pageExtensions config:

module.exports = {
  pageExtensions: ['page.tsx', 'page.ts', 'page.jsx', 'page.js'],
}

>Then, rename your pages to have a file extension that includes .page (e.g. rename MyPage.tsx to MyPage.page.tsx). Ensure you rename all Next.js pages, including the files mentioned above.

Next.js docs

huangapple
  • 本文由 发表于 2023年3月12日 07:33:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710215.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定