Gathering detailed insights and metrics for vs-payloadcms-plugin-form-label
Gathering detailed insights and metrics for vs-payloadcms-plugin-form-label
Gathering detailed insights and metrics for vs-payloadcms-plugin-form-label
Gathering detailed insights and metrics for vs-payloadcms-plugin-form-label
npm install vs-payloadcms-plugin-form-label
Typescript
Module System
Node Version
NPM Version
TypeScript (91.08%)
JavaScript (8.92%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
5 Commits
2 Watchers
1 Branches
1 Contributors
Updated on Dec 17, 2024
Latest Version
1.0.2
Package Id
vs-payloadcms-plugin-form-label@1.0.2
Unpacked Size
47.87 kB
Size
10.98 kB
File Count
20
NPM Version
10.9.0
Node Version
22.10.0
Published on
Dec 17, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
2
A template repo to create a Payload CMS plugin.
Payload is built with a robust infrastructure intended to support Plugins with ease. This provides a simple, modular, and reusable way for developers to extend the core capabilities of Payload.
To build your own Payload plugin, all you need is:
Here is a short recap on how to integrate plugins with Payload, to learn more visit the plugin overview page.
To install any plugin, simply add it to your payload.config() in the Plugin array.
1import samplePlugin from 'sample-plugin';
2
3export const config = buildConfig({
4 plugins: [
5 // You can pass options to the plugin
6 samplePlugin({
7 enabled: true,
8 }),
9 ]
10});
The initialization process goes in the following order:
When you build a plugin, you are purely building a feature for your project and then abstracting it outside of the project.
In the payload-plugin-template, you will see a common file structure that is used across all plugins:
In the root folder, you will see various files that relate to the configuration of the plugin. We set up our environment in a similar manner in Payload core and across other projects, so hopefully these will look familiar:
IMPORTANT*: You will need to modify these files.
In the dev folder, you’ll find a basic payload project, created with npx create-payload-app
and the blank template.
The samplePlugin
has already been installed to the payload.config()
file in this project.
1plugins: [ 2 samplePlugin({ 3 enabled: false, 4 }) 5]
Later when you rename the plugin or add additional options, make sure to update them here.
You may wish to add collections or expand the test project depending on the purpose of your plugin. Just make sure to keep this dev environment as simplified as possible - users should be able to install your plugin without additional configuration required.
When you’re ready to start development, navigate into this folder with cd dev
And then start the project with yarn dev
and pull up http://localhost:3000/ in your browser.
Now that we have our environment setup and we have a dev project ready to - it’s time to build the plugin!
index.ts
First up, the src/index.ts
file. It is best practice not to build the plugin directly in this file, instead we use this to export the plugin and types from separate files.
Plugin.ts
To reiterate, the essence of a payload plugin is simply to extend the payload config - and that is exactly what we are doing in this file.
1export const samplePlugin = 2 (pluginOptions: PluginTypes) => 3 (incomingConfig: Config): Config => { 4 let config = { ...incomingConfig } 5 6 // do something cool with the config here 7 8 return config 9 } 10
First, we receive the existing payload config along with any plugin options.
Then we set the variable config
to be equal to the existing config.
From here, you can extend the config as you wish.
Finally, you return the config and that is it!
Spread syntax (or the spread operator) is a feature in JavaScript that uses the dot notation (...) to spread elements from arrays, strings, or objects into various contexts.
We are going to use spread syntax to allow us to add data to existing arrays without losing the existing data. It is crucial to spread the existing data correctly – else this can cause adverse behavior and conflicts with Payload config and other plugins.
Let’s say you want to build a plugin that adds a new collection:
1config.collections = [ 2 ...(config.collections || []), 3 // Add additional collections here 4]
First we spread the config.collections
to ensure that we don’t lose the existing collections, then you can add any additional collections just as you would in a regular payload config.
This same logic is applied to other properties like admin, hooks, globals:
1config.globals = [ 2 ...(config.globals || []), 3 // Add additional globals here 4] 5 6config.hooks = { 7 ...(incomingConfig.hooks || {}), 8 // Add additional hooks here 9}
Some properties will be slightly different to extend, for instance the onInit property:
1import { onInitExtension } from './onInitExtension' // example file 2 3config.onInit = async payload => { 4 if (incomingConfig.onInit) await incomingConfig.onInit(payload) 5 // Add additional onInit code by defining an onInitExtension function 6 onInitExtension(pluginOptions, payload) 7}
If you wish to add to the onInit, you must include the async/await. We don’t use spread syntax in this case, instead you must await the existing onInit before running additional functionality.
In the template, we have stubbed out a basic onInitExtension
file that you can use, if not needed feel free to delete it.
If your plugin uses packages or dependencies that are not browser compatible (fs, stripe, nodemailer, etc), you will need to alias them using your bundler to prevent getting errors in build.
You can read more about aliasing files with Webpack or Vite in the excluding server modules docs.
If your plugin has options, you should define and provide types for these options in a separate file which gets exported from the main index.ts.
1export interface PluginTypes { 2 /** 3 * Enable or disable plugin 4 * @default false 5 */ 6 enabled?: boolean 7}
If possible, include JSDoc comments to describe the options and their types. This allows a developer to see details about the options in their editor.
Having a test suite for your plugin is essential to ensure quality and stability. Jest is a popular testing framework, widely used for testing JavaScript and particularly for applications built with React.
Jest organizes tests into test suites and cases. We recommend creating individual tests based on the expected behavior of your plugin from start to finish.
Writing tests with Jest is very straightforward and you can learn more about how it works in the Jest documentation.
For this template, we stubbed out plugin.spec.ts
in the dev
folder where you can write your tests.
1describe('Plugin tests', () => { 2 // Create tests to ensure expected behavior from the plugin 3 it('some condition that must be met', () => { 4 // Write your test logic here 5 expect(...) 6 }) 7})
With this tutorial and the payload-plugin-template
, you should have everything you need to start building your own plugin.
In addition to the setup, here are other best practices aim we follow:
Please contact Payload with any questions about using this plugin template.
No vulnerabilities found.
No security vulnerabilities found.