From be9a9ff7e638a69f51f0be10fd9427263b119b52 Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Tue, 19 May 2015 19:57:39 +0100 Subject: [PATCH] Fixes #575 - missing directory as a `target`. 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 | 1 + lib/clean.js | 10 +++++++++- test/integration-test.js | 19 ++++++++++++++++++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/History.md b/History.md index 1da7df07..321e8e78 100644 --- a/History.md +++ b/History.md @@ -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) ================== diff --git a/lib/clean.js b/lib/clean.js index 794e684e..53470994 100644 --- a/lib/clean.js +++ b/lib/clean.js @@ -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: {}, diff --git a/test/integration-test.js b/test/integration-test.js index 2678e67f..2c0f68cb 100644 --- a/test/integration-test.js +++ b/test/integration-test.js @@ -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}', -- 2.34.1