**Description:** The algorithm here is directly copied from terser, I don't if it's correct or can be improved, but it does fix the issue. **Related issue:** - Closes #7749