rpetrich / babel-plugin-transform-async-to-promises

Transform async/await to somewhat idiomatic JavaScript promise chains
MIT License
246 stars 17 forks source link

Inline helpers without globals #65

Open lennart-m opened 3 years ago

lennart-m commented 3 years ago

Hi, I'm heavily using this plugin for SAPUI5 apps. Thanks for this great plugin!

But I am thinking about how this can be adjusted even better for UI5. With UI5 we use custom AMD and custom bundling of apps. A typical module looks like this:

sap.ui.define([ /* dependencies as strings */ ], (/* resolved dependencies */) => {
  /* module definition */ return 3;
});

The UI5 bundler works best if the first call is sap.ui.define, without any defined global variables. In this case the bundler will extract all dependencies and use this information to optimize the bundle.

But when using this plugin (even with inlineHelpers activated) some of the helpers (like _catch, _finally) get inserted on the top level, before the sap.ui.define call. Would it be possible to insert the inline helpers directly in the function where they are used, or maybe have some kind of "target" variable/comment that determines where the helpers are to be inserted? I'm thinking of something like this:

sap.ui.define([], () => {
let _finally; // plugin automatically inserts helper code here
// or:
/* babel-plugin-transform-async-to-promises target */
});

Alternatively I might try to use the provided helpers but I would have to convert them to use the custom AMD syntax.

My current workaround is using babel-plugin-iife-wrap in order not to have global variables in my modules, but this just removes bundler warnings about global variables; extraction of dependencies still does not work.

Thanks a lot!

rpetrich commented 2 years ago

I suspect that transform-async-to-promises is processing the output of your custom bundling step rather than the other way around. Could you describe your bundling setup in more detail if this is still an issue for you?