-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: Async file transformers #112
Comments
In principle this makes a lot of sense, and I'm all for it. Tricky part is determining whether it's async or not – returning a Promise is easy enough, but for transformers that are callback-based, adding a third So, I see a couple of potential solutions:
var fileTransformer = require( 'gobble-file-transformer' );
var myPlugin = fileTransformer( ( input, options ) => {...}); ...but I'm not really a fan of this approach for lots of reasons.
function myPlugin ( input, options ) {
const done = this.async();
doSomethingWith( input, options, done );
} So file transformers would continue to have two arguments, and the fourth argument to directory transformers could be gradually phased out. |
Suggesting dropping callbacks and just use promises to be uniform in returns as well as accommodate both synchronous and asynchronous transforms. Callback-based transforms can just migrate to using promises. function fileTransform(input, options){.
return Promise.resolve(stuff); // The usual return values are accepted as resolution.
return Promise.reject(error);
}
function dirTransform(input, output, options){
return Promise.resolve(); // Not sure if stuff is needed. We're working directly on the fs.
return Promise.reject(error);
} |
Trouble is a lot of libraries still use node-style callbacks, and insisting on promises means this sort of nonsense: function myPlugin ( inputdir, outputdir, options ) {
return new Promise( function ( fulfil, reject ) {
legacyBullshit( inputdir, outputdir, options, function ( err, result ) {
if ( err ) return reject( err );
fulfil( result );
});
});
}
// as opposed to
function myPlugin ( inputdir, outputdir, options ) {
var done = this.async();
legacyBullshit( inputdir, outputdir, options, done );
}
// or even
function myPlugin ( inputdir, outputdir, options ) {
legacyBullshit( inputdir, outputdir, options, this.async() );
} |
👍 on proposal 2) above (using |
👍 for |
On second thought (sorry, I'm on vacation), How about requiring that async file transformers have This way we can still distinguish between a file transformer with a callback and a directory transformer. |
Why not disambiguate and add a different method for file transforms? Async would only be in new method, could be done in non-breaking fashion by logging deprecation warnings for use of |
So we can have |
@fskreuz right, but with @Rich-Harris's supreme naming skills we'd have even cooler, more obvious names like |
Looking at the code, I see that the transform calling context already has other properties (such as So on third thought, |
While this is being worked out, I thought it'd be nice to have it for Promise-based transforms ASAP, as it is very simple and covers at least a percentage of this use case. Also, for The PR is #124. |
👍 |
Any chance of this happening? |
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 😞
So this is my request for making file transformers async just like directory transformers.
I think it would make gobble much more powerful.
The text was updated successfully, but these errors were encountered: