Fixed #734 - `--root` option edge case.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Tue, 5 Apr 2016 04:51:34 +0000 (06:51 +0200)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Sat, 9 Apr 2016 15:27:57 +0000 (17:27 +0200)
When a root was specified (in CLI only) and it was given a path to
a folder (not a file) then we were taking its parent folder as a
root path instead.

History.md
bin/cleancss
test/binary-test.js

index d1266d0..6f6980c 100644 (file)
@@ -3,6 +3,11 @@
 
 * Requires Node.js 4.0+ to run.
 
+[3.4.12 / 2016-xx-xx](https://github.com/jakubpawlowicz/clean-css/compare/v3.4.11...3.4)
+==================
+
+* Fixed issue [#734](https://github.com/jakubpawlowicz/clean-css/issues/734) - `--root` option edge case.
+
 [3.4.11 / 2016-04-01](https://github.com/jakubpawlowicz/clean-css/compare/v3.4.10...v3.4.11)
 ==================
 
index 5f95b7e..afe7c84 100755 (executable)
@@ -82,8 +82,19 @@ var options = {
   target: commands.output
 };
 
-if (options.root || commands.args.length > 0)
-  options.relativeTo = path.dirname(path.resolve(options.root || commands.args[0]));
+if (options.root || commands.args.length > 0) {
+  var relativeTo = options.root || commands.args[0];
+
+  if (isRemote(relativeTo)) {
+    options.relativeTo = relativeTo;
+  } else {
+    var resolvedRelativeTo = path.resolve(relativeTo);
+
+    options.relativeTo = fs.statSync(resolvedRelativeTo).isFile() ?
+      path.dirname(resolvedRelativeTo) :
+      resolvedRelativeTo;
+  }
+}
 
 if (options.sourceMap && !options.target) {
   outputFeedback(['Source maps will not be built because you have not specified an output file.'], true);
@@ -105,6 +116,10 @@ if (commands.args.length > 0) {
   });
 }
 
+function isRemote(path) {
+  return /^https?:\/\//.test(path) || /^\/\//.test(path);
+}
+
 function processImportFrom(rules) {
   if (rules.length === 0) {
     return ['all'];
index 9b68076..2cc769f 100644 (file)
@@ -315,6 +315,20 @@ vows.describe('./bin/cleancss')
       })
     }
   })
+  .addBatch({
+    'relative import with just a filename': pipedContext('@import "one.css";', '-r ./test/fixtures/partials', {
+      'imports sources correctly': function(error, stdout) {
+        assert.equal(error, null);
+        assert.include(stdout, '.one{color:red}');
+      }
+    }),
+    'relative import with ./': pipedContext('@import "./one.css";', '-r ./test/fixtures/partials', {
+      'imports sources correctly': function(error, stdout) {
+        assert.equal(error, null);
+        assert.include(stdout, '.one{color:red}');
+      }
+    })
+  })
   .addBatch({
     'remote import': {
       topic: function () {