【问题标题】:Compiling scss into css using node-sass with Node.js使用 node-sass 和 Node.js 将 scss 编译成 css
【发布时间】:2019-03-30 20:56:03
【问题描述】:

我正在尝试使用 node-sass 将我的 scss 文件编译成 css 文件,我已经知道如何使用命令行执行此操作。 我有这个npm-script 在我的package.json 文件中:

"scripts": {
"scss": "node-sass --watch fileName/scss -o FileName/css"
}

当我跑步时:

npm run scss

它工作正常。 我想知道的是如何使用(来自node-sass项目页面):

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

sass.render({
  file: scss_filename,
  [, options..]
}, function(err, result) { /*...*/ });

我在我的app.js 中尝试这个:

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

sass.render({
  file: __dirname + '/scss/style.scss',
  outFile: __dirname + '/css/style.css',
}); 

但它不起作用,我对此没有什么经验,我可以使用一些帮助。

【问题讨论】:

    标签: node.js npm node-sass


    【解决方案1】:

    在您链接到的同一文档中

    sass.render({
        ...
        outFile: yourPathTotheFile,
      }, function(error, result) { // node-style callback from v3.0.0 onwards
        if(!error){
          // No errors during the compilation, write this result on the disk
          fs.writeFile(yourPathTotheFile, result.css, function(err){
            if(!err){
              //file written on disk
            }
          });
        }
      });
    });
    

    sass.render() 不会写入文件,它只会将result 传递给回调。您需要使用fs 自己编写,如上例所示。

    【讨论】:

    • 这里的fs 是什么?我收到fs is not defined 的错误。应该指什么?
    • node file system 添加const fs = require('fs');
    猜你喜欢
    • 2017-12-31
    • 2015-10-05
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2019-04-17
    • 1970-01-01
    • 2016-01-23
    相关资源
    最近更新 更多