JSPM

  • Created
  • Published
  • Downloads 1311
  • Score
    100M100P100Q108673F
  • License MIT

Compiler for Riot.js .riot files

Package Exports

  • @riotjs/compiler
  • @riotjs/compiler/essential
  • @riotjs/compiler/src/constants.js
  • @riotjs/compiler/src/generators/css/index.js
  • @riotjs/compiler/src/generators/javascript/index.js
  • @riotjs/compiler/src/generators/javascript/utils.js
  • @riotjs/compiler/src/generators/template/bindings/each.js
  • @riotjs/compiler/src/generators/template/bindings/if.js
  • @riotjs/compiler/src/generators/template/bindings/simple.js
  • @riotjs/compiler/src/generators/template/bindings/slot.js
  • @riotjs/compiler/src/generators/template/bindings/tag.js
  • @riotjs/compiler/src/generators/template/builder.js
  • @riotjs/compiler/src/generators/template/checks.js
  • @riotjs/compiler/src/generators/template/constants.js
  • @riotjs/compiler/src/generators/template/expressions/attribute.js
  • @riotjs/compiler/src/generators/template/expressions/event.js
  • @riotjs/compiler/src/generators/template/expressions/index.js
  • @riotjs/compiler/src/generators/template/expressions/text.js
  • @riotjs/compiler/src/generators/template/expressions/value.js
  • @riotjs/compiler/src/generators/template/find.js
  • @riotjs/compiler/src/generators/template/index.js
  • @riotjs/compiler/src/generators/template/utils.js
  • @riotjs/compiler/src/index.js
  • @riotjs/compiler/src/postprocessors.js
  • @riotjs/compiler/src/preprocessors.js
  • @riotjs/compiler/src/transformer.js
  • @riotjs/compiler/src/utils/add-lines-offset.js
  • @riotjs/compiler/src/utils/ast-nodes-checks.js
  • @riotjs/compiler/src/utils/build-types.js
  • @riotjs/compiler/src/utils/clone-deep.js
  • @riotjs/compiler/src/utils/compose-sourcemaps.js
  • @riotjs/compiler/src/utils/create-sourcemap.js
  • @riotjs/compiler/src/utils/custom-ast-nodes.js
  • @riotjs/compiler/src/utils/generate-ast.js
  • @riotjs/compiler/src/utils/generate-javascript.js
  • @riotjs/compiler/src/utils/get-line-and-column-by-position.js
  • @riotjs/compiler/src/utils/get-preprocessor-type-by-attribute.js
  • @riotjs/compiler/src/utils/has-html-outside-root-node.js
  • @riotjs/compiler/src/utils/html-entities/encode.js
  • @riotjs/compiler/src/utils/html-entities/entities.json
  • @riotjs/compiler/src/utils/is-empty-array.js
  • @riotjs/compiler/src/utils/is-empty-sourcemap.js
  • @riotjs/compiler/src/utils/mock/assert-mock-api.js
  • @riotjs/compiler/src/utils/mock/os-mock-api.js
  • @riotjs/compiler/src/utils/mock/sourcemap-mock-api.js
  • @riotjs/compiler/src/utils/preprocess-node.js
  • @riotjs/compiler/src/utils/replace-in-range.js
  • @riotjs/compiler/src/utils/sourcemap-as-json.js
  • @riotjs/compiler/src/utils/split-string-by-EOL.js
  • @riotjs/compiler/src/utils/trim-end.js
  • @riotjs/compiler/src/utils/trim-start.js
  • @riotjs/compiler/src/utils/unescape-char.js

Readme

Build Status Issue Count Coverage Status NPM version NPM downloads MIT License

Important

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

Installation

npm i @riotjs/compiler -D

Usage

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'
import pug from 'pug'
import * as babel from '@babel/core'

// process your tag template before it will be compiled
registerPreprocessor('template', 'pug', function (code, { options }) {
  const { file } = options
  console.log('your file path is:', file)

  return {
    code: pug.render(code),
    // no sourcemap here
    map: null,
  }
})

// your compiler output will pass from here
registerPostprocessor(function (code, { options }) {
  const { file } = options
  console.log('your file path is:', file)

  // notice that babel.transformSync returns {code, map}
  return babel.transformSync(code)
})

const { code, map } = compile('<p>{hello}</p>', {
  // specify the template preprocessor
  template: 'pug',
})

API

compile(string, options)

@returns { code, map } output that can be used by Riot.js

  • string: is your tag source code
  • options: the options should contain the file key identifying the source of the string to compile and the template preprocessor 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>
  <style type="scss">
    // ...
  </style>
</my-tag>

registerPreprocessor(type, id, preprocessorFn)

@returns Object containing all the preprocessors registered

  • type: either one of template css or javascript
  • id: unique preprocessor identifier
  • preprocessorFn: function receiving the code as first argument and the current options as second

registerPostprocessor(postprocessorFn)

@returns Set containing all the postprocessors registered

  • postprocessorFn: function receiving the compiler output as first argument and the current options as second

generateTemplateFunctionFromString(string, parserOptions)

@returns string with the code to execute the @riotjs/bindings template function

generateSlotsFromString(string, parserOptions)

@returns string with the code to generate components slots in runtime