Fixes #575 - missing directory as a `target`.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Tue, 19 May 2015 18:57:39 +0000 (19:57 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Tue, 19 May 2015 18:57:39 +0000 (19:57 +0100)
When a `target` is given as a directory which does not exist (perfectly
valid in API mode) we should still handle it correctly.

History.md
lib/clean.js
test/integration-test.js

index 1da7df0..321e8e7 100644 (file)
@@ -13,6 +13,7 @@
 * Fixed issue [#517](https://github.com/jakubpawlowicz/clean-css/issues/517) - turning off color optimizations.
 * Fixed issue [#542](https://github.com/jakubpawlowicz/clean-css/issues/542) - space after closing brace in IE.
 * Fixed issue [#574](https://github.com/jakubpawlowicz/clean-css/issues/574) - rewriting internal URLs.
+* Fixed issue [#575](https://github.com/jakubpawlowicz/clean-css/issues/575) - missing directory as a `target`.
 
 [3.2.10 / 2015-05-14](https://github.com/jakubpawlowicz/clean-css/compare/v3.2.9...v3.2.10)
 ==================
index 794e684..5347099 100644 (file)
@@ -52,13 +52,21 @@ var CleanCSS = module.exports = function CleanCSS(options) {
     shorthandCompacting: undefined === options.shorthandCompacting ? true : !!options.shorthandCompacting,
     sourceMap: options.sourceMap,
     sourceMapInlineSources: !!options.sourceMapInlineSources,
-    target: options.target && fs.existsSync(options.target) && fs.statSync(options.target).isDirectory() ? options.target : path.dirname(options.target)
+    target: !options.target || missingDirectory(options.target) || presentDirectory(options.target) ? options.target : path.dirname(options.target)
   };
 
   this.options.inliner.timeout = this.options.inliner.timeout || DEFAULT_TIMEOUT;
   this.options.inliner.request = this.options.inliner.request || {};
 };
 
+function missingDirectory(filepath) {
+  return !fs.existsSync(filepath) && !/\.css$/.test(filepath);
+}
+
+function presentDirectory(filepath) {
+  return fs.existsSync(filepath) && fs.statSync(filepath).isDirectory();
+}
+
 CleanCSS.prototype.minify = function(data, callback) {
   var context = {
     stats: {},
index 2678e67..2c0f68c 100644 (file)
@@ -1093,7 +1093,7 @@ path")}',
     root: process.cwd(),
     relativeTo: path.join('test', 'fixtures', 'partials-relative')
   }),
-  'urls rewriting - no root but target': cssContext({
+  'urls rewriting - no root but target as file': cssContext({
     'no @import': [
       'a{background:url(../partials/extra/down.gif) no-repeat}',
       'a{background:url(test/fixtures/partials/extra/down.gif) no-repeat}'
@@ -1135,6 +1135,23 @@ path")}',
     target: process.cwd(),
     relativeTo: path.join('test', 'fixtures', 'partials-relative')
   }),
+  'urls rewriting - no root but target as a missing directory': cssContext({
+    'url': [
+      'a{background:url(../partials/extra/down.gif) no-repeat}',
+      'a{background:url(../fixtures/partials/extra/down.gif) no-repeat}'
+    ],
+    'relative @import': [
+      '@import url(base.css);',
+      'a{background:url(../fixtures/partials/extra/down.gif) no-repeat}'
+    ],
+    'absolute @import': [
+      '@import url(/test/fixtures/partials-relative/base.css);',
+      'a{background:url(../fixtures/partials/extra/down.gif) no-repeat}'
+    ]
+  }, {
+    target: path.join('test', 'fixtures2'),
+    relativeTo: path.join('test', 'fixtures', 'partials-relative')
+  }),
   'urls rewriting - root and target': cssContext({
     'no @import': [
       'a{background:url(../partials/extra/down.gif) no-repeat}',