refactor: use explicit exports for runtime-core

This commit is contained in:
Evan You 2020-02-14 00:13:54 -05:00
parent 42d80b5888
commit 112d8f7d86
11 changed files with 103 additions and 124 deletions

View File

@ -1,12 +1,15 @@
import { createBlock, createVNode, openBlock } from '@vue/runtime-test'
import { import {
ShapeFlags, createBlock,
createVNode,
openBlock,
Comment, Comment,
Fragment, Fragment,
Text, Text,
cloneVNode cloneVNode,
} from '@vue/runtime-core' mergeProps,
import { mergeProps, normalizeVNode } from '../src/vnode' normalizeVNode
} from '../src/vnode'
import { ShapeFlags } from '../src/shapeFlags'
import { Data } from '../src/component' import { Data } from '../src/component'
import { PatchFlags } from '@vue/shared' import { PatchFlags } from '@vue/shared'

View File

@ -0,0 +1,20 @@
import {
computed as _computed,
ComputedRef,
WritableComputedOptions,
WritableComputedRef,
ComputedGetter
} from '@vue/reactivity'
import { recordInstanceBoundEffect } from './component'
export function computed<T>(getter: ComputedGetter<T>): ComputedRef<T>
export function computed<T>(
options: WritableComputedOptions<T>
): WritableComputedRef<T>
export function computed<T>(
getterOrOptions: ComputedGetter<T> | WritableComputedOptions<T>
) {
const c = _computed(getterOrOptions as any)
recordInstanceBoundEffect(c.effect)
return c
}

View File

