Skip to content

Commit f950cbf

Browse files
updated: readme file
1 parent 3e36786 commit f950cbf

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

README.md

+41-5
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,31 @@ npm i @riotjs/compiler -D
1313

1414
## Usage
1515

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:
1717

1818
```js
1919
import { compile } from '@riotjs/compiler'
2020

2121
const { code, map } = await compile('<p>{hello}</p>')
2222
```
2323

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:
2525

2626
```js
2727
import { compiler, registerPreprocessor, registerPostprocessor } from '@riotjs/compiler'
2828
import pug from 'pug'
2929
import buble from 'buble'
3030

3131
// 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+
})
3336

3437
// 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)
3741
})
3842

3943
const { code, map } = await compile('<p>{hello}</p>', {
@@ -42,6 +46,38 @@ const { code, map } = await compile('<p>{hello}</p>', {
4246
})
4347
```
4448

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
4581
4682
4783
[travis-image]: https://img.shields.io/travis/riot/compiler.svg?style=flat-square

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export async function compile(source, options = {
3636
template: 'default',
3737
file: '[unknown-source-file]'
3838
}) {
39-
const { code, map } = await runPreprocessor('template', 'default', options, source)
39+
const { code, map } = await runPreprocessor('template', options.template || 'default', options, source)
4040
const { template, css, javascript } = riotParser(options).parse(code).output
4141

4242
return ruit(createInitialInput(map),

0 commit comments

Comments
 (0)