Răsfoiți Sursa

fix performance issue (see #843)

Gildas 4 ani în urmă
părinte
comite
ce34d3f8c6
1 a modificat fișierele cu 20 adăugiri și 15 ștergeri
  1. 20 15
      lib/single-file/vendor/css-minifier.js

+ 20 - 15
lib/single-file/vendor/css-minifier.js

@@ -368,7 +368,8 @@ function collectComments(content, comments) {
  */
 
 // const REGEXP_EMPTY_RULES = /[^};{/]+\{\}/g;
-const REGEXP_PRESERVE_STRING = /("([^\\"]|\\.|\\)*")|('([^\\']|\\.|\\)*')/g;
+const REGEXP_PRESERVE_STRING = /"([^\\"]|\\.|\\)*"/g;
+const REGEXP_PRESERVE_STRING2 = /'([^\\']|\\.|\\)*'/g;
 const REGEXP_MINIFY_ALPHA = /progid:DXImageTransform.Microsoft.Alpha\(Opacity=/gi;
 const REGEXP_PRESERVE_TOKEN1 = /\r\n/g;
 const REGEXP_PRESERVE_TOKEN2 = /[\r\n]/g;
@@ -444,21 +445,25 @@ function processString(content = "", options = defaultOptions) {
 	content = collectComments(content, comments);
 
 	// preserve strings so their content doesn't get accidentally minified
-	pattern = REGEXP_PRESERVE_STRING;
-	content = content.replace(pattern, token => {
-		const quote = token.substring(0, 1);
-		token = token.slice(1, -1);
-		// maybe the string contains a comment-like substring or more? put'em back then
-		if (token.indexOf("___PRESERVE_CANDIDATE_COMMENT_") >= 0) {
-			for (let i = 0, len = comments.length; i < len; i += 1) {
-				token = token.replace("___PRESERVE_CANDIDATE_COMMENT_" + i + "___", comments[i]);
+	preserveString(REGEXP_PRESERVE_STRING);
+	preserveString(REGEXP_PRESERVE_STRING2);
+
+	function preserveString(pattern) {
+		content = content.replace(pattern, token => {
+			const quote = token.substring(0, 1);
+			token = token.slice(1, -1);
+			// maybe the string contains a comment-like substring or more? put'em back then
+			if (token.indexOf("___PRESERVE_CANDIDATE_COMMENT_") >= 0) {
+				for (let i = 0, len = comments.length; i < len; i += 1) {
+					token = token.replace("___PRESERVE_CANDIDATE_COMMENT_" + i + "___", comments[i]);
+				}
 			}
-		}
-		// minify alpha opacity in filter strings
-		token = token.replace(REGEXP_MINIFY_ALPHA, "alpha(opacity=");
-		preservedTokens.push(token);
-		return quote + ___PRESERVED_TOKEN_ + (preservedTokens.length - 1) + "___" + quote;
-	});
+			// minify alpha opacity in filter strings
+			token = token.replace(REGEXP_MINIFY_ALPHA, "alpha(opacity=");
+			preservedTokens.push(token);
+			return quote + ___PRESERVED_TOKEN_ + (preservedTokens.length - 1) + "___" + quote;
+		});
+	}
 
 	// strings are safe, now wrestle the comments
 	for (let i = 0, len = comments.length; i < len; i += 1) {