jlto

1.4.2 • Public • Published

Support Ukraine 🇺🇦

#StandWithUkraine

JLTO

CircleCI Coverage Join the chat at https://gitter.im/dmytro-krekota_jlto/Lobby

NPM

Jinja Like Templates Optimizer (JLTO) is a Nodejs-based tool for optimizing Jinja like templates.

Gulp tool for JLTO:

gulp-jlto

Supported template engines:

Available options:

  • expressionStart - symbols at the beginning of expressions
  • expressionEnd - symbols at the end of expressions
  • blockStart - symbols at the beginning of blocks
  • blockEnd - symbols at the end of blocks
  • commentStart - symbols at the beginning of comments
  • commentEnd - symbols at the beginning of comments
  • specialChars - special chars in blocks and expressions
  • cleanupBlocks - flag for optimize blocks
  • cleanupExpressions - flag for optimize expressions
  • removeComments - flag for removing comments
  • minifyHtml - flag for minifying html code with html-minifier
  • minifyHtmlOptions - options for html-minifier

See default values for above options here.

Usage

Simple example:

let jlto = require('jlto');
let template = `
{{ hello }}
{{   "<John   &   Paul> ?"     | escape   }}
{{ '2.7'   | round }}{%  if  product  %}Product exists.{%  endif  %}
`;
let optimizedTemplate = jlto.optimizeString(template);
// optimizedTemplate:
// `
//{{hello}}
//{{"<John   &   Paul> ?"|escape}}
//{{'2.7'|round}}{%if product%}Product exists.{%endif%}
// `

Example of using minifyHtml option:

let jlto = require('jlto');
let template = `
<div {% if id %}id="{{ id | escape('html_attr') }}"{% endif %} class="section-container {{ classes | join(' ') | html_attribute }}">
  <div class="section-writables">
    {% for writable in writables  %}
      {{ writable | write | raw }}
    {% endfor %}
  </div>
</div>`;
let optimizedTemplate = jlto.optimizeString(template, {minifyHtml: true});
// optimizedTemplate:
// `<div {%if id%} id="{{id|escape('html_attr')}}" {%endif%} class="section-container {{classes|join(' ')|html_attribute}}"><div class="section-writables"> {%for writable in writables%} {{writable|write|raw}} {%endfor%} </div></div>`

Example of "nunjucks" templates minification with the custom GruntJS task:

module.exports = (grunt) => {
  grunt.registerTask('min-nunjucks', 'Min nunjucks templates', () => {
    let jlto = require('jlto');
    let fs = require('fs');
    let glob = require('glob');
    let done = this.async();
    glob('./**/*.nunjucks.html', (error, files) => {
      files.forEach((filePath) => {
        let fileContent;
        fileContent = fs.readFileSync(filePath).toString();
        try {
          fileContent = jlto.optimizeString(fileContent, {minifyHtml: true});
          fs.writeFileSync(filePath, fileContent);
        } catch (ignored) {}
      });
      return done();
    });
  });
};

Tests

Unit tests are written using Mocha and Chai. To run, invoke npm test.

License

JLTO is available under the MIT license, see the LICENSE file for more information.

Package Sidebar

Install

npm i jlto

Weekly Downloads

20

Version

1.4.2

License

MIT

Unpacked Size

14.6 kB

Total Files

11

Last publish

Collaborators

  • dmitry_krekota