From 3374e1b40b5f55a7b7dbe09d78a4f07ebde69ad6 Mon Sep 17 00:00:00 2001 From: sebastienheyd Date: Sun, 31 Jan 2016 17:26:29 +0100 Subject: [PATCH 1/4] Added css minifying --- composer.json | 3 ++- src/Less.php | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index f8bbfa7..3e161d0 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,8 @@ ], "require": { "php": ">=5.5.0", - "oyejorge/less.php": "~1.5" + "oyejorge/less.php": "~1.5", + "matthiasmullie/minify": "^1.3" }, "autoload": { "psr-4": { diff --git a/src/Less.php b/src/Less.php index ee055f8..3965118 100644 --- a/src/Less.php +++ b/src/Less.php @@ -3,6 +3,7 @@ use lessc; use Illuminate\Contracts\Config\Repository as Config; use Illuminate\Contracts\Cache\Repository as Cache; +use MatthiasMullie\Minify; class Less { @@ -45,6 +46,11 @@ public function compile($filename, $options = array()) { if ($written === true) { $this->cleanCache(); } + // Minifying CSS + if(env('LESS_MINIFY', false)) { + $minifier = new Minify\CSS($output_path); + $minifier->minify($output_path); + } return $written; } @@ -68,6 +74,9 @@ public function fresh() { */ protected function writeCss($output_path, $css) { $css = str_replace('{relative_path_fix}', '..', $css); + if(!is_dir(dirname($output_path))) { + mkdir(dirname($output_path), 0777, true); + } return file_put_contents($output_path, $css) !== false; } @@ -99,6 +108,7 @@ protected function cleanCache() { * @return bool true on recompiled, false when not */ public function recompile($filename, $recompile = null, $options = array()) { + if ($this->recompiled === true) { return false; // This instance is already recompiled. Recompile a new or the same instance using Less::fresh() } @@ -208,9 +218,9 @@ public function parseVariables($less) { * @param bool $auto_recompile Automaticly recompile * @return string CSS url */ - public function url($filename, $auto_recompile = false) { + public function url($filename, $auto_recompile = true) { if ($auto_recompile) { - $recompiled = $this->recompile($filename); + $this->recompile($filename); } $css_path = $this->config->get('less.link_path', '/css') . '/' . $filename . '.css'; return asset($css_path); From 534b09635b781e2b7ccd66c34badb45b1275b6d6 Mon Sep 17 00:00:00 2001 From: sebastienheyd Date: Sun, 31 Jan 2016 17:27:03 +0100 Subject: [PATCH 2/4] Modified documentation --- README.md | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 6514030..c53a8b8 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ -# LESS support for Laravel 5.x without Node.js -Use LESS with your Laravel installation. Uses [oyejorge/less.php](http://lessphp.gpeasy.com/) instead of [leafo/lessphp](http://leafo.net/lessphp/) which is a more actively developed port of the official LESS processor. +# LESS support with minifying for Laravel 5.x without Node.js +Use LESS with your Laravel installation. Uses [oyejorge/less.php](http://lessphp.gpeasy.com/) instead of [leafo/lessphp](http://leafo.net/lessphp/) which is a more actively developed port of the official LESS processor. + +Uses [matthiasmullie/minify](https://github.com/matthiasmullie/minify) for minifying. ## Features - Can modify LESS variables on-the-fly @@ -17,20 +19,32 @@ First, pull in the package through Composer. } ``` -And then, if using Laravel 5, include the service provider within `config/app.php`. +And then, include the service provider within `config/app.php`. + +Laravel >= 5.1 ```php 'providers' => [ - 'Langemike\Laravel5Less\LessServiceProvider' + Langemike\Laravel5Less\LessServiceProvider::class +]; + +'aliases' => [ + 'Less' => langemike\Laravel5Less\LessFacade::class ]; + ``` -In the aliases section, add: +Laravel 5.0 : ```php +'providers' => [ + 'Langemike\Laravel5Less\LessServiceProvider' +]; + 'aliases' => [ 'Less' => 'Langemike\Laravel5Less\LessFacade' ]; + ``` ## Configuration @@ -49,14 +63,27 @@ You can specify your configuration through 3 options: `.env`, `config.php` file Your .env configuration will be used by default, it will be overridden by it's config.php settings, but the $options parameter will have the highest preference. ### Recompilation -Additionally you can (and probably should) have different configurations for development +Additionally you can (and probably should) have different configurations for development and production. Specifically, you probably don't want to be generating css files on your production server, since it will slow down your site. - change -- Check if LESS file(s) are modified. If it does, recompile CSS -- never -- Don't check, don't recompile. +- never -- Don't check, don't recompile (best choice on a production server). - always -- Always rewrite CSS +Set LESS_RECOMPILE in .env + +```` +LESS_RECOMPILE=change +```` + +## Minify + +By default, the generated css is not minified. To minify it, just set LESS_MINIFY to true in .env + +```` +LESS_MINIFY=true +```` ## Usage @@ -78,7 +105,7 @@ Within your view you can use the `Less::url()` function to link to your generate ``` -Passing `true` as the second parameter to `Less::url()` will auto-detect, based on your configuration, if recompilation is needed and will do so accordingly. +Passing `true` as the second parameter to `Less::url()` will auto-detect, based on your configuration, if recompilation is needed and will do so accordingly. ## Credits This project is inspired by [Less4Laravel](https://github.com/jtgrimes/less4laravel). From 0804e8c7f10f3cde7d7f21c05710696f2932b83f Mon Sep 17 00:00:00 2001 From: sebastienheyd Date: Sun, 31 Jan 2016 17:35:11 +0100 Subject: [PATCH 3/4] Modified documentation --- README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c53a8b8..f4e8f19 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ Laravel >= 5.1 ]; 'aliases' => [ - 'Less' => langemike\Laravel5Less\LessFacade::class + 'Less' => Langemike\Laravel5Less\LessFacade::class ]; ``` @@ -87,14 +87,15 @@ LESS_MINIFY=true ## Usage +By default, create your .less files into resources/assets/less + Within your models or controllers, you can perform modification to the outputted CSS. Here are some examples: before you perform a redirect... ```php public function recompileCSS() { - Less::modifyVars(['@body-bg' => 'pink'])->recompile('filename'); - + Less::modifyVars(['@body-bg' => 'pink'])->recompile('filename_wo_ext'); return Redirect::back(); } ``` @@ -102,10 +103,10 @@ public function recompileCSS() Within your view you can use the `Less::url()` function to link to your generated CSS ```html - + ``` -Passing `true` as the second parameter to `Less::url()` will auto-detect, based on your configuration, if recompilation is needed and will do so accordingly. +If necessary, passing `false` as second parameter to `Less::url()` will avoid recompilation. ## Credits This project is inspired by [Less4Laravel](https://github.com/jtgrimes/less4laravel). From 8a62d12ffd365ce64c348f04f5ef218b4f42aa1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20HEYD?= Date: Mon, 1 Feb 2016 16:37:49 +0100 Subject: [PATCH 4/4] Updated documentation --- README.md | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index f4e8f19..dea5f01 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,14 @@ -# LESS support with minifying for Laravel 5.x without Node.js +# LESS support (with minifying) for Laravel 5.x without Node.js Use LESS with your Laravel installation. Uses [oyejorge/less.php](http://lessphp.gpeasy.com/) instead of [leafo/lessphp](http://leafo.net/lessphp/) which is a more actively developed port of the official LESS processor. -Uses [matthiasmullie/minify](https://github.com/matthiasmullie/minify) for minifying. +Use [matthiasmullie/minify](https://github.com/matthiasmullie/minify) for minifying. ## Features - Can modify LESS variables on-the-fly - Can parse custom CSS/LESS and append it to the resulting file - Works with Twitter Bootstrap v3.3.5 (thanks to oyejorge/less.php) - Caching support +- Minifying support ## Installation @@ -54,24 +55,25 @@ with Artisan: ```` $ php artisan vendor:publish ```` -This will create a config file 'less.php' in your config directory. +This will create a config file `less.php` in your config directory. ### Settings -You can specify your configuration through 3 options: `.env`, `config.php` file and through `$options` parameter. +You can specify your configuration through 3 options: `.env`, `config/less.php` file or through `$options` argument. -Your .env configuration will be used by default, it will be overridden by it's config.php settings, but the $options parameter will have the highest preference. +Your `.env` configuration will be used by default, it will be overridden by the settings in `config/less.php`, but the `$options` argument will have the highest preference. ### Recompilation + Additionally you can (and probably should) have different configurations for development and production. Specifically, you probably don't want to be generating css files on your production server, since it will slow down your site. -- change -- Check if LESS file(s) are modified. If it does, recompile CSS -- never -- Don't check, don't recompile (best choice on a production server). -- always -- Always rewrite CSS +- `change` -- Check if LESS file(s) are modified. If it does, recompile CSS +- `never` -- Don't check, don't recompile (best choice on a production server). +- `always` -- Always rewrite CSS -Set LESS_RECOMPILE in .env +Set `LESS_RECOMPILE` in `.env` ```` LESS_RECOMPILE=change @@ -79,7 +81,7 @@ LESS_RECOMPILE=change ## Minify -By default, the generated css is not minified. To minify it, just set LESS_MINIFY to true in .env +By default, the generated css is not minified. To minify it, just set `LESS_MINIFY` to `true` in `.env` ```` LESS_MINIFY=true @@ -87,7 +89,7 @@ LESS_MINIFY=true ## Usage -By default, create your .less files into resources/assets/less +By default, you must create your `.less` files into `resources/assets/less` Within your models or controllers, you can perform modification to the outputted CSS. Here are some examples: before you perform a redirect... @@ -106,8 +108,10 @@ Within your view you can use the `Less::url()` function to link to your generate ``` -If necessary, passing `false` as second parameter to `Less::url()` will avoid recompilation. +If necessary, passing `false` as second parameter to `Less::url()` will avoid all recompilation (already if you set `LESS_RECOMPILE` to `always`). ## Credits This project is inspired by [Less4Laravel](https://github.com/jtgrimes/less4laravel). Without the hard work of [oyejorge/less.php](http://lessphp.gpeasy.com/) this project wouldn't be possible. + +Minification added by [Sebastien HEYD](http://sheyd.fr)