From 7dafa75940b7191ee06bbd26dd9e7334545f55df Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Thu, 5 Jun 2014 00:05:40 +0100 Subject: [PATCH] Fixes #265 - adds support for multiple input files. --- History.md | 1 + README.md | 2 +- bin/cleancss | 26 +++++++++++++++----------- lib/clean.js | 2 +- lib/imports/inliner.js | 15 +++++++++++++-- test/binary-test.js | 5 +++++ 6 files changed, 36 insertions(+), 15 deletions(-) diff --git a/History.md b/History.md index 90d744d6..2edf670b 100644 --- a/History.md +++ b/History.md @@ -15,6 +15,7 @@ * Fixed issue [#247](https://github.com/GoalSmashers/clean-css/issues/247) - removes deprecated `selectorsMergeMode` switch. * Refixed issue [#250](https://github.com/GoalSmashers/clean-css/issues/250) - based on new quotation marks removal. * Fixed issue [#257](https://github.com/GoalSmashers/clean-css/issues/257) - turns hsla/rgba to transparent if possible. +* Fixed issue [#265](https://github.com/GoalSmashers/clean-css/issues/265) - adds support for multiple input files. * Fixed issue [#275](https://github.com/GoalSmashers/clean-css/issues/275) - handling transform properties. [2.1.8 / 2014-03-28](https://github.com/GoalSmashers/clean-css/compare/v2.1.7...v2.1.8) diff --git a/README.md b/README.md index bb5fcfb9..047b5842 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ Clean-css accepts the following command line arguments (please make sure you use `` as the very last argument to avoid potential issues): ``` -cleancss [options] +cleancss [options] source-file, [source-file, ...] -h, --help Output usage information -v, --version Output the version number diff --git a/bin/cleancss b/bin/cleancss index a809bcd4..f34fa84f 100755 --- a/bin/cleancss +++ b/bin/cleancss @@ -15,7 +15,7 @@ var isWindows = process.platform == 'win32'; // Specify commander options to parse command line params correctly commands .version(buildVersion, '-v, --version') - .usage('[options] ') + .usage('[options] source-file, [source-file, ...]') .option('-b, --keep-line-breaks', 'Keep line breaks') .option('--s0', 'Remove all special comments, i.e. /*! comment */') .option('--s1', 'Remove all special comments but the first one') @@ -45,7 +45,7 @@ commands.on('--help', function() { commands.parse(process.argv); var options = { - source: null, + sources: null, target: null }; var cleanOptions = {}; @@ -81,18 +81,22 @@ if (commands.debug) if (commands.timeout) cleanOptions.inliner = { timeout: parseFloat(commands.timeout) * 1000 }; if (commands.args.length > 0) { - var source = commands.args[0]; - options.source = source; - cleanOptions.relativeTo = path.dirname(path.resolve(source)); + var relativeTo = (cleanOptions.noRebase ? false : cleanOptions.root) || commands.args[0]; + options.sources = commands.args; + cleanOptions.relativeTo = path.dirname(path.resolve(relativeTo)); } // ... and do the magic! -if (options.source) { - fs.readFile(options.source, 'utf8', function(error, data) { - if (error) - throw error; - minify(data); - }); +if (options.sources) { + var data = options.sources + .map(function(source) { + if (cleanOptions.processImport === false) + source += '@shallow'; + + return '@import url(' + path.relative(cleanOptions.relativeTo, path.resolve(source)) + ');'; + }) + .join(''); + minify(data); } else { var stdin = process.openStdin(); stdin.setEncoding('utf-8'); diff --git a/lib/clean.js b/lib/clean.js index f0a706dd..51bf84c9 100644 --- a/lib/clean.js +++ b/lib/clean.js @@ -53,7 +53,7 @@ CleanCSS.prototype.minify = function(data, callback) { if (Buffer.isBuffer(data)) data = data.toString(); - if (options.processImport) { + if (options.processImport || data.indexOf('@shallow') > 0) { // inline all imports var self = this; var runner = callback ? diff --git a/lib/imports/inliner.js b/lib/imports/inliner.js index 80723b55..02f51036 100644 --- a/lib/imports/inliner.js +++ b/lib/imports/inliner.js @@ -24,6 +24,12 @@ module.exports = function Inliner(context, options) { var inlinerOptions = merge(defaultOptions, options || {}); var process = function(data, options) { + if (options.shallow) { + options.shallow = false; + options._shared.done.push(data); + return processNext(options); + } + options._shared = options._shared || { done: [], left: [] @@ -145,8 +151,11 @@ module.exports = function Inliner(context, options) { }; var inline = function(data, nextStart, nextEnd, options) { + options.shallow = data.indexOf('@shallow') > 0; + var importDeclaration = data .substring(data.indexOf(' ', nextStart) + 1, nextEnd) + .replace(/@shallow\)$/, ')') .trim(); var viaUrl = importDeclaration.indexOf('url(') === 0; @@ -237,7 +246,8 @@ module.exports = function Inliner(context, options) { relativeTo: parsedUrl.protocol + '//' + parsedUrl.host, _shared: options._shared, whenDone: options.whenDone, - visited: options.visited + visited: options.visited, + shallow: options.shallow }); }); }) @@ -294,7 +304,8 @@ module.exports = function Inliner(context, options) { _shared: options._shared, visited: options.visited, whenDone: options.whenDone, - localOnly: options.localOnly + localOnly: options.localOnly, + shallow: options.shallow }); }; diff --git a/test/binary-test.js b/test/binary-test.js index 419379db..ec661ec6 100644 --- a/test/binary-test.js +++ b/test/binary-test.js @@ -147,6 +147,11 @@ exports.commandsSuite = vows.describe('binary commands').addBatch({ assert.equal(stdout, minimized); } }), + 'from multiple sources': binaryContext('./test/data/partials/one.css ./test/data/partials/five.css', { + 'should minimize all': function(error, stdout) { + assert.equal(stdout, '.one{color:red}.five{background:url(data:image/jpeg;base64,/9j/)}'); + } + }), 'to file': binaryContext('-o ./reset1-min.css ./test/data/reset.css', { 'should give no output': function(error, stdout) { assert.equal(stdout, ''); -- 2.34.1