Skip to content

slav123/CodeIgniter-minify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CodeIgniter - minify Build Status

Simple CodeIgniter library to compress CSS and JavaScript files on the fly.

Library is based on few other scripts like http://code.google.com/p/minify/ or https://code.google.com/p/cssmin to minify CSS and it uses Google Closure compiler to compress JavaScript

Installation

Just put Minify.php file in libraries path, and create minify.php config file on config directory.

Using the library

Configure the library:

All directories needs to be writable. Next you can set your own values for config file.

// enable/disable library (default value: 'TRUE')
$config['enabled'] = TRUE;

// output path where the compiled files will be stored (default value: 'assets')
$config['assets_dir'] = 'assets';

// optional - path where the compiled css files will be stored (default value: '' - for backward compatibility)
$config['assets_dir_css'] = ''; 

// optional - path where the compiled js files will be stored (default value: '' - for backward compatibility)
$config['assets_dir_js'] = '';     

// optional - handy when your assets are in a different domain than main website (default value: '')
$config['base_url'] = '';

// where to look for css files (default value: 'assets/css')
$config['css_dir'] = 'assets/css';

// where to look for js files (default value: 'assets/js')
$config['js_dir'] = 'assets/js';

// default file name for css (default value: 'style.css')
$config['css_file'] = 'styles.css';

// default file name for js (default value: 'scripts.js')
$config['js_file'] = 'scripts.js';

// default tag for css (default value: '<link href="%s" rel="stylesheet" type="text/css" />')
$config['css_tag'] = '<link href="%s" rel="stylesheet" type="text/css" />';

// default tag for js (default value: '<script type="text/javascript" src="%s"></script>')
$config['js_tag'] = '<script type="text/javascript" src="%s"></script>';

// use html tags on output and return as a string (default value: 'TRUE')
// if html_tags === FALSE - array with links to assets is returned
$config['html_tags'] = TRUE;

// use automatic file names (default value: 'FALSE')
$config['auto_names'] = FALSE;

// use to enable versioning your assets (default value: 'FALSE')
$config['versioning'] = FALSE;

// automatically deploy when there are any changes in files (default value: 'TRUE')
$config['deploy_on_change'] = TRUE;

// compress files or not (default value: 'TRUE')
$config['compress'] = TRUE;

// compression engine setting (default values: 'minify' and 'closurecompiler')
$config['compression_engine'] = array(
	'css' => 'minify', // minify || cssmin
	'js'  => 'closurecompiler' // closurecompiler || jsmin || jsminplus
);

// when you use closurecompiler as compression engine you can choose compression level (default value: 'SIMPLE_OPTIMIZATIONS')
// avaliable options: "WHITESPACE_ONLY", "SIMPLE_OPTIMIZATIONS" or "ADVANCED_OPTIMIZATIONS"
$config['closurecompiler']['compilation_level'] = 'SIMPLE_OPTIMIZATIONS';

Available engines

  • CSS - minify or cssmin - both of them are local, just try out which one is better for you,
  • JS - closurecompiler makes API call to external server, it's slower then regular inline engine, but it's super efficient with compression, jsmin and jsminplus are local

Run the library

In the controller:

// load the library
$this->load->library('minify');
// or load and assign custom config (will override values from config file)
$this->load->library('minify', $config);
// by default library's functionality is enabled, but in some cases you would like to return
// assets without compilation and compression - when debugging or in development environment
// in that case you can use config variable to disable it
$config['enabled'] = FALSE;
// or
$this->minify->enabled = FALSE;

In controller or view:

// set css files - you can use array or string with commas
// when using this method, you replaces previously added files
$this->minify->css(array('reset.css', 'style.css', 'tinybox.css'));
$this->minify->css('reset.css, style.css, tinybox.css');

// add css files - you can use array or string with commas
// when using this method, you're adding new files to previous ones
$this->minify->add_css(array('reset.css'))->add_css('style.css, tinybox.css');

// set js files - you can use array or string with commas
// when using this method, you replaces previously added files
$this->minify->js(array('html5.js', 'main.js'));
$this->minify->js('html5.js, main.js');

// set js files - you can use array or string with commas
// when using this method, you're adding new files to previous ones
$this->minify->add_js(array('html5.js'))->add_js('main.js');

// with methods: css(), js(), add_css() and add_js()
// you can pass group name for given files as second parameter
// default group name is "default"
$this->minify->js(array('html5.js', 'main.js'), 'extra');
$this->minify->add_css('style.css, tinybox.css', 'another');

