zhanjh / minify

Automatically exported from code.google.com/p/minify
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Allow CDN path or prefix as direct config option #214

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Minify version 2.1.3

Feature request: CDN path as a config option

http://code.google.com/p/minify/wiki/UriRewriting#Manual_Rewriting shows how to 
add a prefix with relative paths (../../images/123.jpg => /cdn/images/123.jpg), 
but the same style won't work if you want to prepend a domain instead of a path 
(you end up with paths like cdn.example.com/../../images/123.jpg).

It would be wonderful to have a 'cdnPath' option to add a prefix to all CSS 
images when they get minified, as it stands now you have to something like:

function minify_post_process_css_cdn($content, $type) {
  if ($type === Minify::TYPE_CSS) {
    return str_replace('url(/', 'url(' . $CDN_PATH, $content);
  }
  return $content;
}
$min_serveOptions['postprocessor'] = 'minify_post_process_css_cdn';

This will properly rewrite "../../images/123.jpg" to 
"http://cdn.example.com/images/123.jpg" if $CDN_PATH is set appropriately.

Original issue reported on code.google.com by darp...@gmail.com on 14 Dec 2010 at 3:01

GoogleCodeExporter commented 9 years ago
A simple solution would be for the user to prepend something like 
"http://cdn.example.org/a/a/" and then Minify to automatically remove parent 
traversals where possible.
../../images/123.jpg => (prepend)
http://cdn.example.org/a/a/../../images/123.jpg => (remove traversal)
http://cdn.example.org/a/../images/123.jpg => (remove traversal)
http://cdn.example.org/images/123.jpg

Thoughts?

Original comment by mrclay....@gmail.com on 14 Dec 2010 at 4:27

GoogleCodeExporter commented 9 years ago
That would work, but it doesn't address the use case I had in mind.

For development, user creates CSS and assets with relative paths.

For deployment, minify is used to combine, compress and rewrite the CSS to 
appropriate paths. This case is exactly how it is used and works today.

The enhancement would be that I don't want to point at my production server 
(CSS-relative path), I want to point to an absolute path for assets.

Original comment by darp...@gmail.com on 15 Dec 2010 at 7:11

GoogleCodeExporter commented 9 years ago
In config.php you would have code like:

if (/* is production environment */) {
  $min_serveOptions['rewriteCssUris'] = false;
  $min_serveOptions['minifierOptions']['text/css']['prependRelativePath'] = 'http://cdn.example.org/a/a/';
}

The result is that, on development Minify rewrites as normal, but on production 
the rewrited URLs point to the CDN (with "/a/a/../../" replaced with "/").

Original comment by mrclay....@gmail.com on 16 Dec 2010 at 7:09

GoogleCodeExporter commented 9 years ago
R436 adds "../" removal to prependRlativePath operations. Just replace 
UriRewriter.php from the new version. With my suggestion in comment 3 
(branching config code per environment) you should be able to get the desired 
behavior.

Original comment by mrclay....@gmail.com on 19 Dec 2010 at 9:07