P.K. P.K. - 10 days ago 6
Javascript Question

compile scss with node.js from string

I am trying to compile scss using node.js and node-sass module, the problem is that i was only able to compile sass code, when i insert scss code it says error wrong syntax. Odd is that when i put the same code to file and compile from *.scss file it works. Unfortunately nature of my application requires that it is compiled from string. Anyone has some ideas ? ;)

this is the code that works:

var sass = require('node-sass');

/* test.scss content: $text-color:#555555;\nbody{background:$text-color;} */

var output = sass.render({
file: __dirname + '/test.scss',
indentedSyntax: true,
outputStyle : 'expanded'
}, function(err, result) {
if(err) console.log(err);
if(result) console.log(result, result.css.toString());
});


and this is the one that doesnt:

var sass = require('node-sass');

var dataTemp = '$text-color:#555555;\nbody{background:$text-color;}';

var output = sass.render({
data: dataTemp,
indentedSyntax: true,
outputStyle : 'expanded'
}, function(err, result) {
if(err) console.log(err);
if(result) console.log(result, result.css.toString());
});


It seems odd because official doc contains example with using scss code from data.

Answer

According to docs, indentedSyntax option enables indent-sensitive sass mode. Looks like render expects sass and fails when gets scss instead. Try to remove this line:

indentedSyntax: true,

I guess in the first case when you loaded code from the file, renderer used .scss extension to determine syntax rules. Hope it helps.