diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/README.md b/README.md index 62f5df7..270e816 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,14 @@ Convert and replace image-files within your DOM/HTML to base64-encoded data. +## Options + +```js +img64({ + baseDir: 'path/to/images' +}) +``` + ## Example ##### gulpfile.js @@ -42,7 +50,6 @@ gulp.task('default', function () { ... ``` - ### License MIT © 247even diff --git a/index.js b/index.js index 115a3db..3f18f96 100644 --- a/index.js +++ b/index.js @@ -6,7 +6,8 @@ var fs = require('fs'); var path = require('path'); var mime = require('mime'); -module.exports = function() { +module.exports = function(opts) { + opts = opts || {} // create a stream through which each file will pass return through.obj(function(file, enc, callback) { @@ -29,10 +30,9 @@ module.exports = function() { var ssrc = this.attr('src'); var isdata = ssrc.indexOf("data"); if (ssrc != "" && typeof ssrc != 'undefined' && isdata !== 0) { - var spath = path.join(file.base, ssrc); + var spath = path.join(opts.baseDir || file.base, ssrc); var mtype = mime.lookup(spath); if (mtype != 'application/octet-stream') { - console.log(mtype); var sfile = fs.readFileSync(spath); var simg64 = new Buffer(sfile).toString('base64'); this.attr('src', 'data:' + mtype + ';base64,' + simg64); diff --git a/test/fixtures/clock.png b/test/fixtures/custom_dir/elsewhere/clock2.png similarity index 100% rename from test/fixtures/clock.png rename to test/fixtures/custom_dir/elsewhere/clock2.png diff --git a/test/fixtures/custom_dir/input.html b/test/fixtures/custom_dir/input.html new file mode 100644 index 0000000..3ccce45 --- /dev/null +++ b/test/fixtures/custom_dir/input.html @@ -0,0 +1,3 @@ + + + diff --git a/test/fixtures/default_dir/clock.png b/test/fixtures/default_dir/clock.png new file mode 100644 index 0000000..fa3c8a7 Binary files /dev/null and b/test/fixtures/default_dir/clock.png differ diff --git a/test/fixtures/input.html b/test/fixtures/default_dir/input.html similarity index 80% rename from test/fixtures/input.html rename to test/fixtures/default_dir/input.html index 0184554..85f5280 100644 --- a/test/fixtures/input.html +++ b/test/fixtures/default_dir/input.html @@ -1,3 +1,3 @@ - \ No newline at end of file + diff --git a/test/fixtures/output.html b/test/fixtures/output.html index 09df86e..ac090b5 100644 --- a/test/fixtures/output.html +++ b/test/fixtures/output.html @@ -1,3 +1,3 @@ - \ No newline at end of file + diff --git a/test/test.js b/test/test.js index e2067c9..5459193 100644 --- a/test/test.js +++ b/test/test.js @@ -8,20 +8,26 @@ var mime = require('mime'); describe('gulp-img64', function() { describe('in buffer mode', function() { - it('should replace images in DOM with base64 data', function(done) { - - var filename = path.join(__dirname, '/fixtures/input.html'); + function getInput(file) { + var filename = path.join(__dirname, file); - var input = new gutil.File({ + return new gutil.File({ base: path.dirname(filename), path: filename, contents: new Buffer(fs.readFileSync(filename, 'utf8')) }); + } + function getOutput() { + return fs.readFileSync(path.join(__dirname, '/fixtures/output.html'), 'utf8') + } + + it('should replace images in DOM with base64 data', function(done) { + var input = getInput('/fixtures/default_dir/input.html'); var stream = img64(); stream.on('data', function(newFile) { - assert.equal(String(newFile.contents), fs.readFileSync(path.join(__dirname, '/fixtures/output.html'), 'utf8')); + assert.equal(String(newFile.contents), getOutput()); done(); }); @@ -29,5 +35,20 @@ describe('gulp-img64', function() { }); + context('when baseDir option is given', function() { + it('should replace images in DOM with base64 data', function(done) { + var input = getInput('/fixtures/custom_dir/input.html'); + var stream = img64({baseDir: path.join(__dirname, '/fixtures/custom_dir/elsewhere')}); + + stream.on('data', function(newFile) { + assert.equal(String(newFile.contents), getOutput()); + done(); + }); + + stream.write(input); + + }) + }) + }); -}); \ No newline at end of file +});