Created
March 17, 2017 19:27
-
-
Save lourd/e7a38c73c997e05ba430f2cba5135756 to your computer and use it in GitHub Desktop.
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 loaderUtils = require('loader-utils') | |
/** | |
* Turns a javascript object into sass variables | |
* @param {Object} obj POJO, values should be strings | |
* @param {String} options.prefix pre-pended to each sass variable | |
* @return {String} string to be injected into a Sass stylesheet | |
*/ | |
function sassify(obj, { prefix = '' } = {}) { | |
return Object.entries(obj).reduce((str, [key, value]) => { | |
return `${str}$${prefix}${key}: ${value};\n` | |
}, '') | |
} | |
function loader(content) { | |
this.cacheable() | |
const options = Object.assign({ prefix: '' }, loaderUtils.getOptions(this)) | |
const obj = this.exec(content, this.resourcePath) | |
const res = sassify(obj, options) | |
console.log('SASSIFY!', res) | |
return res | |
} | |
module.exports = loader | |
module.exports.sassify = sassify |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment