cssnode.jssassnode-sass

Convert Sass String (not SCSS) to CSS String



I need to compile SASS and SCSS Strings to CSS Strings for a project. Therefore I´m using the node package node-sass. Converting SCSS works fine, but SASS doesn't.

Example code for SASS:

'use strict';

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

var dataTemp = 'body{background:blue; a{color:black;}}';

var output = sass.renderSync({
  data: dataTemp
});

console.log(output.css.toString());

The problem comes, when I try to convert something like this, which should be valid .sass syntax

'use strict';

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

var dataTemp = '.red\n color: red';

var output = sass.renderSync({
  data: dataTemp
});

console.log(output.css.toString());

Console output:

Error: Invalid CSS after ".": expected 1 selector or at-rule, was ".red color: red "

So obviously node-sass can't compile .sass code that way. The syntax of node-sass is:

var result = sass.renderSync({
  data: scss_content
  [, options..]
});

Inside the object you can define options and even functions. Maybe is there a way to compile the data in such a function?

Any other solutions or recommendations for other packages?


Solution

  • Well by adding an option named 'indentedSyntax' and setting it to 'true', node-sass can compile .sass code as well. Sorry, should have read the documentation more precisely. Thanks for helping!

    'use strict';
    
    var sass = require('node-sass');
    
    var dataTemp = '.red\n color: red';
    
    var output = sass.renderSync({
      data: dataTemp,
      indentedSyntax: true,
      outputStyle : 'compressed'
    });
    
    console.log(output.css.toString());
    

    Log: ".red{color:red}"