michaelwayman / node-sass-chokidar

A thin wrapper around node-sass to replicate the --watch using chokidar instead of Gaze
MIT License
291 stars 34 forks source link

a way to inject process.env to our sass files? #64

Closed sprlwrksAprilmintacpineda closed 6 years ago

sprlwrksAprilmintacpineda commented 6 years ago

Could it be possible for node-sass-chokidar to inject process.env files or maybe allow us to create a js file that would be run that would allow us to inject any variables that we like. inclusing the process.env?

sprlwrksAprilmintacpineda commented 6 years ago

I found my answer using --functions tag.

example here:

require('dotenv').config();
const types = require('node-sass').types;

function cleanSlashes (str) {
  return str
    .replace(/^\//, '')
    .replace(/\/$/, '');
}

module.exports = {
  url: function (_path, done) {
    const public_url = cleanSlashes(process.env.PUBLIC_URL) + '/';

    if (public_url !== '/') {
      done (types.String(`url('/${public_url}${cleanSlashes(_path.getValue())}')`));
    } else {
      done(types.String(`url('${public_url}${cleanSlashes(_path.getValue())}')`));
    }
  }
};
ratul-srit commented 6 years ago

@sprlwrksAprilmintacpineda how do you use the url function in scss file?