Permalink
Browse files

Move CSS @import statements to top of combined file

Thanks to jhuriez for pointing this out and for the basis of this patch.

See #39.
  • Loading branch information...
1 parent 2a1835b commit 0defa6039bd00f44f29440783bbce53caca03848 @canton7 committed Feb 3, 2013
Showing with 27 additions and 1 deletion.
  1. +19 −1 classes/casset.php
  2. +8 −0 readme.md
View
@@ -107,7 +107,7 @@ class Casset {
*/
protected static $post_load_callback = null;
- /*
+ /**
* @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
* page.
@@ -127,6 +127,11 @@ class Casset {
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.
*/
public static $initialized = false;
@@ -199,6 +204,8 @@ public static function _init()
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;
}
@@ -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);
$mtime = time();
}
View
@@ -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.
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
------

0 comments on commit 0defa60

Please sign in to comment.