Skip to content

Instantly share code, notes, and snippets.

@asbjornenge
Created March 6, 2015 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asbjornenge/c39ff44b33680eeab2fd to your computer and use it in GitHub Desktop.
Save asbjornenge/c39ff44b33680eeab2fd to your computer and use it in GitHub Desktop.
Browserify img -> base64 data uri transform
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() }
)
}
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