Skip to content

Commit c5f0f63

Browse files
committed
refactor(runtime-core): make setup attrs proxy dev only
1 parent ec4a4c1 commit c5f0f63

File tree

5 files changed

+30
-8
lines changed

5 files changed

+30
-8
lines changed

packages/runtime-core/src/component.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -504,12 +504,25 @@ const SetupProxyHandlers: { [key: string]: ProxyHandler<any> } = {}
504504
}
505505
})
506506

507+
const attrsProxyHandlers: ProxyHandler<Data> = {
508+
get(target, key: string) {
509+
if (__DEV__) {
510+
markAttrsAccessed()
511+
}
512+
return target[key]
513+
},
514+
set: () => false,
515+
deleteProperty: () => false
516+
}
517+
507518
function createSetupContext(instance: ComponentInternalInstance): SetupContext {
508519
const context = {
509520
// attrs & slots are non-reactive, but they need to always expose
510521
// the latest values (instance.xxx may get replaced during updates) so we
511522
// need to expose them through a proxy
512-
attrs: new Proxy(instance, SetupProxyHandlers.attrs),
523+
attrs: __DEV__
524+
? new Proxy(instance.attrs, attrsProxyHandlers)
525+
: instance.attrs,
513526
slots: new Proxy(instance, SetupProxyHandlers.slots),
514527
get emit() {
515528
return instance.emit

packages/runtime-core/src/componentEmits.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {
55
EMPTY_OBJ,
66
capitalize,
77
hyphenate,
8-
isFunction
8+
isFunction,
9+
def
910
} from '@vue/shared'
1011
import { ComponentInternalInstance } from './component'
1112
import { callWithAsyncErrorHandling, ErrorCodes } from './errorHandling'
@@ -96,7 +97,7 @@ export function normalizeEmitsOptions(
9697
}
9798
const normalized: ObjectEmitsOptions = {}
9899
options.forEach(key => (normalized[key] = null))
99-
Object.defineProperty(options, '_n', { value: normalized })
100+
def(options, '_n', normalized)
100101
return normalized
101102
} else {
102103
return options

packages/runtime-core/src/componentProps.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ import {
1313
PatchFlags,
1414
makeMap,
1515
isReservedProp,
16-
EMPTY_ARR
16+
EMPTY_ARR,
17+
def
1718
} from '@vue/shared'
1819
import { warn } from './warning'
1920
import { Data, ComponentInternalInstance } from './component'
2021
import { isEmitListener } from './componentEmits'
22+
import { InternalObjectSymbol } from './vnode'
2123

2224
export type ComponentPropsOptions<P = Data> =
2325
| ComponentObjectPropsOptions<P>
@@ -102,6 +104,7 @@ export function initProps(
102104
) {
103105
const props: Data = {}
104106
const attrs: Data = {}
107+
def(attrs, InternalObjectSymbol, true)
105108
setFullProps(instance, rawProps, props, attrs)
106109
const options = instance.type.props
107110
// validation
@@ -310,7 +313,7 @@ export function normalizePropsOptions(
310313
}
311314
}
312315
const normalizedEntry: NormalizedPropsOptions = [normalized, needCastKeys]
313-
Object.defineProperty(raw, '_n', { value: normalizedEntry })
316+
def(raw, '_n', normalizedEntry)
314317
return normalizedEntry
315318
}
316319

packages/runtime-core/src/vnode.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
import {
1414
ComponentInternalInstance,
1515
Data,
16-
SetupProxySymbol,
1716
Component,
1817
ClassComponent
1918
} from './component'
@@ -235,6 +234,8 @@ const createVNodeWithArgsTransform = (
235234
)
236235
}
237236

237+
export const InternalObjectSymbol = Symbol()
238+
238239
export const createVNode = (__DEV__
239240
? createVNodeWithArgsTransform
240241
: _createVNode) as typeof _createVNode
@@ -261,7 +262,7 @@ function _createVNode(
261262
// class & style normalization.
262263
if (props) {
263264
// for reactive or proxy objects, we need to clone it to enable mutation.
264-
if (isReactive(props) || SetupProxySymbol in props) {
265+
if (isReactive(props) || InternalObjectSymbol in props) {
265266
props = extend({}, props)
266267
}
267268
let { class: klass, style } = props

packages/shared/src/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,12 @@ export const toDisplayString = (val: unknown): string => {
120120
: String(val)
121121
}
122122

123-
export function invokeArrayFns(fns: Function[], arg?: any) {
123+
export const invokeArrayFns = (fns: Function[], arg?: any) => {
124124
for (let i = 0; i < fns.length; i++) {
125125
fns[i](arg)
126126
}
127127
}
128+
129+
export const def = (obj: object, key: string | symbol, value: any) => {
130+
Object.defineProperty(obj, key, { value })
131+
}

0 commit comments

Comments
 (0)