You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sloppy mode requires the value for this to be an object while strict mode doesn't change the value of this, and this code breaks when not run in strict mode. The problem remains if you change --format=iife to --format=esm, so it's not really caused by the IIFE output mode. It's caused by the use of the .js file extension instead of the .mjs file extension.
You can get this code to work even with the .js file extension by adding "use strict"; to the top of the file, which can be done in esbuild with something like --banner:js="use strict;" (quoting this argument in a cross-shell way can be difficult, so you may want to use the JS API for this instead).
It may not be straightforward to automatically solve this in esbuild's case because strict mode infects all scopes inside it by design, and not all code that esbuild bundles can be run in strict mode. And esbuild's scope-flattening bundling approach combines scopes from different files with potentially different strict mode settings. I suppose I could potentially insert "use strict"; inside every top-level function inside every strict mode module (either implicitly or explicitly so) but I suspect that would be a very unpopular decision due to the code bloat that would cause. I wonder how other bundlers handle this.
This is due to a statement incorrectly inserted before strict mode directive:
// node_modules/.pnpm/@babel+types@7.16.0/node_modules/@babel/types/lib/utils/shallowEqual.jsvarrequire_shallowEqual=__commonJS({"node_modules/.pnpm/@babel+types@7.16.0/node_modules/@babel/types/lib/utils/shallowEqual.js"(exports){init_shims();"use strict";// directives must appear before any other statements in the block
Activity
loynoir commentedon Dec 7, 2021
@babel/types/lib/builders/builder.jscontains athisevanw commentedon Dec 8, 2021
As far as I can tell, the problem is caused by strict mode vs. sloppy mode, which is caused by the
.mjsvs..jsfile extension. Here's an example:Sloppy mode requires the value for
thisto be an object while strict mode doesn't change the value ofthis, and this code breaks when not run in strict mode. The problem remains if you change--format=iifeto--format=esm, so it's not really caused by the IIFE output mode. It's caused by the use of the.jsfile extension instead of the.mjsfile extension.You can get this code to work even with the
.jsfile extension by adding"use strict";to the top of the file, which can be done in esbuild with something like--banner:js="use strict;"(quoting this argument in a cross-shell way can be difficult, so you may want to use the JS API for this instead).It may not be straightforward to automatically solve this in esbuild's case because strict mode infects all scopes inside it by design, and not all code that esbuild bundles can be run in strict mode. And esbuild's scope-flattening bundling approach combines scopes from different files with potentially different strict mode settings. I suppose I could potentially insert
"use strict";inside every top-level function inside every strict mode module (either implicitly or explicitly so) but I suspect that would be a very unpopular decision due to the code bloat that would cause. I wonder how other bundlers handle this.lightmare commentedon Dec 10, 2021
This is due to a statement incorrectly inserted before strict mode directive:
fix evanw#1837: move "use strict" before injected files