我想使用NodeJS和“fs.writeFile”生成.scss文件,但它不起作用,我不知道最好的方法是什么。
我的js代码(路径='/ sass/bottom/_settings.scss'):
BottomGenerator.prototype.createSettings = function(path, color, columnWidth){
var _settings = "
@import \"compass/css3\";
@import \"compass/typography\";
@import \"compass/css3/transition\";
@import \"compass/css3/transform\";
@import \"compass/css3/text-shadow\";
@import \"compass/css3/box-shadow\";
@import \"compass/css3/user-interface\";
@import url(http://fonts.googleapis.com/css?family=Open+Sans:400,600);
$total-columns : 12;
$column-width : "+columnWidth+"px;
$gutter-width : 10px;
$container-width : ( $total-columns * $column-width) + ( $total-columns * $gutter-width ) - 10;
$color : "+color+";
$gray : lighten(#000000, 10%);
";
fs.writeFile(path, _settings, function (err) {
if (err) throw err;
});
}
编辑1(发现问题):
var str = fs.readFileSync(lib+'/_settings.ejs', 'utf8');
BottomGenerator.prototype.createSettings = function(path, color, columnWidth){
var _settings = ejs.render(str, {
color: color,
columnWidth: columnWidth
});
fs.writeFile(path, _settings, function (err) {
if (err) throw err;
});
}
和一个ejs文件(_settings.ejs):
@import "compass/css3";
@import "compass/typography";
@import "compass/css3/transition";
@import "compass/css3/transform";
@import "compass/css3/text-shadow";
@import "compass/css3/box-shadow";
@import "compass/css3/user-interface";
@import url(http://fonts.googleapis.com/css?family=Open+Sans:400,600);
$total-columns : 12;
$column-width : <%= columnWidth %>px;
$gutter-width : 10px;
$container-width : ( $total-columns * $column-width) + ( $total-columns * $gutter-width ) - 10;
$color : <%= color %>;
$gray : lighten(#000000, 10%);