diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..a4f4247 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,11 @@ +# http://editorconfig.org +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 2 +indent_style = space +insert_final_newline = true +max_line_length = 80 +trim_trailing_whitespace = true diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..5054ab3 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,7 @@ +# Images +*.gif binary +*.jpg binary +*.png binary + +# Text +* text eol=lf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f0af85c --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +# Directories +/.idea +/.sass-cache +/node_modules + +# Files +*.DS_Store +*.log diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..b6af4c5 --- /dev/null +++ b/.npmignore @@ -0,0 +1,9 @@ +# Directories +/.idea +/node_modules +/test_files + +# Files +*.DS_Store +*.log +test.js \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..3dc476d --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +language: node_js + +node_js: + - "6.9.1" + +notifications: + email: false + +sudo: false diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..44ab5ed --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 Joshua McFarland + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..9a8ec2e --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +## datauri-brunch +[![Build Status](https://travis-ci.org/mcfarljw/datauri-brunch.svg?branch=master)](https://travis-ci.org/mcfarljw/datauri-brunch) + +## Usage +Install the plugin via npm with `npm install --save-dev datauri-brunch`. + +### Configuration + +```javascript +module.exports = { + plugins: { + datauri: { + pattern: /\.(gif|jpg|png)/ + } + } +}; +``` diff --git a/index.js b/index.js new file mode 100644 index 0000000..72371a9 --- /dev/null +++ b/index.js @@ -0,0 +1,65 @@ +'use strict'; + +const filesystem = require('fs'); +const lodash = require('lodash'); +const mimeTypes = require('mime-types'); + +class DataUriCompiler { + + constructor(config) { + this.config = lodash.defaultsDeep( + lodash.cloneDeep(config && config.plugins && config.plugins.datauri || {}), + { + pattern: /\.(gif|jpg|png)/ + } + ); + + if (this.config.pattern) { + this.pattern = this.config.pattern; + } + } + + getDataUri(path) { + return new Promise( + (resolve, reject) => { + filesystem.readFile( + path, + (error, data) => { + if (error) { + reject(error); + } + + resolve('data:' + mimeTypes.lookup(path) + ';base64,' + data.toString('base64')); + } + ); + } + ); + } + + compile(file) { + return new Promise( + (resolve, reject) => { + const result = this.getDataUri(file.path); + + result.catch( + (error) => { + reject(error); + } + ); + + result.then( + (data) => { + resolve('module.exports = ' + JSON.stringify(data) + ';'); + } + ); + } + ); + } + +} + +DataUriCompiler.prototype.brunchPlugin = true; +DataUriCompiler.prototype.pattern = /\.(gif|jpg|png)/; +DataUriCompiler.prototype.type = 'template'; + +module.exports = DataUriCompiler; diff --git a/package.json b/package.json new file mode 100644 index 0000000..c1501da --- /dev/null +++ b/package.json @@ -0,0 +1,32 @@ +{ + "name": "datauri-brunch", + "version": "2.0.0", + "description": "Adds data uri format support to Brunch.", + "author": { + "name": "Joshua McFarland", + "email": "mcfarljw@gmail.com", + "url": "https://github.com/mcfarljw" + }, + "license": "MIT", + "keywords": [ + "brunchplugin", + "datauri" + ], + "repository": { + "type": "git", + "url": "https://github.com/mcfarljw/datauri-brunch.git" + }, + "scripts": { + "test": "./node_modules/.bin/mocha" + }, + "dependencies": { + "async": "~2.1.0", + "lodash": "~4.17.0", + "loggy": "~0.3.0", + "mime-types": "~2.1.0" + }, + "devDependencies": { + "chai": "~3.5.0", + "mocha": "~3.1.0" + } +} diff --git a/test.js b/test.js new file mode 100644 index 0000000..ce53fb9 --- /dev/null +++ b/test.js @@ -0,0 +1,92 @@ +'use strict'; + +const expect = require('chai').expect; + +describe('DataUriPlugin', function() { + const Plugin = require('./index.js'); + + it('should use gif, jpg and png file patterns by default', + () => { + const plugin = new Plugin(); + const pattern = /\.(gif|jpg|png)/.toString(); + + expect(plugin.pattern.toString()).to.equal(pattern); + } + ); + + it('should compile gif files to data uri format', + () => { + const plugin = new Plugin(); + + return new Promise( + (resolve, reject) => { + const result = plugin.compile({path: 'test_files/dog1.gif'}); + + result.catch( + (error) => { + reject(error); + } + ); + + result.then( + (data) => { + expect(data.indexOf('data:image/gif;base64,')).to.not.equal(-1); + resolve(); + } + ); + } + ); + } + ); + + it('should compile jpg files to data uri format', + () => { + const plugin = new Plugin(); + + return new Promise( + (resolve, reject) => { + const result = plugin.compile({path: 'test_files/cat1.jpg'}); + + result.catch( + (error) => { + reject(error); + } + ); + + result.then( + (data) => { + expect(data.indexOf('data:image/jpeg;base64,')).to.not.equal(-1); + resolve(); + } + ); + } + ); + } + ); + + it('should compile png files to data uri format', + () => { + const plugin = new Plugin(); + + return new Promise( + (resolve, reject) => { + const result = plugin.compile({path: 'test_files/bird1.png'}); + + result.catch( + (error) => { + reject(error); + } + ); + + result.then( + (data) => { + expect(data.indexOf('data:image/png;base64,')).to.not.equal(-1); + resolve(); + } + ); + } + ); + } + ); + +}); diff --git a/test_files/bird1.png b/test_files/bird1.png new file mode 100644 index 0000000..bf4c072 Binary files /dev/null and b/test_files/bird1.png differ diff --git a/test_files/cat1.jpg b/test_files/cat1.jpg new file mode 100644 index 0000000..95e55ed Binary files /dev/null and b/test_files/cat1.jpg differ diff --git a/test_files/dog1.gif b/test_files/dog1.gif new file mode 100644 index 0000000..ea6766f Binary files /dev/null and b/test_files/dog1.gif differ