Fixes #265 - adds support for multiple input files.
authorJakub Pawlowicz <jakub@goalsmashers.com>
Wed, 4 Jun 2014 23:05:40 +0000 (00:05 +0100)
committerJakub Pawlowicz <jakub@goalsmashers.com>
Thu, 5 Jun 2014 21:44:47 +0000 (22:44 +0100)
History.md
README.md
bin/cleancss
lib/clean.js
lib/imports/inliner.js
test/binary-test.js

index 90d744d..2edf670 100644 (file)
@@ -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)
index bb5fcfb..047b584 100644 (file)
--- a/README.md
+++ b/README.md
@@ -57,7 +57,7 @@ Clean-css accepts the following command line arguments (please make sure
 you use `<source-file>` as the very last argument to avoid potential issues):
 
 ```
-cleancss [options] <source-file>
+cleancss [options] source-file, [source-file, ...]
 
 -h, --help                      Output usage information
 -v, --version                   Output the version number
index a809bcd..f34fa84 100755 (executable)
@@ -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] <source-file>')
+  .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');
index f0a706d..51bf84c 100644 (file)
@@ -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 ?
index 80723b5..02f5103 100644 (file)
@@ -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
     });
   };
 
index 419379d..ec661ec 100644 (file)
@@ -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, '');