vuejs/core

use IIFE in script setup throws error

heart-er opened this issue · 7 comments

Version

3.2.21

Reproduction link

sorry, online sfc not throws error

This is my local error and my source code

image

image

Steps to reproduce

  1. use IIFE function in script setup
  2. chrome devTool throws error about expose(...) is not a function

What is expected?

not throw error

What is actually happening?

use IIFE function in script setup throws error

If you only can reproduce it in a git repo, please share a git repo.

We can't debug what we can't reproduce reliably.

Issues without reproductions are closed. We can reopen the issue when you provide a runnable reproduction.

OK,thanks, this is my demo repo, it will throws error when i use IIFE in script setup

https://github.com/heart-er/vue3-error-repo
@LinusBorg @yyx990803

Okay, can reproduce it and also found a workaround/solution: a leading semicolon:

<script setup>
;(() => {
  console.log('----')
})()
</script>

It seems there's an issue with generated code that's being inserted before that IIFE which breaks ASI.

However, it's also a good practice to have a leading semicolon here in general. prettier even adds it for me, usually.

OK, Thanks for the solution!

I look into the code and find another problem. see sfc

<script setup>
defineExpose({ foo: 123 })
(() => {
    console.log('----');
})();
</script>

in this case, the expression node is not a defineExpose call(processDefineExpose return false). see AST
image

@edison1105 Well, you need to add a semicolon after defineExpose() - that's just a requirement in JS. (A linter would likely warn you about such code as well.)

In your example you caused it yourself and can fix it yourself. In OP's example there's no previous code that would require a semicolon before compilation, so it must likely be caused and solved by the compiler.

In your example you caused it yourself and can fix it yourself. In OP's example there's no previous code that would require a semicolon before compilation, so it must likely be caused and solved by the compiler.

make sence.