33 * @Author: nguyen
44 * @Date: 2020-02-12 14:01:01
55 * @Last Modified by: Alex Dong
6- * @Last Modified time: 2020-05-21 14:43:02
6+ * @Last Modified time: 2020-05-21 15:07:46
77 */
88
99namespace Magepow \SpeedOptimizer \Plugin ;
@@ -113,7 +113,7 @@ public function beforeSendResponse(Http $response)
113113 } else {
114114 $ body = preg_replace_callback (
115115 '~<\s*\bscript\b[^>]*>(.*?)<\s*\/\s*script\s*>~is ' ,
116- function ($ match ) use ( $ pattern , & $ scripts ) {
116+ function ($ match ){
117117 $ scriptId = trim ($ match [1 ], ' ' );
118118 if ($ scriptId && isset ($ this ->scripts [$ scriptId ])){
119119 return $ this ->scripts [$ scriptId ];
@@ -287,26 +287,15 @@ public function addLazyLoadJs( $content, $selector='img', $exclude='.loaded' )
287287 return $ this ->addToBottomBody ($ content , $ script );
288288 }
289289
290- public function processExcludeJs ($ content , $ minify =true , $ deferJs =true )
290+ public function processExcludeJs ($ content , $ minify =false , $ deferJs =false )
291291 {
292- $ regex = '~//?\s*\*[\s\S]*?\*\s*//?~ ' ; // RegEx to remove /** */ and // ** **// php comments
293292 $ content = preg_replace_callback (
294293 '~<\s*\bscript\b[^>]*>(.*?)<\s*\/\s*script\s*>~is ' ,
295294 function ($ match ) use ($ minify , $ deferJs ){
296295 // if(stripos($match[0], 'type="text/x-magento') !== false) return $match[0];
297296 $ scriptId = 'script_ ' . uniqid ();
298297 if ($ minify && trim ($ match [1 ], ' ' )){
299- $ search = array (
300- '/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:| \\\| \')\/\/.*))/ ' ,
301- '/(\s)+/s ' , // shorten multiple whitespace sequences
302- );
303-
304- $ replace = array (
305- '' ,
306- '\\1 ' ,
307- );
308-
309- $ this ->scripts [$ scriptId ] = preg_replace ($ search , $ replace , $ match [0 ]);
298+ $ this ->scripts [$ scriptId ] = $ this ->minifyJs ( $ match [0 ] );
310299 }else {
311300 $ this ->scripts [$ scriptId ] = $ match [0 ];
312301 }
@@ -319,6 +308,21 @@ function($match) use($minify, $deferJs){
319308 return $ content ;
320309 }
321310
311+ public function minifyJs ($ script )
312+ {
313+ $ regex = '~//?\s*\*[\s\S]*?\*\s*//?~ ' ; // RegEx to remove /** */ and // ** **// php comments
314+ $ search = array (
315+ '/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:| \\\| \')\/\/.*))/ ' ,
316+ '/(\s)+/s ' , // shorten multiple whitespace sequences
317+ );
318+
319+ $ replace = array (
320+ '' ,
321+ '\\1 ' ,
322+ );
323+ return preg_replace ($ search , $ replace , $ script );
324+ }
325+
322326 public function minifyHtml ($ content )
323327 {
324328 $ search = array (
0 commit comments