Skip to content
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

allow ignoring specific exposed setup vars with internal prefixes ("_" and "$") #4532

Closed
theoephraim opened this issue Sep 6, 2021 · 7 comments · Fixed by #4570
Closed
Labels
has workaround A workaround has been found to avoid the problem 🔩 p2-edge-case scope: compiler ✨ feature request New feature or request

Comments

@theoephraim
Copy link

What problem does this feature solve?

it would be great to be able to specify specific variables to disregard when showing this warning:
setup() return property "_" should not start with "$" or "_" which are reserved prefixes for Vue internals

Specifically I am using lodash, so I often have something like import _ from 'lodash' in my files.

When using the options api, this isn't really a huge issue, because usually lodash functions aren't needed in the template, and renaming lodash itself or a specific function while explicitly exposing it to the template isn't too common. However with the new script setup API, this would mean having to always rename lodash itself when importing it. While that's fine, it's not great for readability when lodash being named _ is the norm.

What does the proposed API look like?

A few things come to mind that could help

  • ignore the variable name _ itself from this warning
  • allow users to explicitly allow specific vars that are prefixed with _ or $, similar to app.config.globalProperties but something like app.config.allowInternalPrefixSetupProps = ['_', '_myVar', '$somethingSpecial']
@posva posva added the has workaround A workaround has been found to avoid the problem label Sep 7, 2021
@posva
Copy link
Member

posva commented Sep 7, 2021

In practice, you should really import individual functions from lodash because of the size difference, but a compiler option would definitely help.

@yyx990803
Copy link
Member

You are not limited by this when using <script setup> - you should probably migrate if you haven't already?

@theoephraim
Copy link
Author

@yyx990803 - thanks, I am using script setup already. This issue has not been a blocker at all, it's just been filling my browser console with warnings. I was able to figure out how to hide it yesterday using a custom warning handler, but I still think another config setting as mentioned above could be helpful.

For anyone else finding this - here is the warning handler:

app.config.warnHandler = function (msg, vm, trace) {
  if (msg === 'setup() return property "_" should not start with "$" or "_" which are reserved prefixes for Vue internals.') return;
  console.warn(msg, trace);
};

@posva - yeah I'm aware. I personally find a single lodash import and calling _.funcName much more readable and simpler than individual imports. I'm hoping to eventually get something like https://github.com/lodash/babel-plugin-lodash set up with vite to do it automatically, but in the meantime I just have my own lodash utils file that I import instead of lodash directly:

// import all the lodash functions individually in a way that unused functions will be shaken
...
import add from 'lodash-es/add';
import after from 'lodash-es/after';
import ary from 'lodash-es/ary';
import assign from 'lodash-es/assign';
import assignIn from 'lodash-es/assignIn';
...

// export only the ones we want to keep
export default {
  assign,
 ...
}

@yyx990803
Copy link
Member

Hmm, I don't think this should happen with <script setup> (see playground example)

Do you have a reproduction?

@ygj6
Copy link
Member

ygj6 commented Sep 13, 2021

I found that in dev mode, if internal exposed _ variable in <script setup>, and worked with $refs will report Uncaught TypeError: Cannot read property '__isScriptSetup' of undefined, but worked fine in prod mode. The reproduction link:https://github.com/ygj6/bug-report-ctx.git

  1. npm install / yarn
  2. npm run dev / yarn dev
  3. click the 'button'
  4. console Uncaught TypeError: Cannot read property '__isScriptSetup' of undefined
<script setup>
import { ref } from 'vue'

const msg = ref('Hello World!')
const _ = ref(0)
</script>

<template>
  <h1>{{ msg }}</h1>
  <input v-model="msg" ref="input">
  <!-- in dev mode will report 'Uncaught TypeError: Cannot read property '__isScriptSetup' of undefined',work fine in prod mode -->
  <button @click="$refs.input.focus()">
    Focus $refs.input
  </button>
</template>

@yyx990803
Copy link
Member

@ygj6 's PR should fix usage of _ during dev mode, and I can't reproduce the original issue. Make sure to upgrade to latest version of Vue when it's released!

@theoephraim
Copy link
Author

Thanks so much! I just confirmed that @ygj6's bug report is more up to date than mine and seems to be the same thing - I reproduced his issue importing lodash rather than using a ref named _.

(I'm still using a slightly older version of vue 3 due to volar + typescript not quite being up to date with the latest and greatest, last time I tried to update anyway...)

@github-actions github-actions bot locked and limited conversation to collaborators Oct 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
has workaround A workaround has been found to avoid the problem 🔩 p2-edge-case scope: compiler ✨ feature request New feature or request
Projects
None yet
4 participants