jlto 1.5.0 → 1.5.2
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/README.md +45 -5
- package/lib/helpers/spaceCleanupHelper.js +6 -6
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -7,6 +7,9 @@
|
|
|
7
7
|
|
|
8
8
|
> Jinja Like Templates Optimizer (JLTO) is a Nodejs-based tool for optimizing Jinja like templates.
|
|
9
9
|
|
|
10
|
+
If you are thinking about contributing to open source, this article is a good read:
|
|
11
|
+
[Why You Should Write Open Source Code and How It Helps Your Career](https://explainme.online/article/why-you-should-write-open-source-code-and-how-it-helps-your-career).
|
|
12
|
+
|
|
10
13
|
**Gulp tool for JLTO:**
|
|
11
14
|
|
|
12
15
|
[gulp-jlto](https://www.npmjs.com/package/gulp-jlto)
|
|
@@ -84,12 +87,13 @@ jlto.optimizeString(template, {minifyHtml: true}).then((optimizedTemplate) => {
|
|
|
84
87
|
|
|
85
88
|
```js
|
|
86
89
|
module.exports = (grunt) => {
|
|
87
|
-
grunt.registerTask('min-nunjucks', 'Min nunjucks templates', ()
|
|
90
|
+
grunt.registerTask('min-nunjucks', 'Min nunjucks templates', function () {
|
|
88
91
|
let jlto = require('jlto');
|
|
89
92
|
let fs = require('fs');
|
|
90
|
-
let glob = require('glob');
|
|
93
|
+
let {glob} = require('glob');
|
|
91
94
|
let done = this.async();
|
|
92
|
-
|
|
95
|
+
(async () => {
|
|
96
|
+
let files = await glob('./**/*.nunjucks.html');
|
|
93
97
|
for (const filePath of files) {
|
|
94
98
|
let fileContent;
|
|
95
99
|
fileContent = fs.readFileSync(filePath).toString();
|
|
@@ -99,14 +103,50 @@ module.exports = (grunt) => {
|
|
|
99
103
|
} catch (ignored) {}
|
|
100
104
|
}
|
|
101
105
|
return done();
|
|
102
|
-
});
|
|
106
|
+
})().catch(() => done());
|
|
103
107
|
});
|
|
104
108
|
};
|
|
105
109
|
```
|
|
106
110
|
|
|
111
|
+
**Example of "nunjucks" templates minification with the custom webpack plugin:**
|
|
112
|
+
|
|
113
|
+
```js
|
|
114
|
+
let jlto = require('jlto');
|
|
115
|
+
let webpack = require('webpack');
|
|
116
|
+
|
|
117
|
+
class MinNunjucksPlugin {
|
|
118
|
+
apply(compiler) {
|
|
119
|
+
compiler.hooks.thisCompilation.tap('MinNunjucksPlugin', (compilation) => {
|
|
120
|
+
compilation.hooks.processAssets.tapPromise(
|
|
121
|
+
{
|
|
122
|
+
name: 'MinNunjucksPlugin',
|
|
123
|
+
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE,
|
|
124
|
+
},
|
|
125
|
+
async (assets) => {
|
|
126
|
+
for (const filePath of Object.keys(assets)) {
|
|
127
|
+
if (!filePath.endsWith('.nunjucks.html')) {
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
let fileContent = compilation.getAsset(filePath).source.source().toString();
|
|
131
|
+
try {
|
|
132
|
+
fileContent = await jlto.optimizeString(fileContent, {minifyHtml: true});
|
|
133
|
+
compilation.updateAsset(filePath, new webpack.sources.RawSource(fileContent));
|
|
134
|
+
} catch (ignored) {}
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
);
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
module.exports = {
|
|
143
|
+
plugins: [new MinNunjucksPlugin()],
|
|
144
|
+
};
|
|
145
|
+
```
|
|
146
|
+
|
|
107
147
|
## Tests
|
|
108
148
|
|
|
109
|
-
|
|
149
|
+
Tests are written using Jest and Node's `assert` module. To run them, invoke `npm test`.
|
|
110
150
|
|
|
111
151
|
## License
|
|
112
152
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
let isEscaped = (text, index) => {
|
|
2
2
|
let backslashCount = 0;
|
|
3
3
|
let i;
|
|
4
4
|
|
|
@@ -7,9 +7,9 @@ function isEscaped(text, index) {
|
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
return backslashCount % 2 === 1;
|
|
10
|
-
}
|
|
10
|
+
};
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
let getTagWithoutExtraSpaces = (tag, startDelimiter, endDelimiter, specialChars) => {
|
|
13
13
|
let body = tag.slice(startDelimiter.length).slice(0, -endDelimiter.length);
|
|
14
14
|
let resultBody = '';
|
|
15
15
|
let i;
|
|
@@ -46,9 +46,9 @@ function getTagWithoutExtraSpaces(tag, startDelimiter, endDelimiter, specialChar
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
return startDelimiter + resultBody + endDelimiter;
|
|
49
|
-
}
|
|
49
|
+
};
|
|
50
50
|
|
|
51
|
-
|
|
51
|
+
let clearTemplateExtraSpaces = (template, startDelimiter, endDelimiter, specialChars) => {
|
|
52
52
|
let i;
|
|
53
53
|
let startIndex = null;
|
|
54
54
|
let tempSubstring;
|
|
@@ -78,7 +78,7 @@ function clearTemplateExtraSpaces(template, startDelimiter, endDelimiter, specia
|
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
return resultString;
|
|
81
|
-
}
|
|
81
|
+
};
|
|
82
82
|
|
|
83
83
|
module.exports = {
|
|
84
84
|
clearTemplateExtraSpaces,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jlto",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.2",
|
|
4
4
|
"description": "Nodejs-based tool for optimizing Jinja like templates",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"jinja",
|
|
@@ -57,13 +57,16 @@
|
|
|
57
57
|
"eslint": "^9.39.3",
|
|
58
58
|
"eslint-config-prettier": "^10.1.8",
|
|
59
59
|
"eslint-plugin-prettier": "^5.5.5",
|
|
60
|
+
"glob": "^13.0.6",
|
|
60
61
|
"globals": "^17.3.0",
|
|
62
|
+
"grunt": "^1.6.1",
|
|
61
63
|
"jest": "^30.2.0",
|
|
62
64
|
"liquid": "^5.1.1",
|
|
63
65
|
"nunjucks": "^3.2.4",
|
|
64
66
|
"pre-commit": "^1.2.2",
|
|
65
67
|
"prettier": "^3.8.1",
|
|
66
|
-
"twig": "^
|
|
68
|
+
"twig": "^3.0.0",
|
|
69
|
+
"webpack": "^5.105.4"
|
|
67
70
|
},
|
|
68
71
|
"dependencies": {
|
|
69
72
|
"html-minifier-next": "^4.19.1"
|