Skip to content

dmytro-krekota/jlto

Repository files navigation

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-next
  • minifyHtmlOptions - options for html-minifier-next

See default values for above options here.

optimizeString always returns a Promise, so use await (or .then(...)) for all calls.

Usage

Simple example:

let jlto = require('jlto');
let template = `
{{ hello }}
{{   "<John   &   Paul> ?"     | escape   }}
{{ '2.7'   | round }}{%  if  product  %}Product exists.{%  endif  %}
`;
jlto.optimizeString(template).then((optimizedTemplate) => {
  // 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>`;
jlto.optimizeString(template, {minifyHtml: true}).then((optimizedTemplate) => {
  // 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', async (error, files) => {
      for (const filePath of files) {
        let fileContent;
        fileContent = fs.readFileSync(filePath).toString();
        try {
          fileContent = await 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.

Releases

No releases published

Packages

 
 
 

Contributors