@small-tools/html 1.2.0 → 1.3.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/dist/api/minify-html.d.ts +4 -0
- package/dist/api/minify-html.js +25 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const minifyHtml = (html) => {
|
|
2
|
+
const preservedTagContentRegExp = /<(pre|code|textarea|script|style)(?:[\s\S]*?)<\/\1>/gi;
|
|
3
|
+
const preservedAttributeContentRegExp = /\s+(?:title|alt|placeholder|value|style|srcdoc|data-.+?|aria-.+?|on.+?)\s*=\s*(?:"(?:[\s\S]*?)")/gi;
|
|
4
|
+
const preservedTagContents = [];
|
|
5
|
+
const preservedAttributesContents = [];
|
|
6
|
+
return html
|
|
7
|
+
.replaceAll(preservedTagContentRegExp, (match) => {
|
|
8
|
+
preservedTagContents.push(match);
|
|
9
|
+
return `___PRESERVED_TAG_${preservedTagContents.length - 1}___`;
|
|
10
|
+
}) // Preserve tag contents
|
|
11
|
+
.replaceAll(preservedAttributeContentRegExp, (match) => {
|
|
12
|
+
preservedAttributesContents.push(match.trim());
|
|
13
|
+
return `___PRESERVED_ATTRIBUTE_${preservedAttributesContents.length - 1}___`;
|
|
14
|
+
}) // Preserve attribute contents
|
|
15
|
+
.replaceAll(/<!--.*?-->/gs, "") // Remove comments
|
|
16
|
+
.replaceAll(/\n|\r|\t/g, "") // Remove tabs and new lines
|
|
17
|
+
.replaceAll(/\s{2,}/g, " ") // Remove multiple spaces
|
|
18
|
+
.replaceAll(/>\s+</g, "><") // Remove spaces between tags
|
|
19
|
+
.replaceAll(/\s+>/g, ">") // Remove spaces before closing tags
|
|
20
|
+
.replaceAll(/<\s+/g, "<") // Remove spaces before opening tags
|
|
21
|
+
.replaceAll(/\s+=\s+/g, "=") // Remove spaces around =
|
|
22
|
+
.replaceAll(/___PRESERVED_TAG_(\d+)___/g, (match, index) => preservedTagContents[index] ?? match)
|
|
23
|
+
.replaceAll(/___PRESERVED_ATTRIBUTE_(\d+)___/g, (_match, index) => ` ${preservedAttributesContents[index]}`);
|
|
24
|
+
};
|
|
25
|
+
export { minifyHtml };
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED