djdigitalemedien/laravel-minify
Composer 安装命令:
composer require djdigitalemedien/laravel-minify
包简介
Minify your blade views, html, css and js files on the fly for Laravel 6, 7, 8, 9 and 10
README 文档
README
Fork of https://github.com/fahlisaputra/laravel-minify
Installation
Minify for Laravel requires PHP 7.2 or higher. This particular version supports Laravel 8.x, 9.x, 10.x, and 11.x.
To get the latest version, simply require the project using Composer:
composer require djdigitalemedien/laravel-minify
Configuration
Minify for Laravel supports optional configuration. To get started, you'll need to publish all vendor assets:
php artisan vendor:publish --provider="djdigitalemedien\Minify\MinifyServiceProvider"
This will create a config/minify.php file in your app that you can modify to set your configuration. Also, make sure you check for changes to the original config file in this package between releases.
Register the Middleware (Laravel 10 or older)
In order Minify for Laravel can intercept your request to minify and obfuscate, you need to add the Minify middleware to the app/Http/Kernel.php file:
protected $middleware = [ .... // Middleware to minify CSS \djdigitalemedien\Minify\Middleware\MinifyCss::class, // Middleware to minify Javascript \djdigitalemedien\Minify\Middleware\MinifyJavascript::class, // Middleware to minify Blade \djdigitalemedien\Minify\Middleware\MinifyHtml::class, ];
You can choose which middleware you want to use. Put all of them if you want to minify html, css, and javascript at the same time.
Register the Middleware (Laravel 11 or newer)
In order Minify for Laravel can intercept your request to minify and obfuscate, you need to add the Minify middleware to the bootstrap/app.php file:
->withMiddleware(function (Middleware $middleware) { $middleware->web(append: [ \djdigitalemedien\Minify\Middleware\MinifyHtml::class, \djdigitalemedien\Minify\Middleware\MinifyCss::class, \djdigitalemedien\Minify\Middleware\MinifyJavascript::class, ]); })
Usage
This is how you can use Minify for Laravel in your project.
Minify Asset Files
You must set true on assets_enabled in the config/minify.php file to minify your asset files. For example:
"assets_enabled" => env("MINIFY_ASSETS_ENABLED", true),
You can minify your asset files by using the minify() helper function. This function will minify your asset files and return the minify designed route. In order to work properly, you need to put your asset files in the resources/js or resources/css directory. For example:
<link rel="stylesheet" href="{{ minify('/css/test.css') }}">
where test.css is located in the resources/css directory.
<script src="{{ minify('/js/test.js') }}"></script>
where test.js is located in the resources/js directory.
Automatic Insert Semicolon on Javascript or CSS
Use this option if Minify for Laravel makes your javascript or css not working properly. You can enable automatic insert semicolon on javascript or css by setting true on insert_semicolon in the config/minify.php file. For example:
"insert_semicolon" => [ 'css' => env("MINIFY_CSS_SEMICOLON", true), 'js' => env("MINIFY_JS_SEMICOLON", true), ],
Caution: this option is experimental. If the code still not working properly, you can disable this option and add semicolon manually to your Javascript or CSS code.
Skip Minify on Blade
You can skip minify on blade by using attribute ignore--minify inside script or style tag. For example:
<style ignore--minify> /* css */ </style> <script ignore--minify> /* javascript */ </script>
Skip Minify when Rendering View
You can skip minify when rendering view by passing ignore_minify = true in the view data. For example:
return view('welcome', ['ignore_minify' => true]);
Skip Minify by Route
You can skip minify by route by adding the route name to the ignore array in the config/minify.php file. For example:
"ignore" => [ '/admin' ],
Custom Directives Replacement
You can replace custom directives by adding the directive name to the directives array in the config/minify.php file. For example in AlpineJS you can write @click="function()". Unfortunately, Minify for Laravel will remove the @ symbol. You can replace it by adding @ => x-on: to the directives array. For example:
"directives" => [ '@' => 'x-on:', ],
Keep Directives
You can keep directives by adding the directive name to the keep_directives array in the config/minify.php file. For example when you use @vite, you can add @vite to the keep_directives array. For example:
"keep_directives" => [ '@vite' ],
Known Issues
- Minify for Laravel will remove the
@symbol in the blade file. This will make the blade directive not working properly. You can fix this by adding@ => x-on:to thedirectivesarray in theconfig/minify.phpfile. - Does not support for some Javascript framework. You can try experiment by changing the
insert_semicolonoption totrueorfalsein theconfig/minify.phpfile.
Contributing
If you find an issue, or have a better way to do something, feel free to open an issue, or a pull request. The package is far from perfect, and any help is welcome. There are no formal contribution guidelines, and there should be no contribution too small. All coding styles will be fixed during the pull request by StyleCI. So, don't worry too much about the code style. We'd love to hear from you!
Thanks
Big thanks to the people who have contributed to this package:
License
Laravel Minify is licensed under the MIT license.
Support
If you are having general issues with this package, feel free to contact us on saputra@fahli.net
Report Vulnerability
Please read our security policy for more details.
djdigitalemedien/laravel-minify 适用场景与选型建议
djdigitalemedien/laravel-minify 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 10 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「html-minifier」 「css-minifier」 「js-minifier」 「js-obfuscate」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 djdigitalemedien/laravel-minify 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 djdigitalemedien/laravel-minify 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 djdigitalemedien/laravel-minify 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Minify your blade views, html, css and obfuscate js files on the fly. Lightweight minifier for your Laravel project.
Minify your blade views, html, css and obfuscate js files on the fly. Lightweight minifier for your Laravel project. Laravel 13 compatible version.
A simple PHP wrapper for Uglify JS and Uglify CSS
Minify your blade views, html, css and js files on the fly for Laravel 6, 7, 8, 9 and 10
Alfabank REST API integration
Laravel package for Accurate Online API integration.
统计信息
- 总下载量: 13
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-10-11