Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Last active October 6, 2024 12:35
Show Gist options
  • 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.

@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.

@shellscape
Copy link

dear lord yes. I've been futilely trying to develop TypeScript types for cross-framework JSX compat and this proposal is exactly what we need.

@shellscape
Copy link

@jogibear9988 that invite link is invalid

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