Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-template-html-minifier

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-template-html-minifier

Minify HTML in tagged template strings using html-minifier

  • 3.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.8K
increased by26.21%
Maintainers
1
Weekly downloads
 
Created
Source

babel-plugin-template-html-minifier

Travis CI Greenkeeper badge NPM Version NPM Downloads MIT

Minify HTML in tagged template strings using html-minifier.

Install

npm install --save-dev babel-plugin-template-html-minifier

Usage

In .babelrc:

{
  "plugins": [
    ["template-html-minifier", {
      "modules": {
        "lit-html": ["html"],
        "lit-element": [
          "html",
          {"name": "css", "encapsulation": "style"}
        ],
        "choo/html": [null],
        "hyperhtml": [{"name": "bind", "type": "factory"}],
        "hyperhtml-element": [{"name": null, "member": "html"}]
      },
      "htmlMinifier": {
        "collapseWhitespace": true
      }
    }]
  ]
}

Options

htmlMinifier

The value of this property is passed unmodified to html-minifier. See the html-minifier docs.

Note for usage with lit-html and lit-element:

  • To preserve case sensitiveness of property binding "caseSensitive": true must be added.

  • collapseBooleanAttributes should not be used when working with lit-html or other templating systems which give special meaning to non-static boolean attributes. Enabling collapseBooleanAttributes will cause this plugin to throw an exception:

    html`<input readonly="${readonly}">`;
    

    This exception is for two reasons. First because it means the chosen options have caused html-minifier to change the meaning of the HTML template. Second because it deletes the point where ${readonly} goes into the final output.

  • removeComments will cause the following template to throw an exception:

    html`<!-- <input value="${value}"> -->`;
    

    This exception is because ${value} inside an HTML template gets deleted. It should be noted that an HTML template does not prevent code within ${} from running. This means that in the following template getValue() is still executed when processing the html template:

    html`<!-- <input value="${getValue()}"> -->`;
    

    It is recommended to use binding-positions from eslint-plugin-lit to catch this error. This babel transformation can only determine that a template is broken, the eslint plugin will tell you which binding is invalid.

modules

A list of module names or import paths where tags are imported from. The values in the arrays refers to the export names, not the import names. null refers to the default export.

import choo from 'choo/html';
import * as lit from 'lit-html';
import {html as litHtml, css} from 'lit-element';
import HyperHTMLElement from 'hyperhtml-element';
import html from 'some-module';
import {bind} from 'hyperhtml';

choo`
  <div class="hello">
    Hello World
  </div>
`;

lit.html`
  <div class="hello">
    Hello World
  </div>
`;

litHtml`
  <div class="hello">
    Hello World
  </div>
`;

css`
  .sel {
    background: red;
  }
`;

class MyHyperHTMLElement extends HyperHTMLElement {
  created() {
    this.render();
  }

  render() {
    this.html`
      <div>
        Hello World
      </div>
    `;
  }
}

bind(document.body)`
  <div>
    Hello World
  </div>
`;

html`
  This
  is
  not
  processed
`;

Using the .babelrc shown in usage produces the following output:

import choo from 'choo/html';
import * as lit from 'lit-html';
import {html as litHtml, css} from 'lit-element';
import HyperHTMLElement from 'hyperhtml-element';
import html from 'some-module';
import {bind} from 'hyperhtml';

choo`<div class="hello"> Hello World </div>`;

lit.html`<div class="hello"> Hello World </div>`;

litHtml`<div class="hello"> Hello World </div>`;

css`.sel{background:red}`;

class MyHyperHTMLElement extends HyperHTMLElement {
  created() {
    this.render();
  }

  render() {
    this.html`<div> Hello World </div>`;
  }
}

bind(document.body)`<div> Hello World </div>`;

html`
  This
  is
  not
  processed
`;
  • choo is processed because of "choo/html": [null] specifies that the default export should be processed.
  • lit.html is processed because "lit-html": ["html"].
  • litHtml is processed because "lit-element": ["html"].
  • css is processed because "lit-element": [{"name": "css", "encapsulation": "style"}]. The encapsulation argument ensures that html-minifier understands that the template contains CSS, without it the template would be processed as HTML.
  • this.html in MyHyperHTMLElement is processed because "hyperhtml-element": [{"name": null, "member": "html"}] specifies that the html member of classes which extend the default export should be processed.
  • bind is processed because of "hyperhtml": [{"name": "bind", "type": "factory"}], the type factory specifies the bind returns a function which processes the tagged templates.
  • html is not processed because it was exported from an unlisted module.

All matching is done based on the exported name, not the local/imported name.

Running tests

Tests are provided by xo and ava.

npm install
npm test

Attribution

This module was originally created by goto-bus-stop.

Keywords

FAQs

Package last updated on 07 Jun 2019

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc