From b168ad06644822281d38c877b8eee2d73f77b88d Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Mon, 25 Aug 2014 18:55:35 +0100 Subject: [PATCH] Allows `target` option to be a path to a folder instead of a file. * 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 | 5 +++++ README.md | 5 +++-- lib/images/url-rebase.js | 10 ++++++++-- test/unit-test.js | 17 +++++++++++++++++ 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/History.md b/History.md index 17d6fea8..3b338546 100644 --- a/History.md +++ b/History.md @@ -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) ================== diff --git a/README.md b/README.md index feca130b..872aeefc 100644 --- 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. diff --git a/lib/images/url-rebase.js b/lib/images/url-rebase.js index 8d5c59b6..789d9b18 100644 --- a/lib/images/url-rebase.js +++ b/lib/images/url-rebase.js @@ -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; diff --git a/test/unit-test.js b/test/unit-test.js index e0e845b0..4002ee3a 100644 --- a/test/unit-test.js +++ b/test/unit-test.js @@ -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}', -- 2.34.1