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

babel-plugin-transform-dom

v0.5.0

Published

A babel transformer to provide an elegant interface for writing dom code in JS.

Downloads

79

Readme

babel-plugin-transform-dom

babel-plugin-transform-dom provides a dom interface while writing code that will transform to document.createElement and friends when you process through Babel.

Documentation

The method you invoke on dom will be your HTML tag, so dom.span becomes <span> and dom.fake will become <fake>.

The only exception to this dom.text which will reaturen a text node.

Arguments

The order of the arguments you pass to dom.tag doesn’t matter. Here’s a list of acceptable arguments:

  • Class name, as indicated by a string started with a period
  • ID, as indicated by a string started with a number sign
  • Text content for the tag, any string that does not start with a period or number sign
  • Child elements to add to your tag. These can be passed as top level args or in as an array of args
  • Tag attributes via the optiosns argument: { attributes: { text: "hello" } }

You can also nest dom.tag calls to create complex trees.

Alternative Invocation

Rather than calling dom.div, you can also call dom('div'). When you invoke with the tag as an argument, everything above is true except the first argument must always be the tag name. After the first argument the order of arguments do not matter.

Example

Here’s an example of creating a simple span element with a class and some text:

In

const hello = dom.div('.hello', 'Hello there!')

Out

var _div;

const hello = (_div = document.createElement('div'), _div.classList.add('hello'), _div.appendChild(document.createTextNode('Hello there!')), _div);

Here’s a more complex example:

In


const body = dom.body(
  '.page',
  dom.h1('Title', '#header'),
  dom.div(
    '.content',
    dom.p('Paragraph 1'),
    dom.p('Paragraph 2', '.small')
  ),
  dom.aside('#sidebar', {
    attributes: {
      'data-affixed': '.content'
    }
  }, dom.span('.sidebar-content .sticky', dom.p('Sidebar')))
)

Out

var _h, _div, _p, _p2, _aside, _span, _p3;

var _body = document.createElement('body');

_body.classList.add('page');

_body.appendChild((_h = document.createElement('h1'), _h.appendChild(document.createTextNode('Title')), _h.setAttribute('id', 'header'), _h));

_body.appendChild((_div = document.createElement('div'), _div.classList.add('content'), _div.appendChild((_p = document.createElement('p'), _p.appendChild(document.createTextNode('Paragraph 1')), _p)), _div.appendChild((_p2 = document.createElement('p'), _p2.appendChild(document.createTextNode('Paragraph 2')), _p2.classList.add('small'), _p2)), _div));

_body.appendChild((_aside = document.createElement('aside'), _aside.setAttribute('id', 'sidebar'), _aside.setAttribute('data-affixed', '.content'), _aside.appendChild((_span = document.createElement('span'), _span.classList.add('sidebar-content', 'sticky'), _span.appendChild((_p3 = document.createElement('p'), _p3.appendChild(document.createTextNode('Sidebar')), _p3)), _span)), _aside));

const body = _body;

JSX

You can also use babel-plugin-transform-dom to compile JSX! To enable control over JSX, just use dom.jsx as the JSX pragma and make sure to add babel-plugin-transform-dom after jsx in your plugins list:

.babelrc

{
  "plugins": [
    [ "transform-react-jsx", { "pragma": "dom.jsx" } ],
    "transform-dom"
  ]
}

Example

In

var profile = <div>
  <img src="avatar.png" class="profile" />
  <h3>{[user.firstName, user.lastName].join(' ')}</h3>
</div>;

Out

var _img, _h, _div;

var profile = (_div = document.createElement("div"), _div.appendChild((_img = document.createElement("img"), _img.setAttribute("src", "avatar.png"), _img.setAttribute("class", "profile"), _img)), _div.appendChild((_h = document.createElement("h3"), _h.appendChild(document.createTextNode([user.firstName, user.lastName].join(' '))), _h)), _div);

License

babel-plugin-transform-dom was created by Shaun Harrison and is made available under the MIT license.