Closed tspink closed 1 year ago
Very thanks for providing the detail and investigation!
My plugin generates code that assign the function to a variable with the same name, i.e.:
let foo;
foo = function foo() { foo = /* ... */; };
And the esbuild post-process changes the function name to foo2
. But there's no different on the execution result.
Removing the function name resolves this issue. The assignments in the function will assign to the let
variable instead the function name, which is read-only. i.e.:
let foo;
foo = function () { foo = /* ... */; };
I found that foo.name
is also "foo"
even if the function is a anonymous function, so I think there's no breaking in this way.
I'll commit the fix. BTW how do you think of this?
Please try latest v1.3.1
.
Hello,
I've recently been encountering a serious issue in my project, where Chrome started to report the following error:
Uncaught (in promise) TypeError: Assignment to constant variable.
The specific problem (in my project) is to do with a Babel helper function (
_typeof
) that reassigns its own definition:But this only becomes an issue when this particular transform is triggered: https://github.com/Menci/vite-plugin-top-level-await/blob/main/src/transform.ts#L204
Normally, this reassigning would be OK (although not ideal, as I understand it), but that particular transform converts it to something like:
Which causes the "assignment to constant variable" error, since the internal assignment to
_typeof
(and tail call) has not been re-written to theXX
variable. Ideally, it should look like this:I can reproduce this behaviour with a small example:
main.js:
foo.js:
vite.config.js:
And, then generated chunk foo-XXX.js:
Note, the
await
construct inside foo.js is required to trigger thedeclarationToExpression
transform.Please let me know if I can provide any more details, or if I can help in any way. Thanks to @nicolo-ribaudo for helping me debug this issue further, when I first reached out to the Babel folk!
Thanks, Tom