jlto 1.2.1 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.nvmrc ADDED
@@ -0,0 +1 @@
1
+ v16
package/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # JLTO
2
2
 
3
- [![Codeship](https://app.codeship.com/projects/c496b690-12e2-0137-3a15-526d29fa1101/status?branch=master)](https://app.codeship.com/projects/327499) [![Coverage](https://coveralls.io/repos/github/dmytro-krekota/jlto/badge.svg?branch=master)](https://coveralls.io/github/dmytro-krekota/jlto?branch=master) [![Dependencies](https://david-dm.org/dmytro-krekota/jlto.svg)](https://david-dm.org/dmytro-krekota/jlto) [![DevDependencies](https://david-dm.org/dmytro-krekota/jlto/dev-status.svg)](https://david-dm.org/dmytro-krekota/jlto?type=dev) [![Join the chat at https://gitter.im/dmytro-krekota_jlto/Lobby](https://badges.gitter.im/dmytro-krekota_jlto/Lobby.svg)](https://gitter.im/dmytro-krekota_jlto/community?source=orgpage)
3
+ [![CircleCI](https://circleci.com/gh/dmytro-krekota/jlto.svg?style=svg)](https://app.circleci.com/pipelines/github/dmytro-krekota/jlto)
4
+ [![Coverage](https://coveralls.io/repos/github/dmytro-krekota/jlto/badge.svg?branch=master)](https://coveralls.io/github/dmytro-krekota/jlto?branch=master) [![Join the chat at https://gitter.im/dmytro-krekota_jlto/Lobby](https://badges.gitter.im/dmytro-krekota_jlto/Lobby.svg)](https://gitter.im/dmytro-krekota_jlto/community?source=orgpage)
4
5
 
5
6
  [![NPM](https://nodei.co/npm/jlto.png?downloads=true)](https://nodei.co/npm/jlto/)
6
7
 
@@ -12,29 +13,29 @@
12
13
 
13
14
  **Supported template engines:**
14
15
 
15
- * [Nunjucks](https://mozilla.github.io/nunjucks/) (Tested with unit tests)
16
- * [Twig.js](https://github.com/twigjs/twig.js) (Tested with unit tests)
17
- * [LiquidNode](https://github.com/sirlantis/liquid-node) (Tested with unit tests)
18
- * [Twig](https://twig.sensiolabs.org/)
19
- * [Jinja](http://jinja.pocoo.org/)
20
- * [Django](https://docs.djangoproject.com/en/1.11/ref/templates/language/)
21
- * [Liquid](https://shopify.github.io/liquid/)
22
- * [Jinjava](https://github.com/HubSpot/jinjava)
16
+ - [Nunjucks](https://mozilla.github.io/nunjucks/) (Tested with unit tests)
17
+ - [Twig.js](https://github.com/twigjs/twig.js) (Tested with unit tests)
18
+ - [LiquidNode](https://github.com/sirlantis/liquid-node) (Tested with unit tests)
19
+ - [Twig](https://twig.sensiolabs.org/)
20
+ - [Jinja](http://jinja.pocoo.org/)
21
+ - [Django](https://docs.djangoproject.com/en/1.11/ref/templates/language/)
22
+ - [Liquid](https://shopify.github.io/liquid/)
23
+ - [Jinjava](https://github.com/HubSpot/jinjava)
23
24
 
24
25
  **Available options:**
25
26
 
26
- * expressionStart - symbols at the beginning of expressions
27
- * expressionEnd - symbols at the end of expressions
28
- * blockStart - symbols at the beginning of blocks
29
- * blockEnd - symbols at the end of blocks
30
- * commentStart - symbols at the beginning of comments
31
- * commentEnd - symbols at the beginning of comments
32
- * specialChars - special chars in blocks and expressions
33
- * cleanupBlocks - flag for optimize blocks
34
- * cleanupExpressions - flag for optimize expressions
35
- * removeComments - flag for removing comments
36
- * minifyHtml - flag for minifying html code with [html-minifier](https://www.npmjs.com/package/html-minifier)
37
- * minifyHtmlOptions - options for html-minifier
27
+ - expressionStart - symbols at the beginning of expressions
28
+ - expressionEnd - symbols at the end of expressions
29
+ - blockStart - symbols at the beginning of blocks
30
+ - blockEnd - symbols at the end of blocks
31
+ - commentStart - symbols at the beginning of comments
32
+ - commentEnd - symbols at the beginning of comments
33
+ - specialChars - special chars in blocks and expressions
34
+ - cleanupBlocks - flag for optimize blocks
35
+ - cleanupExpressions - flag for optimize expressions
36
+ - removeComments - flag for removing comments
37
+ - minifyHtml - flag for minifying html code with [html-minifier](https://www.npmjs.com/package/html-minifier)
38
+ - minifyHtmlOptions - options for html-minifier
38
39
 
39
40
  See default values for above options [here](https://github.com/dmytro-krekota/jlto/blob/master/lib/core/default.js).
40
41
 
@@ -43,13 +44,13 @@ See default values for above options [here](https://github.com/dmytro-krekota/jl
43
44
  **Simple example:**
44
45
 
45
46
  ```js
46
- let jlto = require("jlto");
47
+ let jlto = require('jlto')
47
48
  let template = `
48
49
  {{ hello }}
49
50
  {{ "<John & Paul> ?" | escape }}
50
51
  {{ '2.7' | round }}{% if product %}Product exists.{% endif %}
51
- `;
52
- let optimizedTemplate = jlto.optimizeString(template);
52
+ `
53
+ let optimizedTemplate = jlto.optimizeString(template)
53
54
  // optimizedTemplate:
54
55
  // `
55
56
  //{{hello}}
@@ -61,7 +62,7 @@ let optimizedTemplate = jlto.optimizeString(template);
61
62
  **Example of using minifyHtml option:**
62
63
 
63
64
  ```js
64
- let jlto = require("jlto");
65
+ let jlto = require('jlto')
65
66
  let template = `
66
67
  <div {% if id %}id="{{ id | escape('html_attr') }}"{% endif %} class="section-container {{ classes | join(' ') | html_attribute }}">
67
68
  <div class="section-writables">
@@ -69,8 +70,8 @@ let template = `
69
70
  {{ writable | write | raw }}
70
71
  {% endfor %}
71
72
  </div>
72
- </div>`;
73
- let optimizedTemplate = jlto.optimizeString(template, {minifyHtml: true});
73
+ </div>`
74
+ let optimizedTemplate = jlto.optimizeString(template, {minifyHtml: true})
74
75
  // optimizedTemplate:
75
76
  // `<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>`
76
77
  ```
@@ -80,24 +81,23 @@ let optimizedTemplate = jlto.optimizeString(template, {minifyHtml: true});
80
81
  ```js
81
82
  module.exports = (grunt) => {
82
83
  grunt.registerTask('min-nunjucks', 'Min nunjucks templates', () => {
83
- let jlto = require('jlto');
84
- let fs = require('fs');
85
- let glob = require('glob');
86
- let done = this.async();
84
+ let jlto = require('jlto')
85
+ let fs = require('fs')
86
+ let glob = require('glob')
87
+ let done = this.async()
87
88
  glob('./**/*.nunjucks.html', (error, files) => {
88
89
  files.forEach((filePath) => {
89
- let fileContent;
90
- fileContent = fs.readFileSync(filePath).toString();
90
+ let fileContent
91
+ fileContent = fs.readFileSync(filePath).toString()
91
92
  try {
92
- fileContent = jlto.optimizeString(fileContent, {minifyHtml: true});
93
- fs.writeFileSync(filePath, fileContent);
94
- } catch (ignored) {
95
- }
96
- });
97
- return done();
98
- });
99
- });
100
- };
93
+ fileContent = jlto.optimizeString(fileContent, {minifyHtml: true})
94
+ fs.writeFileSync(filePath, fileContent)
95
+ } catch (ignored) {}
96
+ })
97
+ return done()
98
+ })
99
+ })
100
+ }
101
101
  ```
102
102
 
103
103
  ## Tests
package/index.js CHANGED
@@ -1,3 +1,3 @@
1
- let jlto = require('./lib/jlto')
1
+ let jlto = require('./lib/jlto');
2
2
 
3
- module.exports = new jlto()
3
+ module.exports = new jlto();
@@ -20,10 +20,10 @@ class Default {
20
20
  collapseWhitespace: true,
21
21
  minifyJS: true,
22
22
  minifyCSS: true,
23
- ignoreCustomFragments: [/\{([{%])[^}]+[%}]\}/]
24
- }
25
- }
23
+ ignoreCustomFragments: [/\{([{%])[^}]+[%}]\}/],
24
+ },
25
+ };
26
26
  }
27
27
  }
28
28
 
29
- module.exports = Default
29
+ module.exports = Default;
package/lib/core/utils.js CHANGED
@@ -3,12 +3,12 @@
3
3
  */
4
4
  class Utils {
5
5
  static isNull(value) {
6
- return value === null
6
+ return value === null;
7
7
  }
8
8
 
9
9
  static isUndefined(value) {
10
- return value === undefined
10
+ return value === undefined;
11
11
  }
12
12
  }
13
13
 
14
- module.exports = Utils
14
+ module.exports = Utils;
@@ -3,72 +3,72 @@
3
3
  */
4
4
  class BlocksHelper {
5
5
  constructor(options) {
6
- this.options = options
7
- this.utils = require('./../core/utils')
6
+ this.options = options;
7
+ this.utils = require('./../core/utils');
8
8
  }
9
9
 
10
10
  getBlockWithoutExtraSpaces(expression) {
11
- let body = expression.slice(this.options.blockStart.length).slice(0, -this.options.blockEnd.length)
12
- let resultBody = ''
13
- let i
14
- let waitForChar
11
+ let body = expression.slice(this.options.blockStart.length).slice(0, -this.options.blockEnd.length);
12
+ let resultBody = '';
13
+ let i;
14
+ let waitForChar;
15
15
 
16
- body = body.trim()
16
+ body = body.trim();
17
17
  for (i = 0; i < body.length; i++) {
18
18
  if (waitForChar && body[i] !== waitForChar) {
19
- resultBody += body[i]
20
- continue
19
+ resultBody += body[i];
20
+ continue;
21
21
  }
22
- if (body[i] === '"' || body[i] === '\'') {
22
+ if (body[i] === '"' || body[i] === "'") {
23
23
  if (!waitForChar) {
24
- waitForChar = body[i]
24
+ waitForChar = body[i];
25
25
  } else {
26
- waitForChar = null
26
+ waitForChar = null;
27
27
  }
28
28
  }
29
29
  if (this.options.specialChars.indexOf(body[i]) >= 0) {
30
- resultBody = resultBody.trim()
30
+ resultBody = resultBody.trim();
31
31
  }
32
32
  if (this.options.specialChars.indexOf(resultBody[resultBody.length - 1]) >= 0 && body[i] === ' ') {
33
- continue
33
+ continue;
34
34
  }
35
35
  if (resultBody[resultBody.length - 1] === ' ' && body[i] === ' ') {
36
- continue
36
+ continue;
37
37
  }
38
38
  if (body[i] !== '\n') {
39
- resultBody += body[i]
39
+ resultBody += body[i];
40
40
  }
41
41
  }
42
42
 
43
- return this.options.blockStart + resultBody + this.options.blockEnd
43
+ return this.options.blockStart + resultBody + this.options.blockEnd;
44
44
  }
45
45
 
46
46
  clearExtraSpaces(template) {
47
- let i
48
- let startIndex = null
49
- let tempSubstring
50
- let resultString = ''
47
+ let i;
48
+ let startIndex = null;
49
+ let tempSubstring;
50
+ let resultString = '';
51
51
 
52
52
  for (i = 0; i < template.length + 1; i++) {
53
53
  if (!this.utils.isNull(startIndex)) {
54
- tempSubstring = template.substring(i - this.options.blockEnd.length, i)
54
+ tempSubstring = template.substring(i - this.options.blockEnd.length, i);
55
55
  if (tempSubstring === this.options.blockEnd) {
56
- resultString += this.getBlockWithoutExtraSpaces(template.substring(startIndex, i))
57
- startIndex = null
56
+ resultString += this.getBlockWithoutExtraSpaces(template.substring(startIndex, i));
57
+ startIndex = null;
58
58
  }
59
59
  }
60
60
  if (this.utils.isNull(startIndex)) {
61
- tempSubstring = template.substring(i, i + this.options.blockStart.length)
61
+ tempSubstring = template.substring(i, i + this.options.blockStart.length);
62
62
  if (tempSubstring === this.options.blockStart) {
63
- startIndex = i
63
+ startIndex = i;
64
64
  } else if (!this.utils.isUndefined(template[i])) {
65
- resultString += template[i]
65
+ resultString += template[i];
66
66
  }
67
67
  }
68
68
  }
69
69
 
70
- return resultString
70
+ return resultString;
71
71
  }
72
72
  }
73
73
 
74
- module.exports = BlocksHelper
74
+ module.exports = BlocksHelper;
@@ -3,35 +3,35 @@
3
3
  */
4
4
  class CommentsHelper {
5
5
  constructor(options) {
6
- this.options = options
7
- this.utils = require('./../core/utils')
6
+ this.options = options;
7
+ this.utils = require('./../core/utils');
8
8
  }
9
9
 
10
10
  removeComments(template) {
11
- let i
12
- let startIndex = null
13
- let tempString
14
- let resultString = ''
11
+ let i;
12
+ let startIndex = null;
13
+ let tempString;
14
+ let resultString = '';
15
15
 
16
16
  for (i = 0; i < template.length + 1; i++) {
17
17
  if (!this.utils.isNull(startIndex)) {
18
- tempString = template.substring(i - this.options.commentEnd.length, i)
18
+ tempString = template.substring(i - this.options.commentEnd.length, i);
19
19
  if (tempString === this.options.commentEnd) {
20
- startIndex = null
20
+ startIndex = null;
21
21
  }
22
22
  }
23
23
  if (this.utils.isNull(startIndex)) {
24
- tempString = template.substring(i, i + this.options.commentStart.length)
24
+ tempString = template.substring(i, i + this.options.commentStart.length);
25
25
  if (tempString === this.options.commentStart) {
26
- startIndex = i
26
+ startIndex = i;
27
27
  } else if (!this.utils.isUndefined(template[i])) {
28
- resultString += template[i]
28
+ resultString += template[i];
29
29
  }
30
30
  }
31
31
  }
32
32
 
33
- return resultString
33
+ return resultString;
34
34
  }
35
35
  }
36
36
 
37
- module.exports = CommentsHelper
37
+ module.exports = CommentsHelper;
@@ -3,72 +3,72 @@
3
3
  */
4
4
  class ExpressionsHelper {
5
5
  constructor(options) {
6
- this.options = options
7
- this.utils = require('./../core/utils')
6
+ this.options = options;
7
+ this.utils = require('./../core/utils');
8
8
  }
9
9
 
10
10
  getExpressionWithoutExtraSpaces(expression) {
11
- let body = expression.slice(this.options.expressionStart.length).slice(0, -this.options.expressionEnd.length)
12
- let resultBody = ''
13
- let i
14
- let waitForChar
11
+ let body = expression.slice(this.options.expressionStart.length).slice(0, -this.options.expressionEnd.length);
12
+ let resultBody = '';
13
+ let i;
14
+ let waitForChar;
15
15
 
16
- body = body.trim()
16
+ body = body.trim();
17
17
  for (i = 0; i < body.length; i++) {
18
18
  if (waitForChar && body[i] !== waitForChar) {
19
- resultBody += body[i]
20
- continue
19
+ resultBody += body[i];
20
+ continue;
21
21
  }
22
- if (body[i] === '"' || body[i] === '\'') {
22
+ if (body[i] === '"' || body[i] === "'") {
23
23
  if (!waitForChar) {
24
- waitForChar = body[i]
24
+ waitForChar = body[i];
25
25
  } else {
26
- waitForChar = null
26
+ waitForChar = null;
27
27
  }
28
28
  }
29
29
  if (this.options.specialChars.indexOf(body[i]) >= 0) {
30
- resultBody = resultBody.trim()
30
+ resultBody = resultBody.trim();
31
31
  }
32
32
  if (this.options.specialChars.indexOf(resultBody[resultBody.length - 1]) >= 0 && body[i] === ' ') {
33
- continue
33
+ continue;
34
34
  }
35
35
  if (resultBody[resultBody.length - 1] === ' ' && body[i] === ' ') {
36
- continue
36
+ continue;
37
37
  }
38
38
  if (body[i] !== '\n') {
39
- resultBody += body[i]
39
+ resultBody += body[i];
40
40
  }
41
41
  }
42
42
 
43
- return this.options.expressionStart + resultBody + this.options.expressionEnd
43
+ return this.options.expressionStart + resultBody + this.options.expressionEnd;
44
44
  }
45
45
 
46
46
  clearExtraSpaces(template) {
47
- let i
48
- let startIndex = null
49
- let tempSubstring
50
- let resultString = ''
47
+ let i;
48
+ let startIndex = null;
49
+ let tempSubstring;
50
+ let resultString = '';
51
51
 
52
52
  for (i = 0; i < template.length + 1; i++) {
53
53
  if (!this.utils.isNull(startIndex)) {
54
- tempSubstring = template.substring(i - this.options.expressionEnd.length, i)
54
+ tempSubstring = template.substring(i - this.options.expressionEnd.length, i);
55
55
  if (tempSubstring === this.options.expressionEnd) {
56
- resultString += this.getExpressionWithoutExtraSpaces(template.substring(startIndex, i))
57
- startIndex = null
56
+ resultString += this.getExpressionWithoutExtraSpaces(template.substring(startIndex, i));
57
+ startIndex = null;
58
58
  }
59
59
  }
60
60
  if (this.utils.isNull(startIndex)) {
61
- tempSubstring = template.substring(i, i + this.options.expressionStart.length)
61
+ tempSubstring = template.substring(i, i + this.options.expressionStart.length);
62
62
  if (tempSubstring === this.options.expressionStart) {
63
- startIndex = i
63
+ startIndex = i;
64
64
  } else if (!this.utils.isUndefined(template[i])) {
65
- resultString += template[i]
65
+ resultString += template[i];
66
66
  }
67
67
  }
68
68
  }
69
69
 
70
- return resultString
70
+ return resultString;
71
71
  }
72
72
  }
73
73
 
74
- module.exports = ExpressionsHelper
74
+ module.exports = ExpressionsHelper;
package/lib/jlto.js CHANGED
@@ -4,38 +4,38 @@
4
4
  */
5
5
  class JLTO {
6
6
  constructor() {
7
- this.defaultOptions = new (require('./core/default'))().options
7
+ this.defaultOptions = new (require('./core/default'))().options;
8
8
  }
9
9
 
10
10
  optimizeString(string, options = {}) {
11
- let expressionsHelper
12
- let blocksHelper
13
- let commentsHelper
14
- this.options = {...this.defaultOptions, ...options}
15
- expressionsHelper = new (require('./helpers/expressionsHelper'))(this.options)
16
- blocksHelper = new (require('./helpers/blocksHelper'))(this.options)
17
- commentsHelper = new (require('./helpers/commentsHelper'))(this.options)
11
+ let expressionsHelper;
12
+ let blocksHelper;
13
+ let commentsHelper;
14
+ this.options = {...this.defaultOptions, ...options};
15
+ expressionsHelper = new (require('./helpers/expressionsHelper'))(this.options);
16
+ blocksHelper = new (require('./helpers/blocksHelper'))(this.options);
17
+ commentsHelper = new (require('./helpers/commentsHelper'))(this.options);
18
18
 
19
19
  if (this.options.removeComments) {
20
- string = commentsHelper.removeComments(string)
20
+ string = commentsHelper.removeComments(string);
21
21
  }
22
22
  if (this.options.cleanupExpressions) {
23
- string = expressionsHelper.clearExtraSpaces(string)
23
+ string = expressionsHelper.clearExtraSpaces(string);
24
24
  }
25
25
  if (this.options.cleanupBlocks) {
26
- string = blocksHelper.clearExtraSpaces(string)
26
+ string = blocksHelper.clearExtraSpaces(string);
27
27
  }
28
28
  if (this.options.minifyHtml) {
29
- let minify = require('html-minifier').minify
29
+ let minify = require('html-minifier').minify;
30
30
  this.options.minifyHtmlOptions = {
31
31
  ...this.defaultOptions.minifyHtmlOptions,
32
- ...(options.minifyHtmlOptions || {})
33
- }
34
- string = minify(string, this.options.minifyHtmlOptions)
32
+ ...(options.minifyHtmlOptions || {}),
33
+ };
34
+ string = minify(string, this.options.minifyHtmlOptions);
35
35
  }
36
36
 
37
- return string
37
+ return string;
38
38
  }
39
39
  }
40
40
 
41
- module.exports = JLTO
41
+ module.exports = JLTO;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jlto",
3
- "version": "1.2.1",
3
+ "version": "1.4.0",
4
4
  "description": "Nodejs-based tool for optimizing Jinja like templates",
5
5
  "keywords": [
6
6
  "jinja",
@@ -34,11 +34,11 @@
34
34
  "scripts": {
35
35
  "lint": "eslint test lib --max-warnings=0 --fix",
36
36
  "test": "npm run lint && jest --coverage",
37
- "codeship-testing": "jest --coverage && cat ./coverage/lcov.info | coveralls",
37
+ "ci-testing": "jest --coverage && cat ./coverage/lcov.info | coveralls",
38
38
  "prettier": "prettier \"{test,lib}/**/*.js\" --write"
39
39
  },
40
40
  "engines": {
41
- "node": ">=10"
41
+ "node": ">=16"
42
42
  },
43
43
  "pre-commit": [
44
44
  "prettier",
@@ -54,15 +54,17 @@
54
54
  ]
55
55
  },
56
56
  "devDependencies": {
57
- "chai": "4.2.0",
58
- "coveralls": "3.0.4",
59
- "eslint": "6.0.1",
60
- "jest": "24.8.0",
61
- "liquid-node": "3.0.1",
62
- "nunjucks": "3.2.0",
57
+ "chai": "4.3.4",
58
+ "coveralls": "3.1.1",
59
+ "eslint": "8.6.0",
60
+ "eslint-config-prettier": "^8.3.0",
61
+ "eslint-plugin-prettier": "^4.0.0",
62
+ "jest": "27.4.7",
63
+ "liquid": "5.1.1",
64
+ "nunjucks": "3.2.3",
63
65
  "pre-commit": "1.2.2",
64
- "prettier": "1.18.2",
65
- "twig": "1.13.3"
66
+ "prettier": "2.5.1",
67
+ "twig": "1.15.4"
66
68
  },
67
69
  "dependencies": {
68
70
  "html-minifier": "4.0.0"
package/test.dockerfile DELETED
@@ -1,9 +0,0 @@
1
- FROM node:12.6.0
2
-
3
- # Move project to /jlto directory
4
- RUN mkdir -p /jlto
5
- COPY . /jlto
6
- WORKDIR /jlto
7
-
8
- # Install dependencies
9
- RUN npm ci