gobblejs / gobble

The last build tool you'll ever need
333 stars 20 forks source link

File transformers cannot handle binary files #105

Closed teehemkay closed 8 years ago

teehemkay commented 8 years ago

Gobble provides the contents of an individual file to a file transformer plugin as text input. This prevents a file transformer from handling binary files which is quite a bummer.

For example, since image optimisation can be quite onerous in terms of performance, a file transformer for image optimisation could be more performant than a directory transformer thanks to caching.

Rich-Harris commented 8 years ago

Thanks – this is fixed in 0.11.2. Use sourceEncoding: null, like so:

function myPlugin ( inputBuffer ) {
  return doSomethingWith( inputBuffer );
}

myPlugin.defaults = {
  sourceEncoding: null
};
teehemkay commented 8 years ago

👍

teehemkay commented 8 years ago

Unless I'm mistaken, file transformers cannot be async, right?

If that's the case, it's quite unfortunate because it means that one has to chose between async or caching but can't have both?

For example: I want to have a gobble plugin for imagemin. Since optimizing is onerous and a 1-to-1 op, I'd rather use a file transform.

But imagemin processors are async so it's a no go 😞

IvanSanchez commented 8 years ago

I guess it should be possible for Gobble to detect when a file transformer returns an instance of Promise and wait for it?

teehemkay commented 8 years ago

Ideally yes. And also maybe use the number of arguments trick (like for the directory transformer) so that one can use a callback instead of returning a promise.

But for the moment, it seems that gobble just assumes that the returned value is the transformed contents (see https://github.com/gobblejs/gobble/blob/f508f0bcef770fc3e66ff16bfbb89f00b2015e80/src/builtins/map.js#L75)

teehemkay commented 8 years ago

I've opened a feature request (#112) for this btw. So maybe it's better to further discuss this there 😄