jysperu / helpers-compress
v1.0.2
2023-09-05 23:36 UTC
Requires
- php: ^8.1
- matthiasmullie/minify: ^1.3
- symfony/cache: ^6.3
README
通过composer安装
composer require jysperu/helpers-compress
提供静态函数以压缩Html/CSS/JS/JSON内容
## Comprimir un contenido HTML
JCompressor::html(string $buffer): string;
## Comprimir un contenido CSS
JCompressor::css(string $content, bool $use_toptal = false, int|null $cache_time = null, string|null $cache_key = null): string;
## Comprimir un contenido JS
JCompressor::js(string $content, bool $use_toptal = false, int|null $cache_time = null, string|null $cache_key = null): string;
## Comprimir un contenido JSON
JCompressor::json(string|array $content): string;
使用简单
示例 1
本地和快速地压缩CSS内容
require_once 'vendor/autoload.php';
$css = '
p {
color : red;
}
';
$minified = JCompressor::css($css); // p{color:red}
示例 2
简单且无缓存的压缩JS内容
require_once 'vendor/autoload.php';
$js = '
(function (factory) {
if (typeof define === "function" && define.amd) {
// AMD. Register as an anonymous module.
define(["jquery"], factory);
} else if (typeof module === "object" && module.exports) {
// Node/CommonJS
module.exports = function (root, jQuery) {
if (jQuery === undefined) {
// require("jQuery") returns a factory that requires window to
// build a jQuery instance, we normalize how we use modules
// that require this pattern but the window provided is a noop
// if it"s defined (how jquery works)
if (typeof window !== "undefined") {
jQuery = require("jquery");
} else {
jQuery = require("jquery")(root);
}
}
factory(jQuery);
return jQuery;
};
} else {
// Browser globals
factory(jQuery);
}
}(function ($) {
$.fn.api = function () {
return "";
};
}));
';
$minified = JCompressor::js($js);
/* // Resultado:
(function(factory){if(typeof define==="function"&&define.amd){define(["jquery"],factory)}else if(typeof module==="object"&&module.exports){module.exports=function(root,jQuery){if(jQuery===undefined){if(typeof window!=="undefined"){jQuery=require("jquery")}else{jQuery=require("jquery")(root)}}
factory(jQuery);return jQuery}}else{factory(jQuery)}}(function($){$.fn.api=function(){return""}}))
*/
示例 3
使用Toptal平台更全面地压缩JS内容
require_once 'vendor/autoload.php';
$js = '
(function (factory) {
if (typeof define === "function" && define.amd) {
// AMD. Register as an anonymous module.
define(["jquery"], factory);
} else if (typeof module === "object" && module.exports) {
// Node/CommonJS
module.exports = function (root, jQuery) {
if (jQuery === undefined) {
// require("jQuery") returns a factory that requires window to
// build a jQuery instance, we normalize how we use modules
// that require this pattern but the window provided is a noop
// if it"s defined (how jquery works)
if (typeof window !== "undefined") {
jQuery = require("jquery");
} else {
jQuery = require("jquery")(root);
}
}
factory(jQuery);
return jQuery;
};
} else {
// Browser globals
factory(jQuery);
}
}(function ($) {
$.fn.api = function () {
return "";
};
}));
';
$minified = JCompressor::js($js, true);
/* // Valor del $minified:
!function(n){"function"==typeof define&&define.amd?define(["jquery"],n):"object"==typeof module&&module.exports?module.exports=function(e,t){return void 0===t&&(t="undefined"!=typeof window?require("jquery"):require("jquery")(e)),n(t),t}:n(jQuery)}(function(n){n.fn.api=function(){return""}});
*/
示例 4
使用Toptal平台更全面地压缩JS内容,并通过缓存存储以避免多次查询的延迟
require_once 'vendor/autoload.php';
$js = '
(function (factory) {
if (typeof define === "function" && define.amd) {
// AMD. Register as an anonymous module.
define(["jquery"], factory);
} else if (typeof module === "object" && module.exports) {
// Node/CommonJS
module.exports = function (root, jQuery) {
if (jQuery === undefined) {
// require("jQuery") returns a factory that requires window to
// build a jQuery instance, we normalize how we use modules
// that require this pattern but the window provided is a noop
// if it"s defined (how jquery works)
if (typeof window !== "undefined") {
jQuery = require("jquery");
} else {
jQuery = require("jquery")(root);
}
}
factory(jQuery);
return jQuery;
};
} else {
// Browser globals
factory(jQuery);
}
}(function ($) {
$.fn.api = function () {
return "";
};
}));
';
## Primera ejecución (enviará el contenido a toptal para comprimirse y guarda el resultado en cache)
$minified = JCompressor::js($js, true, 3600); // time: 0.66719508171082
## segunda ejecución (retornará la cache previamente guardada)
$minified = JCompressor::js($js, true, 3600); // time: 0.062622785568237
## tercera ejecución (retornará la cache previamente guardada y cargada)
$minified = JCompressor::js($js, true, 3600); // time: 0.00041890144348145