jasonlewis / basset

A better asset management package for Laravel.
http://jasonlewis.me/code/basset
240 stars 76 forks source link

Minification is not working (CssMin, JsMin) #151

Open Marwelln opened 11 years ago

Marwelln commented 11 years ago

Just as https://github.com/jasonlewis/basset/issues/65, using ->apply('CssMin') is not working.

In my config.php file I have this collections array: 'collections' => array( 'application' => function($collection) { $directory = $collection->directory('assets/css', function($collection) { $collection->requireDirectory(); });

            $directory->apply('CssMin');
            $directory->apply('UriRewriteFilter');

            $directory = $collection->directory('assets/js', function($collection) {
                $collection->requireDirectory('transactions')->apply('JsMin');
                $collection->requireDirectory('user')->apply('JsMin');
                $collection->requireDirectory();
            });

            $directory->apply('JsMin');
        }
    ),

Then I run this artisan command: php artisan basset:build application

It builds fine, but the problem is that the output is not minified.

jasonlewis commented 11 years ago

Hm, my guess is it's not building because the environment isn't production. This probably isn't ideal. If you look at the alias for CssMin it uses whenEnvironmentIs('production', 'prod') which means only when your application is running on that environment will it apply.

I might add a requirement like whenProductionBuild or whenDevelopmentBuild to only apply during a production or development build. Fix coming shortly.

Marwelln commented 11 years ago

I changed my environment between local and production, none are minimizing the files.

jasonlewis commented 11 years ago

You'd need to remove the whenEnvironmentIs requirement from the CssMin filter alias.

Marwelln commented 11 years ago

Removing it did nothing. I deleted the previous builds and ran the command again, but it's still not minimized.

'CssMin' => array('CssMinFilter', function($filter) { $filter->whenClassExists('CssMin'); }),

This in the config.php file.

PS. I'm on Windows 7.

jallits commented 11 years ago

How are you loading CssMin and JSMin? Have you seen: http://jasonlewis.me/code/basset/4.0/filters#windows-compatibility

jasonlewis commented 11 years ago

I've pushed out a fix that may or may not solve your issue. The config file has been updated to include to changes.

There are now whenProductionBuild and whenDevelopmentBuild requirements available for filters. So the filter will only be applied to either of the builds. The config has been updated so that CssMin and JsMin are only applied when it's a production build.

Now, make sure you have the CssMin library downloaded and autoloaded with Laravel. If you want Composer to manage it all then add a repositories key to your composer.json like so:

"repositories": [
    {
        "type": "package",
        "package": {
            "name": "cssmin/cssmin",
            "version": "3.0.1",
            "dist": { "url": "http://cssmin.googlecode.com/files/cssmin-v3.0.1.php", "type": "file" },
            "autoload": { "classmap": [ "cssmin-v3.0.1.php" ] }
        }
    }
],

Then in your requires key:

"cssmin/cssmin": "3.0.*"

You should now be able to use the CssMin filter.

jallits commented 11 years ago

He shouldn't have to modify his composer.json. CSSmin is a required repo in Assetic.

jasonlewis commented 11 years ago

It's a dev dependency.

codewithfeeling commented 11 years ago

I am struggling with this too. I've added CssMin via composer as suggested by Jason, and also removed the requirement for a production build from my Basset config file, but this just won't work:

Basset::collection('bootstrap', function($collection)
{
    $collection->add('assets/css/less/bootstrap-custom.less');
})->apply('Less')->andApply('CssMin');

I can see cssmin in both node_modules in my application root (I installed via node rpm) and also now in vendor/cssmin.

I have verified by class_exists('CssMin') that the CssMin class does exist.

Removing ->andApply('CssMin') from the filter chain triggers a build. But adding it back in gives Stylesheets build was not required for collection. when I run the artisan build command.

Any idea what's up?

(and thanks for Basset - I love it, with this minor glitch sorted it will be my asset publishing dream realised!)

codewithfeeling commented 11 years ago

Just a follow up...

Adding the --production flag to the Artisan build command sorts this out in my case.

jallits commented 11 years ago

@raffjones if you look at the filter definition for CSSmin around line 234 in the config.php, you will see that whenProductionBuild() returns true is when CSSmin is actually applied. Same goes for JSmin

dvdkruk commented 11 years ago

The problem in my case is that the nested composer.json of kriswallsmith/assetic isn't executed when using composer install from the root. Therefore no dependencies are loaded/installed and so nothing is compressed.

ux-engineer commented 11 years ago

I have the same problem. I composer installed CSSmin and it does minification now but with a weird problem: the styles aren't applied even the css file is ok and firebug shows all the style declarations there, but not on any elements.

But the main problem here is that when CSSmin and JSmin are included in Assetic, they aren't loaded for some reason?

barryvdh commented 11 years ago

Can't you just add https://packagist.org/packages/natxet/CssMin instead (packagist clone of the google repo). Assetic doesn't just download all the filters, because it doesn't know that filters you are going to use..

ux-engineer commented 11 years ago

@barryvdh I have mrclay/minify installed which seems to work with JS but not with CSS - weird? How does Basset know which libraries are there and how it chooses which to use?

@jasonlewis I'm also having a weird issue with debugging... When I have defined environment as 'local' and added that to Basset configs production environment array, neither minification nor combining of files are happening. Issue persists also if I have set 'production' key in $app->detectEnvironment array to 'projectname.local' (my working MAMP host) and dd($env) right after to verify set environment.

On the real production environment all is running ok except for the CSS minification not happening with mrclay/minify installed - do I need to config that somewhere?

Addition: Could mrclay/minify intervene with Assetic's required cssmin/cssmin? Though before I added mrclay/minify neither CSS nor JS minification was happening.

barryvdh commented 11 years ago

It uses mrclay for js, not for css. Depends in the filter. You can write your own filter if you want, like this https://github.com/barryvdh/assetic-filters/blob/master/src/CSSminFilter.php