Bläddra i källkod

moved minifying code into htmlnano

Gildas 7 år sedan
förälder
incheckning
db5ddd4fbd

+ 11 - 0
lib/single-file/htmlnano.js

@@ -135,6 +135,9 @@ this.htmlnano = this.htmlnano || (() => {
 					previousNode.remove();
 				}
 			}
+		},
+		postProcess: doc => {
+			removeEmptyInlineElements(doc);
 		}
 	};
 
@@ -212,4 +215,12 @@ this.htmlnano = this.htmlnano || (() => {
 		}
 	}
 
+	function removeEmptyInlineElements(doc) {
+		doc.querySelectorAll("style, script").forEach(element => {
+			if (!element.textContent.trim()) {
+				element.remove();
+			}
+		});
+	}
+
 })();

+ 4 - 1
lib/single-file/single-file-browser.js

@@ -95,7 +95,10 @@ this.SingleFile = this.SingleFile || (() => {
 				serialize: () => getDoctype(doc) + doc.documentElement.outerHTML,
 				parseSrcset: srcset => parseSrcset.process(srcset),
 				uglifycss: (content, options) => uglifycss.processString(content, options),
-				htmlnano: doc => htmlnano.process(doc)
+				htmlnano: {
+					process: doc => htmlnano.process(doc),
+					postProcess: doc => htmlnano.postProcess(doc),
+				}
 			};
 		}
 	}

+ 9 - 11
lib/single-file/single-file-core.js

@@ -148,7 +148,9 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 			if (!this.options.removeImports) {
 				await this.processor.htmlImports();
 			}
-			this.processor.removeEmptyInlineElements();
+			if (this.options.compressHTML) {
+				this.processor.compressHTML(true);
+			}
 			this.processor.removeBase();
 		}
 
@@ -311,14 +313,6 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 			}
 		}
 
-		removeEmptyInlineElements() {
-			this.doc.querySelectorAll("style, script").forEach(element => {
-				if (element.textContent.trim() == "") {
-					element.remove();
-				}
-			});
-		}
-
 		removeBase() {
 			this.doc.querySelectorAll("base").forEach(element => element.remove());
 		}
@@ -379,8 +373,12 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 			this.doc.querySelectorAll("[" + REMOVED_CONTENT_ATTRIBUTE_NAME + "]").forEach(element => element.remove());
 		}
 
-		compressHTML() {
-			this.dom.htmlnano(this.doc);
+		compressHTML(postProcess) {
+			if (postProcess) {
+				this.dom.htmlnano.postProcess(this.doc);
+			} else {
+				this.dom.htmlnano.process(this.doc);
+			}
 		}
 
 		insertSingleFileCommentNode() {