3
3
Scully uses a plugin system to allow users to define new ways for Scully to pre-render your app. There are three main
4
4
types of plugins:
5
5
6
+ 1 . [ Register Plugin] ( #register-plugin )
6
7
1 . [ Router Plugins] ( #router-plugin )
7
- 2 . [ Render Plugins] ( #render-plugin )
8
- 3 . [ File Handler Plugins] ( #file-plugin )
8
+ 1 . [ Render Plugins] ( #render-plugin )
9
+ 1 . [ File Handler Plugins] ( #file-plugin )
9
10
10
11
See our [ Recommended Plugins] ( recommended-plugins.md ) page to find a list of available plugins.
11
12
12
13
---
13
14
15
+ ## Register Plugin
16
+
17
+ The ` registerPlugin ` is the method created for add new plugins to scully. This method has 4 parameters:
18
+
19
+ - type
20
+ - name
21
+ - plugin
22
+ - validator
23
+
24
+ ### type: PluginTypes
25
+
26
+ ` type ` is a reference the to the type of plugin. It could be ` render ` , ` router ` or ` fileHandler ` .
27
+
28
+ ### name: string
29
+
30
+ ` name ` is a reference to the name of the plugin.
31
+
32
+ ### plugin: any
33
+
34
+ ` plugin ` is a reference to the plugin function.
35
+
36
+ ### validator: function
37
+
38
+ ` validator ` is a reference to the validations function. It should return an array of errors.
39
+
40
+ > Cool tip: you can add color to the validator errors using the colors inside the ` log.ts ` file.
41
+
42
+ ##### Example
43
+
44
+ ``` typescript
45
+ import {yellow } from ' @scullyio/scully/utils/log' ;
46
+
47
+ // Omited code ...
48
+
49
+ const validator = async options => {
50
+ const errors = [];
51
+
52
+ if (options .numberOfPages && typeof options .numberOfPages !== ' number' ) {
53
+ errors .push (
54
+ ` my-custom-pluging plugin numberOfPages should be a number, not a ${yellow (
55
+ typeof options .numberOfPages
56
+ )} `
57
+ );
58
+ }
59
+
60
+ return errors ;
61
+ };
62
+ ```
63
+
64
+ ### IMPORTANT
65
+
66
+ Scully plugins are files with ` .js ` extension, which should be exported and used in ` scully.config.js `
67
+ file using the ` require() ` method.
68
+
14
69
## <a name =" router-plugin " ></a > Router Plugins
15
70
16
71
### <a name =" router-plugin-what-is " ></a > What is a Router Plugin?
@@ -105,7 +160,9 @@ The `HandledRoute[]` gets added into the `scully-routes.json` that is generated
105
160
In our previous example of an app with the route ` /user/:userId ` where we have five distinct userIds, here is a ** router
106
161
plugin** that would turn the raw route into five distinct HandledRoutes.
107
162
108
- ``` typescript
163
+ ``` javascript
164
+ const {registerPlugin } = require (' @scullyio/scully' );
165
+
109
166
function userIdPlugin (route : string , config = {}): Promise<HandledRoute[]> {
110
167
return Promise .resolve ([
111
168
{route: ' /user/1' },
@@ -116,7 +173,8 @@ function userIdPlugin(route: string, config = {}): Promise<HandledRoute[]> {
116
173
]);
117
174
}
118
175
// DON'T FORGET THIS STEP
119
- registerPlugin (' router' , ' userIds' , userIdPlugin );
176
+ const validator = async conf => [];
177
+ registerPlugin (' router' , ' userIds' , userIdPlugin, validator);
120
178
```
121
179
122
180
Once we have built a plugin, we can configure our ` scully.config.js ` to use our plugin.
@@ -219,6 +277,8 @@ function exampleContentPlugin(HTML: string, route: HandledRoute): Promise<string
219
277
The following is a sample ** render plugin** that adds a title to the head of a page if it doesn't find one.
220
278
221
279
``` typescript
280
+ const {registerPlugin} = require (' @scullyio/scully' );
281
+
222
282
function defaultTitlePlugin(html , route ) {
223
283
// If no title in the document
224
284
if (html .indexOf (' <title' ) < 0 ) {
@@ -230,7 +290,10 @@ function defaultTitlePlugin(html, route) {
230
290
return Promise .resolve (html );
231
291
}
232
292
// DON'T FORGET THIS STEP
233
- registerPlugin (' render' , ' defaultTitle' , defaultTitlePlugin );
293
+ const validator = async conf => [];
294
+ registerPlugin (' render' , ' defaultTitle' , defaultTitlePlugin , validator );
295
+
296
+ module .exports .defaultTitlePlugin = defaultTitlePlugin ;
234
297
```
235
298
236
299
In this example, the HTML that the Angular app rendered is transformed to include a title (if one wasn't found). This
@@ -239,11 +302,16 @@ is the primary function of a render plugin.
239
302
Here is another example that would replace any instances of ` :) ` with a smiley emoji:
240
303
241
304
``` typescript
305
+ const {registerPlugin} = require (' @scullyio/scully' );
306
+
242
307
function smileEmojiPlugin(html , route ) {
243
308
return Promise .resolve (html .replace (/ \:\) / g , ' 😊' ));
244
309
}
245
310
// This registers your plugin as
246
- registerPlugin (' render' , ' smiles' , smileEmojiPlugin );
311
+ const validator = async conf => [];
312
+ registerPlugin (' render' , ' smiles' , smileEmojiPlugin , validator );
313
+
314
+ module .exports .smileEmojiPlugin = smileEmojiPlugin ;
247
315
```
248
316
249
317
### <a name =" render-plugin-configure " ></a > Render Plugin Examples
@@ -295,6 +363,8 @@ function csvFilePlugin(raw) {
295
363
}
296
364
// This registers your plugin
297
365
registerPlugin (' fileHandler' , ' csv' , {handler: csvFilePlugin });
366
+
367
+ module .exports .csvFilePlugin = csvFilePlugin ;
298
368
```
299
369
300
370
### <a name =" file-plugin-configure " ></a > File Handler Plugin Examples
0 commit comments