Skip to content

Commit

Permalink
feat: Updates to plugin example
Browse files Browse the repository at this point in the history
  • Loading branch information
SamTolmay committed Feb 28, 2023
1 parent b9360e8 commit 8d21a52
Show file tree
Hide file tree
Showing 14 changed files with 2,044 additions and 834 deletions.
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
strict-peer-dependencies=false
auto-install-peers=true
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"printWidth": 100,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5"
}
17 changes: 17 additions & 0 deletions .swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"exclude": [".*.test.js$", ".*/tests/.*", ".*/__mocks__/.*"],
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": true
},
"target": "es2020",
"keepClassNames": true,
"transform": { "react": { "runtime": "classic" } }
},
"module": {
"type": "es6",
"noInterop": true,
"ignoreDynamic": true
}
}
73 changes: 36 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ plugins:
In Lowdefy v4, you can have plugins for Actions, Blocks, Operators (both build, client and server), Connectors and Requests, as well as the auth plugins, Callbacks, Provider and Events. Your plugin package can export any one of these types and does not need to include all types. Consider the following simple examples.
Plugins needs at least the following:
- A `package.json` which declares the package and exports the plugin types.
- A `types.js` file which exports the plugin names.
- A file that exports all the types as named exports.
Expand All @@ -28,12 +29,12 @@ You can then use all of the types declared by the plugin as normal types in your

> NOTE: You should have a `types.js` file, which exports all the types declared by the package - the declared names in this file must be the same as the named exported in your package. This is used so that the build knows which types can be used from that plugin. The type names exported in the `types.js` are the type names you can use in your Lowdefy config.


# A project setup example

If your projects uses local plugins (plugins not installed from npm), then it is useful to setup a pnpm workspace or monorepo. pnpm is the only package manager that will be supported for local plugin development with lowdefy apps in the same monorepo. This example repo can be used as a example of how the workspace should be setup.

This is an example of the proposed project structure:

```
project
/app
Expand All @@ -51,23 +52,24 @@ project
```

In the minimum setup there is 4 important files.
1) The project root `package.json`:

1. The project root `package.json`:

```json
{
"name": "lowdefy-example-plugins-repo",
"version": "1.0.0",
"packageManager": "[email protected]",
"dependencies": {
"lowdefy": "4.0.0-alpha.36"
},
"scripts": {
"lowdefy": "4.0.0-rc.5"
},
"devDependencies": {
}
"scripts": {},
"devDependencies": {}
}
```

2) The project root `pnpm-workspace.yaml`
2. The project root `pnpm-workspace.yaml`

```yaml
packages:
- 'app/*'
Expand All @@ -76,9 +78,10 @@ packages:
- '*'
```

3) Each plugin needs the following files:
3. Each plugin needs the following files:

A `package.json`, only include the relevant exports:

```json
{
"name": "plugin-name",
Expand All @@ -95,21 +98,20 @@ A `package.json`, only include the relevant exports:
"./operators/server": "./src/operators/server.js",
"./types": "./src/types.js"
},
"files": [
"src/*"
],
"files": ["src/*"],
"dependencies": {}
}
```

A `types.js` file, defining all type names (only include the names of your custom types):

```js
export default {
actions: ['Action'],
auth: {
callbacks: ['Callback'],
events: ['Event'],
provider: ['Provider']
provider: ['Provider'],
},
blocks: ['Block'],
connections: ['Connection'],
Expand All @@ -128,38 +130,31 @@ And the type exports files for the various custom types, for example `project/pl
export { default as FooAction } from './FooAction.js';
```


The type names in the `types.js` file and in the exports in plugin export files must match.`

4. The lowdefy app `package.json`:

4) The lowdefy app `package.json`:
```yaml
{
"name": "lowdefy-example-plugins",
"version": "1.0.0",
"type": "module",
"scripts": {
"lowdefy": "lowdefy"
},
"dependencies": {
"lowdefy": "4.0.0-alpha.36"
}
'name': 'lowdefy-example-plugins',
'version': '1.0.0',
'type': 'module',
'scripts': { 'lowdefy': 'lowdefy' },
'dependencies': { 'lowdefy': '4.0.0-rc.5' },
}
```

5) And the app's `lowdefy.yaml` linking to the plugins, your project can also contain multiple Lowdefy apps:
5. And the app's `lowdefy.yaml` linking to the plugins, your project can also contain multiple Lowdefy apps:

```yaml
lowdefy: 4.0.0-alpha.36 # Please check for latest Lowdefy version.
lowdefy: 4.0.0-rc.5 # Please check for latest Lowdefy version.
plugins:
- name: 'plugin-name'
version: 'workspace:*'
pages:
...
pages: ...
```



## To run this example

1. Install pnpm see: https://pnpm.io/installation, it is advise to update to node v16.17 or greater and running `corepack enable`.
Expand All @@ -169,6 +164,7 @@ pages:
## A simple client operator plugin

Your plugin `package.json` needs to export the operator type:

```json
{
"name": "plugin-times-eleven-operator",
Expand All @@ -178,14 +174,13 @@ Your plugin `package.json` needs to export the operator type:
"./operators/client": "./operatorsClient.js",
"./types": "./types.js"
},
"files": [
"*"
],
"files": ["*"],
"dependencies": {}
}
```

The `types.js` file should export the plugin name:

```js
export default {
operators: {
Expand All @@ -195,11 +190,13 @@ export default {
```

The `operatorsClient.js` file:

```js
export { default as _times_eleven } from './_times_eleven.js';
```

And the `_times_eleven` code:

```js
function _times_eleven({ params }) {
return params.number * 11;
Expand All @@ -211,6 +208,7 @@ export default _times_eleven;
## Multiple types in one plugin

All plugin exports in `package.json` file that you can apply, only include the ones used:

```json
{
"name": "plugin-name",
Expand All @@ -227,21 +225,20 @@ All plugin exports in `package.json` file that you can apply, only include the o
"./operators/server": "./operatorsServer.js",
"./types": "./types.js"
},
"files": [
"*"
],
"files": ["*"],
"dependencies": {}
}
```

The export schema for the `types.js` file should look like, use the same type names here as in the named exports:

```js
export default {
actions: ['Action'],
auth: {
callbacks: ['Callback'],
events: ['Event'],
provider: ['Provider']
provider: ['Provider'],
},
blocks: ['Block'],
connections: ['Connection'],
Expand All @@ -255,11 +252,13 @@ export default {
```

Export the types as named exports. So for example, you should have a `blocks.js`:

```js
export { default as Block } from './blocks/Block/Block.js';
```

The block file also needs to change slightly, an example block is:

```js
import React from 'react';
import { blockDefaultProps } from '@lowdefy/block-utils';
Expand Down
2 changes: 1 addition & 1 deletion app/lowdefy.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
lowdefy: 4.0.0-alpha.36 # Please check for latest Lowdefy version.
lowdefy: 4.0.0-rc.5 # Please check for latest Lowdefy version.
license: MIT

plugins:
Expand Down
14 changes: 7 additions & 7 deletions app/package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"name": "lowdefy-example-plugins",
"name": "lowdefy-app",
"version": "1.0.0",
"license": "MIT",
"type": "module",
"scripts": {
"ldf:b": "lowdefy build",
"ldf:d": "lowdefy dev --log-level=debug --no-open",
"ldf:s": "lowdefy start",
"ldf": "lowdefy dev --no-open",
"lowdefy": "lowdefy"
"lowdefy": "lowdefy",
"lowdefy:build": "lowdefy build",
"lowdefy:debug": "lowdefy dev --log-level=debug --no-open",
"lowdefy:dev": "lowdefy dev --no-open",
"lowdefy:start": "lowdefy start"
},
"dependencies": {
"lowdefy": "4.0.0-alpha.36"
"lowdefy": "4.0.0-rc.5"
}
}
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
"version": "1.0.0",
"license": "MIT",
"packageManager": "[email protected]",
"dependencies": {
"lowdefy": "4.0.0-alpha.36"
},
"dependencies": {},
"scripts": {
"lowdefy:build": "pnpm -r --workspace-root --filter='lowdefy-app' lowdefy:build",
"lowdefy:dev": "pnpm -r --workspace-root --filter='lowdefy-app' lowdefy:dev",
"lowdefy:debug": "pnpm -r --workspace-root --filter='lowdefy-app' lowdefy:debug",
"lowdefy:start": "pnpm -r --workspace-root --filter='lowdefy-app' lowdefy:start",
"update-packages": "pnpm up -i -r --latest"
},
"devDependencies": {
"prettier": "^2.8.4"
}
}
29 changes: 18 additions & 11 deletions plugins/plugin-name/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@
"license": "MIT",
"type": "module",
"exports": {
"./auth/callbacks": "./src/auth/callbacks.js",
"./auth/events": "./src/auth/events.js",
"./auth/providers": "./src/auth/providers.js",
"./blocks": "./src/blocks.js",
"./connections": "./src/connections.js",
"./operators/build": "./src/operators/build.js",
"./operators/client": "./src/operators/client.js",
"./operators/server": "./src/operators/server.js",
"./types": "./src/types.js"
"./auth/callbacks": "./dist/auth/callbacks.js",
"./auth/events": "./dist/auth/events.js",
"./auth/providers": "./dist/auth/providers.js",
"./blocks": "./dist/blocks.js",
"./connections": "./dist/connections.js",
"./operators/build": "./dist/operators/build.js",
"./operators/client": "./dist/operators/client.js",
"./operators/server": "./dist/operators/server.js",
"./types": "./dist/types.js"
},
"files": [
"src/*"
"dist/*"
],
"dependencies": {}
"scripts": {
"build": "swc src --out-dir dist --config-file ../../.swcrc --delete-dir-on-start --copy-files",
"prepare": "pnpm build"
},
"devDependencies": {
"@swc/cli": "^0.1.57",
"@swc/core": "^1.2.194"
}
}
Loading

0 comments on commit 8d21a52

Please sign in to comment.