Pre-processors

You can pre-process your components contents using your favorite programming language.

The @riotjs/compiler gives you the possibility to register your preprocessors:

  1. import { registerPreprocessor } from '@riotjs/compiler'
  2. import pug from 'pug'
  3. import sass from 'node-sass'
  4. import ts from 'typescript'
  5. registerPreprocessor('template', 'pug', function(code, { options }) {
  6. const { file } = options
  7. console.log('Preprocess the template', file)
  8. return {
  9. code: pug.render(code),
  10. // no sourcemap here
  11. map: null
  12. }
  13. })
  14. registerPreprocessor('css', 'sass', function(code, { options }) {
  15. const { file } = options
  16. console.log('Compile the sass code in', file)
  17. const {css} = sass.renderSync({
  18. data: code
  19. })
  20. return {
  21. code: css.css.toString(),
  22. map: null
  23. }
  24. })
  25. registerPreprocessor('javascript', 'ts', function(code, { options }) {
  26. const { file } = options
  27. const result = ts.transpileModule(code, {
  28. fileName: file,
  29. compilerOptions: {
  30. module: ts.ModuleKind.ESNext
  31. }
  32. })
  33. return {
  34. code: result.outputText,
  35. map: null
  36. }
  37. })

The Riot.js preprocessors can be only of three types template, css, javascript (the first argument of the registerPreprocessor function).To compile your components with a different template engine you will need to specify the template option via compiler:

  1. import { compile } from '@riotjs/compiler'
  2. compile(source, {
  3. template: 'pug'
  4. })

For the css and javascript preprocessors you can simply enable them directly in your components via type="{preprocessor}" attribute

  1. <my-component>
  2. <p>{getMessage}</p>
  3. <style type="scss">
  4. import 'color/vars'
  5. p {
  6. color: $primary;
  7. }
  8. </style>
  9. <script type="ts">
  10. export default {
  11. getMessage():string {
  12. return 'hello world'
  13. }
  14. }
  15. </script>
  16. </my-component>

Pre-processors Caveats

The Riot.js compiler generates sourcempas out of the code provided by the pre-processors. If your preprocessor will not provide any map output the compiler will not output proper sourcemaps.

  1. import { registerPreprocessor } from '@riotjs/compiler'
  2. import babel from '@babel/core'
  3. registerPreprocessor('javascript', 'babel', function(code, { options }) {
  4. // the babel.transform returns properly an object containing the keys {map, code}
  5. return babel.transform(code, {
  6. sourceMaps: true,
  7. // notice that whitelines should be preserved
  8. retainLines: true,
  9. sourceFileName: options.file,
  10. presets: [[
  11. '@babel/env',
  12. {
  13. targets: {
  14. edge: true
  15. },
  16. loose: true,
  17. modules: false,
  18. useBuiltIns: 'usage'
  19. }
  20. ]]
  21. })
  22. })
  23. registerPreprocessor('javascript', 'my-js-preprocessor', function(code, { options }) {
  24. // the Riot.js compiler will not be able to generate sourcemaps
  25. return {
  26. code: myPreprocessor(code),
  27. map: null
  28. }
  29. })

The javascript preprocessors should preserve the code whitelines of the original source code otherwise the resulting sourcemap will have a broken offset.