Subscribe on changes!
avatar
Nov 8th 2021

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

avatar
Nov 8th 2021

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.

avatar
Nov 8th 2021

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

avatar
Nov 8th 2021

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.

avatar
Nov 8th 2021

OK, Thanks for the solution!

avatar
Nov 8th 2021

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

avatar
Nov 8th 2021

@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.

avatar
Nov 9th 2021

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.