Created
July 18, 2018 15:01
-
-
Save DorkNstein/15baab8f928dca78dfa95a98a59e2cb1 to your computer and use it in GitHub Desktop.
Easy way to import local image files in nodejs.
This file contains hidden or 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
const Module = require('module'); | |
const fs = require('fs'); | |
Module._extensions['.jpg'] = function(module, fn) { | |
let base64 = fs.readFileSync(fn).toString('base64'); | |
module._compile('module.exports="data:image/jpg;base64,' + base64 + '"', fn); | |
}; | |
Module._extensions['.png'] = function(module, fn) { | |
let base64 = fs.readFileSync(fn).toString('base64'); | |
module._compile('module.exports="data:image/png;base64,' + base64 + '"', fn); | |
}; | |
const image = require('./logo.jpg'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment