plugins: shorter way for init to return unload
Massimo Melina committed
Dec 8, 2024 at 23:36 UTC
bcd48a6ab256455fdb3172ab61227d3bae5dddf7
3 files changed
+13
-6
dev-plugins.md
+9
-3
@@ -16,6 +16,7 @@ but nothing is preventing a single plug-in from doing both tasks.
16
Plugins can run both in backend (the server) and frontend (the browser). Frontend files reside in the "public" folder, while all the rest is backend.
17
18
## Exported object
19
+
20
`plugin.js` is a javascript module (executed by Node.js), and its main way to communicate with HFS is by exporting things.
21
For example, it can define its description like this
22
```js
@@ -23,6 +24,9 @@ exports.description = "I'm a nice plugin"
24
```
25
26
The set of things exported goes by the name "exported object".
27
+
28
+### init
29
+
30
A plugin can define an `init` function like this:
31
```js
32
exports.init = function(api) {
@@ -80,8 +84,9 @@ All the following properties are optional unless otherwise specified.
84
WARNING: All the properties above are a bit special and must go in `exports` only (thus, not returned in `init`) and the syntax
85
used must be strictly JSON (thus, no single quotes, only double quotes for strings and objects), and must fit one line.
86
83
-- `init: (api: object) => void | object` described in the previous section. If an object is returned,
84
- it will be merged with other "exported" properties described in this section, so you can return `{ unload }` for example.
87
+- `init: (api: object) => void | object | function` described in the previous section. If an object is returned,
88
+ it will be merged with other "exported" properties described in this section, so you can return `{ unload }` for example.
89
+ If you return a function, this is just a shorter way to return the `unload`.
90
- `frontend_css: string | string[]` path to one or more css files that you want the frontend to load. These are to be placed in the `public` folder (refer below).
91
You can also include external files, by entering a full URL. Multiple files can be specified as `['file1.css', 'file2.css']`.
92
- `frontend_js: string | string[]` path to one or more js files that you want the frontend to load. These are to be placed in the `public` folder (refer below).
@@ -678,11 +683,12 @@ If you want to override a text regardless of the language, use the special langu
683
684
## API version history
685
681
-- 10.1 (v0.55.0)
686
+ - 10.2 (v0.55.0)
687
- HFS.copyTextToClipboard
688
- HFS.urlParams
689
- exports.beforePlugin + afterPlugin
690
- config.type: color
691
+ - init can now return directly the unload function
692
- 9.6 (v0.54.0)
693
- frontend event: showPlay
694
- api.addBlock
src/const.ts
+1
-1
@@ -8,7 +8,7 @@ import { basename, dirname, join } from 'path'
8
import { formatTimestamp } from './cross'
9
export * from './cross-const'
10
11
-export const API_VERSION = 10.1
11
+export const API_VERSION = 10.2
12
export const COMPATIBLE_API_VERSION = 1 // while changes in the api are not breaking, this number stays the same, otherwise it is made equal to API_VERSION
13
14
export const argv = minimist(process.argv.slice(2))
src/plugins.ts
+3
-2
@@ -108,7 +108,7 @@ export function getPluginConfigFields(id: string) {
108
}
109
110
async function initPlugin<T>(pl: any, morePassedToInit?: T) {
111
- return Object.assign(pl, await pl.init?.({
111
+ const res = await pl.init?.({
112
Const,
113
require,
114
getConnections,
@@ -120,7 +120,8 @@ async function initPlugin<T>(pl: any, morePassedToInit?: T) {
120
addBlock,
121
misc,
122
...morePassedToInit
123
- }))
123
+ })
124
+ return Object.assign(pl, typeof res === 'function' ? { unload: res } : res)
125
}
126
127
const already = new Set()