basset icon indicating copy to clipboard operation
basset copied to clipboard

Minification is not working (CssMin, JsMin)

Open Marwelln opened this issue 11 years ago • 16 comments

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.

Marwelln avatar Jun 06 '13 11:06 Marwelln

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.

jasonlewis avatar Jun 06 '13 12:06 jasonlewis

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

Marwelln avatar Jun 06 '13 12:06 Marwelln

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

jasonlewis avatar Jun 06 '13 12:06 jasonlewis

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.

Marwelln avatar Jun 06 '13 12:06 Marwelln

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

jallits avatar Jun 07 '13 02:06 jallits

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.

jasonlewis avatar Jun 07 '13 11:06 jasonlewis

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

jallits avatar Jun 07 '13 15:06 jallits

It's a dev dependency.

jasonlewis avatar Jun 07 '13 16:06 jasonlewis

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 avatar Jun 14 '13 05:06 codewithfeeling

Just a follow up...

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

codewithfeeling avatar Jun 14 '13 06:06 codewithfeeling

@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

jallits avatar Jun 14 '13 14:06 jallits

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.

dvdkruk avatar Jun 18 '13 22:06 dvdkruk

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?

ux-engineer avatar Jun 22 '13 18:06 ux-engineer

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..

barryvdh avatar Sep 19 '13 11:09 barryvdh

@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.

ux-engineer avatar Sep 19 '13 12:09 ux-engineer

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

barryvdh avatar Sep 19 '13 13:09 barryvdh