esbuild
esbuild copied to clipboard
DCE doesn't remove unreferenced `new Regexp`.
While inspecting my bundle output I just saw that an unreferenced regex wasn't removed by DCE.
It looks like it only affects regex created by new RegExp(...)
.
Is there a reason for that ?
const regex1 = new RegExp('foo'); // KO
const regex2 = /foo/ // OK
This is because esbuild considers throwing an exception to be a side effect, and new RegExp
can potentially throw an exception (such as new RegExp('[')
). The string literal is not checked for whether it's a valid regular expression or not.