npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

trucks-transform-skate

v1.0.11

Published

Compiler for skatejs components

Downloads

25

Readme

Skate Transform

Compiles web component templates to render functions

Install

npm i trucks-transform-skate --save-dev

For the command line interface see trucks-cli.



Usage

Programmatic usage:

const trucks = require('trucks');

trucks(
  {
    files: ['example/components.html'],
    transforms: ['skate']
  }, (err, res) => {
    if(err) {
      throw err; 
    }
    console.log(res);
  }
);

For command line usage see trucks-cli.

Overview

The library takes an HTML template and compiles it to a render function.

An HTML template such as:

<template id="x-blog-post">
  <div class="post">
    <script>
      if(this.title) {
        html('<h3>${this.title}</h3>'); 
      }
    </script>
    <p>Article content</p>
  </div>
</template>

Will result in the compiled function:

function render(elem) {
  skate.vdom.element("div", {
    "class": "post"
  }, () => {
    var _this = this;

    if (this.title) {
      skate.vdom.element("h3", () => {
        skate.vdom.text(`${ _this.title }`);
      });
    }

    skate.vdom.element("p", () => {
      skate.vdom.text("Article content");
    });
  });
}

Note that whitespace in the source template is normalized by default.

The compiler then creates a map of component identifiers to template render functions:

const templates = {
  "x-blog-post": function render(elem) {
    skate.vdom.element("div", {
      "class": "post"
    }, () => {
      var _this = this;

      if (this.title) {
        skate.vdom.element("h3", () => {
          skate.vdom.text(`${ _this.title }`);
        });
      }

      skate.vdom.element("p", () => {
        skate.vdom.text("Article content");
      });
    });
  }
};

And exposes a main function that performs a lookup in the template map by element tag name:

function template(elem) {
  return templates[elem.tagName.toLowerCase()].call(elem, elem);
}

Component authors may now proxy the render function to the template function, for example:

skate.define('x-blog-post', {
  render: template
});

API

skate

public skate(state, conf)

Compiles HTML <template> elements to render functions.

  • state Object compiler state.
  • conf Object plugin configuration object.

options

options(opts)

Get computed compiler options.

Merges input compiler options with the default option configuration.

Returns computed processing options.

  • opts Object processing options.

Options

  • attr String=id attribute name used for the component id.
  • skate String=skate name of the skatejs variable.
  • idom String=vdom name of the vdom property.
  • element String=element name of the element function.
  • text String=text name of the text function.
  • templates String=templates name of the templates map.
  • main String=template name of the main function.
  • scripts Boolean=true parse template script elements.
  • html String=html name of the html function for inline scripts.
  • normalize Boolean=true normalize whitespace in templates.
  • literals Object|Boolean=true flags for template literal support.
  • dom Object options to use when parsing the DOM.

html

html(html, opts)

Compile an HTML string to AST programs representing each <template> element in the input HTML.

Template literal support is enabled by default. You can pass the literals option as false to disable template literals for attributes and text nodes or an object that configures the text and attribute flags.

The following examples are equivalent:

html(tpl, {literals: false});
html(tpl, {literals: {text: false, attribute: false});

Returns a list of compiled templates.

  • html String an HTML string.
  • opts Object processing options.

Throws

  • Error if a template element does not define an identifier.

main

public main(opts)

Build a main function that accepts an elem argument and performs a lookup in the templates map to execute the template function.

Returns program representing the main function.

  • opts Object processing options.

map

public map(templates, opts)

Converts the output of a compile pass to an object map of component identifiers to render functions.

Returns AST program mapping components to render functions.

  • templates Array list of compiled template programs.
  • opts Object processing options.

render

public render(el, opts)

Convert a single DOM <template> element to an AST program representing the contents for a render function body.

Returns function body AST.

  • el Object the element DOM.
  • opts Object processing options.

License

MIT


Created by mkdoc on July 27, 2016