Skip to content
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

(0, fn)(...args) type of calls #6

Open
mr-cheater opened this issue Apr 29, 2023 · 6 comments · May be fixed by #50
Open

(0, fn)(...args) type of calls #6

mr-cheater opened this issue Apr 29, 2023 · 6 comments · May be fixed by #50
Labels
enhancement New feature or request webpack

Comments

@mr-cheater
Copy link

Is there any particular reason to not replace calls like (0, fn)(...args) to just fn(...args)? Also, would be great to know in which cases webpack does this kind of transformation if anyone knows...

@j4k0xb
Copy link
Owner

j4k0xb commented Apr 29, 2023

(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

@mr-cheater
Copy link
Author

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.

@mr-cheater
Copy link
Author

Seems like webpack does this only in case fn has no internal references to this so should be perfectly safe to convert it?

@j4k0xb
Copy link
Owner

j4k0xb commented Apr 29, 2023

Just checked: TS and webpack do it for all functions
Here's an example where it would fail without: es-shims/Promise.allSettled#5
That's what I meant with analyzing... Have to make sure the functions don't reference this to transform it back safely

@j4k0xb j4k0xb added the enhancement New feature or request label May 1, 2023
@j4k0xb
Copy link
Owner

j4k0xb commented Nov 13, 2023

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

@j4k0xb j4k0xb added the webpack label Dec 22, 2023
@j4k0xb j4k0xb linked a pull request Jan 13, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request webpack
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants