function smarty_out_jsmin_callback($matches) { // 如果是 script src, 則不動 if (preg_match("/src=/i", $matches[1])) { return sprintf("<script%s>%s</script>", $matches[1], $matches[2]); } if (preg_match("/x\\-jquery\\-tmpl/", $matches[1])) { return sprintf("<script%s>%s</script>", $matches[1], str_replace("\n", "", $matches[2])); } // 剩下的 minify 一下 return "<script>" . Minifier::minify($matches[2]) . "</script>\n"; }
protected function jsParser() { require_once 'include/jssource/Minifier.php'; return Minifier::minify($this->text); }
/** * Minimize css files * * @return void */ public function minimize($id_theme, $name) { $msg = null; // check permission $msg = AdmUtils_helper::chk_priv_level($_SESSION['xuid'], 'themes', $id_theme, 4); if (is_null($msg)) { $qs = X4Route_core::get_query_string(); // do action $res = 1; // get the templates in the theme $mod = new Theme_model(); // CSS section $path = PATH . 'themes/' . $name . '/css/'; $items = $mod->get_css($id_theme); foreach ($items as $i) { if (file_exists($path . $i->css . '.css')) { $txt = file_get_contents($path . $i->css . '.css'); $txt = $mod->compress_css($txt); $chk = file_put_contents($path . $i->css . '.min.css', $txt); if (!$chk) { $res = 0; } } } // JS section X4Core_core::auto_load('jshrink_library'); $path = PATH . 'themes/' . $name . '/js/'; $items = $mod->get_js($id_theme); foreach ($items as $i) { if (file_exists($path . $i->js . '.js')) { $txt = file_get_contents($path . $i->js . '.js'); $txt = Minifier::minify($txt, array('flaggedComments' => false)); $chk = file_put_contents($path . $i->js . '.min.js', $txt); if (!$chk) { $res = 0; } } } $result = array(0, $res); // set message $this->dict->get_words(); $msg = AdmUtils_helper::set_msg($result); // set update if ($result[1]) { $msg->update[] = array('element' => $qs['div'], 'url' => urldecode($qs['url']), 'title' => null); } } $this->response($msg); }
function minify($type, $files, $excludes) { if (!in_array($type, array('css', 'js'))) { return $files; } //extract local fylesystem path clean_file_paths($files, $excludes); global $registry; $oc_root = dirname(DIR_APPLICATION); $cache = NULL; $cachefile = NULL; $filename = NULL; if (!defined('DS')) { define('DS', DIRECTORY_SEPARATOR); } if (!file_exists($oc_root . DS . 'assets')) { mkdir($oc_root . DS . 'assets'); } if (!file_exists($oc_root . DS . 'assets' . DS . $type)) { mkdir($oc_root . DS . 'assets' . DS . $type); } $cachefile = $oc_root . DS . 'assets' . DS . $type . DS . getSSLCachePrefix() . $type . '.cache'; if (!file_exists($cachefile)) { touch($cachefile); file_put_contents($cachefile, json_encode(array())); } $cache = json_decode(file_get_contents($cachefile), true); switch ($type) { case 'js': include_once NITRO_LIB_FOLDER . 'minifier' . DS . 'JSShrink.php'; break; case 'css': if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == '1')) { $webshopUrl = $registry->get('config')->get('config_ssl'); } else { $webshopUrl = $registry->get('config')->get('config_url'); } $webshopUrl = rtrim(preg_replace('~^https?\\:~i', '', $webshopUrl), '/'); include_once NITRO_LIB_FOLDER . 'minifier' . DS . 'CSSMin.php'; break; } foreach ($files as $hash => $file) { $recache = false; if (!is_excluded_file($file, $excludes)) { $filename = $oc_root . DS . trim(str_replace('/', DS, $file), DS); $basefilename = basename($file, '.' . $type); $target = '/assets/' . $type . '/' . getSSLCachePrefix() . 'nitro-mini-' . encode_filename($filename) . '.' . $type; $targetAbsolutePath = $oc_root . DS . trim(str_replace('/', DS, $target), DS); if (!empty($cache[$filename])) { if (!is_url($file) && $cache[$filename] != filemtime($filename)) { $recache = true; } } else { $recache = true; } if ($recache || !file_exists($targetAbsolutePath)) { touch($targetAbsolutePath); $content = is_url($file) ? fetchRemoteContent($file) : file_get_contents($filename); switch ($type) { case 'js': $content = Minifier::minify($content, array('flaggedComments' => false)); break; case 'css': $urlToCurrentDir = is_url($file) ? dirname($file) : $webshopUrl . dirname('/' . trim($file, '/')); $content = preg_replace('/(url\\()(?![\'\\"]?(?:(?:https?\\:\\/\\/)|(?:data\\:)|(?:\\/)))([\'\\"]?)\\/?(?!\\/)/', '$1$2' . $urlToCurrentDir . '/', $content); $content = Nitro_Minify_CSS_Compressor::process($content); break; } file_put_contents($targetAbsolutePath, $content); $cache[$filename] = is_url($file) ? time() : filemtime($filename); } $files[$hash] = trim($target, '/'); } } file_put_contents($cachefile, json_encode($cache)); return $files; }
public function renderJsCritical() { $config = $this->getPresenter()->context->parameters['scriptLoader']['js']; if (!$this->getPresenter()->context->parameters['scriptLoader']['enable']) { foreach ($config['critical'] as $js) { echo '<script src="/' . $js . '"></script>'; } } else { $cache = new Cache($this->getPresenter()->storage, 'scriptLoader'); $jsFile = $cache->load('javascript-critical'); if (is_null($jsFile)) { //zminimalizuju $jsFile = ''; $jsFiles = array(); foreach ($config['critical'] as $js) { $jsFile .= Minifier::minify(file_get_contents($this->getPresenter()->context->parameters['wwwDir'] . '/' . $js), array('flaggedComments' => false)); $jsFiles[] = $this->getPresenter()->context->parameters['wwwDir'] . '/' . $js; } $cache->save('javascript-critical', $jsFile, array(Cache::FILES => $jsFiles)); } echo '<script type="text/javascript">' . $jsFile . '</script>'; } }
function ler_css($path, $string) { preg_match_all('/@import"(?P<arquivo>[^"]*)";/i', $string, $math); if (count($math['arquivo']) > 0) { foreach ($math['arquivo'] as $subarquivo) { $subarray = explode('.', $subarquivo); $subext_arquivo = array_pop($subarray); $subnome_arquivo = implode('.', $subarray); $subarquivo_min = $path . '/' . $subnome_arquivo . '.min.' . $subext_arquivo; $string2 = Minifier::minify(file_get_contents($path . '/' . $subarquivo)); file_put_contents($subarquivo_min, $string2); $string = preg_replace('/@import"' . $subarquivo . '";/', $string2, $string); } $string = $this->ler_css($path, $string); } preg_match_all('/@import url\\("(?P<arquivo>[^"]*)"\\);/i', $string, $math); if (count($math['arquivo']) > 0) { foreach ($math['arquivo'] as $subarquivo) { $subarray = explode('.', $subarquivo); $subext_arquivo = array_pop($subarray); $subnome_arquivo = implode('.', $subarray); $subarquivo_min = $path . '/' . $subnome_arquivo . '.min.' . $subext_arquivo; $string2 = Minifier::minify(file_get_contents($path . '/' . $subarquivo)); file_put_contents($subarquivo_min, $string2); $string = preg_replace('/@import url\\("' . $subarquivo . '"\\);/', $string2, $string); } $string = $this->ler_css($path, $string); } return $string; }
public function minify($code) { //---------------------------------------------------------- //init var //---------------------------------------------------------- $chk = array("bool" => true, 'traceID' => "minJs"); //---------------------------------------------------------- if (strpos($this->fileName, "js") !== false) { $content = Minifier::minify($code); } else { if (strpos($this->fileName, "css") !== false) { $content = CssMin::minify($code); } else { if (strpos($this->fileName, "html") !== false) { $content = $code; } } } //---------------------------------------------------------- $chk['result'] = $content; //---------------------------------------------------------- if (is_null($fileLocation)) { $chk['message'] = "code has been successfully minified!!!!!"; } //---------------------------------------------------------- $chk = CreateFile_v0::go($this->fileName, $content, $this->fileLocation, true); //---------------------------------------------------------- Minifier_v0::createDependantFiles($content); //---------------------------------------------------------- return $chk; }
require 'class.magic-min.php'; //Default usage will echo from function calls and leave images untouched $minified = new Minifier(); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries --> <!--[if lt IE 9]> <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> <![endif]--> <title>Carousel Template for Bootstrap</title> <link href="<?php $minified->minify('./stylesheets/bootstrap.css'); ?> " rel="stylesheet" /> <link href="<?php $minified->minify('./stylesheets/carousel.css'); ?> " rel="stylesheet" /> <link href="<?php $minified->minify('./stylesheets/hello.css'); ?> " rel="stylesheet" /> <link href="<?php $minified->minify('./stylesheets/navbar.css'); ?> " rel="stylesheet" />
function minify_js($js) { include 'support/Minifier.php'; return Minifier::minify($js); }
/* <!--Output a new merged stylesheet with only the $included_styles included in order--> <?php */ $included_styles = array('css/bootstrap.css', 'https://raw.github.com/zurb/reveal/master/reveal.css', 'css/base/jquery-ui.css'); //readfile( 'css/bootstrap.css'); ?> <link rel="stylesheet" href="<?php ?> <?php/* $minified->merge( 'css/awesome.min.css','css', $included_styles ); */?>" /> <!--Output a default minified stylesheet: will output as css/bootstrap.min.css--> <link rel="stylesheet" href="<?php $minified->minify('css/bootstrap.css', 'css/bootstrap-mino.css'); ?> " /> <?php /* <!--Output a minified javascript file with completely different name, and ?v=1.8 param--> <script src="<?php $minified->minify( 'js/jquery.reveal.js', 'js/jquery-magicreveal.min.js', '1.8' ); ?>"></script> <!--Retrieve the contents of all javascript files in the /js directory as master.min.js (excluding a couple AND making sure bootstrap and validate are first and second)--> /* $exclude = array( 'js/autogrow.js', 'js/jquery.reveal.js'