This compiler will not work with older Riot.js versions. It's designed to work with Riot.js > 4.0.0. For Riot.js < 4.0.0 please check the v3 branch
npm i @riotjs/compiler -D
The riot compiler can compile only strings:
import{compile}from'@riotjs/compiler'const{ code, map }=compile('<p>{hello}</p>')You can compile your tags also using the new registerPreprocessor and registerPostprocessor APIs for example:
import{compiler,registerPreprocessor,registerPostprocessor}from'@riotjs/compiler'importpugfrom'pug'importbublefrom'buble'// process your tag template before it will be compiledregisterPreprocessor('template','pug',function(code,{ options }){const{ file }=optionsconsole.log('your file path is:',file)return{code: pug.render(code),// no sourcemap heremap: null}})// your compiler output will pass from hereregisterPostprocessor(function(code,{ options }){const{ file }=optionsconsole.log('your file path is:',file)// notice that buble.transform returns {code, map}returnbuble.transform(code)})const{ code, map }=compile('<p>{hello}</p>',{// specify the template preprocessortemplate: 'pug'})- string: is your tag source code
- options: the options should contain the
filekey identifying the source of the string to compile and thetemplatepreprocessor to use as string
Note: specific preprocessors like the css or the javascript ones can be enabled simply specifying the type attribute
in the tag source code for example
<my-tag><styletype='scss'>
// ...
</style></my-tag>- type: either one of
templatecssorjavascript - id: unique preprocessor identifier
- preprocessorFn: function receiving the code as first argument and the current options as second
- postprocessorFn: function receiving the compiler output as first argument and the current options as second