/*
 * grunt-css-amd
 * http://gruntjs.com/
 *
 * Copyright (c) 2012 Thiago Felix, contributors
 * Licensed under the MIT license.
 * https://github.com/thiagofelix/grunt-css-amd/blob/master/LICENSE-MIT
 */

module.exports = function(grunt) {
  "use strict";

  grunt.registerMultiTask("cssamd", "Compile all css files into javascript amd module files", function() {

    var helpers = require('grunt-lib-contrib').init(grunt);
    var options = helpers.options(this);

    grunt.verbose.writeflags(options, "Options");
    
    // TODO: ditch this when grunt v0.4 is released
    this.files = this.files || helpers.normalizeMultiTaskFiles(this.data, this.target);

    var srcFiles;
    var sourceCss;
    var sourceModuled;
    var fileName; 
    var outputFilename;
    var moduleName;
    
    this.files.forEach(function(files) {      

      srcFiles = grunt.file.expandFiles(files.src);

      srcFiles.forEach(function(srcFile) {        
        grunt.log.writeln('Processing "' + srcFile + ".");

        fileName = srcFile.substr(srcFile.lastIndexOf('/')+1);

        try{                      
          moduleName = options.moduleNamePrefix || files.dest + fileName;   
          sourceCss = grunt.file.read(srcFile);
          sourceModuled = modulyCSS(moduleName, sourceCss);          
        } catch(e) {
          grunt.log.error(e);
          grunt.fail.warn('Failed to compile css '+srcFile+'.'); 
        }

        try{          
          outputFilename = files.dest + fileName + '.js';
          grunt.file.write(outputFilename, sourceModuled);
          grunt.log.writeln('File "' + outputFilename + '" created.');
        } catch(e) {
          grunt.log.error(e);
          grunt.fail.warn('Failed to save transformed css '+outputFilename+'.'); 
        }
      });
    });    
  });
  
  var modulyCSS = function(moduleName, source) {
    try {
      
      var output  = "";

      output += "define('"+moduleName+"',[], function(){\n";
      output += "\tvar style = document.createElement('style');\n";
      output += "\tstyle.appendChild(\n";
      output += "\t\tdocument.createTextNode('"+source.replace(/[\r\n]/g,"")+"')\n";
      output += "\t);\n";
      output += "\treturn style;\n";
      output += "});\n";
      return output;
    } catch (e) {
      grunt.log.error(e);
      grunt.fail.warn('CSS modularization failed.');
    }
  };

};
@@ -200,16 +200,31 @@
 IT%0A */%0A%0A
+%22use strict%22;%0A%0A
 module.e
@@ -253,24 +253,8 @@
 t) %7B
-%0A  %22use strict%22;
 %0A%0A  
@@ -362,434 +362,208 @@
 ) %7B%0A
-%0A
     
-var helpers = require('grunt-lib-contrib').init(grunt
+grunt.log.writeln('Yeah, we are rocking! ', this.files.length
 );%0A
+%0A
     
-var options = helpers.options(this);%0A%0A    grunt.verbose.writeflags(options, %22Options%22);%0A    %0A    // TODO: ditch this when grunt v0.4 is released%0A    this.files = this.files %7C%7C helpers.normalizeMultiTaskFiles(this.data, this.target);%0A%0A    var srcFiles;%0A    var sourceCss;%0A    var sourceModuled;%0A    var fileName; %0A    var outputFilename;%0A    var moduleName
+if (this.files.length %3C 1) %7B%0A      grunt.verbose.warn('Destination not written because no source files were provided.')
 ;%0A    
+%7D%0A
 %0A   
@@ -596,98 +596,40 @@
 on(f
-iles) %7B      %0A%0A      srcFiles = grunt.file.expandFiles(files.src);%0A%0A      srcFiles.forEach
+) %7B%0A      var src = f.src.filter
 (fun
@@ -638,26 +638,28 @@
 ion(
-srcFile
+filepath
 ) %7B
+%0A
         
 %0A   
@@ -658,291 +658,209 @@
     
-%0A        grunt.log.writeln('Processing %22' + srcFile + %22.%22);%0A%0A        fileName = srcFile.substr(srcFile.lastIndexOf('/')+1);%0A%0A        try%7B                      %0A          moduleName = options.moduleNamePrefix %7C%7C files.dest + fileName;   %0A          sourceCss = grunt.file.read(srcFile)
+// Warn on and remove invalid source files (if nonull was set).%0A        if (!grunt.file.exists(filepath)) %7B%0A          grunt.log.warn('Source file %22' + filepath + '%22 not found.');%0A          return false
 ;%0A  
@@ -869,123 +869,91 @@
     
-  sourceModuled = modulyCSS(moduleName, sourceCss);          %0A        %7D catch(e) %7B%0A          grunt.log.error(e);%0A  
+%7D else %7B%0A          return true;%0A        %7D%0A      %7D);%0A%0A      if (src.length === 0) %7B%0A
     
@@ -966,57 +966,91 @@
 unt.
-fail
+log
 .warn('
-Failed to compile css '+srcFile+'
+Destination (' + f.dest + ') not written because src files were empty
 .');
- 
 %0A   
@@ -1058,230 +1058,185 @@
     
-%7D%0A%0A        try%7B          
+return;%0A      %7D%0A%0A      var css = grunt.file.read(src);%0A
 %0A      
-   
+var
  output
-Filename = files.dest + fileName + '.js';%0A          grunt.file.write(outputFilename, sourceModuled);
+ = CSS2Module(css);%0A%0A      // Write the destination file.
 %0A      
-    
 grunt.
-log
+file
 .write
-ln('File %22' + outputFilename + '%22 created.'
+(f.dest, output
 );
-%0A
         
 %7D ca
@@ -1235,55 +1235,48 @@
     
-%7D catch(e) %7B%0A          grunt.log.error(e);%0A    
+    %0A%0A      // Print a success message.%0A
     
@@ -1287,130 +1287,88 @@
 unt.
-fail.war
+log.writel
 n('F
-a
 ile
-d to save transformed css '+outputFilename+'.'); %0A        %7D%0A      %7D);%0A    %7D);    
+ %22' + f.dest + '%22 created.');%0A%0A    %7D);%0A
 %0A  %7D);%0A
-  
 %0A  var 
-modulyCSS
+CSS2Module
  = f
@@ -1379,20 +1379,8 @@
 ion(
-moduleName, 
 sour
@@ -1455,25 +1455,8 @@
 ine(
-'%22+moduleName+%22',
 %5B%5D, 
@@ -1887,7 +1887,7 @@
  %7D;%0A
-%0A
 %7D;
+%0A

Update cssamd.js