Allows `target` option to be a path to a folder instead of a file.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Mon, 25 Aug 2014 17:55:35 +0000 (18:55 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Mon, 25 Aug 2014 17:55:35 +0000 (18:55 +0100)
* Should fix #336.
* For examples regarding root / target / relativeTo take a look at https://github.com/jakubpawlowicz/clean-css/blob/master/test/unit-test.js#L971 - 'urls rewriting - root and target' test.

History.md
README.md
lib/images/url-rebase.js
test/unit-test.js

index 17d6fea..3b33854 100644 (file)
@@ -1,3 +1,8 @@
+[2.3.0 / 2014-xx-xx](https://github.com/jakubpawlowicz/clean-css/compare/v2.2.15...v2.3.0)
+==================
+
+* Allows `target` option to be a path to a folder instead of a file.
+
 [2.2.15 / 2014-xx-xx](https://github.com/jakubpawlowicz/clean-css/compare/v2.2.14...v2.2.15)
 ==================
 
index feca130..872aeef 100644 (file)
--- a/README.md
+++ b/README.md
@@ -125,8 +125,9 @@ CleanCSS constructor accepts a hash as a parameter, i.e.,
 * `keepBreaks` - whether to keep line breaks (default is false)
 * `benchmark` - turns on benchmarking mode measuring time spent on cleaning up
   (run `npm run bench` to see example)
-* `root` - path to resolve absolute `@import` rules and rebase relative URLs
-* `relativeTo` - path with which to resolve relative `@import` rules and URLs
+* `root` - path to __resolve__ absolute `@import` rules and __rebase__ relative URLs
+* `relativeTo` - path to __resolve__ relative `@import` rules and URLs
+* `target` - path to a folder or an output file to which __rebase__ all URLs
 * `processImport` - whether to process `@import` rules
 * `noRebase` - whether to skip URLs rebasing
 * `noAdvanced` - set to true to disable advanced optimizations - selector & property merging, reduction, etc.
index 8d5c59b..789d9b1 100644 (file)
@@ -1,3 +1,4 @@
+var fs = require('fs');
 var path = require('path');
 
 var UrlRewriter = require('./url-rewriter');
@@ -19,8 +20,13 @@ module.exports = function UrlRebase(options, context) {
     if (rebaseOpts.absolute)
       rebaseOpts.toBase = path.resolve(options.root);
 
-    if (rebaseOpts.relative)
-      rebaseOpts.toBase = path.resolve(path.dirname(options.target));
+    if (rebaseOpts.relative) {
+      var target = fs.existsSync(options.target) && fs.statSync(options.target).isDirectory() ?
+        options.target :
+        path.dirname(options.target);
+
+      rebaseOpts.toBase = path.resolve(target);
+    }
 
     if (!rebaseOpts.fromBase || !rebaseOpts.toBase)
       return data;
index e0e845b..4002ee3 100644 (file)
@@ -968,6 +968,23 @@ path")}',
     target: path.join(process.cwd(), 'test.css'),
     relativeTo: path.join('test', 'data', 'partials-relative')
   }),
+  'urls rewriting - no root but target as a directory': cssContext({
+    'no @import': [
+      'a{background:url(../partials/extra/down.gif) no-repeat}',
+      'a{background:url(test/data/partials/extra/down.gif) no-repeat}'
+    ],
+    'relative @import': [
+      '@import url(base.css);',
+      'a{background:url(test/data/partials/extra/down.gif) no-repeat}'
+    ],
+    'absolute @import': [
+      '@import url(/test/data/partials-relative/base.css);',
+      'a{background:url(test/data/partials/extra/down.gif) no-repeat}'
+    ]
+  }, {
+    target: process.cwd(),
+    relativeTo: path.join('test', 'data', 'partials-relative')
+  }),
   'urls rewriting - root and target': cssContext({
     'no @import': [
       'a{background:url(../partials/extra/down.gif) no-repeat}',