Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

CSS Minify fast fix


Go to End


8 Posts   3568 Views

Avatar
Tonyair

Community Member, 81 Posts

6 July 2010 at 9:24am

Edited: 06/07/2010 9:28am

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

Avatar
Martijn

Community Member, 271 Posts

6 July 2010 at 10:43pm

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

Avatar
Tonyair

Community Member, 81 Posts

7 July 2010 at 5:57pm

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

Avatar
Martijn

Community Member, 271 Posts

12 July 2010 at 9:59pm

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);
			}

Avatar
TotalNet

Community Member, 181 Posts

13 July 2010 at 10:44am

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

Avatar
Willr

Forum Moderator, 5523 Posts

13 July 2010 at 4:39pm

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.

Avatar
brice

Community Member, 52 Posts

3 August 2010 at 8:31am

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.

Avatar
Tonyair

Community Member, 81 Posts

21 September 2010 at 1:22am

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