Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DCE doesn't remove unreferenced new Regexp. #3750

Open
JeanMeche opened this issue May 6, 2024 · 1 comment
Open

DCE doesn't remove unreferenced new Regexp. #3750

JeanMeche opened this issue May 6, 2024 · 1 comment

Comments

@JeanMeche
Copy link

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

Demo

@JeanMeche JeanMeche changed the title DCE doesn't remove unused new Regexp. DCE doesn't remove unreferenced new Regexp. May 6, 2024
@evanw
Copy link
Owner

evanw commented May 6, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants