Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mcfarljw committed Nov 26, 2016
0 parents commit 09ee419
Show file tree
Hide file tree
Showing 13 changed files with 271 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Images
*.gif binary
*.jpg binary
*.png binary

# Text
* text eol=lf
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Directories
/.idea
/.sass-cache
/node_modules

# Files
*.DS_Store
*.log
9 changes: 9 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Directories
/.idea
/node_modules
/test_files

# Files
*.DS_Store
*.log
test.js
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: node_js

node_js:
- "6.9.1"

notifications:
email: false

sudo: false
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)/
}
}
};
```
65 changes: 65 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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;
32 changes: 32 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "datauri-brunch",
"version": "2.0.0",
"description": "Adds data uri format support to Brunch.",
"author": {
"name": "Joshua McFarland",
"email": "[email protected]",
"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"
}
}
92 changes: 92 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -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();
}
);
}
);
}
);

});
Binary file added test_files/bird1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test_files/cat1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test_files/dog1.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 09ee419

Please sign in to comment.