Include function declarations that are referenced by top-level function calls during bundling#4182
Include function declarations that are referenced by top-level function calls during bundling#4182ozkutuk wants to merge 4 commits intopurescript:masterfrom
Conversation
|
I ran the test and it passes. 👍 |
|
Just skimming the code changes, it looks like this might be too narrow a fix; is this case handled? if (condition) {
localFunction();
} |
|
@rhendric Such cases are indeed not handled. I think rather than trying extend this more and more into a full-fledged JS dead code analysis tool, we should set a boundary regarding what we support on FFI files. @hdgarrood put this better than I could in #4039 (comment). To be honest, I even was on the fence regarding adding support for top-level function calls, but I guess they are useful for global polyfills (as in the case of purescript-contrib/purescript-arraybuffer#34). Do you think it is essential to support conditionals as well? |
|
I think what's essential is that FFI files either work (possibly with unused code in the result, which a more advanced bundler might have eliminated) or fail at compile time (such as when a JS feature is not supported by our parser). That was the motivation behind my support of #4044, and it's why I'd rather support arbitrary statements at the top level in this fix. I could be convinced that throwing a compile-time error if such statements are encountered is the right thing to do instead (in which case this is a breaking change). But I wouldn't expect adding support for those statements to be difficult; we already have the ability to analyze arbitrary statements for their dependencies. |
|
@rhendric What do you think is the best way to proceed with this? Should I add every possible |
|
I haven't tried it so I can't be sure it would work, but I probably would have started by leaving |
|
Superseded by #4255, much as plans to give a drab hallway a beautiful new coat of paint are superseded by the demolition of the building. (Thank you for working on this anyway; hopefully this is not too frustrating an outcome for you!) |
Description of the change
Fixes #4181
As stated in #4181, #4044 introduced a regression to the bundler. Bundler only concerns itself with require statements, member declarations, and export lists; and it ignores everything else (leaving them in the bundle verbatim). Before #4044 was introduced, both the function declaration and the call to that function were falling back to "other" case, leaving them untouched in the bundler output. However, since #4044, the function declarations are treated specially and are now properly parsed as member declarations. This causes them to be subject to DCE check, and since bundler can't detect that the declaration is referenced by a top-level function call, it eliminates the declaration during DCE.
This PR fixes the issue by parsing top-level function calls as member declarations, and also specifies them as potential entry points to the module. This allows the DCE check to detect that the function declaration is indeed referenced.
Checklist:
Updated or added relevant documentation