Pre-compilation on gives you following benefits:
- Ability to compile tags with your .
- Big performance benefit. No need to load and execute the compiler on browser.
- Sourcemaps support for debugging.
Tools like and 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:
import {compile} from '@riotjs/compiler'
const { code, map } = compile('<p>{hello}</p>', {
//...options
file: 'my/path/to/my-component.riot',
// transform the `:host` css rules
scopedCss: true,
// expressions delimiters
brackets: ['{', '}'],
// keep HTML comments
})
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.
npm install @riotjs/cli -g
Using
Here is how riot
command works:
# compile a file to current folder
riot some.riot
# compile file to target folder
riot some.riot --output some-folder
# compile file to target path
# compile all files from source folder to target folder
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.
Custom extension
You’re free to use any file extension for your tags (instead of default .riot
):
riot --extension html
ES6 Config file
riot --config riot.config src
The riot riot.config.js
file:
export default {
output: 'tags/dist',
// sourcemap type
sourcemap: 'inline',
// files extension
extension: 'foo'
}
If you want to use custom preprocessors in your project you should install @riotjs/cli
as devDependency
running it via npm scripts as follows:
That’s how your riot.config.js
file might look like in case you want to use pug
as components template engine
import { registerPreprocessor } from '@riotjs/compiler'
import { render } from 'pug'
// register the pug preprocessor
const { file } = options
return {
code: render(code, {
filename: file,
pretty: true,
doctype: 'html'
})
}
})
export default {
extension: 'pug',
// assign the pug preprocessor to the riot compiler options
riot: {
template: 'pug'
}
}
Build your whole application
You can also use the CLI to bundle your entire application.
import {component} from 'riot'
import App from './app.riot'
component(App)(document.getElementById('root'))
Your file will contain all the Riot.js components imported in your application and the code to run it.