tschaub / gulp-newer

Pass through newer source files only
https://npmjs.org/package/gulp-newer
226 stars 24 forks source link

Add options.suffix #10

Closed karlvr closed 9 years ago

karlvr commented 9 years ago

I have a gulpflow where I want to check newer against a file that I will append a suffix to. I can't predict the extension, so I think I need a suffix option. It's trivial to implement, of course! I've put together this little pull request. I hope you're keen to add it.

Best, Karl

tschaub commented 9 years ago

The change looks sensible, but it's not immediately clear to me where this would be used instead of the ext option. Can you provide a bit more detail on the use case?

Ideally we could add tests and documentation that make it clear when to use suffix versus ext. Also, if you rebase on master, tests should be passing again.

karlvr commented 9 years ago

Sure thing! My example is this:

I have an image optimisation task followed by renaming of the image file for cache busting. I optimise the images using gulp-imagemin and then rename the files using gulp-rev-all. I also output a file pointing to the revved file using gulp-rev-pointer (which I wrote) so I can easily find the revved file name from the original file name. That pointer file gets the same stats as the revved file.

So my plan for newer is to check newer on the source image file against the pointer file, as I can predict how the pointer file will be named. The pointer file is named by applying a suffix to the source file. That is, image.png becomes image.png.rev, and image.jpg becomes image.jpg.rev.

I can't use the ext property in gulp-newer because it will replace the png or jpg component. So I needed something that just added a suffix. I figured it was a nice flexibility to have in general.

karlvr commented 9 years ago

And rebased.

karlvr commented 9 years ago

@tschaub Sorry to bump, what do you think about this? I keep having to manually sync my patch between my machines :-)

tschaub commented 9 years ago

Thanks for explaining. So the problem is that ext doesn't handle all the ways that a source path can be transformed to become a destination path. The suffix option handles your specific case, but isn't really more general. If others have problem with the limitations of ext, it might be that suffix isn't enough either.

I'd rather add a truly general option as an alternative - like a function that accepts a relative source path and returns a relative destination path. In your case you would simply append a suffix, but others could do more (like prepend 'min' to the extension or whatever).

karlvr commented 9 years ago

@tschaub You're absolutely right. I also thought I was duplicating the behaviour of gulp-rename, but in fact suffix was different from their suffix.

I'll prepare another PR with this function.