jlto 1.5.0 → 1.5.3
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 +42 -5
- package/lib/helpers/spaceCleanupHelper.js +6 -6
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -84,12 +84,13 @@ jlto.optimizeString(template, {minifyHtml: true}).then((optimizedTemplate) => {
|
|
|
84
84
|
|
|
85
85
|
```js
|
|
86
86
|
module.exports = (grunt) => {
|
|
87
|
-
grunt.registerTask('min-nunjucks', 'Min nunjucks templates', ()
|
|
87
|
+
grunt.registerTask('min-nunjucks', 'Min nunjucks templates', function () {
|
|
88
88
|
let jlto = require('jlto');
|
|
89
89
|
let fs = require('fs');
|
|
90
|
-
let glob = require('glob');
|
|
90
|
+
let {glob} = require('glob');
|
|
91
91
|
let done = this.async();
|
|
92
|
-
|
|
92
|
+
(async () => {
|
|
93
|
+
let files = await glob('./**/*.nunjucks.html');
|
|
93
94
|
for (const filePath of files) {
|
|
94
95
|
let fileContent;
|
|
95
96
|
fileContent = fs.readFileSync(filePath).toString();
|
|
@@ -99,14 +100,50 @@ module.exports = (grunt) => {
|
|
|
99
100
|
} catch (ignored) {}
|
|
100
101
|
}
|
|
101
102
|
return done();
|
|
102
|
-
});
|
|
103
|
+
})().catch(() => done());
|
|
103
104
|
});
|
|
104
105
|
};
|
|
105
106
|
```
|
|
106
107
|
|
|
108
|
+
**Example of "nunjucks" templates minification with the custom webpack plugin:**
|
|
109
|
+
|
|
110
|
+
```js
|
|
111
|
+
let jlto = require('jlto');
|
|
112
|
+
let webpack = require('webpack');
|
|
113
|
+
|
|
114
|
+
class MinNunjucksPlugin {
|
|
115
|
+
apply(compiler) {
|
|
116
|
+
compiler.hooks.thisCompilation.tap('MinNunjucksPlugin', (compilation) => {
|
|
117
|
+
compilation.hooks.processAssets.tapPromise(
|
|
118
|
+
{
|
|
119
|
+
name: 'MinNunjucksPlugin',
|
|
120
|
+
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE,
|
|
121
|
+
},
|
|
122
|
+
async (assets) => {
|
|
123
|
+
for (const filePath of Object.keys(assets)) {
|
|
124
|
+
if (!filePath.endsWith('.nunjucks.html')) {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
let fileContent = compilation.getAsset(filePath).source.source().toString();
|
|
128
|
+
try {
|
|
129
|
+
fileContent = await jlto.optimizeString(fileContent, {minifyHtml: true});
|
|
130
|
+
compilation.updateAsset(filePath, new webpack.sources.RawSource(fileContent));
|
|
131
|
+
} catch (ignored) {}
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
);
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
module.exports = {
|
|
140
|
+
plugins: [new MinNunjucksPlugin()],
|
|
141
|
+
};
|
|
142
|
+
```
|
|
143
|
+
|
|
107
144
|
## Tests
|
|
108
145
|
|
|
109
|
-
|
|
146
|
+
Tests are written using Jest and Node's `assert` module. To run them, invoke `npm test`.
|
|
110
147
|
|
|
111
148
|
## License
|
|
112
149
|
|
|
@@ -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.3",
|
|
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"
|