File modules/util/ui/minifier/JSMinifier.class.php
Last commit: Sun Oct 13 13:17:32 2024 +0200 Jan Dankert New Minifier for CSS and JS: Leave JS Linebreaks as they are (for better debugging); Exploded CSS files (instead of a combined one), LESS is necessary but should be avoided in the future.
1 <?php 2 3 namespace util\ui\minifier; 4 5 class JSMinifier extends AbstractMinifier 6 { 7 const FLAG_IMPORT_MIN_JS = 1; 8 const FLAG_REMOVE_MULTILINE_COMMENT = 2; 9 const FLAG_REMOVE_BLANK_LINES = 4; 10 const FLAG_REMOVE_LINE_COMMENTS = 8; 11 const FLAG_REMOVE_WHITESPACE = 16; 12 const FLAG_REMOVE_LINEBREAK = 32; 13 const FLAG_REMOVE_TABS = 64; 14 const FLAG_REMOVE_INDENTING_SPACES = 128; 15 16 17 public function linkToSourcemap($sourcemap) 18 { 19 return "# sourceMappingURL=$sourcemap"; 20 } 21 22 protected function compress($chars) 23 { 24 $sourceLine = 1; 25 $sourcePos = 1; 26 $buffer = ''; 27 while( true ) { 28 if ( empty( $chars ) ) 29 break; 30 31 $char = array_shift( $chars ); 32 33 if ($char == '/') { 34 $nextChar = array_shift( $chars ); 35 if ( $nextChar == '/' && $this->hasFlag(self::FLAG_REMOVE_LINE_COMMENTS )) { 36 // single-line comment 37 while(true) { 38 $nextChar = array_shift( $chars ); 39 if ( $nextChar == "\n" ) { 40 array_unshift($chars,$nextChar); 41 $sourceLine++; 42 break; 43 } 44 } 45 } 46 elseif ( $nextChar == '*' && $this->hasFlag( self::FLAG_REMOVE_MULTILINE_COMMENT)) { 47 // multi-line comment 48 while(true) { 49 $nextChar = array_shift( $chars ); 50 if ( $nextChar == "\n" ) { 51 if ( ! $this->hasFlag(self::FLAG_REMOVE_LINEBREAK )) 52 $buffer .= $nextChar; // leave line-break as it is. 53 $sourceLine++; 54 } 55 elseif ( $nextChar == "*" ) { 56 $nextChar = array_shift( $chars ); 57 if ( $nextChar == "/" ) 58 break; 59 else 60 // it's not the end of the multiline comment. 61 array_unshift($chars,$nextChar); 62 } 63 } 64 } 65 else { 66 // it's not a comment. 67 array_unshift($chars,$nextChar); 68 $buffer .= $char; 69 } 70 } 71 elseif ($char == "\t" && $this->hasFlag(self::FLAG_REMOVE_TABS)) { 72 ; 73 } 74 elseif ($char == "\r" && $this->hasFlag(self::FLAG_REMOVE_LINEBREAK)) { 75 ; 76 } 77 elseif ($char == "\n" && $this->hasFlag(self::FLAG_REMOVE_LINEBREAK)) { 78 ; 79 } else { 80 $buffer .= $char; 81 } 82 83 // Strip indenting space. 84 if ( $char == "\n" && $this->hasFlag( self::FLAG_REMOVE_INDENTING_SPACES )) { 85 while(true) { 86 $nextChar = array_shift($chars); 87 if ($nextChar == " ") { 88 ; // it's a blank, so we want to remove it. 89 } 90 else { 91 array_unshift($chars,$nextChar); 92 break; 93 } 94 95 } 96 } 97 98 } 99 100 // Change the link in import statements, it should point to the minified version 101 if ( $this->hasFlag( self::FLAG_IMPORT_MIN_JS )) 102 $buffer = preg_replace('/^(import\s+\S+\s+from\s+"?\'?\S+)\.js/m','${1}.min.js',$buffer); 103 $this->addCompressedContent($buffer); 104 } 105 }
Downloadmodules/util/ui/minifier/JSMinifier.class.php
History Sun, 13 Oct 2024 13:17:32 +0200 Jan Dankert New Minifier for CSS and JS: Leave JS Linebreaks as they are (for better debugging); Exploded CSS files (instead of a combined one), LESS is necessary but should be avoided in the future.