/** * Internal function for (maybe) minifying assets * * @param Type of asset to minify (css/js) * @param Contents to be minified * @param The name of the file being minified (used for logging) * @param mixed A relative path to use, if provided (for css minification) * @return String (maybe) minified contents of file */ protected function _minify($type, $contents, $filename, $rel = FALSE) { // used in case we need to return orig $contents_orig = $contents; switch ($type) { case 'js': /** * JS pre-minify hook */ if ($this->EE->extensions->active_hook('minimee_pre_minify_js')) { Minimee_helper::log('Hook `minimee_pre_minify_js` has been activated.', 3); // pass contents to be minified, and instance of self $contents = $this->EE->extensions->call('minimee_pre_minify_js', $contents, $filename, $this); if ($this->EE->extensions->end_script === TRUE) { return $contents; } // re-set $contents_orig in case we need to return $contents_orig = $contents; } // HOOK END // be sure we want to minify if ($this->config->is_yes('minify_js')) { // See if JSMinPlus was explicitly requested if ($this->config->js_library == 'jsminplus') { Minimee_helper::log('Running minification with JSMinPlus.', 3); Minimee_helper::library('jsminplus'); $contents = JSMinPlus::minify($contents); } else { if ($this->config->js_library == 'jsmin') { Minimee_helper::log('Running minification with JSMin.', 3); Minimee_helper::library('jsmin'); $contents = JSMin::minify($contents); } } } break; case 'css': /** * CSS pre-minify hook */ if ($this->EE->extensions->active_hook('minimee_pre_minify_css')) { Minimee_helper::log('Hook `minimee_pre_minify_css` has been activated.', 3); // pass contents to be minified, relative path, and instance of self $contents = $this->EE->extensions->call('minimee_pre_minify_css', $contents, $filename, $rel, $this); if ($this->EE->extensions->end_script === TRUE) { return $contents; } // copy to $contents_orig in case we need to return $contents_orig = $contents; } // HOOK END // prepend URL if relative path exists & configured to do so if ($rel !== FALSE && $this->config->is_yes('css_prepend_mode')) { Minimee_helper::library('css_urirewriter'); $contents = Minify_CSS_UriRewriter::prepend($contents, $rel . '/'); // copy to $contents_orig in case we need to return $contents_orig = $contents; } // minify if configured to do so if ($this->config->is_yes('minify_css')) { // See if CSSMin was explicitly requested if ($this->config->css_library == 'cssmin') { Minimee_helper::log('Running minification with CSSMin.', 3); Minimee_helper::library('cssmin'); $cssmin = new CSSmin(FALSE); $contents = $cssmin->run($contents); unset($cssmin); } else { if ($this->config->css_library == 'minify') { Minimee_helper::log('Running minification with Minify_CSS.', 3); Minimee_helper::library('minify'); $contents = Minify_CSS::minify($contents); } } } break; } // calculate weight loss $before = strlen($contents_orig); $after = strlen($contents); $diff = $before - $after; // quick check that contents are not empty if ($after == 0) { Minimee_helper::log('Minification has returned an empty string for `' . $filename . '`.', 2); } // did we actually reduce our file size? It's possible an already minified asset // uses a more aggressive algorithm than Minify; in that case, keep original contents if ($diff > 0) { $diff_formatted = $diff < 100 ? $diff . 'b' : round($diff / 1000, 2) . 'kb'; $change = round($diff / $before * 100, 2); Minimee_helper::log('Minification has reduced ' . $filename . ' by ' . $diff_formatted . ' (' . $change . '%).', 3); // add to our running total $this->diff_total($diff); } else { Minimee_helper::log('Minification unable to reduce ' . $filename . ', so using original content.', 3); $contents = $contents_orig; } // cleanup (leave some smaller variables because they may or may not have ever been set) unset($contents_orig); // return our (maybe) minified contents return $contents; }
/** * Method for template_post_parse hook * * @param string Parsed template string * @param bool Whether is a sub-template or not * @param string Site ID * @return string Template string, possibly minified */ public function template_post_parse($template, $sub, $site_id) { // play nice with others if (isset($this->EE->extensions->last_call) && $this->EE->extensions->last_call) { $template = $this->EE->extensions->last_call; } // do nothing if not final template if ($sub !== FALSE) { return $template; } // see if we need to post-render any plugin methods if (isset($this->cache['template_post_parse'])) { if (!class_exists('Minimee')) { include_once PATH_THIRD . 'minimee/pi.minimee.php'; } // create a new instance of Minimee each time to guarantee defaults $m = new Minimee(); // save our TMPL values to put back into place once finished $tagparams = $this->EE->TMPL->tagparams; // loop through & call each method foreach ($this->cache['template_post_parse'] as $needle => $tag) { Minimee_helper::log('Calling Minimee::display("' . $tag['method'] . '") during template_post_parse: ' . serialize($tag['tagparams']), 3); $this->EE->TMPL->tagparams = $tag['tagparams']; // our second parameter tells Minimee we are calling from template_post_parse $out = $m->display($tag['method'], TRUE); // replace our needle with output $template = str_replace(LD . $needle . RD, $out, $template); // reset Minimee for next loop $m->reset(); } // put things back into place $this->EE->TMPL->tagparams = $tagparams; } // do nothing if not (likely) html! if (!preg_match('/webpage|static/i', $this->EE->TMPL->template_type)) { return $template; } // Are we configured to run through HTML minifier? if ($this->config->is_no('minify_html')) { Minimee_helper::log('HTML minification is disabled.', 3); return $template; } // is Minimee nonetheless disabled? if ($this->config->is_yes('disable')) { Minimee_helper::log('HTML minification aborted because Minimee has been disabled completely.', 3); return $template; } // we've made it this far, so... Minimee_helper::log('Running HTML minification.', 3); Minimee_helper::library('html'); // run css & js minification? $opts = array(); if ($this->config->is_yes('minify_css')) { $opts['cssMinifier'] = array('Minify_CSS', 'minify'); } if ($this->config->is_yes('minify_js')) { $opts['jsMinifier'] = array('JSMin', 'minify'); } return Minify_HTML::minify($template, $opts); }