In an application I'm working, I have to dynamically compile SASS before rendering on the client (caching system is coming, don't worry). Currently I'm using node-sass and everything is working great.
Here is what I'm working on so far. Other project-specific code has been removed for brevity:
var sass = require('node-sass'),
autoprefixer = require('autoprefixer-core'),
vars = require('postcss-simple-vars'),
postcss = require('postcss'),
function compileCSS() {
var result = sass.renderSync({
file: 'path/to/style.scss'
});
return postcss([autoprefixer]).process(result.css.toString()).css;
}
The wrinkle is that now I need to pass in dynamic data from Node and have that compile like a normal SASS variable. Initially I tried using PostCSS, because I noticed that variable injection was something it could do. Unfortunately, that didn't work. PostCSS kicks in after the compilation phase, which fails miserably by this point.
Next, I tried to use underscore templates to try and overwrite using node-sass' importer()
:
var result = sass.renderSync({
file: 'path/to/style.scss',
importer: function(url, prev, done) {
var content = fs.readFileSync(partial_path),
partial = _.template(content.toString());
return {
contents: partial({ test: 'test' })
};
}
});
Which resulted in the following error:
Error: error reading values after :
Obviously SASS didn't like underscore's variable syntax..
TL;DR
How can I pass dynamic variables to SASS from within my Node application?
Additional Information
- My team and I are not completely adverse to switching to something like Stylus; however, we have made significant progress so far and it would be a pain.