twolfson / grunt-spritesmith

Grunt task for converting a set of images into a spritesheet and corresponding CSS variables
MIT License
1.14k stars 92 forks source link

Issue with grunt-newer #111

Closed ArmorDarks closed 9 years ago

ArmorDarks commented 9 years ago

Hi

I've read that grunt-spritesmith should work with grunt-newer, but it seems that there are some issues with it or I'm missing something.

Spritesmith task:

module.exports = ->
  @config 'sprite',
    build:
      src: ['source/sprites/{,**/}*']
      dest: 'images/sprite.png %>'
      destCss: 'styles/_sprites.scss'
      padding: 2
      engine: 'gmsmith'
      cssTemplate: '_sprites.map.mustache'

Watch task:

module.exports = ->
  @config 'watch',
    sprite:
      files: ['source/sprites/{,**/}*.{jpg,jpeg,gif,png}']
      tasks: ['newer:sprite']

When I try to put new file in a watched directory, renaming or deleting it — nothing happens.

Particularly, for that example I've renamed file. Output of the console:

>> File "source\sprites\test.png" deleted.
>> File "source\sprites\foo.png" renamed.
Running "newer:sprite" (newer) task

Running "newer:sprite:build" (newer) task
No newer files to process.

Done, without errors.

If I remove newer: from watch — everything works as expected

twolfson commented 9 years ago

grunt-newer operates based on filesystem timestamps. Renaming or deleting a file will not affect those. If you are copying the file and it is older than the last time the sprite task was run, then it will not run either. Can you try running touch on a file and then running grunt-newer?

Also, by using grunt --verbose you can see all the files it is looking at.

ArmorDarks commented 9 years ago

Hi

Thanks for reply

Renaming file with grunt --verbose:

Running "newer:sprite" (newer) task

Running "newer:sprite:build" (newer) task
Options: cache="C:\\Working\\Kotsu\\node_modules\\grunt-newer\\.cache", override=undefined
Files: source/sprites/new.png -> build/assets/sprites/sprite.png
No newer files to process.

Done, without errors.

Copying new file into source directory:

Running tasks: newer:sprite

Running "newer:sprite" (newer) task

Running "newer:sprite:build" (newer) task
Options: cache="C:\\Working\\Kotsu\\node_modules\\grunt-newer\\.cache", override=undefined
Files: source/sprites/copied-here.png, source/sprites/new.png -> build/assets/sprites/sprite.png
No newer files to process.

Done, without errors.

Using touch command on file result in expected successful detection:

Running tasks: newer:sprite

Running "newer:sprite" (newer) task

Running "newer:sprite:build" (newer) task
Options: cache="C:\\Working\\Kotsu\\node_modules\\grunt-newer\\.cache", override=undefined
Files: source/sprites/copied-here.png, source/sprites/new.png -> build/assets/sprites/sprite.png

Running "sprite:build" (sprite) task
Verifying property sprite.build exists in config...OK
Files: source/sprites/copied-here.png, source/sprites/new.png -> build/assets/sprites/sprite.png
Files "temp/styles/_sprites.map.scss", "build/assets/sprites/sprite.png" created.

Running "newer-postrun:sprite:build:1:C:\Working\Kotsu\node_modules\grunt-newer\.cache" (newer-postrun) task
Writing C:\Working\Kotsu\node_modules\grunt-newer\.cache\sprite\build\timestamp...OK

Done, without errors.

So, grunt-newer becomes quite unusable with grunt-spritesmith, since require manual update of timestamps. It even doesn't work when you're pasting in directory new file — something, which should work. I though newer should track name changes too, isn't it?

It also quite strange that, if I'm, let's say, rename one of my templates — grunt-newer tasks launches needed tasks properly, but it doesn't work for sprites for some reason:

Running "watch" task
Waiting...
>> File "source\layouts\index.nj" deleted.
>> File "source\layouts\rest.nj" renamed.
Running "newer:nunjucks" (newer) task

Running "newer:nunjucks:build" (newer) task

Running "nunjucks:build" (nunjucks) task
File "build/rest.html" created.

Running "newer-postrun:nunjucks:build:1:C:\Working\Kotsu\node_modules\grunt-newer\.cache" (newer-postrun) task

Done, without errors.

Also, just to ensure that there is no similar bug with spritesmith, here https://github.com/tschaub/grunt-newer/issues/45 sapegin mentioned about this.data object passed to grunt-newer issue.

twolfson commented 9 years ago

The issue that you linked to in grunt-newer is about the data passed from grunt-newer to the underlying grunt task (e.g. grunt-spritesmith). We have a workaround for this inside of our code:

https://github.com/Ensighten/grunt-spritesmith/blob/3.6.0/src/grunt-spritesmith.js#L53-L56

grunt-newer only operates based off of timestamps. It creates a timestamp file inside of its .cache directory and compares that timestamp and dest's timestamp against that of the src files. If you want to learn more about the inner workings, the source code can be found here:

https://github.com/tschaub/grunt-newer/blob/v1.1.0/tasks/newer.js#L80-L106