bigjoevtrj / cssmin

Automatically exported from code.google.com/p/cssmin
0 stars 0 forks source link

URL rewriting / prefixing #30

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What version of CssMin are you using (source and/or build)?

2.0.2.1

Please provide any additional information below.

It would be great to have an option to rewrite urls in a stylesheet. In simple 
cases, a "url-prefix" option could be sufficient.

Imagine that the source stylesheet is contained in a different directory than 
the target one, and you want to use the source css for development and the 
minified css in the release version.

Original issue reported on code.google.com by robert.f...@gmail.com on 14 Mar 2011 at 12:45

GoogleCodeExporter commented 9 years ago

Original comment by joe.scylla on 22 Mar 2011 at 9:46

GoogleCodeExporter commented 9 years ago
A cssmin plugin would really be nice! I need to rewrite all 
url("../gfx/foo.png") ... :(

Original comment by fiedler....@gmail.com on 9 Jun 2011 at 3:17

GoogleCodeExporter commented 9 years ago
I´ve done one using cssmin-v3.0.0.b7-source. See attachment.
How to use:

   $css = CssMin::minify($css, array(), array(
       "UrlPrefix" => array( "BaseUrl" => "http://www.xyz.de/your_css_folder/" )
   ));

Original comment by fiedler....@gmail.com on 9 Jun 2011 at 4:46

Attachments:

GoogleCodeExporter commented 9 years ago
Pardon my hard headedness, but I noticed this issue while searching for a 
solution to a problem I'm having. A bit of background:

We have a core code base which a build system matches to a custom project 
(customer) base. Using this melded system, we can override CSS files in the 
core with files in the custom project by placing them in a doc-root/custom/ dir 
(named the same as they are named in core). The following Apache mod rewrite 
then causes the actual override:

    RewriteCond   %{DOCUMENT_ROOT}/custom%{REQUEST_FILENAME} -f
    RewriteRule (.*) /custom/%{REQUEST_FILENAME} [L]

Basically, if a requested file exists in the custom/ dir, use it.  If not, 
allow the request to go through to the core dir as per normal.

The problem I am having comes into play when using CSSMinifier on a CSS file 
with @import statements that point at a file which has been overridden in 
custom. I can't tell CSSMinifier to try the custom URL and, if it doesn't find 
the file, try the core as I only get one setting for import-base-path.

Is that what this issue is asking to be corrected?

Thanks,
Jim

Original comment by auldrid...@gmail.com on 17 Jun 2011 at 3:28

GoogleCodeExporter commented 9 years ago
@auldridgej 
Fixing that issue will not fix your problem. My plan is to create a filter that 
allows to use regular expressions to rewrite url's in the css source. But your 
problem requires some special logic. You have to check every import at-rule and 
rewrite the url if there is a css file with the same name in the custom 
directory. With CSSMin version 3.x you can archive this by creating a custom 
minifier filter.

Original comment by joe.scylla on 17 Jun 2011 at 5:01

GoogleCodeExporter commented 9 years ago
Thanks for the clarification.
Jim

Original comment by auldrid...@gmail.com on 17 Jun 2011 at 5:39

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
The plugin works great for me but I made one modification:

{{{
  private function urlPrefix($url) {
    $url = str_replace(array('"', '\''), "", $url);

    return $this->resolveHref($this->configuration["BaseUrl"], $url);
  }
}}}

This is because sometimes people will use single quotes (I do). However, in any 
case, quotes are not necessary.

Original comment by audvare on 15 Oct 2011 at 10:28

GoogleCodeExporter commented 9 years ago
Just wanted to post a little update here.

I downloaded the CssUrlPrefixMinifierPlugin.php added by Comment 4. With some 
small changes it works really nice! :)

First I changed the reg exp a bit to find all url() parts. Because I had some 
css that was like this:
 background-image: url(image1.png),url(image2.png);
(Don't ask me if that works. It's in a theme we bought for the page we are 
working on). :)
New regexp:
 private $reMatch = "/url\s*\(\s*([^\)]+)\)/iS";

Then I changed the apply() function a bit to use preg_match_all() to catch all 
those url()s.
 public function apply(aCssToken &$token) {
    if (get_class($token) === "CssRulesetDeclarationToken" && stripos($token->Value, "url") !== false) {
        preg_match_all($this->reMatch, $token->Value, $m);
        if (!empty($m)) {
            foreach ($m[1] AS $index => $src) {
                $token->Value = str_replace($src, $this->urlPrefix($src), $token->Value);
            }
        } 
    }
    return false;
 }
(There might be ways of making this nicer though).

Then I also changed urlPrefix() a bit because I wanted to remove all url(' ') 
and url(" ") and unify them all to url(' ').
 private function urlPrefix($url) {
    $url = str_replace('"', "", $url);
    $url = str_replace('\'', "", $url);

    return "'" . $this->resolveHref($this->configuration["BaseUrl"], $url) . "'";
 }

I have quite an advance minifier system now in a Silex env with Assetics. I 
have to pre-minify with this url rewrite filter. Then merge the files. And 
move. Because we have a theme that has a dark and a light versions. With a lot 
of different css's in different places. But this filter seams to have helped me 
a lot.

(I'll try to let you guys know here if I find any more "bugs").

Original comment by david.ma...@gmail.com on 28 Nov 2012 at 9:28

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Here is the new file...

Original comment by david.ma...@gmail.com on 28 Nov 2012 at 9:31

Attachments:

GoogleCodeExporter commented 9 years ago
We put a github fork up where the CssUrlPrefixMinifierPlugin is included in the 
file. Easier to use. :) 
https://github.com/ztormab/CssMin

Original comment by david.ma...@gmail.com on 29 Nov 2012 at 9:30