htmlnano 0.2.5 → 0.2.9
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/CHANGELOG.md +81 -2
- package/README.md +268 -15
- package/lib/helpers.js +16 -36
- package/lib/htmlnano.js +33 -69
- package/lib/modules/collapseAttributeWhitespace.js +21 -19
- package/lib/modules/collapseBooleanAttributes.js +36 -48
- package/lib/modules/collapseWhitespace.js +58 -32
- package/lib/modules/custom.js +12 -12
- package/lib/modules/deduplicateAttributeValues.js +34 -36
- package/lib/modules/mergeScripts.js +46 -66
- package/lib/modules/mergeStyles.js +33 -32
- package/lib/modules/minifyConditionalComments.js +58 -0
- package/lib/modules/minifyCss.js +59 -41
- package/lib/modules/minifyJs.js +79 -64
- package/lib/modules/minifyJson.js +23 -17
- package/lib/modules/minifySvg.js +20 -28
- package/lib/modules/minifyUrls.js +116 -0
- package/lib/modules/removeAttributeQuotes.js +19 -0
- package/lib/modules/removeComments.js +52 -34
- package/lib/modules/removeEmptyAttributes.js +19 -21
- package/lib/modules/removeOptionalTags.js +220 -0
- package/lib/modules/removeRedundantAttributes.js +89 -66
- package/lib/modules/removeUnusedCss.js +94 -116
- package/lib/modules/sortAttributes.js +115 -0
- package/lib/modules/sortAttributesWithLists.js +138 -0
- package/lib/presets/ampSafe.js +16 -13
- package/lib/presets/max.js +23 -17
- package/lib/presets/safe.js +38 -25
- package/package.json +28 -19
- package/test.js +30 -7
- package/lib/presets/hard.js +0 -26
|
@@ -1,30 +1,32 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
|
|
4
|
+
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = collapseAttributeWhitespace;
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
exports.attributesWithLists = void 0;
|
|
8
|
+
const attributesWithLists = new Set(['class', 'rel', 'ping']);
|
|
9
9
|
/** Collapse whitespaces inside list-like attributes (e.g. class, rel) */
|
|
10
|
+
|
|
11
|
+
exports.attributesWithLists = attributesWithLists;
|
|
12
|
+
|
|
10
13
|
function collapseAttributeWhitespace(tree) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
tree.walk(node => {
|
|
15
|
+
if (!node.attrs) {
|
|
16
|
+
return node;
|
|
17
|
+
}
|
|
15
18
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
if (!attributesWithLists.has(attrNameLower)) {
|
|
19
|
-
return;
|
|
20
|
-
}
|
|
19
|
+
Object.entries(node.attrs).forEach(([attrName, attrValue]) => {
|
|
20
|
+
const attrNameLower = attrName.toLowerCase();
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
if (!attributesWithLists.has(attrNameLower)) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
const newAttrValue = attrValue.replace(/\s+/g, ' ').trim();
|
|
27
|
+
node.attrs[attrName] = newAttrValue;
|
|
27
28
|
});
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
return node;
|
|
30
|
+
});
|
|
31
|
+
return tree;
|
|
30
32
|
}
|
|
@@ -1,56 +1,44 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
|
|
4
|
+
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = collapseBooleanAttributes;
|
|
7
7
|
// Source: https://github.com/kangax/html-minifier/issues/63
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
var amphtmlBooleanAttributes = new Set(['⚡', 'amp', '⚡4ads', 'amp4ads', '⚡4email', 'amp4email', 'amp-custom', 'amp-boilerplate', 'amp4ads-boilerplate', 'amp4email-boilerplate', 'allow-blocked-ranges', 'amp-access-hide', 'amp-access-template', 'amp-keyframes', 'animate', 'arrows', 'data-block-on-consent', 'data-enable-refresh', 'data-multi-size', 'date-template', 'disable-double-tap', 'disable-session-states', 'disableremoteplayback', 'dots', 'expand-single-section', 'expanded', 'fallback', 'first', 'fullscreen', 'inline', 'lightbox', 'noaudio', 'noautoplay', 'noloading', 'once', 'open-after-clear', 'open-after-select', 'open-button', 'placeholder', 'preload', 'reset-on-refresh', 'reset-on-resize', 'resizable', 'rotate-to-fullscreen', 'second', 'standalone', 'stereo', 'submit-error', 'submit-success', 'submitting', 'subscriptions-actions', 'subscriptions-dialog']);
|
|
8
|
+
const htmlBooleanAttributes = new Set(['allowfullscreen', 'allowpaymentrequest', 'allowtransparency', 'async', 'autofocus', 'autoplay', 'checked', 'compact', 'controls', 'declare', 'default', 'defaultchecked', 'defaultmuted', 'defaultselected', 'defer', 'disabled', 'enabled', 'formnovalidate', 'hidden', 'indeterminate', 'inert', 'ismap', 'itemscope', 'loop', 'multiple', 'muted', 'nohref', 'noresize', 'noshade', 'novalidate', 'nowrap', 'open', 'pauseonexit', 'readonly', 'required', 'reversed', 'scoped', 'seamless', 'selected', 'sortable', 'truespeed', 'typemustmatch', 'visible']);
|
|
9
|
+
const amphtmlBooleanAttributes = new Set(['⚡', 'amp', '⚡4ads', 'amp4ads', '⚡4email', 'amp4email', 'amp-custom', 'amp-boilerplate', 'amp4ads-boilerplate', 'amp4email-boilerplate', 'allow-blocked-ranges', 'amp-access-hide', 'amp-access-template', 'amp-keyframes', 'animate', 'arrows', 'data-block-on-consent', 'data-enable-refresh', 'data-multi-size', 'date-template', 'disable-double-tap', 'disable-session-states', 'disableremoteplayback', 'dots', 'expand-single-section', 'expanded', 'fallback', 'first', 'fullscreen', 'inline', 'lightbox', 'noaudio', 'noautoplay', 'noloading', 'once', 'open-after-clear', 'open-after-select', 'open-button', 'placeholder', 'preload', 'reset-on-refresh', 'reset-on-resize', 'resizable', 'rotate-to-fullscreen', 'second', 'standalone', 'stereo', 'submit-error', 'submit-success', 'submitting', 'subscriptions-actions', 'subscriptions-dialog']);
|
|
11
10
|
|
|
12
11
|
function collapseBooleanAttributes(tree, options, moduleOptions) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
} finally {
|
|
46
|
-
if (_didIteratorError) {
|
|
47
|
-
throw _iteratorError;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return node;
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
return tree;
|
|
12
|
+
tree.walk(node => {
|
|
13
|
+
if (!node.attrs) {
|
|
14
|
+
return node;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (!node.tag) {
|
|
18
|
+
return node;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
for (const attrName of Object.keys(node.attrs)) {
|
|
22
|
+
if (attrName === 'visible' && node.tag.startsWith('a-')) {
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (htmlBooleanAttributes.has(attrName)) {
|
|
27
|
+
node.attrs[attrName] = true;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (moduleOptions.amphtml && amphtmlBooleanAttributes.has(attrName) && node.attrs[attrName] === '') {
|
|
31
|
+
node.attrs[attrName] = true;
|
|
32
|
+
} // collapse crossorigin attributes
|
|
33
|
+
// Specification: https://html.spec.whatwg.org/multipage/urls-and-fetching.html#cors-settings-attributes
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
if (attrName.toLowerCase() === 'crossorigin' && (node.attrs[attrName] === 'anonymous' || node.attrs[attrName] === '')) {
|
|
37
|
+
node.attrs[attrName] = true;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return node;
|
|
42
|
+
});
|
|
43
|
+
return tree;
|
|
56
44
|
}
|
|
@@ -1,50 +1,76 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
|
|
4
|
+
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = collapseWhitespace;
|
|
7
7
|
|
|
8
|
-
var
|
|
9
|
-
|
|
10
|
-
var _normalizeHtmlWhitespace2 = _interopRequireDefault(_normalizeHtmlWhitespace);
|
|
11
|
-
|
|
12
|
-
var _helpers = require('../helpers');
|
|
13
|
-
|
|
14
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
-
|
|
16
|
-
var noWhitespaceCollapseElements = new Set(['script', 'style', 'pre', 'textarea']);
|
|
8
|
+
var _helpers = require("../helpers");
|
|
17
9
|
|
|
10
|
+
const noWhitespaceCollapseElements = new Set(['script', 'style', 'pre', 'textarea']);
|
|
11
|
+
const noTrimWhitespacesArroundElements = new Set([// non-empty tags that will maintain whitespace around them
|
|
12
|
+
'a', 'abbr', 'acronym', 'b', 'bdi', 'bdo', 'big', 'button', 'cite', 'code', 'del', 'dfn', 'em', 'font', 'i', 'ins', 'kbd', 'label', 'mark', 'math', 'nobr', 'object', 'q', 'rp', 'rt', 'rtc', 'ruby', 's', 'samp', 'select', 'small', 'span', 'strike', 'strong', 'sub', 'sup', 'svg', 'textarea', 'time', 'tt', 'u', 'var', // self-closing tags that will maintain whitespace around them
|
|
13
|
+
'comment', 'img', 'input', 'wbr']);
|
|
14
|
+
const noTrimWhitespacesInsideElements = new Set([// non-empty tags that will maintain whitespace within them
|
|
15
|
+
'a', 'abbr', 'acronym', 'b', 'big', 'del', 'em', 'font', 'i', 'ins', 'kbd', 'mark', 'nobr', 'rp', 's', 'samp', 'small', 'span', 'strike', 'strong', 'sub', 'sup', 'time', 'tt', 'u', 'var']);
|
|
16
|
+
const whitespacePattern = /\s+/g;
|
|
17
|
+
const NONE = '';
|
|
18
|
+
const SINGLE_SPACE = ' ';
|
|
19
|
+
const validOptions = ['all', 'aggressive', 'conservative'];
|
|
18
20
|
/** Collapses redundant whitespaces */
|
|
21
|
+
|
|
19
22
|
function collapseWhitespace(tree, options, collapseType, tag) {
|
|
20
|
-
|
|
21
|
-
|
|
23
|
+
collapseType = validOptions.includes(collapseType) ? collapseType : 'conservative';
|
|
24
|
+
tree.forEach((node, index) => {
|
|
25
|
+
if (typeof node === 'string') {
|
|
26
|
+
const prevNode = tree[index - 1];
|
|
27
|
+
const nextNode = tree[index + 1];
|
|
28
|
+
const prevNodeTag = prevNode && prevNode.tag;
|
|
29
|
+
const nextNodeTag = nextNode && nextNode.tag;
|
|
30
|
+
const isTopLevel = !tag || tag === 'html' || tag === 'head';
|
|
31
|
+
const shouldTrim = collapseType === 'all' || isTopLevel ||
|
|
32
|
+
/*
|
|
33
|
+
* When collapseType is set to 'aggressive', and the tag is not inside 'noTrimWhitespacesInsideElements'.
|
|
34
|
+
* the first & last space inside the tag will be trimmed
|
|
35
|
+
*/
|
|
36
|
+
collapseType === 'aggressive' && !noTrimWhitespacesInsideElements.has(tag);
|
|
37
|
+
node = collapseRedundantWhitespaces(node, collapseType, shouldTrim, tag, prevNodeTag, nextNodeTag);
|
|
22
38
|
}
|
|
23
39
|
|
|
24
|
-
|
|
25
|
-
if (typeof node === 'string' && !(0, _helpers.isComment)(node)) {
|
|
26
|
-
var isTopLevel = !tag || tag === 'html' || tag === 'head';
|
|
27
|
-
node = collapseRedundantWhitespaces(node, collapseType, isTopLevel);
|
|
28
|
-
}
|
|
40
|
+
const isAllowCollapseWhitespace = !noWhitespaceCollapseElements.has(node.tag);
|
|
29
41
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
tree[index] = node;
|
|
36
|
-
});
|
|
42
|
+
if (node.content && node.content.length && isAllowCollapseWhitespace) {
|
|
43
|
+
node.content = collapseWhitespace(node.content, options, collapseType, node.tag);
|
|
44
|
+
}
|
|
37
45
|
|
|
38
|
-
|
|
46
|
+
tree[index] = node;
|
|
47
|
+
});
|
|
48
|
+
return tree;
|
|
39
49
|
}
|
|
40
50
|
|
|
41
|
-
function collapseRedundantWhitespaces(text, collapseType) {
|
|
42
|
-
|
|
51
|
+
function collapseRedundantWhitespaces(text, collapseType, shouldTrim = false, currentTag, prevNodeTag, nextNodeTag) {
|
|
52
|
+
if (!text || text.length === 0) {
|
|
53
|
+
return NONE;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (!(0, _helpers.isComment)(text)) {
|
|
57
|
+
text = text.replace(whitespacePattern, SINGLE_SPACE);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (shouldTrim) {
|
|
61
|
+
if (collapseType === 'aggressive') {
|
|
62
|
+
if (!noTrimWhitespacesArroundElements.has(prevNodeTag)) {
|
|
63
|
+
text = text.trimStart();
|
|
64
|
+
}
|
|
43
65
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
66
|
+
if (!noTrimWhitespacesArroundElements.has(nextNodeTag)) {
|
|
67
|
+
text = text.trimEnd();
|
|
68
|
+
}
|
|
69
|
+
} else {
|
|
70
|
+
// collapseType is 'all', trim spaces
|
|
71
|
+
text = text.trim();
|
|
47
72
|
}
|
|
73
|
+
}
|
|
48
74
|
|
|
49
|
-
|
|
75
|
+
return text;
|
|
50
76
|
}
|
package/lib/modules/custom.js
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
|
|
4
|
+
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = custom;
|
|
7
|
+
|
|
7
8
|
/** Meta-module that runs custom modules */
|
|
8
9
|
function custom(tree, options, customModules) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
if (!Array.isArray(customModules)) {
|
|
14
|
-
customModules = [customModules];
|
|
15
|
-
}
|
|
10
|
+
if (!customModules) {
|
|
11
|
+
return tree;
|
|
12
|
+
}
|
|
16
13
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
14
|
+
if (!Array.isArray(customModules)) {
|
|
15
|
+
customModules = [customModules];
|
|
16
|
+
}
|
|
20
17
|
|
|
21
|
-
|
|
18
|
+
customModules.forEach(customModule => {
|
|
19
|
+
tree = customModule(tree, options);
|
|
20
|
+
});
|
|
21
|
+
return tree;
|
|
22
22
|
}
|
|
@@ -1,48 +1,46 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
|
|
4
|
+
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = collapseAttributeWhitespace;
|
|
7
7
|
|
|
8
|
-
var _collapseAttributeWhitespace = require(
|
|
8
|
+
var _collapseAttributeWhitespace = require("./collapseAttributeWhitespace");
|
|
9
9
|
|
|
10
10
|
/** Deduplicate values inside list-like attributes (e.g. class, rel) */
|
|
11
11
|
function collapseAttributeWhitespace(tree) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
tree.walk(node => {
|
|
13
|
+
if (!node.attrs) {
|
|
14
|
+
return node;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
Object.keys(node.attrs).forEach(attrName => {
|
|
18
|
+
const attrNameLower = attrName.toLowerCase();
|
|
19
|
+
|
|
20
|
+
if (!_collapseAttributeWhitespace.attributesWithLists.has(attrNameLower)) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const attrValues = node.attrs[attrName].split(/\s/);
|
|
25
|
+
const uniqeAttrValues = new Set();
|
|
26
|
+
const deduplicatedAttrValues = [];
|
|
27
|
+
attrValues.forEach(attrValue => {
|
|
28
|
+
if (!attrValue) {
|
|
29
|
+
// Keep whitespaces
|
|
30
|
+
deduplicatedAttrValues.push('');
|
|
31
|
+
return;
|
|
15
32
|
}
|
|
16
33
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
var attrValues = node.attrs[attrName].split(/\s/);
|
|
24
|
-
var uniqeAttrValues = new Set();
|
|
25
|
-
var deduplicatedAttrValues = [];
|
|
26
|
-
attrValues.forEach(function (attrValue) {
|
|
27
|
-
if (!attrValue) {
|
|
28
|
-
// Keep whitespaces
|
|
29
|
-
deduplicatedAttrValues.push('');
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (uniqeAttrValues.has(attrValue)) {
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
deduplicatedAttrValues.push(attrValue);
|
|
38
|
-
uniqeAttrValues.add(attrValue);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
node.attrs[attrName] = deduplicatedAttrValues.join(' ');
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
return node;
|
|
45
|
-
});
|
|
34
|
+
if (uniqeAttrValues.has(attrValue)) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
46
37
|
|
|
47
|
-
|
|
38
|
+
deduplicatedAttrValues.push(attrValue);
|
|
39
|
+
uniqeAttrValues.add(attrValue);
|
|
40
|
+
});
|
|
41
|
+
node.attrs[attrName] = deduplicatedAttrValues.join(' ');
|
|
42
|
+
});
|
|
43
|
+
return node;
|
|
44
|
+
});
|
|
45
|
+
return tree;
|
|
48
46
|
}
|
|
@@ -1,83 +1,63 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
|
|
4
|
+
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = mergeScripts;
|
|
7
|
+
|
|
7
8
|
/* Merge multiple <script> into one */
|
|
8
9
|
function mergeScripts(tree) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
10
|
+
let scriptNodesIndex = {};
|
|
11
|
+
let scriptSrcIndex = 1;
|
|
12
|
+
tree.match({
|
|
13
|
+
tag: 'script'
|
|
14
|
+
}, node => {
|
|
15
|
+
const nodeAttrs = node.attrs || {};
|
|
16
|
+
|
|
17
|
+
if (nodeAttrs.src) {
|
|
18
|
+
scriptSrcIndex++;
|
|
19
|
+
return node;
|
|
20
|
+
}
|
|
18
21
|
|
|
19
|
-
|
|
20
|
-
if (scriptType !== 'text/javascript' && scriptType !== 'application/javascript') {
|
|
21
|
-
return node;
|
|
22
|
-
}
|
|
22
|
+
const scriptType = nodeAttrs.type || 'text/javascript';
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
type: scriptType,
|
|
28
|
-
defer: nodeAttrs.defer !== undefined,
|
|
29
|
-
async: nodeAttrs.async !== undefined,
|
|
30
|
-
index: scriptSrcIndex
|
|
31
|
-
});
|
|
32
|
-
if (!scriptNodesIndex[scriptKey]) {
|
|
33
|
-
scriptNodesIndex[scriptKey] = [];
|
|
34
|
-
}
|
|
24
|
+
if (scriptType !== 'text/javascript' && scriptType !== 'application/javascript') {
|
|
25
|
+
return node;
|
|
26
|
+
}
|
|
35
27
|
|
|
36
|
-
|
|
37
|
-
|
|
28
|
+
const scriptKey = JSON.stringify({
|
|
29
|
+
id: nodeAttrs.id,
|
|
30
|
+
class: nodeAttrs.class,
|
|
31
|
+
type: scriptType,
|
|
32
|
+
defer: nodeAttrs.defer !== undefined,
|
|
33
|
+
async: nodeAttrs.async !== undefined,
|
|
34
|
+
index: scriptSrcIndex
|
|
38
35
|
});
|
|
39
36
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
scriptNodes.reverse().forEach(function (scriptNode) {
|
|
44
|
-
var scriptContent = (scriptNode.content || []).join(' ');
|
|
45
|
-
scriptContent = scriptContent.trim();
|
|
46
|
-
if (scriptContent.slice(-1) !== ';') {
|
|
47
|
-
scriptContent += ';';
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
lastScriptNode.content.unshift(scriptContent);
|
|
37
|
+
if (!scriptNodesIndex[scriptKey]) {
|
|
38
|
+
scriptNodesIndex[scriptKey] = [];
|
|
39
|
+
}
|
|
51
40
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
};
|
|
41
|
+
scriptNodesIndex[scriptKey].push(node);
|
|
42
|
+
return node;
|
|
43
|
+
});
|
|
56
44
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
45
|
+
for (const scriptNodes of Object.values(scriptNodesIndex)) {
|
|
46
|
+
let lastScriptNode = scriptNodes.pop();
|
|
47
|
+
scriptNodes.reverse().forEach(scriptNode => {
|
|
48
|
+
let scriptContent = (scriptNode.content || []).join(' ');
|
|
49
|
+
scriptContent = scriptContent.trim();
|
|
60
50
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
51
|
+
if (scriptContent.slice(-1) !== ';') {
|
|
52
|
+
scriptContent += ';';
|
|
53
|
+
}
|
|
64
54
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
try {
|
|
72
|
-
if (!_iteratorNormalCompletion && _iterator.return) {
|
|
73
|
-
_iterator.return();
|
|
74
|
-
}
|
|
75
|
-
} finally {
|
|
76
|
-
if (_didIteratorError) {
|
|
77
|
-
throw _iteratorError;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
55
|
+
lastScriptNode.content = lastScriptNode.content || [];
|
|
56
|
+
lastScriptNode.content.unshift(scriptContent);
|
|
57
|
+
scriptNode.tag = false;
|
|
58
|
+
scriptNode.content = [];
|
|
59
|
+
});
|
|
60
|
+
}
|
|
81
61
|
|
|
82
|
-
|
|
62
|
+
return tree;
|
|
83
63
|
}
|
|
@@ -1,41 +1,42 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
|
|
4
|
+
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = mergeStyles;
|
|
7
7
|
|
|
8
|
-
var _helpers = require(
|
|
8
|
+
var _helpers = require("../helpers");
|
|
9
9
|
|
|
10
10
|
/* Merge multiple <style> into one */
|
|
11
11
|
function mergeStyles(tree) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
12
|
+
const styleNodes = {};
|
|
13
|
+
tree.match({
|
|
14
|
+
tag: 'style'
|
|
15
|
+
}, node => {
|
|
16
|
+
const nodeAttrs = node.attrs || {}; // Skip <style scoped></style>
|
|
17
|
+
// https://developer.mozilla.org/en/docs/Web/HTML/Element/style
|
|
18
|
+
|
|
19
|
+
if (nodeAttrs.scoped !== undefined) {
|
|
20
|
+
return node;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if ((0, _helpers.isAmpBoilerplate)(node)) {
|
|
24
|
+
return node;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const styleType = nodeAttrs.type || 'text/css';
|
|
28
|
+
const styleMedia = nodeAttrs.media || 'all';
|
|
29
|
+
const styleKey = styleType + '_' + styleMedia;
|
|
30
|
+
|
|
31
|
+
if (styleNodes[styleKey]) {
|
|
32
|
+
const styleContent = (node.content || []).join(' ');
|
|
33
|
+
styleNodes[styleKey].content.push(' ' + styleContent);
|
|
34
|
+
return '';
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
node.content = node.content || [];
|
|
38
|
+
styleNodes[styleKey] = node;
|
|
39
|
+
return node;
|
|
40
|
+
});
|
|
41
|
+
return tree;
|
|
41
42
|
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = minifyConditionalComments;
|
|
7
|
+
|
|
8
|
+
var _htmlnano = _interopRequireDefault(require("../htmlnano"));
|
|
9
|
+
|
|
10
|
+
var _helpers = require("../helpers");
|
|
11
|
+
|
|
12
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
|
+
|
|
14
|
+
// Spec: https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/compatibility/ms537512(v=vs.85)
|
|
15
|
+
const CONDITIONAL_COMMENT_REGEXP = /(<!--\[if\s+?[^<>[\]]+?]>)([\s\S]+?)(<!\[endif\]-->)/gm;
|
|
16
|
+
/** Minify content inside conditional comments */
|
|
17
|
+
|
|
18
|
+
async function minifyConditionalComments(tree, htmlnanoOptions) {
|
|
19
|
+
// forEach, tree.walk, tree.match just don't support Promise.
|
|
20
|
+
for (let i = 0, len = tree.length; i < len; i++) {
|
|
21
|
+
const node = tree[i];
|
|
22
|
+
|
|
23
|
+
if (typeof node === 'string' && (0, _helpers.isConditionalComment)(node)) {
|
|
24
|
+
tree[i] = await minifycontentInsideConditionalComments(node, htmlnanoOptions);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (node.content && node.content.length) {
|
|
28
|
+
tree[i].content = await minifyConditionalComments(node.content, htmlnanoOptions);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return tree;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function minifycontentInsideConditionalComments(text, htmlnanoOptions) {
|
|
36
|
+
let match;
|
|
37
|
+
const matches = []; // FIXME!
|
|
38
|
+
// String#matchAll is supported since Node.js 12
|
|
39
|
+
|
|
40
|
+
while ((match = CONDITIONAL_COMMENT_REGEXP.exec(text)) !== null) {
|
|
41
|
+
matches.push([match[1], match[2], match[3]]);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (!matches.length) {
|
|
45
|
+
return Promise.resolve(text);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return Promise.all(matches.map(async match => {
|
|
49
|
+
const result = await _htmlnano.default.process(match[1], htmlnanoOptions, {}, {});
|
|
50
|
+
let minified = result.html;
|
|
51
|
+
|
|
52
|
+
if (match[1].includes('<html') && minified.includes('</html>')) {
|
|
53
|
+
minified = minified.replace('</html>', '');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return match[0] + minified + match[2];
|
|
57
|
+
}));
|
|
58
|
+
}
|