diff --git a/README.md b/README.md
index 6514030..dea5f01 100644
--- a/README.md
+++ b/README.md
@@ -1,11 +1,14 @@
-# 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.
+
+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
@@ -17,20 +20,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
@@ -40,34 +55,49 @@ 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
+
+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.
-- 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`
+
+````
+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
+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...
```php
public function recompileCSS()
{
- Less::modifyVars(['@body-bg' => 'pink'])->recompile('filename');
-
+ Less::modifyVars(['@body-bg' => 'pink'])->recompile('filename_wo_ext');
return Redirect::back();
}
```
@@ -75,11 +105,13 @@ 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 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)
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);