【发布时间】:2018-08-23 12:27:00
【问题描述】:
如何配置 grunt-sass 以使用底层 node-sass 监视选项来监视更改?
我知道我可以使用 grunt-contrib-watch 实现相同的功能来监视更改并重新运行 grunt-sass 任务,但这会更慢,因为它会重新编译整个事情而不是只编译更改。
谢谢!
【问题讨论】:
如何配置 grunt-sass 以使用底层 node-sass 监视选项来监视更改?
我知道我可以使用 grunt-contrib-watch 实现相同的功能来监视更改并重新运行 grunt-sass 任务,但这会更慢,因为它会重新编译整个事情而不是只编译更改。
谢谢!
【问题讨论】:
回答我自己的问题,以防万一这对其他人有帮助:
我发现解决这个问题的唯一方法是通过 grunt 使用 node-sass CLI。为此,请安装令人惊叹的 grunt-exec 任务并将其设置为运行带有 --watch 选项的命令。
使用示例(支持多个 includePath 目录):
exec: {
nodeSass: {
cmd: function() {
// Include path option string built from the gruntConfig.cssFiles array.
var includeFilesOption = gruntConfig.cssFiles.map(function(cssFilePath) {
return '--include-path ' + cssFilePath;
}).join(' ');
return 'node-sass app/scss/style.scss app/generated/style.css' + includeFilesOption + ' --watch';
}
}
}
您还需要通过 npm 安装 node-sass。在这种情况下,您只需将以下内容添加到您的 package.json 文件中:
"scripts": {
"install": "npm install node-sass -g"
},
【讨论】: