From b79a06c60598038714ddb0cc3293b5d9be3120f6 Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 16 Nov 2020 11:28:37 -0500 Subject: [PATCH] wip: optimize expose --- .../runtime-core/__tests__/apiExpose.spec.ts | 46 +++++++++++++++++++ packages/runtime-core/src/componentOptions.ts | 18 ++++++-- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/packages/runtime-core/__tests__/apiExpose.spec.ts b/packages/runtime-core/__tests__/apiExpose.spec.ts index febf3452..7695360b 100644 --- a/packages/runtime-core/__tests__/apiExpose.spec.ts +++ b/packages/runtime-core/__tests__/apiExpose.spec.ts @@ -95,4 +95,50 @@ describe('api: expose', () => { expect(childRef.value.bar).toBe(2) expect(childRef.value.baz).toBeUndefined() }) + + test('options: empty', () => { + const Child = defineComponent({ + render() {}, + expose: [], + data() { + return { + foo: 1 + } + } + }) + + const childRef = ref() + const Parent = { + setup() { + return () => h(Child, { ref: childRef }) + } + } + const root = nodeOps.createElement('div') + render(h(Parent), root) + expect(childRef.value).toBeTruthy() + expect('foo' in childRef.value).toBe(false) + }) + + test('options: empty + setup context', () => { + const Child = defineComponent({ + render() {}, + expose: [], + setup(_, { expose }) { + expose({ + foo: 1 + }) + } + }) + + const childRef = ref() + const Parent = { + setup() { + return () => h(Child, { ref: childRef }) + } + } + const root = nodeOps.createElement('div') + render(h(Parent), root) + expect(childRef.value).toBeTruthy() + expect(childRef.value.foo).toBe(1) + }) }) diff --git a/packages/runtime-core/src/componentOptions.ts b/packages/runtime-core/src/componentOptions.ts index 8a158ee6..dd95e29a 100644 --- a/packages/runtime-core/src/componentOptions.ts +++ b/packages/runtime-core/src/componentOptions.ts @@ -743,11 +743,19 @@ export function applyOptions( onUnmounted(unmounted.bind(publicThis)) } - if (!asMixin && expose) { - const exposed = instance.exposed || (instance.exposed = proxyRefs({})) - expose.forEach(key => { - exposed[key] = toRef(publicThis, key as any) - }) + if (isArray(expose)) { + if (!asMixin) { + if (expose.length) { + const exposed = instance.exposed || (instance.exposed = proxyRefs({})) + expose.forEach(key => { + exposed[key] = toRef(publicThis, key as any) + }) + } else if (!instance.exposed) { + instance.exposed = EMPTY_OBJ + } + } else if (__DEV__) { + warn(`The \`expose\` option is ignored when used in mixins.`) + } } }