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

[question] format: 'esm' generates var instead of let or const #1301

Closed
BePo65 opened this issue May 21, 2021 · 2 comments
Closed

[question] format: 'esm' generates var instead of let or const #1301

BePo65 opened this issue May 21, 2021 · 2 comments

Comments

@BePo65
Copy link

BePo65 commented May 21, 2021

If I run esbuild to generate esm output with the following options:

  platform: 'browser',
  format: 'esm',
  bundle: true,
  target: 'es2020'

the created output contains var xx = class ... for every class defined in the source file in addition to the expected export {...}.

Question: is the use of var by intention (e.g. for use in "classical" html pages) or should it better be const to avoid cluttering the global context / window object?

@nettybun
Copy link

It's intentional because let/const introduces a "TDZ" Temporal Dead Zone which is fine in theory but in practice caused extreme performance issues in Safari. #478. There was some talk with the WebKit team I think. Ultimately switched everything to var :)

ESM is for modules so the code will run as an ESM import or as a <script type=module ... block, both of which do not clutter the global context/window - you need to explicitly say window.x = to do that.

If you're running the ESM output code in a normal <script> block and are worried about the global context then use format IIFE.

@BePo65
Copy link
Author

BePo65 commented May 21, 2021

oh, so much for my knowledge about using var in a browser environment ;-)

Thank you for your very detailed answer.

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