@ -15,7 +15,7 @@ import {
EMPTY_OBJ, EMPTY_OBJ,
NOOP NOOP
} from '@vue/shared' } from '@vue/shared'
import { computed } from './apiReactivity' import { computed } from './apiComputed'
import { watch, WatchOptions, WatchCallback } from './apiWatch' import { watch, WatchOptions, WatchCallback } from './apiWatch'
import { provide, inject } from './apiInject' import { provide, inject } from './apiInject'
import { import {

View File

@ -1,54 +0,0 @@
export {
ref,
isRef,
toRefs,
reactive,
isReactive,
readonly,
isReadonly,
shallowReactive,
toRaw,
markReadonly,
markNonReactive,
// types
ReactiveEffect,
ReactiveEffectOptions,
DebuggerEvent,
TrackOpTypes,
TriggerOpTypes,
Ref,
ComputedRef,
UnwrapRef,
WritableComputedOptions
} from '@vue/reactivity'
import {
computed as _computed,
ComputedRef,
WritableComputedOptions,
ReactiveEffect,
WritableComputedRef,
ComputedGetter
} from '@vue/reactivity'
import { currentInstance } from './component'
// record effects created during a component's setup() so that they can be
// stopped when the component unmounts
export function recordEffect(effect: ReactiveEffect) {
if (currentInstance) {
;(currentInstance.effects || (currentInstance.effects = [])).push(effect)
}
}
export function computed<T>(getter: ComputedGetter<T>): ComputedRef<T>
export function computed<T>(
options: WritableComputedOptions<T>
): WritableComputedRef<T>
export function computed<T>(
getterOrOptions: ComputedGetter<T> | WritableComputedOptions<T>
) {
const c = _computed(getterOrOptions as any)
recordEffect(c.effect)
return c
}

View File

@ -16,13 +16,13 @@ import {
hasChanged, hasChanged,
NOOP NOOP
} from '@vue/shared' } from '@vue/shared'
import { recordEffect } from './apiReactivity'
import { import {
currentInstance, currentInstance,
ComponentInternalInstance, ComponentInternalInstance,
currentSuspense, currentSuspense,
Data, Data,
isInSSRComponentSetup isInSSRComponentSetup,
recordInstanceBoundEffect
} from './component' } from './component'
import { import {
ErrorCodes, ErrorCodes,
@ -235,7 +235,7 @@ function doWatch(
} }
} }
recordEffect(runner) recordInstanceBoundEffect(runner)
return () => { return () => {
stop(runner) stop(runner)
if (instance) { if (instance) {

View File

@ -501,3 +501,11 @@ function createSetupContext(instance: ComponentInternalInstance): SetupContext {
} }
return __DEV__ ? Object.freeze(context) : context return __DEV__ ? Object.freeze(context) : context
} }
// record effects created during a component's setup() so that they can be
// stopped when the component unmounts
export function recordInstanceBoundEffect(effect: ReactiveEffect) {
if (currentInstance) {
;(currentInstance.effects || (currentInstance.effects = [])).push(effect)
}
}

View File

@ -417,6 +417,8 @@ function createSuspenseBoundary<HostNode, HostElement>(
pushWarningContext(vnode) pushWarningContext(vnode)
} }
handleSetupResult(instance, asyncSetupResult, suspense) handleSetupResult(instance, asyncSetupResult, suspense)
// unset placeholder, otherwise this will be treated as a hydration mount
vnode.el = null
setupRenderEffect( setupRenderEffect(
instance, instance,
vnode, vnode,

View File

@ -15,8 +15,8 @@ import { warn } from './warning'
import { PatchFlags, isReservedProp, isOn } from '@vue/shared' import { PatchFlags, isReservedProp, isOn } from '@vue/shared'
// Note: hydration is DOM-specific // Note: hydration is DOM-specific
// but we have to place it in core due to tight coupling with core renderer // but we have to place it in core due to tight coupling with core - splitting
// logic - splitting it out // it out creates a ton of unnecessary complexity.
export function createHydrateFn( export function createHydrateFn(
mountComponent: any, // TODO mountComponent: any, // TODO
patchProp: any // TODO patchProp: any // TODO

View File

@ -1,10 +1,35 @@
// Public API ------------------------------------------------------------------ // Public API ------------------------------------------------------------------
export const version = __VERSION__ export const version = __VERSION__
export * from './apiReactivity' export {
export * from './apiWatch' ref,
export * from './apiLifecycle' isRef,
export * from './apiInject' toRefs,
reactive,
isReactive,
readonly,
isReadonly,
shallowReactive,
toRaw,
markReadonly,
markNonReactive
} from '@vue/reactivity'
export { computed } from './apiComputed'
export { watch } from './apiWatch'
export {
onBeforeMount,
onMounted,
onBeforeUpdate,
onUpdated,
onBeforeUnmount,
onUnmounted,
onActivated,
onDeactivated,
onRenderTracked,
onRenderTriggered,
onErrorCaptured
} from './apiLifecycle'
export { provide, inject } from './apiInject'
export { nextTick } from './scheduler' export { nextTick } from './scheduler'
export { defineComponent } from './apiDefineComponent' export { defineComponent } from './apiDefineComponent'
@ -24,37 +49,23 @@ export {
createBlock createBlock
} from './vnode' } from './vnode'
// Internal Components // Internal Components
export { Fragment, Portal } from './vnode' export { Text, Comment, Fragment, Portal } from './vnode'
export { Suspense, SuspenseProps } from './components/Suspense' export { Suspense, SuspenseProps } from './components/Suspense'
export { KeepAlive, KeepAliveProps } from './components/KeepAlive' export { KeepAlive, KeepAliveProps } from './components/KeepAlive'
export { export {
BaseTransition, BaseTransition,
BaseTransitionProps BaseTransitionProps
} from './components/BaseTransition' } from './components/BaseTransition'
// VNode flags export { PatchFlags } from '@vue/shared'
export { PublicShapeFlags as ShapeFlags } from './shapeFlags' export { ShapeFlags } from './shapeFlags'
import { PublicPatchFlags } from '@vue/shared'
export const PatchFlags = PublicPatchFlags as {
// export patch flags as plain numbers to avoid d.ts relying on @vue/shared
// the enum type is internal anyway.
TEXT: number
CLASS: number
STYLE: number
PROPS: number
NEED_PATCH: number
FULL_PROPS: number
STABLE_FRAGMENT: number
KEYED_FRAGMENT: number
UNKEYED_FRAGMENT: number
DYNAMIC_SLOTS: number
BAIL: number
}
// SFC CSS Modules // SFC CSS Modules
export { useCSSModule } from './helpers/useCssModule' export { useCSSModule } from './helpers/useCssModule'
// Internal API ----------------------------------------------------------------
// For custom renderers // For custom renderers
export { createRenderer, RootRenderFunction } from './renderer' export { createRenderer } from './renderer'
export { warn } from './warning' export { warn } from './warning'
export { export {
handleError, handleError,
@ -63,14 +74,10 @@ export {
} from './errorHandling' } from './errorHandling'
export { export {
useTransitionState, useTransitionState,
TransitionState,
resolveTransitionHooks, resolveTransitionHooks,
setTransitionHooks, setTransitionHooks
TransitionHooks
} from './components/BaseTransition' } from './components/BaseTransition'
// Internal API ----------------------------------------------------------------
// For compiler generated code // For compiler generated code
// should sync with '@vue/compiler-core/src/runtimeConstants.ts' // should sync with '@vue/compiler-core/src/runtimeConstants.ts'
export { withDirectives } from './directives' export { withDirectives } from './directives'
@ -104,6 +111,7 @@ export const camelize = _camelize as (s: string) => string
export { registerRuntimeCompiler } from './component' export { registerRuntimeCompiler } from './component'
// SSR ------------------------------------------------------------------------- // SSR -------------------------------------------------------------------------
import { createComponentInstance, setupComponent } from './component' import { createComponentInstance, setupComponent } from './component'
import { import {
renderComponentRoot, renderComponentRoot,
@ -125,6 +133,26 @@ export const ssrUtils = (__NODE_JS__ ? _ssrUtils : null) as typeof _ssrUtils
// Types ----------------------------------------------------------------------- // Types -----------------------------------------------------------------------
export {
ReactiveEffect,
ReactiveEffectOptions,
DebuggerEvent,
TrackOpTypes,
TriggerOpTypes,
Ref,
ComputedRef,
UnwrapRef,
WritableComputedOptions
} from '@vue/reactivity'
export {
// types
WatchOptions,
WatchCallback,
CleanupRegistrator,
WatchSource,
StopHandle
} from './apiWatch'
export { InjectionKey } from './apiInject'
export { export {
App, App,
AppConfig, AppConfig,
@ -152,9 +180,8 @@ export {
ComponentOptionsWithObjectProps as ComponentOptionsWithProps, ComponentOptionsWithObjectProps as ComponentOptionsWithProps,
ComponentOptionsWithArrayProps ComponentOptionsWithArrayProps
} from './apiOptions' } from './apiOptions'
export { ComponentPublicInstance } from './componentProxy' export { ComponentPublicInstance } from './componentProxy'
export { RendererOptions } from './renderer' export { RendererOptions, RootRenderFunction } from './renderer'
export { Slot, Slots } from './componentSlots' export { Slot, Slots } from './componentSlots'
export { export {
Prop, Prop,
@ -171,4 +198,5 @@ export {
DirectiveArguments DirectiveArguments
} from './directives' } from './directives'
export { SuspenseBoundary } from './components/Suspense' export { SuspenseBoundary } from './components/Suspense'
export { TransitionState, TransitionHooks } from './components/BaseTransition'
export { HMRRuntime } from './hmr' export { HMRRuntime } from './hmr'

View File

@ -10,17 +10,3 @@ export const enum ShapeFlags {
COMPONENT_KEPT_ALIVE = 1 << 8, COMPONENT_KEPT_ALIVE = 1 << 8,
COMPONENT = ShapeFlags.STATEFUL_COMPONENT | ShapeFlags.FUNCTIONAL_COMPONENT COMPONENT = ShapeFlags.STATEFUL_COMPONENT | ShapeFlags.FUNCTIONAL_COMPONENT
} }
// For runtime consumption
export const PublicShapeFlags = {
ELEMENT: ShapeFlags.ELEMENT,
FUNCTIONAL_COMPONENT: ShapeFlags.FUNCTIONAL_COMPONENT,
STATEFUL_COMPONENT: ShapeFlags.STATEFUL_COMPONENT,
TEXT_CHILDREN: ShapeFlags.TEXT_CHILDREN,
ARRAY_CHILDREN: ShapeFlags.ARRAY_CHILDREN,
SLOTS_CHILDREN: ShapeFlags.SLOTS_CHILDREN,
SUSPENSE: ShapeFlags.SUSPENSE,
COMPONENT_SHOULD_KEEP_ALIVE: ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE,
COMPONENT_KEPT_ALIVE: ShapeFlags.COMPONENT_KEPT_ALIVE,
COMPONENT: ShapeFlags.COMPONENT
}

View File

@ -82,20 +82,6 @@ export const enum PatchFlags {
BAIL = -2 BAIL = -2
} }
// runtime object for public consumption
export const PublicPatchFlags = {
TEXT: PatchFlags.TEXT,
CLASS: PatchFlags.CLASS,
STYLE: PatchFlags.STYLE,
PROPS: PatchFlags.PROPS,
NEED_PATCH: PatchFlags.NEED_PATCH,
FULL_PROPS: PatchFlags.FULL_PROPS,
KEYED_FRAGMENT: PatchFlags.KEYED_FRAGMENT,
UNKEYED_FRAGMENT: PatchFlags.UNKEYED_FRAGMENT,
DYNAMIC_SLOTS: PatchFlags.DYNAMIC_SLOTS,
BAIL: PatchFlags.BAIL
}
// dev only flag -> name mapping // dev only flag -> name mapping
export const PatchFlagNames = { export const PatchFlagNames = {
[PatchFlags.TEXT]: `TEXT`, [PatchFlags.TEXT]: `TEXT`,