-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CSS files containing url(...) - rewrite path for pipeline? #38
Comments
@fisharebest, for those cases what you need is an assets filter/preprocessor. The library doesn't include a built in one but it offers a way to provide your custom one. Simply use a closure in your I didn't know about the |
As a quick-and-dirty proof of concept, I have this, which works.
Obviously, I'll need a more sophisticated processor, to rewrite only local files (not data: urls, etc.), but that should be straightforward. Thanks! |
This is my fetch command for Laravel:
I have added this additional line:
font-awesome.css has urls like:
In this case the above mentioned won't work |
According to http://www.w3.org/TR/CSS2/syndata.html#value-def-uri:
A non greedy PHP regex to match that specification could be So here is a possible approach to rewrite relative CSS URLs: 'fetch_command' => function ($asset) {
$content = file_get_contents($asset);
$regex = '~url\(\s?[\'"]?(.*?)[\'"]?\s?\)~';
$filter = function ($match) {
// Do not process absolute URLs
if('http://' === substr($match[1], 0, 7) or 'https://' === substr($match[1], 0, 8) or '//' === substr($match[1], 0, 2))
return $match[0];
// Add your filter logic here
return 'url("../'. $match[1] . '")';
};
// Apply filter
return preg_replace_callback($regex, $filter, $content);
}, |
I am not well versed with regex but I have faced some problems with yours (which I was facing with mine too): I had to change my regex to:
This included all the rules you have covered and adds more conditions that the resource is an image or a font file. This way I won't touch urls which don't need any fixing. For example Thanks for your input, right now my fetch_command is: 'fetch_command' => function ($asset) {
$content = file_get_contents($asset);
$prefix = str_replace(public_path() . '/', '', dirname($asset)) . '/';
$regex = "/\burl\(\s?[\'\"]?(([^';]+)\.(jpg|eot|jpg|jpeg|gif|png|ttf|woff|svg).*?)[\'\"]?\s?\)/";
$filter = function ($match) use ($prefix) {
// Do not process absolute URLs
if('http://' === substr($match[1], 0, 7) or 'https://' === substr($match[1], 0, 8) or '//' === substr($match[1], 0, 2))
{
return $match[0];
}
// Add your filter logic here
return 'url(\''. $prefix.$match[1] . '\')';
};
// Apply filter
return preg_replace_callback($regex, $filter, $content);
}, |
I used Assetic to do the rewrite stuff. This way I don't have to mess around with RegEx and can rely on battle-tested solutions. Anyway I'm sure the following snippet can be improved: 'fetch_command' => function ($asset) {
$content = file_get_contents($asset);
$info = pathinfo($asset);
$sourceRoot = 'assets/'. $info['extension'];
$sourcePath = $info['extension'] . DIRECTORY_SEPARATOR . $info['basename'];
$rewrite = new Assetic\Filter\CssRewriteFilter();
$assetic = new Assetic\Asset\StringAsset($content, [$rewrite], $sourceRoot, $sourcePath);
$assetic->setTargetPath('assets/min');
return $assetic->dump();
}, |
Suppose I have a CSS file
/css/style.css
, which contains a rule such as#id { background-image: url(img.png); }
and I use the pipeline option.My pipeline file (e.g.
/min/7c358b3d092429876a4fb752721bfca8.css
) still contains the exact same rule#id { background-image: url(img.png); }
. However this doesn't work, becauseimg.png
is in thecss
folder and not themin
folder.The pipeline file needs to contain
#id { background-image: url(../css/img.png); }
.The Minify_CSS library seems to have an option to rewrite URLs like this (
prependRelativePath
), but I can't work out how to configure/enable it. Am I missing something obvious?The text was updated successfully, but these errors were encountered: