-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
"use strict" not being emitted automatically if needed breaks code #2264
Comments
It is rollup (behind vite) emitting this line by default. The reason for this behavior is that the input format for rollup/vite are always ESM, where there's only strict mode. On the other hand, esbuild can handle non-strict code if it is not told to be strict (without Maybe we could ask esbuild to emit that banner automatically if strict mode is enabled in one of the source code (when it see It seems that esbuild will add a top-level 'use strict'
export let a = 1 esbuild a.js --bundle --format=esm --minify "use strict";var t=1;"use strict";export{t as a};
// ^~~~~~~~~~~~
// not shown in CJS or IIFE format The same output presents in transform mode. |
The If I write |
I think there are a few different things here:
|
I was surprised to see Removing a |
@curiousdannii Are you seeing a The problem with reverting this change is that you as the library author are assuming that your code is running under strict mode, because you are outputting an ESM module, but once the user gets to actually execute your code that may no longer be the case, depending on the bundler used, I guess. |
Yes, If someone is doing additional bundling after esbuild, then it's their responsibility to ensure they're bundling it correctly. I don't see why esbuild should add an unnecessary pragma just in case someone bundles it incorrectly. If esbuild tried to protect against anything that could possibly be misconfigured then it would cease being a very efficient minifier! Though I'm saying that more as an app author than a library author. Maybe things might be slightly different from the perspective of a library author? Not sure. Does esbuild currently add |
I'm running into an issue which seems to be related to this (or might not be?) i'm switching from I'm unclear as to whether adding strict/alwaysStrict to tsconfig should have fixed it. If it should, I'll investigate further. For now the banner workaround in #2264 (comment) fixes this case for me, but it feels a bit ugly :) |
Long story short esbuild is not emitting
"use strict";
while Vite is, even if I point it to my "tsconfig.json" with "alwaysStrict" in it.IMO this is a really bad default that breaks code in subtle ways.
For my sample project I'm basically getting this with esbuild:
But this with Vite:
And for whatever insane reason outside of strict mode this code outputs a String object, while in strict mode it outputs a string primitive.
Outputting the strict mode directive IMO is just a much better default, nobody should be using sloppy mode anyway really.
Also it's unclear how well the banner workaround works, potentially I could be using multiple plugins each setting their own strict mode directive? Maybe some other plugin could override my plugin's request to use strict mode? It just doesn't make a lot of sense to me to use
banner
for this. If anything this should be opt-out not opt-in.Sample project:
voby_bug.zip
The text was updated successfully, but these errors were encountered: