From 002c7e41daee26519a114bd39d3ce87b5e803bb0 Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Tue, 5 Apr 2016 06:51:34 +0200 Subject: [PATCH] Fixed #734 - `--root` option edge case. 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 | 5 +++++ bin/cleancss | 19 +++++++++++++++++-- test/binary-test.js | 14 ++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/History.md b/History.md index d1266d0b..6f6980c8 100644 --- a/History.md +++ b/History.md @@ -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) ================== diff --git a/bin/cleancss b/bin/cleancss index 5f95b7e2..afe7c84d 100755 --- a/bin/cleancss +++ b/bin/cleancss @@ -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']; diff --git a/test/binary-test.js b/test/binary-test.js index 9b68076b..2cc769f5 100644 --- a/test/binary-test.js +++ b/test/binary-test.js @@ -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 () { -- 2.34.1