Pre-compilation

The Compilation phase is asynchronous and it will not block your application rendering. However you should use the browser compilation only for prototyping or for quick experiments.

Pre-compilation on gives you following benefits:

  • Ability to compile tags with your favorite pre-processor.
  • Big performance benefit. No need to load and execute the compiler on browser.
  • Sourcemaps support for debugging.

Riot loaders

Tools like webpack and rollup are the perfect match to bundle your riot application tags.For such tools we provide riot official loaders to let import natively riot components into your source code:

  1. import { component } from 'riot'
  2. import MyTag from './path/to/tags/my-tag.riot'
  3. component(MyTag)(document.getElementById('root'))

Compilation via Node

  1. import {compile} from '@riotjs/compiler'
  2. const { code, map } = compile('<p>{hello}</p>', {
  3. //...options
  4. file: 'my/path/to/my-component.riot',
  5. // transform the `:host` css rules
  6. scopedCss: true,
  7. // expressions delimiters
  8. brackets: ['{', '}'],
  9. // keep HTML comments
  10. comments: false
  11. })

The compile function takes a string and returns an object containing the code and map keys.You can handle the code generated however you like and use it into your build system.

Remember that the riot compiler outputs javascript modules and you might want to transpile them in your bundle.

Compilation via Riot.js CLI

You can precompile Riot.js files also via the riot executable, which can be installed with NPM as follows:

  1. npm install @riotjs/cli -g

Using

Here is how riot command works:

  1. # compile a file to current folder
  2. riot some.riot
  3. # compile file to target folder
  4. riot some.riot --output some-folder
  5. # compile file to target path
  6. riot some.riot --output some-folder/some.js
  7. # compile all files from source folder to target folder
  8. riot some/folder --output path/to/dist

For more information, type: riot —help

Watch mode

You can watch directories and automatically transform files when they are changed.

  1. # watch for
  2. riot -w src -o dist

Custom extension

You’re free to use any file extension for your tags (instead of default .riot):

  1. riot --extension html

ES6 Config file

You can use a config file to store and configure easily all your @riotjs/cli options and create your custom parsers

  1. riot --config riot.config src

The riot riot.config.js file:

  1. export default {
  2. output: 'tags/dist',
  3. // sourcemap type
  4. sourcemap: 'inline',
  5. // files extension
  6. extension: 'foo'
  7. }

If you want to use custom preprocessors in your project you should install @riotjs/cli as devDependency running it via npm scripts as follows:

  1. {
  2. "scripts": {
  3. "build": "npx riot -c riot.config src"
  4. },
  5. "devDependencies": {
  6. "@riotjs/cli": "^4.0.0",
  7. "@riotjs/compiler": "^4.0.0",
  8. "pug": "^2.0.3"
  9. }
  10. }

That’s how your riot.config.js file might look like in case you want to use pug as components template engine

  1. import { registerPreprocessor } from '@riotjs/compiler'
  2. import { render } from 'pug'
  3. // register the pug preprocessor
  4. registerPreprocessor('template', 'pug', (code, options) => {
  5. const { file } = options
  6. return {
  7. code: render(code, {
  8. filename: file,
  9. pretty: true,
  10. doctype: 'html'
  11. })
  12. }
  13. })
  14. export default {
  15. extension: 'pug',
  16. // assign the pug preprocessor to the riot compiler options
  17. riot: {
  18. template: 'pug'
  19. }
  20. }

Build your whole application

You can also use the CLI to bundle your entire application.

The app.js file:

  1. import {component} from 'riot'
  2. import App from './app.riot'
  3. component(App)(document.getElementById('root'))
  1. riot app.js -o dist/app.js

Your dist/app.js file will contain all the Riot.js components imported in your application and the code to run it.