babel / minify

:scissors: An ES6+ aware minifier based on the Babel toolchain (beta)
https://babeljs.io/repl
MIT License
4.39k stars 225 forks source link

Support mangled names cache preserved between runs #969

Open psxvoid opened 4 years ago

psxvoid commented 4 years ago

Is your feature request related to a problem? Please describe. Sometimes you may have to build multiple output files with multiples runs. For example, it may be the case for browser extensions where you may have to build a content-script, a background-script, and a popup separately. Those scripts may reuse common objects and services but in case you have communication between them (what is very likely in case of complex browser extensions) there is one big issue in terms of a minification - object, compiled and minified separately may have different properties after the minification for different compilations. When a content-script sends a message (an event) to a background script it may send an object A with property x (minified, let's say it's eventType property before the minification) while the background script is expecting the same object A but it's eventType property is minified to y. In this case when the background-script retrieves such event with object A then the property y is undefined for it.

Describe the solution you'd like The terser|npm package has --name-cache property which can be set to an object or a file. It's allowed (for example in a gulp file) to specify an object or a file that will be used as a name-cache. This name-cache object may be reused between multiple compilation/minification steps solving the issue described above. Is it possible to add something similar to babel/minify?

Describe alternatives you've considered To solve this issue, one may use string constants for setting/accessing properties in the communication between let's say a content-script and a background-script OR exclude some properties from the minification using exclude configuration option. But both methods do not allow us to use minification fully for its advancement.

Additional context none