Skip to content

Asset Minifier for Symfony Asset Mapper - Made by SensioLabs

License

Notifications You must be signed in to change notification settings

sensiolabs/minify-bundle

Repository files navigation

SensioLabs Minify Bundle

SensioLabs Minify Bundle for Symfony
composer require sensiolabs/minify-bundle

PHP Version CI Release Packagist Downloads License

Minify integration

SensioLabs Minify Bundle integrates Minify into Symfony Asset Mapper.

Asset Minifier

✅ Minify CSS and JS files, remove whitespace, comments, and more..

🌍🌍 Reduces the size of your assets by up to 70% (see metrics below).

🚀🚀🚀 Improves the loading time of your website, and the user experience.

Asset Mapper

🎯 Automatically minify assets during the build process.

📦📦 Stores minified assets in the Symfony cache.

🌿🌿🌿 Download the Minify binary automatically from Github.

Minification

JavaScript

Asset Before After Diff Compression Time
Autocomplete.js 20 kB 9.2 kB -54% ⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩 8 ms
Bootstrap.js 145 kB 62 kB -57% ⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩 10 ms
Video.js 2.3 MB 0.7 MB -71% ⬜️⬜️⬜️⬜️⬜️⬜️🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩 42 ms
w3c.org js 44 kB 19 kB -57% ⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩 6 ms

CSS

Asset Before After Diff Compression Time
Autocomplete.css 3.1 kB 2.5 kB -19% ⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️🟩🟩🟩🟩🟩 2 ms
Bootstrap.css 281 kB 232 kB -18% ⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️🟩🟩🟩🟩 9 ms
Video-js.css 53 kB 47 kB -12% ⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜⬜️⬜️⬜️⬜️⬜️⬜️⬜️️🟩🟩 4 ms
w3c.org css 111 kB 70 kB -37% ⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️⬜️🟩🟩🟩🟩🟩🟩🟩🟩 5 ms

Installation

Make sure Composer is installed globally, as explained in the installation chapter of the Composer documentation.

With Symfony Flex

Open a command console, enter your project directory and execute:

composer require sensiolabs/minify-bundle

Without Symfony Flex

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

composer require sensiolabs/minify-bundle

Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles in the config/bundles.php file of your project:

// config/bundles.php

return [
    // ...
    Sensiolabs\MinifyBundle\SensiolabsMinifyBundle::class => ['all' => true],
];

Depending on your deployment process, you might want to enable the bundle only in the desired environment(s).

Usage

If you use AssetMapper, run the following command to minify all the assets:

php bin/console asset-map:compile

This command is usually run when serving assets in production and the SensioLabs Minify Bundle will hook into it to minify all assets while copying them.

Command Line

You can also minify assets manually with the command line. First, make sure that the binary file used to minify assets is properly installed in your computer:

php bin/console minify:install

Then, run the following command to minify assets:

# this outputs the result in the console
php bin/console minify:assets css/main.css

# this will write the output into the 'main.min.css' file
# (the given output file is created / overwritten if needed)
php bin/console minify:assets css/main.css css/main.min.css

Configuration

AssetMapper Settings

# config/packages/sensiolabs_minify.yaml
sensiolabs_minify:
    asset_mapper:
        # you can minify only CSS files, only JS files or both
        types:
            css: true # (default: true)
            js:  true # (default: true)

        # a list of assets to exclude from minfication (default: [])
        # the values of the list can be any shell wildcard patterns
        ignore_paths:
            - 'admin/*'
            - '*.min.js'

        # whethere to exclude the assets stored in vendor/ from minification;
        # these assets are usually already minified, so it's common to ignore them
        ignore_vendor: true # (default: true)

Minify Binary

The minification is performed by a binary file that can be installed on your computer/server or downloaded automatically by the bundle. This is the default configuration used by the bundle:

# config/packages/sensiolabs_minify.yaml
sensiolabs_minify:
    # ...

    minify:
        # this disables the usage of local binaries
        local_binary: false

        # if TRUE, the bundle will download the binary from GitHub
        download_binary: '%kernel.debug%'

        # the local path where the downloaded binary is stored
        download_directory: '%kernel.project_dir%/var/minify'

You can customize this configuration to use a local binary:

# config/packages/sensiolabs_minify.yaml
sensiolabs_minify:
    # ...

    minify:
        # set it to 'auto' to let the bundle try to find the location of the binary
        local_binary: 'auto'

        #  you can also define the path to the binary explicitly, but this won't work
        # if you run the application in multime servers with different binary locations
        local_binary: "/usr/local/sbin/minify"

Credits

Authors

Acknowledgments

This bundle is inspired by the following projects:

Contributors

Special thanks to the Symfony community for their contributions and feedback.

License

The SensioLabs Minify Bundle is released under the MIT license.