Git Product home page Git Product logo

babel-plugin-yo-yoify's Introduction

DEPRECATED

This plugin was merged with nanohtml. Please use that instead!

// .babelrc
{
  "plugins": ["nanohtml"]
}

babel-plugin-yo-yoify

Like yo-yoify, but as a Babel plugin. Transform yo-yo or bel template strings into raw document calls.

Installation

npm install --save-dev babel-plugin-yo-yoify
# And:
npm install --save yo-yoify

yo-yoify is used in the compiled output of babel-plugin-yo-yoify, so it must be installed next to it.

Example

Using babel --plugins yo-yoify | prettier --stdin:

In

import html from 'bel'

const title = 'yo-yoify'
const header = html`
  <header id="page-header">
    <h1>${title}</h1>
    <button onclick=${event => alert('Hello world!')}>Click here</button>
  </header>
`

Out

var _h, _button, _pageHeader;

var _appendChild = require('yo-yoify/lib/appendChild');

const title = 'yo-yoify';
const header = (_pageHeader = document.createElement(
  'header'
), _pageHeader.setAttribute('id', 'page-header'), _appendChild(_pageHeader, [
  '\n    ',
  (_h = document.createElement('h1'), _appendChild(_h, [title]), _h),
  '\n    ',
  (_button = document.createElement('button'), _button.onclick = event =>
    alert('Hello world!'), _button.textContent = 'Click here', _button),
  '\n  '
]), _pageHeader);

Usage

Without options:

// .babelrc
{
  "plugins": [
    "yo-yoify"
  ]
}

With options:

// .babelrc
{
  "plugins": [
    ["yo-yoify", {
      "useImport": true
    }]
  ]
}

bel v5

When used with bel v5.1.3 or up, it's recommended to tell babel-plugin-yo-yoify to use bel's exported appendChild function. This way, the transformed output will always use the same appending and white space handling logic as the original source.

{
  "plugins": [
    ["yo-yoify", {
      "appendChildModule": "bel/appendChild"
    }]
  ]
}

bel versions v5.1.2 and below do not export the appendChild function--for those, the default "yo-yoify/lib/appendChild" function is used instead. This function may have different behaviour from the bel version being used, though.

Options

  • useImport - Set to true to use import statements for injected modules. By default, require is used. Enable this if you're using Rollup.
  • appendChildModule - Import path to a module that contains an appendChild function. Set this to "bel/appendChild" when using bel v5.1.3 or up! Defaults to "yo-yoify/lib/appendChild".

License

ISC

babel-plugin-yo-yoify's People

Contributors

arturi avatar goto-bus-stop avatar plesiecki avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

arturi

babel-plugin-yo-yoify's Issues

Dynamic attribute names

Right now dynamic attribute names don't work with this transform.

For example:

const handler = isTouchDevice ? 'ontouchstart' : 'onmousedown'

return html`
  <div ${handler}=${() => console.log('hello')}></div>
`

TypeError: Function.caller used to retrieve strict caller when using onload

Thanks for your work, love this transform! Exactly what I needed for http://github.com/transloadit/uppy. I recently created an issue asking if using yo-yoify as a Babel transform was possible choojs/nanohtml#65, then made yo-yoify-standalone, which just wraps original yo-yoify and pipes the parsed version out. And then stumbled upon your babel-plugin-yo-yoify :)

The issue mentioned here, which is resolved by original yo-yoify browserify transform, however, remains: I get TypeError: Function.caller used to retrieve strict caller (in Safari) when running a bundle where onload has been used on an element.

I’ve noticed you are parsing onload attributes somehow, but can’t figure out what how exactly yo-yoify “transforms any Function.caller into plain strings”.

screen shot 2017-02-26 at 10 45 26 pm

Is it possible to remove peerDependencies?

Just tried to use the module and spent some time to figure out why stuff is not working.

Would be nice to have another way - maybe just add them as "dependencies" so they get auto-installed, because I need them anyway:

  • either because i'm using e.g. yo-yo directly in my current module and run babel(ify
  • or because browserify is executing the listed transforms (e.g. babelify with this plugin specified) on a dependency

Or maybe I should just save them as "dependencies" on my current project?

Boolean attributes

It does not seem to handle boolean attributes how I would expect.

example:

bel`<input checked="${true}" />`

results in:

<input checked="true" />

Issues with svg

When it comes to processing SVGs yo-yoify the output is empty in the browser.

Svg tag is there physically but its being done incorrectly (I presume so) as it results in blank boxes.

Remove unnecessary whitespace

Right now

bel`
  <div>
    Contents
  </div>
`

generates something like

_div = document.createElement('div'), _div.textContent = '\n    Contents\n  '

But since it's semantically the same as

bel`<div>Contents</div>`

It could generate

_div = document.createElement('div'), _div.textContent = 'Contents'

instead.

only add iife if necessary

document.body.appendChild(bel`
  <h1>abc</h1>
`)

is currently:

document.body.appendChild(function () {
  var _h1 = document.createElement('h1')
  _h1.textContent = 'abc'
  return _h1
}())

but could be:

var _h1 = document.createElement('h1')
_h1.textContent = 'abc'
document.body.appendChild(_h1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.