Add a rollup plugin for tree shaking Emscripten output. #23964
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Rollup has built in tree shaking which can inform the plugin which exports are no longer used. If those exports correspond to wasm exports they can potentially be removed from wasm file too.
Overview:
While the modules are parsed, we collect info about the exports before any tree shaking occurs to build a map from ESM export name to Wasm export name and info about the local function name within the JS e.g. _foo.
During bundle generation rollup provides a list of exports that are removed. We then try to match the removed exports to Wasm exports. However, they all cannot simply be removed though since they may be used internally in the file still. A new list of the used wasm exports is generated and passed to wasm-metadce to remove any unused exports.
Note: there are a lot more things we could do there, but this seemed like a good stopping point. It also shows that even for specific output, tree shaking is hard to do soundly for dynamically loaded wasm.