Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Last active May 6, 2024 08:49
Show Gist options
  • Star 36 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save WebReflection/2d64f34cf58daa812ec876242c91a97c to your computer and use it in GitHub Desktop.
Save WebReflection/2d64f34cf58daa812ec876242c91a97c to your computer and use it in GitHub Desktop.
Proposal: an ESX for JS implementation

About

Highly Experimental

If you are using this already, consider changes soon due the discussion around current ESX proposal.

Feel free to keep an eye on udomsay as that will be the implementation reference for consumers.


This is a TC39 proposal and there is a Babel transformer for it too.

There is also a zero-tooling solution based on template litearl tags in this repository, which solves almost all pain-points around having ESX as syntax.


In a quest to explore improvements over common JSX transformers I have managed to find great performance able to compete with template literal based libraries.

In this document I would like to describe, via JS itself, and as PoC, how JSX could be both rebranded as ESX and improved.

Goal

Differently from E4X, but also differently from JSX, this proposal leaves developers provide their own "render" implementation, freeing ESX usage from any previous attempt to confine JSX or E4X into the DOM world, where it's been proven, in the JSX case, it's not really where it belongs, as it can be used as neutral, general purpose, DSL.

This document describes all the moving parts of ESX in a way that:

  • there is no DOM at all involved, only new primitives introduced by ESX
  • all relevant details around each part of the transformation are described through simple objects, here represented as classes instances, but these easily work just as object literals (easy polyfills via transformers)
  • no extra scope pollution is needed, hence no jsxPragma or jsxFragment around is required at all (no React.createElement or React.Fragment needed, nor udomsay.interpolation)
  • all classes can be used just as types to infer, as oppsite of being really classes ... no clashing in the logic can happen neither. The only global class needed out there is ESXToken which carries types and the prototypal inheritance for brand check.
  • hints to "parse-once" through templates and/or Components are all over the place, making usignal like alternative implementations possible, but also any SSR related project can benefit from these

ESX

// base class for instanceof operations
class ESXToken {
  static ATTRIBUTE: number;
  static INTERPOLATION: number;
  static STATIC: number;
  static FRAGMENT: number;
  static ELEMENT: number;
  static COMPONENT: number;
}

// possible `attributes` entry
interface ESXAttribute {
  type = ESXToken.ATTRIBUTE;
  dynamic: boolean;
  name: string;
  value: unknown;
}

// possible `attributes` or `children` entry
interface ESXInterpolation {
  type = ESXToken.INTERPOLATION;
  value: unknown;
}

// possible `children` entry
interface ESXStatic {
  type = ESXToken.STATIC;
  value: string;
}

// always the same reference when this is used as outer template
interface ESXNode extends ESXToken {
  id: object?
  children: (ESXStatic | ESXInterpolation | ESXNode)[];
}

// <></>
class ESXFragment extends ESXNode {
  type = ESXToken.FRAGMENT;
}

// <any-element />
class ESXElement extends ESXNode {
  type = ESXToken.ELEMENT;
  name: string;
  value: string;
  attributes: (ESXAttribute | ESXInterpolation)[];
}

// <AnyComponent />
class ESXComponent extends ESXNode {
  type = ESXToken.COMPONENT;
  name: string;
  value: function;
  attributes: (ESXAttribute | ESXInterpolation)[];
  get properties(): object?;
}

Code samples

To better understand what the transformer currently produce, please see this innput and compare it with this output.

@jogibear9988
Copy link

jogibear9988 commented Apr 8, 2024

@WebReflection

you can join here : https://discord.gg/VRzgBSxM (if you'd like to)

the comment in the discord was:

"I don't think something really registers as a proposal until it's put into GitHub based on the TC39 proposal template, and at least briefly presented before the commitee. I think there would be a lot of feedback, and the champion would have to be open to it."

@o-t-w
Copy link

o-t-w commented Apr 8, 2024

Standards is always a long tedious process. I wouldn’t call a thread with over 100 comments “crickets”.

@WebReflection
Copy link
Author

@o-t-w I suggested already TC39 standards that got implemented in ECMAScript, the crickets was around the fact I've no idea what else I should do to move that forward and as I need a champion to show interest it's time and effort to write down any proposal knowing nobody cares or showed interest ... that's crickets to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment