htm icon indicating copy to clipboard operation
htm copied to clipboard

Optimize adjacent static parts+fields

Open developit opened this issue 6 years ago • 0 comments
trafficstars

I've been toying around with optimizing Tagged Templates by merging literal field values (strings, numbers and booleans) into their adjacent static part and collapsing the adjacent static parts into a single part:

// input:
<div class={"foo"}>hello</div>
<button id={1} />

// current output:
html`<div class=${"foo"}>hello</div>`
html `<button id=${1} />`

// optimized output:
html`<div class="foo">`
html`<button id=1 />`

It'd be really neat to get this behavior into babel-plugin-transform-jsx-to-htm, since additional assumptions could be made about nested Arrays:

const color = 'red';

// input:
<style jsx>{`
  .foo { color: ${color}; }
`}</style>

// current output:
html`
  <style jsx>${`
    .foo { color: ${color}; }
  `}</style>
`

// optimized output:
html`
  <style jsx>
    .foo { color: ${color}; }
  </style>
`

Thoughts?

developit avatar May 30 '19 21:05 developit