Jump to:

23358 Posts in 17874 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » CSS Minify fast fix

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 2078 Views
  • Tonyair
    Avatar
    Community Member
    81 Posts

    CSS Minify fast fix Link to this post

    Code is need some optimisations, but I needed to do it fast
    Well ok, instruction:
    1. Download minify from http://code.google.com/p/minify/
    2. Unpack Compressor.php from /min/lib/Minify/CSS/ to sapphire/thirdparty/minify
    3. Open sapphire/core/Requirements.php
    4. Find and replace foreach on 1000th line with that =))

    foreach(array_diff($fileList, $this->blocked) as $file) {
                $fileContent = file_get_contents($base . $file);
                // if we have a javascript file and jsmin is enabled, minify the content
                $isJS = stripos($file, '.js');
                if($isJS && $this->combine_js_with_jsmin) {
                   require_once('thirdparty/jsmin/jsmin.php');
                   
                   increase_time_limit_to();
                   $fileContent = JSMin::minify($fileContent);
                } else {
                   $isCSS = stripos($file, '.css');
                   if($isCSS) {
                      require_once('thirdparty/minify/Compressor.php');
                      increase_time_limit_to();
                      $fileContent = Minify_CSS_Compressor::process($fileContent);
                   }               
                }
                
                // write a header comment for each file for easier identification and debugging
                // also the semicolon between each file is required for jQuery to be combinable properly
                $combinedData .= "/****** FILE: $file *****/\n" . $fileContent . "\n".($isJS ? ';' : '')."\n";
             }

    To-do:

    1. replace $isCSS && $isJS with one property
    2. add $this->combine_css_with_minify property
    3. add minify option for all required css's (not only combined)
    4. like 3rd, but for JS and JSmin

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: CSS Minify fast fix Link to this post

    Note that 2.4 is using compressed javascript files for combined files.

    You can do the same for CSS without touching core files.

    See this method in Page::init();

    http://www.sspaste.com/paste/show/4c2d0737ae58e

    You have to set the site in live mode to use combined files.....

  • Tonyair
    Avatar
    Community Member
    81 Posts

    Re: CSS Minify fast fix Link to this post

    jsmin doesn't minify css and google minify deletes all comments, spaces, new lines and etc in css files

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: CSS Minify fast fix Link to this post

    See this part:

    if(file_exists(Director::baseFolder() . '/' .$combinepath.'/combined.css')){
                $bf = file_get_contents(Director::baseFolder() . '/' .$combinepath.'/combined.css');
                $bf = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $bf);
                $bf = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $bf);
                $fh = fopen(Director::baseFolder() . '/' .$combinepath.'/combined.css', 'w');
                fwrite($fh, $bf);
                fclose($fh);
             }

  • TotalNet
    Avatar
    Community Member
    181 Posts

    Re: CSS Minify fast fix Link to this post

    Great stuff.

    I was looking for a way of doing just this and came across minify.

    Think I'll use a combination of both ideas, create a class based on minify and call it using Martijn's method. That way I get the best of both worlds; an external library to do the work and no need to edit core files.

    Cheers,

    Rich

  • Willr
    Avatar
    Forum Moderator
    5482 Posts

    Re: CSS Minify fast fix Link to this post

    Tonyair I think this would be good to have in core. I think it might have an older version which needs upgrading anyway and being able to combine CSS as well as JS by default would be good.

    If you can get the contents of this thread together and submit a patch it would be good to include. See http://doc.silverstripe.org/contributing#submitting_patches for submission guidelines.

  • brice
    Avatar
    Community Member
    51 Posts

    Re: CSS Minify fast fix Link to this post

    Of note;

    There's a nice project in PHP Minify that handles CSS & JS minification & combining, and support some "advanced" features like skipping files w/ a .min.(css|js) extension (already minified files) while processing as well as security & group definitions.

    Project URL is: http://code.google.com/p/minify/

    It would be great to see SS combine_files on par w/ this project, or perhaps to utilize it instead.

  • Tonyair
    Avatar
    Community Member
    81 Posts

    Re: CSS Minify fast fix Link to this post

    Willr, i will be glad, but I made too many changes so I can update my version only manually.
    For example i changed structure of SS I believe that in public_html should be placed only public files like css, images, javascript, swf, assets and index.php all other files should be placed in the upper directory

    2078 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.