devfactory/minify

一个用于laravel 5压缩样式和javascript的包

2.0.1 2024-03-18 13:09 UTC

This package is auto-updated.

Last update: 2024-09-18 14:05:37 UTC


README

Build Status Latest Stable Version Total Downloads License

使用此包,您可以压缩现有的样式表和JavaScript文件以用于Laravel 10。此过程可能有点困难,但此包简化并自动化了此过程。

对于Laravel 5 - 9,请使用此包的1.x版本。

对于Laravel 4,请使用 ceesvanegmond/minify

安装

首先通过Composer安装此包。

{
    "require": {
        "devfactory/minify": "^2.0"
    }
}

安装包后,自动注册了MinifyServiceProviderMinify外观。您可以在应用程序的任何地方使用Minify外观。

发布配置文件

php artisan vendor:publish --provider="Devfactory\Minify\MinifyServiceProvider" --tag="config"

升级到v2

Minify版本2仅适用于PHP 8.1+和Laravel 10+

所需升级更改

如果实现了Devfactory\Minify\Contracts\MinifyInterface接口,请确保根据更新的类型和异常更新您的实现。

如果使用了Devfactory\Minify\Providers\BaseProvider抽象类,请确保根据更新的类型和异常更新您的类。

方法Devfactory\Minify\Providers\StyleSheet#urlCorrection已被重命名为Devfactory\Minify\Providers\StyleSheet#getFileContentWithCorrectedUrls

将配置文件minify.config.php重命名为minify.php

用法

样式表

// app/views/hello.blade.php

<html>
    <head>
        ...
        {!! Minify::stylesheet('/css/main.css') !!}
        // or by passing multiple files
        {!! Minify::stylesheet(['/css/main.css', '/css/bootstrap.css']) !!}
        // add custom attributes
        {!! Minify::stylesheet(['/css/main.css', '/css/bootstrap.css'], ['foo' => 'bar']) !!}
        // add full uri of the resource
        {!! Minify::stylesheet(['/css/main.css', '/css/bootstrap.css'])->withFullUrl() !!}
        {!! Minify::stylesheet(['//fonts.googleapis.com/css?family=Roboto']) !!}

        // minify and combine all stylesheet files in given folder
        {!! Minify::stylesheetDir('/css/') !!}
        // add custom attributes to minify and combine all stylesheet files in given folder
        {!! Minify::stylesheetDir('/css/', ['foo' => 'bar', 'defer' => true]) !!}
        // minify and combine all stylesheet files in given folder with full uri
        {!! Minify::stylesheetDir('/css/')->withFullUrl() !!}
    </head>
    ...
</html>

JavaScript

// app/views/hello.blade.php

<html>
    <body>
    ...
    </body>
    {!! Minify::javascript('/js/jquery.js') !!}
    // or by passing multiple files
    {!! Minify::javascript(['/js/jquery.js', '/js/jquery-ui.js']) !!}
    // add custom attributes
    {!! Minify::javascript(['/js/jquery.js', '/js/jquery-ui.js'], ['bar' => 'baz']) !!}
    // add full uri of the resource
    {!! Minify::javascript(['/js/jquery.js', '/js/jquery-ui.js'])->withFullUrl() !!}
    {!! Minify::javascript(['//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js']) !!}

    // minify and combine all javascript files in given folder
    {!! Minify::javascriptDir('/js/') !!}
    // add custom attributes to minify and combine all javascript files in given folder
    {!! Minify::javascriptDir('/js/', ['bar' => 'baz', 'async' => true]) !!}
    // minify and combine all javascript files in given folder with full uri
    {!! Minify::javascriptDir('/js/')->withFullUrl() !!}
</html>