Code Monkey home page Code Monkey logo

laravel-minify's Introduction

Logo

Minify for Laravel

Minify for Laravel is a package for minifying and obfuscating Javascript, CSS, HTML and Blade views. It runs automatically when you load a page or view. Increase your website performance on page load and save bandwidth. Obfuscate your Javascript to protect your code from being stolen.

Latest Stable Version Total Downloads License StyleCI

Comparison

This image shows the difference in size between the original file and the minified file of default welcome.blade.php Laravel. The original file size is 28.7 KB and the minified file size is 25.7 KB. The minified file size is 10% smaller than the original file size.

Logo

If you minify all your asset files, you can save up to 50% of your bandwidth. This will make your website load faster and save your hosting cost. When you have big files, the difference in size will be even greater.

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 fahlisaputra/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="Fahlisaputra\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
    \Fahlisaputra\Minify\Middleware\MinifyCss::class,
    // Middleware to minify Javascript
    \Fahlisaputra\Minify\Middleware\MinifyJavascript::class,
    // Middleware to minify Blade
    \Fahlisaputra\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: [
        \Fahlisaputra\Minify\Middleware\MinifyHtml::class,
        \Fahlisaputra\Minify\Middleware\MinifyCss::class,
        \Fahlisaputra\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 the directives array in the config/minify.php file.
  • Does not support for some Javascript framework. You can try experiment by changing the insert_semicolon option to true or false in the config/minify.php file.

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 [email protected]

Report Vulnerability

Please read our security policy for more details.

laravel-minify's People

Contributors

fahlisaputra avatar stylecibot avatar

Stargazers

Gustavo Novaro avatar Tim Slager avatar Osman Gökmen avatar Aditya Rizqi avatar M. Vugteveen avatar INFOBRAND DEV avatar  avatar Fabeni avatar Mutlaq Aldhbuiub avatar Ephraïm SEDDOR avatar Torch avatar asterism612 avatar Development Here Ltd. avatar G. Giani avatar Hamidreza avatar Jorge Thomas - Akrista avatar Reynold Putra Merdeka avatar bil avatar Adam avatar Paulo R Lima avatar Scott Bedard avatar Silverhand avatar  avatar Khaled Breaker avatar Alan avatar Hakkı Konu avatar  avatar sd avatar Rikaz Zarook avatar Sky avatar  avatar Ali Abdian avatar Enes Bayraktar avatar Faruk Çakal avatar Çağlar Çağlayan avatar Elieser Ribeiro avatar Corneille avatar Wahyu Ade Pratama avatar  avatar  avatar  avatar

Watchers

 avatar Rikaz Zarook avatar

laravel-minify's Issues

Cache for minified files

Hi, I've been using this package and so far it works nicely, but I don't see any option/config for caching the already minified files. Using devtools in Chrome I can see the css and js filles being downloaded on every request, instead of coming directly from disk cache (like images and such)

There's this assignment talking about implementing caching: #2 are there any news about it?

Thanks in advance!

Assets UTF-8

It does not recognize special Spanish characters á, é, í, ó, ú, ñ, etc.

<script src="/{{ minify('/js/layouts/example.js') }}" charset="UTF-8"></script>

All attributes starting with"@" are removed after minification.

The following Alipine code:
<button @click="alert('test')">Test</button>

After minification becomes:
<button>Test</button>
This applies to all attributes starting with @

We have to use its equivalent to be able to have it:
<button x-on:click="alert('test')">Test</button>

"@" is shortened version of "x-on"

REGEX_VALID_HTML failing due to Catastrophic Backtracking

Had to dig to find why this package wasn't running.

Turns out the following function was returning false due to Catastrophic Backtracking

    protected function validHtml(string $value): bool
    {
        return (bool) preg_match(self::REGEX_VALID_HTML, $value);
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.