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

Minify breaks eval which refers const #2361

Closed
sapphi-red opened this issue Jun 30, 2022 · 3 comments
Closed

Minify breaks eval which refers const #2361

sapphi-red opened this issue Jun 30, 2022 · 3 comments

Comments

@sapphi-red
Copy link
Contributor

Input:

console.log((function (){ const v = 0; return eval('v') })()) // 0

Output:

console.log(function(){return eval("v")}()); // Error: v is not defined

repl


Input

console.log((() => { const v = 0; return eval('v') })());  // 0

Output

console.log((()=>eval("v"))()); // Error: v is not defined

repl


const v = 0 was dropped here.

// Only keep this declaration if it's top-level or exported (which
// could be in a nested TypeScript namespace), otherwise erase it
if p.currentScope.Parent == nil || s.IsExport {
s.Decls[end] = d
end++
}

I found this while working on #2360.

version: esbuild 0.14.48

@hyrious
Copy link

hyrious commented Jun 30, 2022

Did you mean esbuild should take care of eval? I mean this can be really hard with eval(whatever_expression), which has been stated in the doc.

@evanw
Copy link
Owner

evanw commented Jun 30, 2022

Using eval is supposed to poison all enclosing scopes to disable tree shaking. So this behavior should be disabled in this case. It’s an oversight that this code isn’t checking to see if it’s in an eval scope before removing the variable.

@sapphi-red
Copy link
Contributor Author

Sorry that I forgot to write the expected behavior.
What I expected is to disable this minify transform for this case like other minify transforms.

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

3 participants