Skip to content

Commit

Permalink
fix: only trigger warning in the dev environment (#755)
Browse files Browse the repository at this point in the history
Co-authored-by: webfansplz <>
  • Loading branch information
webfansplz committed Jul 12, 2021
1 parent e044215 commit bc7c2af
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
12 changes: 7 additions & 5 deletions src/apis/createElement.ts
Expand Up @@ -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
3 changes: 2 additions & 1 deletion src/apis/inject.ts
Expand Up @@ -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
}

Expand Down
18 changes: 10 additions & 8 deletions src/mixin.ts
Expand Up @@ -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
)
},
})
})
Expand All @@ -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
)
},
})
})
Expand Down
10 changes: 6 additions & 4 deletions src/utils/helper.ts
Expand Up @@ -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)
Expand Down

0 comments on commit bc7c2af

Please sign in to comment.