diff --git a/Gruntfile.js b/Gruntfile.js index 19850ee..1caf9fb 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -10,6 +10,7 @@ module.exports = function (grunt) { grunt.initConfig({ + jshint: { all: [ 'Gruntfile.js', @@ -20,11 +21,29 @@ module.exports = function (grunt) { jshintrc: '.jshintrc' } }, + clean: { test: ['tmp'] }, + nodeunit: { tasks: ['test/*_test.js'] + }, + + // Configuration to be run (and then tested). + base64: { + default: { + options: { + test: true + }, + src: [ + "test/fixtures/test-png.png", + "test/fixtures/test-gif.gif", + "test/fixtures/test-jpg.jpg", + "test/fixtures/test-bmp.bmp" + ], + dest: "tmp/base64.css" + } } }); @@ -34,11 +53,10 @@ module.exports = function (grunt) { grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks('grunt-contrib-nodeunit'); grunt.loadNpmTasks('grunt-contrib-internal'); - grunt.loadNpmTasks('grunt-base64'); grunt.registerTask('mkdir', grunt.file.mkdir); - + grunt.registerTask('test', [ 'clean', 'mkdir:tmp', 'nodeunit', 'clean' ]); - + grunt.registerTask('default', [ 'test', 'base64' ]); }; diff --git a/package.json b/package.json index 1ac9666..8263426 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,8 @@ "test": "grunt test" }, "dependencies": { - "filesize": "~1.7.9" + "filesize": "~1.7.9", + "datauri": "~0.2.0" }, "devDependencies": { "grunt-contrib-internal": "~0.4.3", diff --git a/tasks/base64.js b/tasks/base64.js index 014f9a8..923f91d 100644 --- a/tasks/base64.js +++ b/tasks/base64.js @@ -14,9 +14,48 @@ module.exports = function (grunt) var fs = require('fs'); var childProcess = require('child_process'); var filesize = require('filesize'); + var datauri = require('datauri'); grunt.registerMultiTask('base64', 'create base64 encoded data-uris for css from images', function () { - var options = this.options(); - }); + var options = this.options({ + + }); + + + this.files.forEach(function(f) + { + // filter valid files and map data uri generation + var data = f.src.filter( fileExists ).map( generateDataUri ).join('\n'); + + // Write the destination file. + grunt.file.write(f.dest, data); + + grunt.log.writeln('File "' + f.dest + '" created.'); + }); + + + // generates a datauri object from file + // ~~~~~~~~~~~~~~~~~~~~~ + function generateDataUri( filepath ) + { + var dataObj = new datauri( filepath ); + + return dataObj.getCss(); + } + + + // Warn on and remove invalid source files (if nonull was set). + // ~~~~~~~~~~~~~~~~~~~~~ + function fileExists( filepath ) + { + if (!grunt.file.exists(filepath)) + { + grunt.log.warn('source file "' + filepath + '" not found.'); + return false; + } + + return true; + } + }); }; diff --git a/test/base64_test.js b/test/base64_test.js index bfd0e5b..80d55e8 100644 --- a/test/base64_test.js +++ b/test/base64_test.js @@ -3,4 +3,17 @@ var grunt = require('grunt'); var fs = require('fs'); exports.imagemin = { + + setUp: function( done ) + { + done(); + }, + + base64_options: function( test ) + { + test.expect( 1 ); + test.equal( 1, 1, "random..." ); + test.done(); + } + }; diff --git a/test/fixtures/test-bmp.bmp b/test/fixtures/test-bmp.bmp new file mode 100644 index 0000000..4a24e4f Binary files /dev/null and b/test/fixtures/test-bmp.bmp differ diff --git a/test/fixtures/test-gif.gif b/test/fixtures/test-gif.gif new file mode 100644 index 0000000..4d23612 Binary files /dev/null and b/test/fixtures/test-gif.gif differ diff --git a/test/fixtures/test-jpg.jpg b/test/fixtures/test-jpg.jpg new file mode 100644 index 0000000..73dcf44 Binary files /dev/null and b/test/fixtures/test-jpg.jpg differ diff --git a/test/fixtures/test-png.png b/test/fixtures/test-png.png new file mode 100644 index 0000000..87908eb Binary files /dev/null and b/test/fixtures/test-png.png differ