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

@ungap/esx

v0.3.2

Published

[![build status](https://github.com/ungap/esx/actions/workflows/node.js.yml/badge.svg)](https://github.com/ungap/esx/actions) [![Coverage Status](https://coveralls.io/repos/github/ungap/esx/badge.svg?branch=main)](https://coveralls.io/github/ungap/esx?bra

Downloads

698

Readme

ESX as Template Literal Tag

build status Coverage Status CSP friendly

EXPERIMENTAL

This project is based on TC39 discussions and it tokenizes JSX like written template literals through a unified ESXToken class.

Features / API

Differently from JSX, or even ESX Babel transformer, components can be passed to the ESX factory utility, producing the expected resulting tokens.

import {ESX, Token} from '@ungap/esx';

// the factory function accepts an object
// that will be used to tokenize components
const esx = ESX({MyComponent});

esx`
  <div data-any="value">
    <MyComponent a="1" b=${2} />
  </div>
`;

function MyComponent(props, ...children) {
  // note: props doesn't need ${...props} (invalid JS)
  //       or ...${props} (ambiguous and unnecesary template chunks)
  //       because any value can be passed as interpolation
  return esx`
    <div ${props}>
      ${children}
    </div>
  `;
}

The main advangate of this approach is that ESX can survive both minification and serialization, beside allowing also lower-case components, namespaces, and so on, as long as the factory function receives an object with those keys:

import {ESX, Token} from '@ungap/esx';

const env = {['some:comp']: someComp};
const esx = ESX(env);

esx`
  <div data-any="value">
    <some:comp a="1" b=${2} />
  </div>
`;

function someComp(props, ...children) {}

Differently from other solutions based either on DOM or callbacks, ESX is simply an intermediate representation of the template literal content.

Thanks to its simple and unified Token shape, it can be serialized as compact, yet readable, JSON, surviving cross realm or environment boundaries.

import {ESX, Token} from '@ungap/esx';
import {stringify, parse} from '@ungap/esx/json';

const environment = {A, B};
const esx = ESX(environment);

const program = esx`
  <A data-any="value">
    <B a="1" b=${2} />
  </A>
`;

// JSON.stringify(program, ...rest)
const str = stringify(program, ...rest);

// JSON.parse(str, ...rest)
const revived = parse(str, environment, ...rest);

function A() {}
function B() {}

| field | type | description | | :----------- |:----------------------------------------:|:-------------------------------------------------------------------------------------------------------------------------------------------:| | type | number | any of the Token types: Token.ATTRIBUTE, Token.COMPONENT, Token.ELEMENT, Token.FRAGMENT, Token.INTERPOLATION or Token.STATIC. | | attributes | (attribute | interpolation)[]? | meaningful only for components or elements, it's an array of attributes and/or interpolations tokens. | | children | (attribute | interpolation | token)[]? | meaningful only for components, elements or fragments, here referred as token, it's an array of static, interpolation, or token children. | | dynamic | boolean | when true it means that future updates will change the token value with latest passed interpolation. interpolation and, optionally, attribute are the only one with dynamic equal true. | | name | string | the attribute, component or element name, otherwise the string #fragment, #interpolation or #static. | | value | unknown? | for an attribute, an interpolation, or a static token, it's the value it represents. It's always a string for non-dynamic attributes and static tokens, it could be anything else in other cases. element and fragment don't carry any value while component points at whatever reference was passed to the factory ESX(...) function to create the tag. |

  • transformer - the tokenizer takes less than 1ms to transform even complex templates, closing it to zero time to update the resulting structure
  • memory - each unique template generates one and one only token, updating interpolations and values only when the same template is executed again. This procedure grants no extra memory or Garbage Collector pressure when the same template is reused multiple times, also weakly relating the template with its own tokens to keep the heap under control
  • serialization - complex structures can still be serialized in less than 1ms and revived in even less than that
  • size - once minified and compressed, the es.js file is around 990bytes while the json.js helper is around 590bytes
  • self-closing - because ESX is fully inspired by JSX, and because it doesn't strictly represent neither XML nor HTML but it's closer to the former, <self-closing /> tags are always allowed for any kind of node
  • short-closing - still inspired by JSX, any element can use the fragment shortcut to close itself. <any-element>...</> is perfectly allowed
  • any interpolation - either attributes or children can contain Token.INTERPOLATION entries, without expecting a spread operation, or even an object. <el ${anyValue} />, as example,is a perfectly valid tokenized tree
  • [x] decide how to deal with types for TypeScript users / improve JSDoc TS at least around exported utilities.
  • [x] align the Babel transformer to provide the same uniqueness around tokens, so that each token is created once and only updates happen on demand.
  • [ ] a VSCode compatible syntax highlighter to see ESX as Template Literal the same as ESX or JSX, with the invetibale ${} interpolation difference, yet exactly the same contraints and highlights JSX has.
  • [x] a library to showcase ESX, either upgrading udomsay to use this instead of the Babel transformer, or creating a variant of that library based on this project. Update latest udomsay is based on ESX 🥳

About ESX

The idea behind ESX is to have a general purpose representation of a tree through well defined tokens that can be understood, and consumed, by any library or framework.

  • provide a way to understand interpolations and components through an always same, well defined, crawable struct
  • components can be global or scoped, as long as the factory created tag knows their name and can provide their values: const tag = ESX({MyComp, Array, any: specialCase})
  • interpolations can be part of the attributes list or the children
  • static parts within children are also well defined among other types: elements, fragments, components, interpolations
  • tokens are unique and (virtually) immutable, but their interpolation values get synchronously updated on repeated calls of the same template
  • a way to handle HTML specifications or create DOM related only solutions. The DOM and any HTML specs is fully ignored, including the names any element can have, or its attributes. @click, ?disabled, .setter, these are all valid attributes represented with their name, including literally any special char that is not a space
  • a way to handle XML specifications or create XML related only solution. Similarly with the previous point, ESX is general purpose, and simple, by design