Giter Club home page Giter Club logo

minifier's Introduction

Minifier

Asset minification and versioning library for CodeIgniter 4.

Installation via composer

> composer require michalsn/minifier

Manual installation

Download this repo and then enable it by editing app/Config/Autoload.php and adding the Michalsn\Minifier namespace to the $psr4 array. For example, if you copied it into app/ThirdParty:

$psr4 = [
    'Config'      => APPPATH . 'Config',
    APP_NAMESPACE => APPPATH,
    'App'         => APPPATH,
    'Michalsn\Minifier' => APPPATH . 'ThirdParty/minifier/src',
];

Configuration

Run command:

> php spark minify:publish

This command will copy a config file to your app namespace. Then you can adjust it to your needs. By default file will be present in app/Config/Minifier.php.

You should define an array of files that you want to minify, ie:

public $js = [
    'all.min.js' => [
        'jquery-3.2.1.min.js', 'bootstrap-3.3.7.min.js', 'main.js',
    ]
];

or

public $css = [
    'all.min.css' => [
        'bootstrap-3.3.7.min.css', 'font-awesome-4.7.0.min.css', 'main.css',
    ]
];

This way requesting for a all.min.js or all.min.css file will return a minified and combined version of all files in a given array.

Usage

To actually minify all the files we have to run command:

> php spark minify:all

This will prepare everything and will set up a versioning. Make sure to load a minifier helper in your controller, by calling:

helper('minifier');

Now to generate a proper tag with desired file to load, you have to make a simple call in your code:

minifier('all.min.js');

or

minifier('all.min.css');

This will produce:

<script type="text/javascript" src="http://localhost/assets/js/all.min.js?v=9ef881911da8d7c4a1c2f19c4878d122"></script> 

or

<link rel="stylesheet" href="http://localhost/assets/css/all.min.css?v=50a35b0b1d1c3798aa556b8245314930">

Config options

After running command:

> php spark minify:publish

You can modify the config file which by default is copied to the path app/Config/Minifier.php. Here are your options with a description.

Variable Default value Options Desctiption
$minify true true, false Use this variable to turn on and off minification of the assets. Turning off can be useful during app development - for easy debugging.
$baseUrl null Use this variable when you want to set absolute path to the asset files. If no other URLs are set, like $baseJsUrl or $baseCssUrl then values set to $dirJS and $dirCss will be added to the final URL.
$baseJsUrl null Use this variable when your JS assets are served from subdomain. Bare in mind that in this case variable $dirJs won't be added to the URL.
$baseCssUrl null Use this variable when your CSS assets are served from subdomain. Bare in mind that in this case variable $dirCSS won't be added to the URL.
$adapterJs \Michalsn\Minifier\Adapters\Js\MinifyAdapter::class Adapter to use for minifying JS files. You can also implement your own JS adapter to minify assets and replace this class.
$adapterCss \Michalsn\Minifier\Adapters\Css\MinifyAdapter::class Adapter to use for minifying CSS files. You can also implement your own CSS adapter to minify assets and replace this class.
$dirJs ./assets/js JS assets directory.
$dirCss ./assets/css CSS assets directory.
$dirVersion ./assets Directory to store assets versioning.
$tagJs <script type="text/javascript" src="%s"></script> JS tag to use in HTML when displaying JS assets.
$tagCss <link rel="stylesheet" href="%s"> CSS tag to use in HTML when displaying CSS assets.
$returnType html html, json, array Determines how the files will be returned. The dafault value is html and it uses the $tagJs and $tagCss variables. Using array will return the php array and json type will return a json string.
$js This array defines JS files to minify.
$css This array defines CSS files to minify.

License

The MIT License (MIT). Please see License File for more information.

minifier's People

Contributors

michalsn avatar paul45 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.