-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 09ee419
Showing
13 changed files
with
271 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Directories | ||
/.idea | ||
/.sass-cache | ||
/node_modules | ||
|
||
# Files | ||
*.DS_Store | ||
*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)/ | ||
} | ||
} | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
); | ||
} | ||
); | ||
} | ||
); | ||
|
||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.