From bc7c2af5aca1f51ae0c1cb3bd30b927f90338fd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?null=E4=BB=94?= <308241863@qq.com> Date: Mon, 12 Jul 2021 18:00:10 +0800 Subject: [PATCH] fix: only trigger warning in the dev environment (#755) Co-authored-by: webfansplz <> --- src/apis/createElement.ts | 12 +++++++----- src/apis/inject.ts | 3 ++- src/mixin.ts | 18 ++++++++++-------- src/utils/helper.ts | 10 ++++++---- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/apis/createElement.ts b/src/apis/createElement.ts index 07ae3254..6e05d9db 100644 --- a/src/apis/createElement.ts +++ b/src/apis/createElement.ts @@ -7,17 +7,19 @@ type CreateElement = Vue['$createElement'] let fallbackCreateElement: CreateElement -export const createElement = (function createElement(...args: any) { +export const createElement = function createElement(...args: any) { const instance = getCurrentInstance()?.proxy if (!instance) { - warn('`createElement()` has been called outside of render function.') + __DEV__ && + warn('`createElement()` has been called outside of render function.') if (!fallbackCreateElement) { - fallbackCreateElement = defineComponentInstance(getVueConstructor()) - .$createElement + fallbackCreateElement = defineComponentInstance( + getVueConstructor() + ).$createElement } return fallbackCreateElement.apply(fallbackCreateElement, args) } return instance.$createElement.apply(instance, args) -} as any) as CreateElement +} as any as CreateElement diff --git a/src/apis/inject.ts b/src/apis/inject.ts index 8247a91c..44183798 100644 --- a/src/apis/inject.ts +++ b/src/apis/inject.ts @@ -54,7 +54,8 @@ export function inject( const vm = getCurrentInstance()?.proxy if (!vm) { - warn(`inject() can only be used inside setup() or functional components.`) + __DEV__ && + warn(`inject() can only be used inside setup() or functional components.`) return } diff --git a/src/mixin.ts b/src/mixin.ts index 8ea9292e..078c846c 100644 --- a/src/mixin.ts +++ b/src/mixin.ts @@ -210,10 +210,11 @@ export function mixin(Vue: VueConstructor) { proxy(ctx, key, { get: () => vm[srcKey], set() { - warn( - `Cannot assign to '${key}' because it is a read-only property`, - vm - ) + __DEV__ && + warn( + `Cannot assign to '${key}' because it is a read-only property`, + vm + ) }, }) }) @@ -237,10 +238,11 @@ export function mixin(Vue: VueConstructor) { return data }, set() { - warn( - `Cannot assign to '${key}' because it is a read-only property`, - vm - ) + __DEV__ && + warn( + `Cannot assign to '${key}' because it is a read-only property`, + vm + ) }, }) }) diff --git a/src/utils/helper.ts b/src/utils/helper.ts index b72ce792..753bc907 100644 --- a/src/utils/helper.ts +++ b/src/utils/helper.ts @@ -34,10 +34,12 @@ export function isComponentInstance(obj: any) { export function createSlotProxy(vm: ComponentInstance, slotName: string) { return (...args: any) => { if (!vm.$scopedSlots[slotName]) { - return warn( - `slots.${slotName}() got called outside of the "render()" scope`, - vm - ) + if (__DEV__) + return warn( + `slots.${slotName}() got called outside of the "render()" scope`, + vm + ) + return } return vm.$scopedSlots[slotName]!.apply(vm, args)