定制 jysperu/helpers-compress 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jysperu/helpers-compress

最新稳定版本:v1.0.2

Composer 安装命令:

composer require jysperu/helpers-compress

包简介

Funciones comprimir

README 文档

README

Instalación via composer

composer require jysperu/helpers-compress

Funciones estáticas para comprimir contenidos 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;

Uso simple

Ejemplo 1:

Minificar contenido CSS de forma local y rápida

require_once 'vendor/autoload.php';

$css = '
p {
    color : red;
}
';

$minified = JCompressor::css($css); // p{color:red}

Ejemplo 2:

Minificar contenido JS de forma simple y sin cache

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""}}))
*/

Ejemplo 3:

Minificar contenido JS de forma mas completa utilizando el portal Toptal

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""}});
*/

Ejemplo 4:

Minificar contenido JS de forma mas completa utilizando el portal Toptal y almacenaje en cache para evitar lentitud en posibles consultas multiples

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

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2023-09-05