In-browser compilation

The riot+compiler.js bundle lets you compile and execute riot tags directly in your browser for quick prototypes and tests.You can load riot tags into your browser by setting a type="riot" attribute on your script tags.For example:

  1. <!-- mount point -->
  2. <my-tag></my-tag>
  3. <!-- <my-tag/> is specified in an external file -->
  4. <script src="path/to/javascript/my-tag.riot" type="riot"></script>
  5. <!-- include riot.js and the compiler -->
  6. <script src="https://unpkg.com/riot@4/riot+compiler.min.js"></script>
  7. <!-- compile and mount -->
  8. <script>
  9. (async function main() {
  10. await riot.compile()
  11. riot.mount('my-tag')
  12. }())
  13. </script>

Notice that in this case riot will internally transform all the export default expressions to enable a better support for browsers that don’t support javascript modules yet.

Riot can compile asynchronously all the external tags included via <script> into the DOM and via riot.mount it will render them.

You might prefer using data-src instead of src on your <script> tags stop your browser prefetching automatically any riot script tag in order to avoid to load the same resources twice. Riot will automatically fetch and compile your tags via ajax.

In-browser compilation with inline templates

Your Riot.js components could be also be included directly in your page via <template> tags. For example:

  1. <!-- somewhere in your page -->
  2. <template id="my-tag">
  3. <my-tag>
  4. <p>{ props.message }</p>
  5. </my-tag>
  6. </template>

The riot+compiler.js bundle exposes the compileFromString and inject methods that combined together, can help you compiling the above component:

  1. const tagString = document.getElementById('my-tag').innerHTML
  2. // get the compiled code
  3. const {code} = riot.compileFromString(tagString)
  4. // create the riot component in runtime
  5. riot.inject(code, 'my-tag', './my-tag.html')
  6. riot.mount('my-tag')