Created
March 6, 2015 08:43
-
-
Save asbjornenge/c39ff44b33680eeab2fd to your computer and use it in GitHub Desktop.
Browserify img -> base64 data uri transform
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
var through = require('through2'); | |
var i = -1 | |
var head = function(type) { i++; return i == 0 ? 'module.exports = "data:image/'+type+';base64,' : '' } | |
var tail = function() { return '"' } | |
var isImg = function(file) { | |
return (/\.((lit)?gif|png|jpg|jpeg)$/).exec(file); | |
} | |
module.exports = function (file) { | |
if (!isImg(file)) return through() | |
var type = isImg(file)[1] | |
return through( | |
function (buf, enc, next) { this.push(head(type)); this.push(buf.toString('base64')); next() }, | |
function (end) { this.push(tail()); end() } | |
) | |
} |
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
import React from 'react' | |
import pony from './graphics/pony.png' | |
export default class Logo extends React.Component { | |
render() { | |
return <img src={pony} /> | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment