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

jsx2ttl

v0.2.3

Published

Covert JSX to tagged template literal

Downloads

11

Readme

jsx2ttl

Warning: Very experimental / proof of concept

Uses Babel to parse JSX into an AST, then converts the AST into a tagged template literal-based Template object.

I have a LiveView-based project that requires the use of a tagged template literal-based Template object. For "technical reasons" specific to the LiveView protocol, traditional JSX components (and JSX runtimes) are not compatible. This project is an attempt to create a JSX-to-TTL transpiler that can be used to convert JSX into a TTL-based Template object that is compatible with LiveView-based projects.

That said, you could theoretically use this project to convert JSX into a TTL-based Template object for use in any project that requires a TTL-based functions.

This is a Bun project but the code that does the heavy lifting is not bun-specific and could be used in any project that requires a JSX-to-TTL transpiler. See src/index.ts for the main logic.

NPM Package

This project is published as an npm package: jsx2ttl

bun add jsx2ttl

or your favorite package manager:

npm install jsx2ttl

Example Conversions

Pretty simple, JSX with no props:

Input JSX:

// source: https://react.dev/learn/your-first-component
export default function Profile() {
  return (
    <img
      src="https://i.imgur.com/MK3eW3Am.jpg"
      alt="Katherine Johnson"
    />
  )
}

Output TTL-based Template:

import { Template } from "../ttl";

export default function Profile() {
  return new Template(["<img src=\"https://i.imgur.com/MK3eW3Am.jpg\" alt=\"Katherine Johnson\">"], []);
}

More complex, JSX with props and children:

Input JSX:

function Foo1(props: {msg: number}) {
  return <div>hi {props.msg}</div>;
}

function App(props: {name: string}) {
  return <h1 className="foo">Hello {props.name}. <Foo1 msg={1+3} /></h1>;
}

export default App;

Output TTL-based Template:

import { Template } from "../ttl";
function Foo1(props: {
  msg: number;
}) {
  return new Template(["<div>hi ", "</div>"], [props.msg]);
}
function App(props: {
  name: string;
}) {
  return new Template(["<h1 className=\"foo\">Hello ", ". ", "</h1>"], [props.name, Foo1({
    msg: 1 + 3
  })]);
}
export default App;

Configuration

Configuring the plugin is done by passing in settings when adding the plugin to the bun runtime. For instance you can use bunfig.toml to preload the plugin with settings:

bunfig.toml:

preload = ["./src/plugin/register_jsx2ttl.ts"]

[test]
preload = ["./src/plugin/register_jsx2ttl.ts"]

Then in ./src/plugin/register_jsx2ttl.ts:

import { plugin } from "bun";
import { jsx2ttlPlugin, type JSX2TTLOptions } from "..";

// could read from bunfig.toml or other config file
const options: JSX2TTLOptions = {
  importName: "myttl",  
  importPath: "../ttl", // or "myttl" if a package
  // other options...
}

// load the plugin via bunfig.toml preload
plugin(jsx2ttlPlugin(options));

Next Steps

  • [ ] Test / add support for more JSX features
  • [x] ~~Handle style and className props properly when converting to TTL~~
  • [ ] Add support for Fragment and <> syntax
  • [x] ~~Support other TTL-based functions or objects (not just new Template)~~
  • [x] ~~Publish as a standalone package on npm~~

Install Dependencies

bun install

Running

There are a couple of scripts you can run to see the project in action.

  • bun plugin - Preloads the jsx2ttlPlugin into the bun runtime, which parses the TSX file before it is imported and converts it into a TTL-based Template object.
  • bun direct - Outputs the transpiled TTL-based code into /out directory for inspection.
  • bun test - Runs src/tests/plugin_test.ts which builds and snapshots the transpiled TTL-based code. These

License

MIT

Author

Donnie Flood