Code Monkey home page Code Monkey logo

compiler's Introduction

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 buble from 'buble'

// 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 buble.transform returns {code, map}
  return buble.transform(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)

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

compiler's People

Contributors

gianlucaguarini avatar amarcruz avatar kuashe avatar cognitom avatar hudelgado avatar kachurun avatar rodrigorodriguez avatar dependabot[bot] avatar appedemic avatar georges-gomes avatar r4j4h avatar imagentleman avatar klen avatar rwky avatar synvox avatar jrx-jsj avatar nippur72 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.