// deploy css
// bool argument for rebuild css - false means skip rebuilding (default value: TRUE) 
echo $this->minify->deploy_css(TRUE);

//Output: '<link href="path-to-compiled-css" rel="stylesheet" type="text/css" />'

// deploy js
// bool argument for rebuild js  - false means skip rebuilding (default value: FALSE)
echo $this->minify->deploy_js(); 

//Output: '<script type="text/javascript" src="path-to-compiled-js"></script>'.

// you can use automatic file name for particular deploy when you have $config['auto_names'] set to FALSE
// to do so, you must set file name to 'auto' during deploy
echo $this->minify->deploy_css(TRUE, 'auto');
echo $this->minify->deploy_js(TRUE, 'auto');

//Output: '<link href="path-to-compiled-css-with-auto-file-name" rel="stylesheet" type="text/css" />'
//Output: '<script type="text/javascript" src="path-to-compiled-js-with-auto-file-name"></script>'.

// you can deploy only particular group of files
echo $this->minify->deploy_css(TRUE, NULL, 'another');
echo $this->minify->deploy_js(TRUE, 'auto', 'extra'); 

//Output: '<link href="path-to-compiled-css-group" rel="stylesheet" type="text/css" />'
//Output: '<script type="text/javascript" src="path-to-compiled-js-group-with-auto-file-name"></script>'.

// you can enable versioning your your assets via config variable `$config['versioning']` or manually
$this->minify->versioning = TRUE;
echo $this->minify->deploy_js(); 

//Output: '<script type="text/javascript" src="path-to-compiled.js?v=hash-here"></script>'.

Changelog

01 Mar 2021

  • comments fixing
  • config checks only when deploy

26 Jul 2019

  • added option to manually change version number for assets (thanks screamingjungle)

11 Feb 2019

  • fixed an issue where not all config variables from the constructor were taken into account

02 Feb 2019

  • new config variable to allow of use a custom domain/subdomain for your assets: $config['base_url'] (default to '')

25 Jul 2018

  • new config variable to disable default behavior - deploy when any file is changed: $config['change_on_deploy'] (default to TRUE)

24 Jul 2018

  • handle errors for closurecompiler engine

26 Feb 2018

  • new config variable to determine if we want to return html tags (as string result) or only links to the assets (as array): $config['html_tags'] (default to TRUE)
  • we can now specify what HTML tag will be used for CSS and JS through $config['css_tag'] and $config['js_tag']

17 Jun 2017

  • new config variable to enable versioning assets $config['versioning'] (default to FALSE)
  • new config variable to enable/disable library - useful for debugging: $config['enabled'] (default to TRUE)

29 Dec 2016

  • introduce option to save compiled css and js files in different folders - new config variables: $config['assets_dir_css'] and $config['assets_dir_js'].

29 Apr 2015

  • allow using automatic file name for particular deploy when you have $config['auto_names'] set to FALSE
  • documentation update

20 Apr 2015

  • Closure compiler configuration extracted to config file

22 Mar 2015

  • method chaining support
  • new methods: add_css() and add_js() - gives ability for adding files to existing files arrays
  • added support to run library with custom array config, assigned as second parameter (during loading) $this->load->library('minify', $config);
  • added support for groups in files arrays - as second (optional) parameter in methods: css(), js(), add_css() and add_js() (i.e. $this->minify->js(array('script.js'), 'extra'); - default group name is default)
  • added support for strings as first parameter in methods: css(), js(), add_css() and add_js() (i.e. $this->minify->js('first.js, second.js');)
  • added support for automatic files names: $config['auto_names'] = TRUE;
  • external compression classes moved to minify folder
  • unit tests for new features

10 Feb 2015

  • Unit testing

09 Feb 2015

  • 2 new engines to compress JS files
  • documentation update

13 Oct 2014

  • changed way of generating JS file

14 July 2014

  • small bug fixes in JS compression

4 July 2014

  • sample JavaScript files to see how it works
  • detection of empty JS file causes force refresh

23 May 2014

  • you can chose your compression engine library in config file (CSS only)
  • speed optimisations
  • force CSS rewrite using $this->minify->deploy_css(TRUE);

11 Mar 2014

  • completely rewrite CSS parser - uses cssmin compress CSS,
  • detects file modification time no longer force rewrites,
  • example usage now included withing app

Any questions?

Report theme here: https://github.com/slav123/CodeIgniter-minify/issues

About

CodeIgniter minify library CSS and JavaScript compression on the fly

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages