Skip to content

Commit 0012083

Browse files
committed
chore: updates template to align with Payload 2.x
1 parent 3f6377f commit 0012083

File tree

16 files changed

+756
-3660
lines changed

16 files changed

+756
-3660
lines changed

README.md

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Payload is built with a robust infrastructure intended to support Plugins with e
77
To build your own Payload plugin, all you need is:
88

99
* An understanding of the basic Payload concepts
10-
* And some JavaScript experience
10+
* And some JavaScript/Typescript experience
1111

1212
## Background
1313

@@ -24,7 +24,7 @@ export const config = buildConfig({
2424
plugins: [
2525
// You can pass options to the plugin
2626
samplePlugin({
27-
enabled: true,
27+
enabled: true,
2828
}),
2929
]
3030
});
@@ -46,7 +46,7 @@ When you build a plugin, you are purely building a feature for your project and
4646

4747
### Template Files
4848

49-
In the [payload-plugin-template](https://github.com/payloadcms/payload-plugin-template), you will see a common file structure that is used across all plugins:
49+
In the [payload-plugin-template](https://github.com/payloadcms/payload-plugin-template), you will see a common file structure that is used across all plugins:
5050

5151
1. root folder
5252
2. /src folder
@@ -75,9 +75,9 @@ The `samplePlugin` has already been installed to the `payload.config()` file in
7575

7676
```ts
7777
plugins: [
78-
samplePlugin({
79-
enabled: false,
80-
})
78+
samplePlugin({
79+
enabled: false,
80+
})
8181
]
8282
```
8383

@@ -95,7 +95,7 @@ Now that we have our environment setup and we have a dev project ready to - it
9595

9696
**index.ts**
9797

98-
First up, the `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.
98+
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.
9999

100100
**Plugin.ts**
101101

@@ -104,13 +104,13 @@ To reiterate, the essence of a payload plugin is simply to extend the payload co
104104
```ts
105105
export const samplePlugin =
106106
(pluginOptions: PluginTypes) =>
107-
(incomingConfig: Config): Config => {
108-
let config = { ...incomingConfig }
107+
(incomingConfig: Config): Config => {
108+
let config = { ...incomingConfig }
109109

110-
// do something cool with the config here
110+
// do something cool with the config here
111111

112-
return config
113-
}
112+
return config
113+
}
114114

115115
```
116116

@@ -156,9 +156,11 @@ config.hooks = {
156156
Some properties will be slightly different to extend, for instance the onInit property:
157157

158158
```ts
159+
import { onInitExtension } from './onInitExtension' // example file
160+
159161
config.onInit = async payload => {
160162
if (incomingConfig.onInit) await incomingConfig.onInit(payload)
161-
// Add additional onInit code by using the onInitExtension function
163+
// Add additional onInit code by defining an onInitExtension function
162164
onInitExtension(pluginOptions, payload)
163165
}
164166
```
@@ -167,30 +169,11 @@ If you wish to add to the onInit, you must include the async/await. We don’t u
167169

168170
In the template, we have stubbed out a basic `onInitExtension` file that you can use, if not needed feel free to delete it.
169171

170-
##### Webpack
171-
172-
If your plugin uses packages or dependencies that are not browser compatible (fs, stripe, nodemailer, etc), you will need to add them to the webpack property to prevent getting errors in build.
173-
174-
Webpack is another part of the payload.config that will be a little more tricky to extend, because we need to carefully pass the existing webpack data to multiple places.
172+
##### File Aliasing
175173

176-
To simplify this, the template includes a `webpack.ts` file which takes care of spreading the existing webpack, so you can just add your new stuff:
177-
178-
```ts
179-
const newWebpack = {
180-
...existingWebpackConfig,
181-
resolve: {
182-
...(existingWebpackConfig.resolve || {}),
183-
alias: {
184-
...(existingWebpackConfig.resolve?.alias ? existingWebpackConfig.resolve.alias : {}),
185-
// Add additional aliases here like so:
186-
[path.resolve(__dirname, './yourFileHere')]: mockModulePath,
187-
},
188-
},
189-
}
190-
191-
```
174+
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.
192175

193-
You can use the alias Webpack feature to tell Webpack to avoid importing the files or modules you want to restrict to server-only.
176+
You can read more about aliasing files with Webpack or Vite in the [excluding server modules](https://payloadcms.com/docs/admin/excluding-server-code#aliasing-server-only-modules) docs.
194177

195178
##### Types.ts
196179

dev/.env.example

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
MONGODB_URI=mongodb://127.0.0.1/plugin-development
1+
DATABASE_URI=mongodb://127.0.0.1/plugin-development
22
PAYLOAD_SECRET=hellohereisasecretforyou
3-
PAYLOAD_SEED=true

dev/.gitignore

Lines changed: 5 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -1,166 +1,6 @@
1-
### Node ###
2-
# Logs
3-
logs
4-
*.log
5-
npm-debug.log*
6-
yarn-debug.log*
7-
yarn-error.log*
8-
lerna-debug.log*
9-
.pnpm-debug.log*
10-
11-
# Diagnostic reports (https://nodejs.org/api/report.html)
12-
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
13-
14-
# Runtime data
15-
pids
16-
*.pid
17-
*.seed
18-
*.pid.lock
19-
20-
# Directory for instrumented libs generated by jscoverage/JSCover
21-
lib-cov
22-
23-
# Coverage directory used by tools like istanbul
24-
coverage
25-
*.lcov
26-
27-
# nyc test coverage
28-
.nyc_output
29-
30-
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
31-
.grunt
32-
33-
# Bower dependency directory (https://bower.io/)
34-
bower_components
35-
36-
# node-waf configuration
37-
.lock-wscript
38-
39-
# Compiled binary addons (https://nodejs.org/api/addons.html)
40-
build/Release
41-
42-
# Dependency directories
43-
node_modules/
44-
jspm_packages/
45-
46-
# Snowpack dependency directory (https://snowpack.dev/)
47-
web_modules/
48-
49-
# TypeScript cache
50-
*.tsbuildinfo
51-
52-
# Optional npm cache directory
53-
.npm
54-
55-
# Optional eslint cache
56-
.eslintcache
57-
58-
# Optional stylelint cache
59-
.stylelintcache
60-
61-
# Microbundle cache
62-
.rpt2_cache/
63-
.rts2_cache_cjs/
64-
.rts2_cache_es/
65-
.rts2_cache_umd/
66-
67-
# Optional REPL history
68-
.node_repl_history
69-
70-
# Output of 'npm pack'
71-
*.tgz
72-
73-
# Yarn Integrity file
74-
.yarn-integrity
75-
76-
# dotenv environment variable files
77-
.env
78-
.env.development.local
79-
.env.test.local
80-
.env.production.local
81-
.env.local
82-
83-
# parcel-bundler cache (https://parceljs.org/)
84-
.cache
85-
.parcel-cache
86-
87-
# Next.js build output
88-
.next
89-
out
90-
91-
# Nuxt.js build / generate output
92-
.nuxt
1+
build
932
dist
94-
95-
# Gatsby files
96-
.cache/
97-
# Comment in the public line in if your project uses Gatsby and not Next.js
98-
# https://nextjs.org/blog/next-9-1#public-directory-support
99-
# public
100-
101-
# vuepress build output
102-
.vuepress/dist
103-
104-
# vuepress v2.x temp and cache directory
105-
.temp
106-
107-
# Docusaurus cache and generated files
108-
.docusaurus
109-
110-
# Serverless directories
111-
.serverless/
112-
113-
# FuseBox cache
114-
.fusebox/
115-
116-
# DynamoDB Local files
117-
.dynamodb/
118-
119-
# TernJS port file
120-
.tern-port
121-
122-
# Stores VSCode versions used for testing VSCode extensions
123-
.vscode-test
124-
125-
# yarn v2
126-
.yarn/cache
127-
.yarn/unplugged
128-
.yarn/build-state.yml
129-
.yarn/install-state.gz
130-
.pnp.*
131-
132-
### Node Patch ###
133-
# Serverless Webpack directories
134-
.webpack/
135-
136-
# Optional stylelint cache
137-
138-
# SvelteKit build / generate output
139-
.svelte-kit
140-
141-
### VisualStudioCode ###
142-
.vscode/*
143-
!.vscode/settings.json
144-
!.vscode/tasks.json
145-
!.vscode/launch.json
146-
!.vscode/extensions.json
147-
!.vscode/*.code-snippets
148-
149-
# Local History for Visual Studio Code
150-
.history/
151-
152-
# Built Visual Studio Code Extensions
153-
*.vsix
154-
155-
### VisualStudioCode Patch ###
156-
# Ignore all local history of files
157-
.history
158-
.ionide
159-
160-
# Support for Project snippet scope
161-
.vscode/*.code-snippets
162-
163-
# Ignore code-workspaces
164-
*.code-workspace
165-
166-
# End of https://www.toptal.com/developers/gitignore/api/node,visualstudiocode
3+
/media
4+
node_modules
5+
.DS_Store
6+
.env

dev/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ ENV PAYLOAD_CONFIG_PATH=dist/payload.config.js
1616

1717
WORKDIR /home/node/app
1818
COPY package*.json ./
19+
COPY yarn.lock ./
1920

2021
RUN yarn install --production
2122
COPY --from=builder /home/node/app/dist ./dist

dev/docker-compose.yml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
11
version: '3'
22

33
services:
4-
54
payload:
65
image: node:18-alpine
76
ports:
8-
- "3000:3000"
7+
- '3000:3000'
98
volumes:
109
- .:/home/node/app
1110
- node_modules:/home/node/app/node_modules
1211
working_dir: /home/node/app/
1312
command: sh -c "yarn install && yarn dev"
1413
depends_on:
1514
- mongo
16-
environment:
17-
MONGODB_URI: mongodb://mongo:27017/payload
18-
PORT: 3000
19-
NODE_ENV: development
20-
PAYLOAD_SECRET: TESTING
15+
env_file:
16+
- .env
2117

2218
mongo:
2319
image: mongo:latest
2420
ports:
25-
- "27017:27017"
21+
- '27017:27017'
2622
command:
2723
- --storageEngine=wiredTiger
2824
volumes:

dev/nodemon.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{
2+
"$schema": "https://json.schemastore.org/nodemon.json",
23
"ext": "ts",
3-
"exec": "ts-node src/server.ts"
4-
}
4+
"exec": "ts-node src/server.ts -- -I",
5+
"stdin": false
6+
}

dev/package.json

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,35 @@
11
{
2-
"name": "plugin-development",
3-
"description": "Payload project created from blank template",
2+
"name": "Payload2Blank",
3+
"description": "A blank template to get started with Payload",
44
"version": "1.0.0",
55
"main": "dist/server.js",
66
"license": "MIT",
77
"scripts": {
88
"dev": "cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts nodemon",
9-
"test": "cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts PAYLOAD_SEED=true jest --detectOpenHandles --forceExit",
109
"build:payload": "cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts payload build",
1110
"build:server": "tsc",
1211
"build": "yarn copyfiles && yarn build:payload && yarn build:server",
1312
"serve": "cross-env PAYLOAD_CONFIG_PATH=dist/payload.config.js NODE_ENV=production node dist/server.js",
1413
"copyfiles": "copyfiles -u 1 \"src/**/*.{html,css,scss,ttf,woff,woff2,eot,svg,jpg,png}\" dist/",
1514
"generate:types": "cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts payload generate:types",
16-
"generate:graphQLSchema": "cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts payload generate:graphQLSchema"
15+
"generate:graphQLSchema": "cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts payload generate:graphQLSchema",
16+
"payload": "cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts payload"
1717
},
1818
"dependencies": {
19+
"@payloadcms/bundler-webpack": "^1.0.0",
20+
"@payloadcms/db-mongodb": "^1.0.0",
21+
"@payloadcms/plugin-cloud": "^2.0.0",
22+
"@payloadcms/richtext-slate": "^1.0.0",
1923
"cross-env": "^7.0.3",
2024
"dotenv": "^8.2.0",
2125
"express": "^4.17.1",
22-
"payload": "^1.11.6"
26+
"payload": "^2.0.0"
2327
},
2428
"devDependencies": {
25-
"@swc/jest": "^0.2.26",
2629
"@types/express": "^4.17.9",
27-
"@types/jest": "^29.5.3",
2830
"copyfiles": "^2.4.1",
29-
"jest": "^29.5.0",
30-
"mongodb-memory-server": "^8.13.0",
3131
"nodemon": "^2.0.6",
3232
"ts-node": "^9.1.1",
3333
"typescript": "^4.8.4"
34-
},
35-
"peerDependencies": {
36-
"@swc/core": "^1.2.71"
3734
}
3835
}

0 commit comments

Comments
 (0)