Skip to content

feat: add Payload CMS plugin#1546

Merged
webpro merged 14 commits intowebpro-nl:mainfrom
Robot-Inventor:add-payload-plugin
Feb 20, 2026
Merged

feat: add Payload CMS plugin#1546
webpro merged 14 commits intowebpro-nl:mainfrom
Robot-Inventor:add-payload-plugin

Conversation

@Robot-Inventor
Copy link
Copy Markdown
Contributor

Added a plugin for Payload CMS.

Add payload.config.ts, which Payload CMS requires as a configuration file, and payload-types.ts, its automatically generated type definitions, to the entry.

In Payload CMS configurations, some components are loaded by specifying their path as a string instead of direct import. Resolved components are imported from the automatically generated app/**/importMap.js or src/app/**/importMap.js. By adding these files to the entry, we can correctly determine whether components are being used.

Additionally, exclude automatically generated migration files from the project.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Feb 18, 2026

Open in StackBlitz

npm i https://pkg.pr.new/knip@1546
npm i https://pkg.pr.new/@knip/language-server@1546
npm i https://pkg.pr.new/@knip/mcp@1546

commit: 246f36d

@Robot-Inventor
Copy link
Copy Markdown
Contributor Author

I checked the CI failure details for the tests, but even if I revert all changes in this pull request, the same tests still fail, so it seems unrelated to the changes in this pull request.

Copy link
Copy Markdown
Member

@webpro webpro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Before we can merge this, some work is probably needed to make it more usable/correct in more projects.

Comment thread packages/knip/src/plugins/payload/index.ts Outdated
'payload-types.ts',
'src/payload-types.ts',
'payload.config.ts',
'src/payload.config.ts',
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having patterns like src/app/**/importMap.js, we should probably import the actual config file and then read config.admin.importMap.importMapFile. If it's not set, we can use default value (e.g. src/app/(payload)/admin/importMap.js).

This way, it would be accurate and works in any project using Payload CMS.

It starts by adding const config = ["src/payload.config.ts"] and implementing resolveConfig — please see https://knip.dev/writing-a-plugin.

It helps to demonstrate/test with public repos such as the templates at https://github.com/payloadcms/payload/tree/main/templates

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed it to load the actual Payload config and determine the importMap path.

I tested the plugin with the templates available via npx create-payload-app excluding the plugin template, and it was able to load both config and importMap. While Knip did report some unused files and unused exports etc., all of those were valid.

With the plugin template, some files that are actually in use were reported as unused. This seems to be caused by the template’s project structure, since even some Next.js-related files are being flagged as unused. To make it work correctly for the plugin template as well, we’d probably need changes on Knip’s Next.js plugin too, so I’m currently unsure whether it makes sense to support that case.

Comment thread packages/knip/test/plugins/payload.test.ts
@webpro
Copy link
Copy Markdown
Member

webpro commented Feb 20, 2026

This is excellent, thank you very much!

@webpro webpro merged commit 58d99de into webpro-nl:main Feb 20, 2026
30 of 34 checks passed
@Robot-Inventor Robot-Inventor deleted the add-payload-plugin branch February 20, 2026 06:48
@webpro
Copy link
Copy Markdown
Member

webpro commented Feb 21, 2026

🚀 This pull request is included in v5.85.0. See Release 5.85.0 for release notes.

Using Knip in a commercial project? Please consider becoming a sponsor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants