@@ -13,27 +13,31 @@ npm i @riotjs/compiler -D
13
13
14
14
## Usage
15
15
16
- The riot compiler is completely asynchronous and it can accepts only strings:
16
+ The riot compiler is completely asynchronous and it can compile only strings:
17
17
18
18
``` js
19
19
import { compile } from ' @riotjs/compiler'
20
20
21
21
const { code , map } = await compile (' <p>{hello}</p>' )
22
22
```
23
23
24
- You can compile your tags also using preprocessors and postprocessors for example:
24
+ You can compile your tags also using the new ` registerPreprocessor ` and ` registerPostprocessor ` APIs for example:
25
25
26
26
``` js
27
27
import { compiler , registerPreprocessor , registerPostprocessor } from ' @riotjs/compiler'
28
28
import pug from ' pug'
29
29
import buble from ' buble'
30
30
31
31
// process your tag template before it will be compiled
32
- registerPreprocessor (' template' , ' pug' , pug .compile )
32
+ registerPreprocessor (' template' , ' pug' , async function (code , { file }) {
33
+ console .log (' your file path is:' , file)
34
+ return await pug .compile (code)
35
+ })
33
36
34
37
// your compiler output will pass from here
35
- registerPostprocessor (function (code ) {
36
- return buble .transform (code)
38
+ registerPostprocessor (async function (code , { file }) {
39
+ console .log (' your file path is:' , file)
40
+ return await buble .transform (code)
37
41
})
38
42
39
43
const { code , map } = await compile (' <p>{hello}</p>' , {
@@ -42,6 +46,38 @@ const { code, map } = await compile('<p>{hello}</p>', {
42
46
})
43
47
```
44
48
49
+ ## API
50
+
51
+ ### compile(string, options)
52
+ #### @returns ` <Promise>{ code, map } ` output that can be used by Riot.js
53
+
54
+ - * string* : is your tag source code
55
+ - * options* : the options should contain the ` file ` key identifying the source of the string to compile and
56
+ the ` template ` preprocessor to use as string
57
+
58
+ Note: specific preprocessors like the ` css ` or the ` javascript ` ones can be enabled simply specifying the ` type ` attribute
59
+ in the tag source code for example
60
+
61
+ ``` html
62
+ <my-tag >
63
+ <style type =' scss' >
64
+ // ...
65
+ </style >
66
+ </my-tag >
67
+ ```
68
+
69
+ ### registerPreprocessor(type, id, preprocessorFn)
70
+ #### @returns `Object` containing all the preprocessors registered
71
+
72
+ - *type*: either one of `template` `css` or `javascript`
73
+ - *id*: unique preprocessor identifier
74
+ - *preprocessorFn*: function receiving the code as first argument and the current options as second, it can be also asynchronous
75
+
76
+
77
+ ### registerPostprocessor(postprocessorFn)
78
+ #### @returns `Set` containing all the postprocessors registered
79
+
80
+ - *postprocessorFn*: function receiving the compiler output as first argument and the current options as second, it can be also asynchronous
45
81
46
82
47
83
[travis-image]: https://img.shields.io/travis/riot/compiler.svg?style=flat-square
0 commit comments