Skip to content

Instantly share code, notes, and snippets.

@lourd
Created March 17, 2017 19:27
Show Gist options
  • Save lourd/e7a38c73c997e05ba430f2cba5135756 to your computer and use it in GitHub Desktop.
Save lourd/e7a38c73c997e05ba430f2cba5135756 to your computer and use it in GitHub Desktop.
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