Socket
Book a DemoInstallSign in
Socket

jsx-vanilla

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsx-vanilla

JSX is a JavaScript XML/HTML syntax extender, that allows you to render XML/HTML tags inside vanilla JavaScript.

3.1.3
latest
Source
npmnpm
Version published
Weekly downloads
32
Maintainers
1
Weekly downloads
 
Created
Source

What is JSX?

(taken from www.reactenlightenment.com)

JSX is an XML/HTML-like syntax used by React that extends ECMAScript so that XML/HTML-like text can co-exist with JavaScript/React code. The syntax is intended to be used by preprocessors (i.e., transpilers like Babel) to transform HTML-like text found in JavaScript files into standard JavaScript objects that a JavaScript engine will parse.

Basically, by using JSX you can write concise HTML/XML-like structures (e.g., DOM like tree structures) in the same file as you write JavaScript code, then Babel will transform these expressions into actual JavaScript code. Unlike the past, instead of putting JavaScript into HTML, JSX allows us to put HTML into JavaScript.

What is JSX Vanilla?

npm version JSX Vanilla is a JavaScript preprocessor that allows you to use JSX syntax in vanilla JavaScript, separately from React, by transforming JSX blocks into string literals.

Common JSX block:

const menu = (
  <ul>
    <li><a href="#">First</></li>
    <li><a href="#">Second</></li>
    <li><a href="#">Third</></li>
  </ul>
);

After the JSX Vanilla preprocess:

const menu = (
  `<ul>
    <li><a href="#">First</></li>
    <li><a href="#">Second</></li>
    <li><a href="#">Third</></li>
  </ul>`
);

If desired, received strings could be transformed into DOM elements:

const menu = (
  `<ul>
    <li><a href="#">First</></li>
    <li><a href="#">Second</></li>
    <li><a href="#">Third</></li>
  </ul>`
);

const node = document.createRange().createContextualFragment(menu);

Syntax examples

JSX Vanilla syntax is almost the same as native one, excepts some specific cases.

Simple variables declaration

const text = <p>Hello world!</p>;
const menu = (
  <ul>
    <li><a href="#">First</></li>
    <li><a href="#">Second</></li>
    <li><a href="#">Third</></li>
  </ul>
);

Passing parameters

const SIX = 6;

const text = 'Hello world!';

const appendHeart = text => `${text} <3`;

const a = <h1>{text}</h1>;
const b = <h2>{(2+2)}</h2>;
const c = <h3>{appendHeart('I love JSX!')}</h3>;
const d = <p>{(SIX > 5 ? 'True' : 'False')}</p>;
const e = (
  <p>{(() => text)()}</p>
);

Cycle example

const array = ['First', 'Second', 'Third'];

const menu = (
  <ul class="menu">
    {array.map(item => (
      <li>{item}</li>
    )).join('')}
  </ul>
);

Rendering elements

const { nodeFromString } = require('jsx-vanilla');

const a = <h1>Hello world!</h1>;

document.body.appendChild(nodeFromString(a));
document.body.appendChild(nodeFromString(<h2>Hello world!</h2>));

Example of file preprocessing in Node.js

const { preprocess } = require('jsx-vanilla');
const fs = require('fs');

fs.readFile('TARGET_FILE_PATH', 'utf8', (error, content) => {
  if (error) return;

  try {
    const output = preprocess(content);

    fs.writeFile('OUTPUT_FILE_PATH', output, error => {
      if (error) return;

      // other code
    });
  } catch (error) {
    console.log(error);
  }
});

If you are using webpack you can install custom jsx-vanilla-loader

Keywords

html

FAQs

Package last updated on 02 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.