Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/tomerigal/babel-plugin-transform-redom-jsx

Babel plugin for RE:DOM jsx
https://github.com/tomerigal/babel-plugin-transform-redom-jsx

Last synced: 3 months ago
JSON representation

Babel plugin for RE:DOM jsx

Awesome Lists containing this project

README

        

# babel-plugin-transform-redom-jsx

[![Build Status](https://github.com/tomerigal/babel-plugin-transform-redom-jsx/workflows/tests/badge.svg)](https://github.com/tomerigal/babel-plugin-transform-redom-jsx/actions)
[![npm version](https://badge.fury.io/js/babel-plugin-transform-redom-jsx.svg)](https://badge.fury.io/js/babel-plugin-transform-redom-jsx)

## Installation

```bash
npm install babel-plugin-transform-redom-jsx --save-dev
```

## Usage

### Babel config:

```json
{
"plugins": [
"babel-plugin-transform-redom-jsx",
[
"transform-react-jsx",
{
"pragma": "el"
}
]
]
}
```

### Component

The plugin transpiles the following JSX:

```jsx
import { el, text, mount } from "redom";
class A {
constructor(attr, text) {


{attr.title}


Hello {text}
;
}
update() {
this.span.textContent = "Hi";
}
}

class B {
constructor() {



World

;
}
update() {
this.span.textContent = "You";
this.a.update();
}
}

const b = ;

mount(document.body, b);

b.update();
```

To the following JavaScript:

```js
import { el, text, mount } from "redom";
class A {
constructor(attr, text) {
this["el"] = el(
"div",
null,
el("h3", null, attr.title),
(this["span"] = el("span", null, "Hello")),
" ",
text
);
}
update() {
this.span.textContent = "Hi";
}
}

class B {
constructor() {
this["el"] = el(
"div",
null,
(this["a"] = new A(
{ title: "Hello World example" },
(this["span"] = el("span", null, "World"))
))
);
}
update() {
this.span.textContent = "You";
this.a.update();
}
}

const b = new B({});

mount(document.body, b);

b.update();
```

### Assign to this

The plugin transpiles the following JSX:

```jsx
import { el, mount } from "redom";

// define Login component
class Login {
constructor() {
{
e.preventDefault();
const email = this.email.value;
const pass = this.pass.value;
console.log(email, pass);
}}
>


Sign in
;
}
}

// mount to DOM
mount(document.body, );
```

To the following JavaScript:

```js
import { el, mount } from "redom";

// define Login component
class Login {
constructor() {
this["el"] = el(
"form",
{
id: "login",
onsubmit: (e) => {
e.preventDefault();
const email = this.email.value;
const pass = this.pass.value;
console.log(email, pass);
},
},
(this["email"] = el("input", { type: "email", class: "email" })),
(this["pass"] = el("input", { type: "password", class: "pass" })),
el("button", { type: "submit" }, "Sign in")
);
}
}

// mount to DOM
mount(document.body, new Login());
```