Open mr-cheater opened 1 year ago
(0, fn)(arg)
should be converted, thanks for the suggestion
but in other cases it's not guaranteed to be safe
webpack or TS do it so exported functions aren't called with a different this
E.g. this
inside (0, lib.fn)(arg)
is undefined
but in lib.fn(arg)
it's the lib
object
Maybe the function can be analyzed, or I'll just add an option to use unsafe transforms
Yeah that's what it does but do you really see any possible issue with not removing context for such function calls? I mean it could be useful in super specific and advanced JS cases, but assuming Webpack usually works with ES/TS codebases, it shouldn't really be the issue. That's why I've asked about cases when it does that as I'm almost sure that it doesn't do this particular transform because of specific of the source code and in raw ES/TS source the calls would be made with this
context anyway.
Seems like webpack does this only in case fn
has no internal references to this
so should be perfectly safe to convert it?
Just checked: TS and webpack do it for all functions
Here's an example where it would fail without: https://github.com/es-shims/Promise.allSettled/issues/5
That's what I meant with analyzing... Have to make sure the functions don't reference this
to transform it back safely
I came across another tool today that seemed to handle this pretty well:
Digging through the code a little lead me to this:
good idea to do it like this 👍🏻
Another thing I noticed is that bundlers only create (0, lib.fn)()
if it was originally an esm import, so it should be enough to identify the module type and convert all require
to named imports
Is there any particular reason to not replace calls like
(0, fn)(...args)
to justfn(...args)
? Also, would be great to know in which cases webpack does this kind of transformation if anyone knows...