Skip to content

Commit

Permalink
Move CSS @import statements to top of combined file
Browse files Browse the repository at this point in the history
Thanks to jhuriez for pointing this out and for the basis of this patch.

See #39.
  • Loading branch information
canton7 committed Feb 3, 2013
1 parent 2a1835b commit 0defa60
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
20 changes: 19 additions & 1 deletion classes/casset.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class Casset {
*/ */
protected static $post_load_callback = null; protected static $post_load_callback = null;


/* /**
* @var function If given, the function to call when we've decided on the name * @var function If given, the function to call when we've decided on the name
* for a file, but want to allow the user to tweak it before we write it to the * for a file, but want to allow the user to tweak it before we write it to the
* page. * page.
Expand All @@ -126,6 +126,11 @@ class Casset {
*/ */
protected static $css_uri_rewriter = 'absolute'; protected static $css_uri_rewriter = 'absolute';


/**
* @var Whether to move @import lines to the top
*/
protected static $move_imports_to_top = true;

/** /**
* @var bool Wether we've been initialized. * @var bool Wether we've been initialized.
*/ */
Expand Down Expand Up @@ -199,6 +204,8 @@ public static function _init()


static::$css_uri_rewriter = \Config::get('casset.css_uri_rewriter', static::$css_uri_rewriter); static::$css_uri_rewriter = \Config::get('casset.css_uri_rewriter', static::$css_uri_rewriter);


static::$move_imports_to_top = \Config::get('casset.move_imports_to_top', static::$move_imports_to_top);

static::$initialized = true; static::$initialized = true;
} }


Expand Down Expand Up @@ -1191,6 +1198,17 @@ protected static function combine($type, $file_group, $minify, $inline)
} }
} }
} }
if ($type == 'css' && static::$move_imports_to_top) {
// Remove @import lines, and record them
$imports = array();
$content = preg_replace_callback('/@import.*?;/', function($match) use (&$imports) {
$imports[] = $match[0];
return '';
}, $content);
if (count($imports))
$content = implode(PHP_EOL, $imports).PHP_EOL.$content;
}

file_put_contents($abs_filepath, $content, LOCK_EX); file_put_contents($abs_filepath, $content, LOCK_EX);
$mtime = time(); $mtime = time();
} }
Expand Down
8 changes: 8 additions & 0 deletions readme.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -851,6 +851,14 @@ If you decide to do this, you'll probably want to turn off Casset's rewriting.
The algorithm is specified using the `css_uri_rewriter` config key. The algorithm is specified using the `css_uri_rewriter` config key.
This can take values of 'absolute', 'relative', or 'none'. This can take values of 'absolute', 'relative', or 'none'.


CSS @import rewriting
---------------------

CSS `@import` statements need to come at the beginning of the file in which they appear.
Combining files obviously screws this up, as lines which were at the beginning of a file could now be somewhere in the middle of the combined file.
If the config key `move_imports_to_top` is true (the default) then casser will take all `@import` lines and move them to the top of the combined file.
If this is causing problems, you can disable this feature.

Addons Addons
------ ------


Expand Down

0 comments on commit 0defa60

Please sign in to comment.