9

Grunt花了相当长的时间来编译css文件,我不确定这是否正常,但是普通的指南针手表大约需要5秒。Grunt with Compass and Watch compiles slow

所以问题是,如果有什么办法可以加快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, 
      } 
     } 
    } 

}); 

回答

16

随着什么西蒙提到有关watch选项咕噜-的contrib罗盘,你可以使用grunt-concurrent运行两个过程,有效地grunt watchcompass watch,沿着对方:

concurrent: { 
    watch: { 
     tasks: ['watch', 'compass:watch'], 
     options: { 
      logConcurrentOutput: true 
     } 
    } 
}, 
compass: { 
    watch: { 
     options: { 
      watch: true 
     } 
    } 
} 

如果你想运行从咕噜指南针构建,部署,或其他任何需要compile而不是watch,则需要进行第二次罗盘​​任务和使用:

compass: { 
    // Compass grunt module requires a named target property with options. 
    compile: { 
     options: {} 
    } 
} 
+0

虽然这适用于重新加载更改的客户端文件,但如果您的watch任务中包含Expressj,则无法重新加载expressjs。 – gerasalus

+0

@gerasalus我认为你需要添加'livereload:true'作为'watch'任务的一个选项。请参阅https://github.com/gruntjs/grunt-contrib-watch#optionslivereload。 –

5

嗯,你can watch using the Grunt-contrib-compass watch option。这将产生指南针手表,这样你会有更好的表现。虽然这不允许你观看多种类型的文件(例如,如果你也注意.coffee文件或者总是重建js等)。

如果您绝对需要grunt-contrib-watch,那么请确保使用grunt任务激活sass缓存。从你的配置粘贴在这里,它看起来像。但缓存问题通常是指南针编译需要很长时间的原因;所以如果我是你,我会再次检查我的Gruntfile.js。

此外,很多精灵和图像处理方法可能需要很长时间才能处理。

2

也许晚了一点对这个派对,但万一这有助于任何人:

我发现grunt-contrib-watch和sass的性能相同。解决这个问题的最好方法似乎是使用不同的手表插件。我发现grunt-watch-nospawn(与grunt-contrib-watch插件相反)编译sass要快得多。相当显着 - 我看到两秒左右的改进。

如果你想进一步调整速度,你可以使用grunt-sass而不是grunt-contrib-sass,它使用libsass来提供另一个速度增加。

这与autoprefixer结合,例如。 nDmitry的(不能链接,没有代表),这应该填补遗漏Compass留下的功能空白。

希望有所帮助。

+2

即使后来参加这个派对,但[grunt-contrib-watch有一个产卵选项](https://github.com/gruntjs/grunt-contrib-watch#optionsspawn)。不是100%确定是否将其设置为false与grunt-watch-nospawn完全相同,但听起来类似。 –

+1

用'grunt-contrib-watch'添加'spawn:false'对我有效,平均编译时间从'4-5'秒到'0.05'秒。 –

+0

是的,它使用这个选项更快地响应文件变更,并且似乎也更快地处理文件 – cincplug

0

我知道这个问题在这一点上已经过了几年,但我想我会添加另一个潜在的原因/解决方案。

首先,尝试使用--verbose开始你的grunt服务器,并观察你的sass任务大部分时间花在哪里。有插件会报告每个部分的时间花费的时间,但对我来说,只是看着--verbose的输出,使得它很清楚延迟的位置。对我来说,这不是真正的sass任务,而是加载不必要的依赖关系。

正如Grunt的GitHub repo上的this问题所述,某些任务可能需要很长时间才能完成的一个原因是Grunt每次运行时都加载所有任务。因此,即使grunt-contrib-watch只在运行compass:dist任务时更改sass文件,grunt仍在加载所有任务及其依赖项。

现在有一个名为jit-grunt(或npm)的插件可以解决这个问题,只会加载运行任务所需的内容。这有助于我的指南针任务完成得更快。

相关问题