Grunt 编译 css 文件需要很长时间,我不确定这是否正常,但常规指南针手表大约需要 5 秒。
所以问题是是否有任何方法可以加快 Grunt 的编译时间,还是坚持使用指南针手表更好?
Running "compass:dist" (compass) task
♀unchanged images/sprite-sf580a96666.png
overwrite stylesheets/app.css (3.263s)
unchanged images/sprite-sf580a96666.png
overwrite stylesheets/app_fr.css (3.289s)
Compilation took 11.116s
Running "watch" task
Completed in 13.974s at Wed Dec 18 2013 13:53:05 GMT-0500 (Eastern Standard Time- Waiting...
OK
>> File "scss_core.scss" changed.
Gruntfile.js:
compass: {
dist: {
options: {
config: 'config.rb'
}
}
},
watch: {
sass: {
files: ['scss/*.scss'],
tasks: ['compass:dist'],
options: {
spawn: false,
}
},
scripts: {
files: ['js/*.js'],
tasks: ['concat', 'uglify'],
options: {
spawn: false,
}
}
}
});
除了 Simon 提到的 grunt-contrib-compass 的watch
选项之外,您还可以使用 grunt-concurrent 来运行两个进程,有效地grunt watch
和compass watch
,彼此并排:
concurrent: {
watch: {
tasks: ['watch', 'compass:watch'],
options: {
logConcurrentOutput: true
}
}
},
compass: {
watch: {
options: {
watch: true
}
}
}
如果你想在构建、部署或任何其他需要compile
而不是watch
的东西时从 Grunt 运行指南针,你需要制作第二个指南针任务并使用它:
compass: {
// Compass grunt module requires a named target property with options.
compile: {
options: {}
}
}
好吧,您可以使用Grunt-contrib-compass watch
选项观看。这将产生指南针手表,因此您将获得更好的性能。虽然这不允许您观看多种类型的文件(例如,如果您还监视 .coffee 文件或始终重建 js 等)。
如果您绝对需要grunt-contrib-watch
,请确保使用 grunt 任务激活 sass 缓存。从粘贴在此处的配置来看,它看起来像是。但是缓存问题通常是指南针需要很长时间才能编译的原因;所以我会仔细检查我的 Gruntfile.js如果我是你的话。
此外,许多精灵和图像处理方法可能需要相当长的时间来处理。
晚了,但如果这对任何人有帮助:
我发现grunt-contrib-watch和sass的表现同样糟糕。解决此问题的最佳方法似乎是使用不同的手表插件。我发现grunt-watch-nospawn(与grunt-contrib-watch插件相反)编译sass要快得多。相当显着 - 我看到了大约两秒钟的改进。
如果你想进一步调整速度,你可以使用grunt-sass而不是grunt-contrib-sass,后者使用libsass来提供另一个速度提升。
这与自动前缀器相结合,例如nDmitry的(无法链接,没有代表)这应该填补省略指南针留下的功能空白。
希望有帮助。
这个问题在这一点上已经有几年了,但我想我会添加另一个潜在的原因/解决方案。
首先,尝试使用 --verbose
启动您的 grunt 服务器,并观察您的 sass 任务花费了大部分时间。 有一些插件会报告品尝的每个部分所需的时间,但对我来说,只需观察--verbose
输出就可以非常清楚地了解延迟的位置。 对我来说,这不是实际的烦恼任务,而是加载不必要的依赖项。
正如 Grunt 的 GitHub 存储库上的此问题中所述,某些任务可能需要很长时间才能完成的一个原因是 Grunt 每次运行时都会加载所有任务。 因此,即使 grunt-contrib-watch 仅在您更改 sass 文件时运行 compass:dist 任务,grunt 仍然会加载所有任务及其依赖项。
现在有一个名为jit-grunt(或在npm上)的插件可以解决这个问题,并且只加载运行任务所需的内容。 这有助于我的指南针任务更快地完成。