Giter Club home page Giter Club logo

laravel-minify's Introduction

Laravel Minify

Laravel Minify is a package for minifying and obfuscating Javascript, CSS, HTML and Blade template files. It runs automatically when you load a page or view. This package can minify entire response and also can minify blade at compile time.

Latest Stable Version Total Downloads License StyleCI

Examples:

HTML

  • Before Minify
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Laravel Minify</title>
    <style>
        h2 {
            color: red;
            background-color: yellow;
        }
    </style>
</head>
<body>
    <h2>Laravel Minify</h2>
    <p>Laravel Minify is a package for minifying and obfuscating Javascript, CSS, HTML and Blade template files.</p>
    <script>
        function helloWorld() {
            alert('Hello World');
        }
    </script>
</body>
</html>
  • After Minify
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Laravel Minify</title><style>h2{color:red;;background-color:yellow;}</style></head><body><h2>Laravel Minify</h2><p>Laravel Minify is a package for minifying and obfuscating Javascript, CSS, HTML and Blade template files.</p><script>eval(((_,__,___,____,_____,______,_______)=>{______[___](x=>_______[__](String[____](x)));return _______[_](_____)})('join','push','forEach','fromCharCode','',[102,117,110,99,116,105,111,110,32,104,101,108,108,111,87,111,114,108,100,40,41,123,97,108,101,114,116,40,39,72,101,108,108,111,32,87,111,114,108,100,39,41,125],[]));</script></body></html>

Javascript Obfuscate

  • Before Obfuscate
function helloWorld() {
    const number1 = 5;
    const number2 = 10;
    alert('Hello World! ' + (number1 + number2));
}
  • After Obfuscate
eval(((_,__,___,____,_____,______,_______)=>{______[___](x=>_______[__](String[____](x)));return _______[_](_____)})('join','push','forEach','fromCharCode','',[102,117,110,99,116,105,111,110,32,104,101,108,108,111,87,111,114,108,100,40,41,123,99,111,110,115,116,32,110,117,109,98,101,114,49,61,53,59,99,111,110,115,116,32,110,117,109,98,101,114,50,61,49,48,59,97,108,101,114,116,40,39,72,101,108,108,111,32,87,111,114,108,100,33,32,39,43,40,110,117,109,98,101,114,49,43,110,117,109,98,101,114,50,41,41,125],[]));

Installation

Laravel Minify requires PHP 7.2 or higher. This particular version supports Laravel 6.x, 7.x, 8.x, 9.x, and 10.x only.

To get the latest version, simply require the project using Composer:

$ composer require fahlisaputra/laravel-minify

Configuration

Laravel Minify 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

In order Laravel Minify 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 html
    \Fahlisaputra\Minify\Middleware\MinifyCss::class,
    // Middleware to minify css
    \Fahlisaputra\Minify\Middleware\MinifyJavascript::class,
    // Middleware to minify javascript
    \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.

Usage

This is how you can use Laravel Minify in your project.

Enable Minify

You can enable minify by setting minify to true in the config/minify.php file. For example:

"enabled" => env("MINFY_ENABLED", true),

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("MINFY_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

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),
],

Please note: this feature is still experimental. It may not work properly and may cause errors to your javascript or css.

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'
],

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

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.