htmlnano 3.0.0 → 3.2.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/README.md +40 -25
- package/dist/_modules/collapseAttributeWhitespace.d.mts +41 -5
- package/dist/_modules/collapseAttributeWhitespace.d.ts +41 -5
- package/dist/_modules/collapseAttributeWhitespace.js +69 -16
- package/dist/_modules/collapseAttributeWhitespace.mjs +67 -17
- package/dist/_modules/collapseBooleanAttributes.d.mts +36 -3
- package/dist/_modules/collapseBooleanAttributes.d.ts +36 -3
- package/dist/_modules/collapseBooleanAttributes.js +18 -11
- package/dist/_modules/collapseBooleanAttributes.mjs +18 -11
- package/dist/_modules/collapseWhitespace.d.mts +36 -3
- package/dist/_modules/collapseWhitespace.d.ts +36 -3
- package/dist/_modules/collapseWhitespace.js +25 -2
- package/dist/_modules/collapseWhitespace.mjs +25 -2
- package/dist/_modules/custom.d.mts +36 -3
- package/dist/_modules/custom.d.ts +36 -3
- package/dist/_modules/deduplicateAttributeValues.d.mts +36 -3
- package/dist/_modules/deduplicateAttributeValues.d.ts +36 -3
- package/dist/_modules/deduplicateAttributeValues.js +20 -5
- package/dist/_modules/deduplicateAttributeValues.mjs +21 -6
- package/dist/_modules/example.d.mts +36 -3
- package/dist/_modules/example.d.ts +36 -3
- package/dist/_modules/mergeScripts.d.mts +36 -3
- package/dist/_modules/mergeScripts.d.ts +36 -3
- package/dist/_modules/mergeScripts.js +111 -24
- package/dist/_modules/mergeScripts.mjs +111 -24
- package/dist/_modules/mergeStyles.d.mts +36 -3
- package/dist/_modules/mergeStyles.d.ts +36 -3
- package/dist/_modules/mergeStyles.js +66 -4
- package/dist/_modules/mergeStyles.mjs +66 -4
- package/dist/_modules/minifyAttributes.d.mts +95 -0
- package/dist/_modules/minifyAttributes.d.ts +95 -0
- package/dist/_modules/minifyAttributes.js +159 -0
- package/dist/_modules/minifyAttributes.mjs +157 -0
- package/dist/_modules/minifyConditionalComments.d.mts +36 -3
- package/dist/_modules/minifyConditionalComments.d.ts +36 -3
- package/dist/_modules/minifyConditionalComments.js +37 -19
- package/dist/_modules/minifyConditionalComments.mjs +37 -19
- package/dist/_modules/minifyCss.d.mts +36 -3
- package/dist/_modules/minifyCss.d.ts +36 -3
- package/dist/_modules/minifyCss.js +13 -27
- package/dist/_modules/minifyCss.mjs +14 -28
- package/dist/_modules/minifyHtmlTemplate.d.mts +91 -0
- package/dist/_modules/minifyHtmlTemplate.d.ts +91 -0
- package/dist/_modules/minifyHtmlTemplate.js +231 -0
- package/dist/_modules/minifyHtmlTemplate.mjs +228 -0
- package/dist/_modules/minifyJs.d.mts +36 -3
- package/dist/_modules/minifyJs.d.ts +36 -3
- package/dist/_modules/minifyJs.js +106 -5
- package/dist/_modules/minifyJs.mjs +107 -6
- package/dist/_modules/minifyJson.d.mts +36 -3
- package/dist/_modules/minifyJson.d.ts +36 -3
- package/dist/_modules/minifyJson.js +8 -11
- package/dist/_modules/minifyJson.mjs +8 -11
- package/dist/_modules/minifySvg.d.mts +36 -3
- package/dist/_modules/minifySvg.d.ts +36 -3
- package/dist/_modules/minifySvg.js +35 -4
- package/dist/_modules/minifySvg.mjs +35 -4
- package/dist/_modules/minifyUrls.d.mts +37 -4
- package/dist/_modules/minifyUrls.d.ts +37 -4
- package/dist/_modules/minifyUrls.js +52 -27
- package/dist/_modules/minifyUrls.mjs +52 -27
- package/dist/_modules/normalizeAttributeValues.d.mts +36 -3
- package/dist/_modules/normalizeAttributeValues.d.ts +36 -3
- package/dist/_modules/normalizeAttributeValues.js +10 -8
- package/dist/_modules/normalizeAttributeValues.mjs +10 -8
- package/dist/_modules/removeAttributeQuotes.d.mts +40 -4
- package/dist/_modules/removeAttributeQuotes.d.ts +40 -4
- package/dist/_modules/removeAttributeQuotes.js +9 -4
- package/dist/_modules/removeAttributeQuotes.mjs +9 -4
- package/dist/_modules/removeComments.d.mts +37 -4
- package/dist/_modules/removeComments.d.ts +37 -4
- package/dist/_modules/removeComments.js +44 -12
- package/dist/_modules/removeComments.mjs +44 -12
- package/dist/_modules/removeEmptyAttributes.d.mts +36 -3
- package/dist/_modules/removeEmptyAttributes.d.ts +36 -3
- package/dist/_modules/removeEmptyAttributes.js +37 -16
- package/dist/_modules/removeEmptyAttributes.mjs +37 -16
- package/dist/_modules/removeEmptyElements.d.mts +95 -0
- package/dist/_modules/removeEmptyElements.d.ts +95 -0
- package/dist/_modules/removeEmptyElements.js +90 -0
- package/dist/_modules/removeEmptyElements.mjs +88 -0
- package/dist/_modules/removeOptionalTags.d.mts +36 -3
- package/dist/_modules/removeOptionalTags.d.ts +36 -3
- package/dist/_modules/removeOptionalTags.js +39 -28
- package/dist/_modules/removeOptionalTags.mjs +39 -28
- package/dist/_modules/removeRedundantAttributes.d.mts +36 -3
- package/dist/_modules/removeRedundantAttributes.d.ts +36 -3
- package/dist/_modules/removeRedundantAttributes.js +43 -28
- package/dist/_modules/removeRedundantAttributes.mjs +43 -28
- package/dist/_modules/removeUnusedCss.d.mts +37 -3
- package/dist/_modules/removeUnusedCss.d.ts +37 -3
- package/dist/_modules/removeUnusedCss.js +40 -14
- package/dist/_modules/removeUnusedCss.mjs +41 -15
- package/dist/_modules/sortAttributes.d.mts +39 -5
- package/dist/_modules/sortAttributes.d.ts +39 -5
- package/dist/_modules/sortAttributes.js +27 -8
- package/dist/_modules/sortAttributes.mjs +27 -8
- package/dist/_modules/sortAttributesWithLists.d.mts +39 -4
- package/dist/_modules/sortAttributesWithLists.d.ts +39 -4
- package/dist/_modules/sortAttributesWithLists.js +61 -52
- package/dist/_modules/sortAttributesWithLists.mjs +62 -53
- package/dist/helpers.d.ts +8 -1
- package/dist/helpers.js +48 -0
- package/dist/helpers.mjs +45 -1
- package/dist/index.d.ts +37 -4
- package/dist/index.js +13 -0
- package/dist/index.mjs +13 -0
- package/dist/presets/ampSafe.d.ts +36 -3
- package/dist/presets/max.d.ts +36 -3
- package/dist/presets/max.js +13 -5
- package/dist/presets/max.mjs +13 -5
- package/dist/presets/safe.d.ts +36 -3
- package/dist/presets/safe.js +6 -0
- package/dist/presets/safe.mjs +6 -0
- package/package.json +27 -15
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
|
+
|
|
3
|
+
var helpers_js = require('../helpers.js');
|
|
4
|
+
|
|
5
|
+
const defaultRules = [
|
|
6
|
+
{
|
|
7
|
+
tag: 'script',
|
|
8
|
+
attrs: {
|
|
9
|
+
type: 'text/html'
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
tag: 'script',
|
|
14
|
+
attrs: {
|
|
15
|
+
type: 'text/template'
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
tag: 'script',
|
|
20
|
+
attrs: {
|
|
21
|
+
type: 'text/x-template'
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
tag: 'script',
|
|
26
|
+
attrs: {
|
|
27
|
+
type: 'text/x-handlebars-template'
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
tag: 'script',
|
|
32
|
+
attrs: {
|
|
33
|
+
type: 'text/x-handlebars'
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
tag: 'script',
|
|
38
|
+
attrs: {
|
|
39
|
+
type: 'text/x-mustache-template'
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
tag: 'script',
|
|
44
|
+
attrs: {
|
|
45
|
+
type: 'text/x-underscore-template'
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
tag: 'script',
|
|
50
|
+
attrs: {
|
|
51
|
+
type: 'text/x-jsrender'
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
tag: 'script',
|
|
56
|
+
attrs: {
|
|
57
|
+
type: 'text/x-jquery-tmpl'
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
tag: 'script',
|
|
62
|
+
attrs: {
|
|
63
|
+
type: 'text/x-kendo-template'
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
tag: 'script',
|
|
68
|
+
attrs: {
|
|
69
|
+
type: 'text/ng-template'
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
tag: 'template'
|
|
74
|
+
}
|
|
75
|
+
];
|
|
76
|
+
const mod = {
|
|
77
|
+
async default (tree, options, moduleOptions) {
|
|
78
|
+
const rules = resolveTemplateRules(moduleOptions);
|
|
79
|
+
if (!rules.length) {
|
|
80
|
+
return tree;
|
|
81
|
+
}
|
|
82
|
+
const { default: htmlnano } = await import('../index.js');
|
|
83
|
+
const innerOptions = {
|
|
84
|
+
...options,
|
|
85
|
+
minifyHtmlTemplate: false,
|
|
86
|
+
skipConfigLoading: true
|
|
87
|
+
};
|
|
88
|
+
const promises = [];
|
|
89
|
+
tree.walk((node)=>{
|
|
90
|
+
if (!node.tag) {
|
|
91
|
+
return node;
|
|
92
|
+
}
|
|
93
|
+
if (!matchesTemplateRule(node, rules)) {
|
|
94
|
+
return node;
|
|
95
|
+
}
|
|
96
|
+
if (node.attrs && 'integrity' in node.attrs) {
|
|
97
|
+
return node;
|
|
98
|
+
}
|
|
99
|
+
if (node.tag === 'script' && node.attrs && 'src' in node.attrs) {
|
|
100
|
+
return node;
|
|
101
|
+
}
|
|
102
|
+
const rawContent = collectNodeContent(tree, node);
|
|
103
|
+
if (rawContent.trim().length === 0) {
|
|
104
|
+
return node;
|
|
105
|
+
}
|
|
106
|
+
const promise = htmlnano.process(rawContent, innerOptions, {}, {}).then((result)=>{
|
|
107
|
+
node.content = [
|
|
108
|
+
result.html
|
|
109
|
+
];
|
|
110
|
+
});
|
|
111
|
+
promises.push(promise);
|
|
112
|
+
return node;
|
|
113
|
+
});
|
|
114
|
+
return Promise.all(promises).then(()=>tree);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
function resolveTemplateRules(moduleOptions) {
|
|
118
|
+
if (!moduleOptions || moduleOptions === true) {
|
|
119
|
+
return normalizeRules(defaultRules);
|
|
120
|
+
}
|
|
121
|
+
if (Array.isArray(moduleOptions)) {
|
|
122
|
+
return normalizeRules(moduleOptions);
|
|
123
|
+
}
|
|
124
|
+
return normalizeRules(defaultRules);
|
|
125
|
+
}
|
|
126
|
+
function normalizeRules(rules) {
|
|
127
|
+
const normalized = [];
|
|
128
|
+
for (const rule of rules){
|
|
129
|
+
const resolved = normalizeRule(rule);
|
|
130
|
+
if (resolved) {
|
|
131
|
+
normalized.push(resolved);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return normalized;
|
|
135
|
+
}
|
|
136
|
+
function normalizeRule(rule) {
|
|
137
|
+
if (rule && typeof rule === 'object') {
|
|
138
|
+
if (typeof rule.tag !== 'string' || !rule.tag.trim()) {
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
const attrs = {};
|
|
142
|
+
if (rule.attrs && typeof rule.attrs === 'object') {
|
|
143
|
+
for (const [name, value] of Object.entries(rule.attrs)){
|
|
144
|
+
if (typeof name !== 'string' || !name.trim()) {
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
attrs[name.toLowerCase()] = value != null ? value : true;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
tag: rule.tag.toLowerCase(),
|
|
152
|
+
attrs: Object.keys(attrs).length ? attrs : undefined
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
function matchesTemplateRule(node, rules) {
|
|
158
|
+
var _node_tag;
|
|
159
|
+
const tag = (_node_tag = node.tag) == null ? void 0 : _node_tag.toLowerCase();
|
|
160
|
+
if (!tag) {
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
const attrs = normalizeNodeAttrs(node);
|
|
164
|
+
for (const rule of rules){
|
|
165
|
+
if (tag !== rule.tag) {
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (!rule.attrs) {
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
let match = true;
|
|
172
|
+
for (const [attrName, ruleValue] of Object.entries(rule.attrs)){
|
|
173
|
+
if (!(attrName in attrs)) {
|
|
174
|
+
match = false;
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
if (ruleValue === true) {
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
const nodeValue = normalizeAttrValue(attrName, attrs[attrName]);
|
|
181
|
+
const normalizedRuleValue = normalizeAttrValue(attrName, ruleValue);
|
|
182
|
+
if (nodeValue !== normalizedRuleValue) {
|
|
183
|
+
match = false;
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (match) {
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
function normalizeNodeAttrs(node) {
|
|
194
|
+
if (!node.attrs) {
|
|
195
|
+
return {};
|
|
196
|
+
}
|
|
197
|
+
const normalized = {};
|
|
198
|
+
for (const [key, value] of Object.entries(node.attrs)){
|
|
199
|
+
normalized[key.toLowerCase()] = value;
|
|
200
|
+
}
|
|
201
|
+
return normalized;
|
|
202
|
+
}
|
|
203
|
+
function normalizeAttrValue(attrName, value) {
|
|
204
|
+
if (typeof value !== 'string') {
|
|
205
|
+
return value;
|
|
206
|
+
}
|
|
207
|
+
if (attrName === 'type') {
|
|
208
|
+
return helpers_js.normalizeMimeType(value);
|
|
209
|
+
}
|
|
210
|
+
return value;
|
|
211
|
+
}
|
|
212
|
+
function collectNodeContent(tree, node) {
|
|
213
|
+
if (!node.content) {
|
|
214
|
+
return '';
|
|
215
|
+
}
|
|
216
|
+
const content = typeof node.content === 'string' ? [
|
|
217
|
+
node.content
|
|
218
|
+
] : node.content;
|
|
219
|
+
let html = '';
|
|
220
|
+
for (const child of content){
|
|
221
|
+
if (typeof child === 'string') {
|
|
222
|
+
html += child;
|
|
223
|
+
} else {
|
|
224
|
+
html += tree.render(child);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return html;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
exports.default = mod;
|
|
231
|
+
exports.defaultRules = defaultRules;
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { normalizeMimeType } from '../helpers.mjs';
|
|
2
|
+
|
|
3
|
+
const defaultRules = [
|
|
4
|
+
{
|
|
5
|
+
tag: 'script',
|
|
6
|
+
attrs: {
|
|
7
|
+
type: 'text/html'
|
|
8
|
+
}
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
tag: 'script',
|
|
12
|
+
attrs: {
|
|
13
|
+
type: 'text/template'
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
tag: 'script',
|
|
18
|
+
attrs: {
|
|
19
|
+
type: 'text/x-template'
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
tag: 'script',
|
|
24
|
+
attrs: {
|
|
25
|
+
type: 'text/x-handlebars-template'
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
tag: 'script',
|
|
30
|
+
attrs: {
|
|
31
|
+
type: 'text/x-handlebars'
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
tag: 'script',
|
|
36
|
+
attrs: {
|
|
37
|
+
type: 'text/x-mustache-template'
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
tag: 'script',
|
|
42
|
+
attrs: {
|
|
43
|
+
type: 'text/x-underscore-template'
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
tag: 'script',
|
|
48
|
+
attrs: {
|
|
49
|
+
type: 'text/x-jsrender'
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
tag: 'script',
|
|
54
|
+
attrs: {
|
|
55
|
+
type: 'text/x-jquery-tmpl'
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
tag: 'script',
|
|
60
|
+
attrs: {
|
|
61
|
+
type: 'text/x-kendo-template'
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
tag: 'script',
|
|
66
|
+
attrs: {
|
|
67
|
+
type: 'text/ng-template'
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
tag: 'template'
|
|
72
|
+
}
|
|
73
|
+
];
|
|
74
|
+
const mod = {
|
|
75
|
+
async default (tree, options, moduleOptions) {
|
|
76
|
+
const rules = resolveTemplateRules(moduleOptions);
|
|
77
|
+
if (!rules.length) {
|
|
78
|
+
return tree;
|
|
79
|
+
}
|
|
80
|
+
const { default: htmlnano } = await import('../index.mjs');
|
|
81
|
+
const innerOptions = {
|
|
82
|
+
...options,
|
|
83
|
+
minifyHtmlTemplate: false,
|
|
84
|
+
skipConfigLoading: true
|
|
85
|
+
};
|
|
86
|
+
const promises = [];
|
|
87
|
+
tree.walk((node)=>{
|
|
88
|
+
if (!node.tag) {
|
|
89
|
+
return node;
|
|
90
|
+
}
|
|
91
|
+
if (!matchesTemplateRule(node, rules)) {
|
|
92
|
+
return node;
|
|
93
|
+
}
|
|
94
|
+
if (node.attrs && 'integrity' in node.attrs) {
|
|
95
|
+
return node;
|
|
96
|
+
}
|
|
97
|
+
if (node.tag === 'script' && node.attrs && 'src' in node.attrs) {
|
|
98
|
+
return node;
|
|
99
|
+
}
|
|
100
|
+
const rawContent = collectNodeContent(tree, node);
|
|
101
|
+
if (rawContent.trim().length === 0) {
|
|
102
|
+
return node;
|
|
103
|
+
}
|
|
104
|
+
const promise = htmlnano.process(rawContent, innerOptions, {}, {}).then((result)=>{
|
|
105
|
+
node.content = [
|
|
106
|
+
result.html
|
|
107
|
+
];
|
|
108
|
+
});
|
|
109
|
+
promises.push(promise);
|
|
110
|
+
return node;
|
|
111
|
+
});
|
|
112
|
+
return Promise.all(promises).then(()=>tree);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
function resolveTemplateRules(moduleOptions) {
|
|
116
|
+
if (!moduleOptions || moduleOptions === true) {
|
|
117
|
+
return normalizeRules(defaultRules);
|
|
118
|
+
}
|
|
119
|
+
if (Array.isArray(moduleOptions)) {
|
|
120
|
+
return normalizeRules(moduleOptions);
|
|
121
|
+
}
|
|
122
|
+
return normalizeRules(defaultRules);
|
|
123
|
+
}
|
|
124
|
+
function normalizeRules(rules) {
|
|
125
|
+
const normalized = [];
|
|
126
|
+
for (const rule of rules){
|
|
127
|
+
const resolved = normalizeRule(rule);
|
|
128
|
+
if (resolved) {
|
|
129
|
+
normalized.push(resolved);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return normalized;
|
|
133
|
+
}
|
|
134
|
+
function normalizeRule(rule) {
|
|
135
|
+
if (rule && typeof rule === 'object') {
|
|
136
|
+
if (typeof rule.tag !== 'string' || !rule.tag.trim()) {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
const attrs = {};
|
|
140
|
+
if (rule.attrs && typeof rule.attrs === 'object') {
|
|
141
|
+
for (const [name, value] of Object.entries(rule.attrs)){
|
|
142
|
+
if (typeof name !== 'string' || !name.trim()) {
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
attrs[name.toLowerCase()] = value != null ? value : true;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return {
|
|
149
|
+
tag: rule.tag.toLowerCase(),
|
|
150
|
+
attrs: Object.keys(attrs).length ? attrs : undefined
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
function matchesTemplateRule(node, rules) {
|
|
156
|
+
var _node_tag;
|
|
157
|
+
const tag = (_node_tag = node.tag) == null ? void 0 : _node_tag.toLowerCase();
|
|
158
|
+
if (!tag) {
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
const attrs = normalizeNodeAttrs(node);
|
|
162
|
+
for (const rule of rules){
|
|
163
|
+
if (tag !== rule.tag) {
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
if (!rule.attrs) {
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
let match = true;
|
|
170
|
+
for (const [attrName, ruleValue] of Object.entries(rule.attrs)){
|
|
171
|
+
if (!(attrName in attrs)) {
|
|
172
|
+
match = false;
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
if (ruleValue === true) {
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
const nodeValue = normalizeAttrValue(attrName, attrs[attrName]);
|
|
179
|
+
const normalizedRuleValue = normalizeAttrValue(attrName, ruleValue);
|
|
180
|
+
if (nodeValue !== normalizedRuleValue) {
|
|
181
|
+
match = false;
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if (match) {
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
function normalizeNodeAttrs(node) {
|
|
192
|
+
if (!node.attrs) {
|
|
193
|
+
return {};
|
|
194
|
+
}
|
|
195
|
+
const normalized = {};
|
|
196
|
+
for (const [key, value] of Object.entries(node.attrs)){
|
|
197
|
+
normalized[key.toLowerCase()] = value;
|
|
198
|
+
}
|
|
199
|
+
return normalized;
|
|
200
|
+
}
|
|
201
|
+
function normalizeAttrValue(attrName, value) {
|
|
202
|
+
if (typeof value !== 'string') {
|
|
203
|
+
return value;
|
|
204
|
+
}
|
|
205
|
+
if (attrName === 'type') {
|
|
206
|
+
return normalizeMimeType(value);
|
|
207
|
+
}
|
|
208
|
+
return value;
|
|
209
|
+
}
|
|
210
|
+
function collectNodeContent(tree, node) {
|
|
211
|
+
if (!node.content) {
|
|
212
|
+
return '';
|
|
213
|
+
}
|
|
214
|
+
const content = typeof node.content === 'string' ? [
|
|
215
|
+
node.content
|
|
216
|
+
] : node.content;
|
|
217
|
+
let html = '';
|
|
218
|
+
for (const child of content){
|
|
219
|
+
if (typeof child === 'string') {
|
|
220
|
+
html += child;
|
|
221
|
+
} else {
|
|
222
|
+
html += tree.render(child);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return html;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export { mod as default, defaultRules };
|
|
@@ -2,16 +2,24 @@ import PostHTML from 'posthtml';
|
|
|
2
2
|
import { MinifyOptions } from 'terser';
|
|
3
3
|
import { Options } from 'cssnano';
|
|
4
4
|
import { Config } from 'svgo';
|
|
5
|
+
import { UserDefinedOptions } from 'purgecss';
|
|
5
6
|
|
|
6
7
|
type PostHTMLTreeLike = [PostHTML.Node] & PostHTML.NodeAPI & {
|
|
7
8
|
options?: {
|
|
8
9
|
quoteAllAttributes?: boolean | undefined;
|
|
10
|
+
quoteStyle?: 0 | 1 | 2 | undefined;
|
|
11
|
+
replaceQuote?: boolean | undefined;
|
|
9
12
|
} | undefined;
|
|
10
13
|
render(): string;
|
|
11
14
|
render(node: PostHTML.Node | PostHTMLTreeLike, renderOptions?: any): string;
|
|
12
15
|
};
|
|
13
16
|
type MaybeArray<T> = T | Array<T>;
|
|
14
17
|
type PostHTMLNodeLike = PostHTML.Node | string;
|
|
18
|
+
type HtmlnanoTemplateRule = {
|
|
19
|
+
tag: string;
|
|
20
|
+
attrs?: Record<string, string | boolean | void>;
|
|
21
|
+
};
|
|
22
|
+
type MinifyHtmlTemplateOptions = boolean | HtmlnanoTemplateRule[];
|
|
15
23
|
interface HtmlnanoOptions {
|
|
16
24
|
skipConfigLoading?: boolean;
|
|
17
25
|
configPath?: string;
|
|
@@ -27,17 +35,42 @@ interface HtmlnanoOptions {
|
|
|
27
35
|
mergeStyles?: boolean;
|
|
28
36
|
mergeScripts?: boolean;
|
|
29
37
|
minifyCss?: Options | boolean;
|
|
38
|
+
minifyHtmlTemplate?: MinifyHtmlTemplateOptions;
|
|
30
39
|
minifyConditionalComments?: boolean;
|
|
31
40
|
minifyJs?: MinifyOptions | boolean;
|
|
32
41
|
minifyJson?: boolean;
|
|
42
|
+
minifyAttributes?: boolean | {
|
|
43
|
+
metaContent?: boolean;
|
|
44
|
+
redundantWhitespaces?: 'safe' | 'agressive' | false;
|
|
45
|
+
};
|
|
33
46
|
minifySvg?: Config | boolean;
|
|
34
47
|
normalizeAttributeValues?: boolean;
|
|
35
|
-
removeAttributeQuotes?: boolean
|
|
36
|
-
|
|
48
|
+
removeAttributeQuotes?: boolean | {
|
|
49
|
+
force?: boolean;
|
|
50
|
+
};
|
|
51
|
+
removeComments?: boolean | RegExp | ((comment: string) => boolean) | string;
|
|
37
52
|
removeEmptyAttributes?: boolean;
|
|
53
|
+
removeEmptyElements?: boolean | {
|
|
54
|
+
removeWithAttributes?: boolean;
|
|
55
|
+
};
|
|
38
56
|
removeRedundantAttributes?: boolean;
|
|
39
57
|
removeOptionalTags?: boolean;
|
|
40
|
-
removeUnusedCss?: boolean
|
|
58
|
+
removeUnusedCss?: boolean | ({
|
|
59
|
+
tool: 'purgeCSS';
|
|
60
|
+
} & Omit<UserDefinedOptions, 'content' | 'css' | 'extractors'>) | {
|
|
61
|
+
banner?: boolean;
|
|
62
|
+
csspath?: string;
|
|
63
|
+
htmlroot?: string;
|
|
64
|
+
ignore?: (string | RegExp)[];
|
|
65
|
+
inject?: string;
|
|
66
|
+
jsdom?: object;
|
|
67
|
+
media?: string[];
|
|
68
|
+
report?: boolean;
|
|
69
|
+
strictSSL?: boolean;
|
|
70
|
+
timeout?: number;
|
|
71
|
+
uncssrc?: string;
|
|
72
|
+
userAgent?: string;
|
|
73
|
+
};
|
|
41
74
|
sortAttributes?: boolean | 'alphabetical' | 'frequency';
|
|
42
75
|
sortAttributesWithLists?: boolean | 'alphabetical' | 'frequency';
|
|
43
76
|
}
|
|
@@ -2,16 +2,24 @@ import PostHTML from 'posthtml';
|
|
|
2
2
|
import { MinifyOptions } from 'terser';
|
|
3
3
|
import { Options } from 'cssnano';
|
|
4
4
|
import { Config } from 'svgo';
|
|
5
|
+
import { UserDefinedOptions } from 'purgecss';
|
|
5
6
|
|
|
6
7
|
type PostHTMLTreeLike = [PostHTML.Node] & PostHTML.NodeAPI & {
|
|
7
8
|
options?: {
|
|
8
9
|
quoteAllAttributes?: boolean | undefined;
|
|
10
|
+
quoteStyle?: 0 | 1 | 2 | undefined;
|
|
11
|
+
replaceQuote?: boolean | undefined;
|
|
9
12
|
} | undefined;
|
|
10
13
|
render(): string;
|
|
11
14
|
render(node: PostHTML.Node | PostHTMLTreeLike, renderOptions?: any): string;
|
|
12
15
|
};
|
|
13
16
|
type MaybeArray<T> = T | Array<T>;
|
|
14
17
|
type PostHTMLNodeLike = PostHTML.Node | string;
|
|
18
|
+
type HtmlnanoTemplateRule = {
|
|
19
|
+
tag: string;
|
|
20
|
+
attrs?: Record<string, string | boolean | void>;
|
|
21
|
+
};
|
|
22
|
+
type MinifyHtmlTemplateOptions = boolean | HtmlnanoTemplateRule[];
|
|
15
23
|
interface HtmlnanoOptions {
|
|
16
24
|
skipConfigLoading?: boolean;
|
|
17
25
|
configPath?: string;
|
|
@@ -27,17 +35,42 @@ interface HtmlnanoOptions {
|
|
|
27
35
|
mergeStyles?: boolean;
|
|
28
36
|
mergeScripts?: boolean;
|
|
29
37
|
minifyCss?: Options | boolean;
|
|
38
|
+
minifyHtmlTemplate?: MinifyHtmlTemplateOptions;
|
|
30
39
|
minifyConditionalComments?: boolean;
|
|
31
40
|
minifyJs?: MinifyOptions | boolean;
|
|
32
41
|
minifyJson?: boolean;
|
|
42
|
+
minifyAttributes?: boolean | {
|
|
43
|
+
metaContent?: boolean;
|
|
44
|
+
redundantWhitespaces?: 'safe' | 'agressive' | false;
|
|
45
|
+
};
|
|
33
46
|
minifySvg?: Config | boolean;
|
|
34
47
|
normalizeAttributeValues?: boolean;
|
|
35
|
-
removeAttributeQuotes?: boolean
|
|
36
|
-
|
|
48
|
+
removeAttributeQuotes?: boolean | {
|
|
49
|
+
force?: boolean;
|
|
50
|
+
};
|
|
51
|
+
removeComments?: boolean | RegExp | ((comment: string) => boolean) | string;
|
|
37
52
|
removeEmptyAttributes?: boolean;
|
|
53
|
+
removeEmptyElements?: boolean | {
|
|
54
|
+
removeWithAttributes?: boolean;
|
|
55
|
+
};
|
|
38
56
|
removeRedundantAttributes?: boolean;
|
|
39
57
|
removeOptionalTags?: boolean;
|
|
40
|
-
removeUnusedCss?: boolean
|
|
58
|
+
removeUnusedCss?: boolean | ({
|
|
59
|
+
tool: 'purgeCSS';
|
|
60
|
+
} & Omit<UserDefinedOptions, 'content' | 'css' | 'extractors'>) | {
|
|
61
|
+
banner?: boolean;
|
|
62
|
+
csspath?: string;
|
|
63
|
+
htmlroot?: string;
|
|
64
|
+
ignore?: (string | RegExp)[];
|
|
65
|
+
inject?: string;
|
|
66
|
+
jsdom?: object;
|
|
67
|
+
media?: string[];
|
|
68
|
+
report?: boolean;
|
|
69
|
+
strictSSL?: boolean;
|
|
70
|
+
timeout?: number;
|
|
71
|
+
uncssrc?: string;
|
|
72
|
+
userAgent?: string;
|
|
73
|
+
};
|
|
41
74
|
sortAttributes?: boolean | 'alphabetical' | 'frequency';
|
|
42
75
|
sortAttributesWithLists?: boolean | 'alphabetical' | 'frequency';
|
|
43
76
|
}
|