feat(compiler): element codegen

This commit is contained in:
Evan You
2019-09-22 16:50:57 -04:00
parent 40307d9642
commit 3a177a18d2
15 changed files with 369 additions and 120 deletions

View File

@@ -1,7 +1,6 @@
import {
ComponentInternalInstance,
Data,
currentInstance,
Component,
SetupContext
} from './component'
@@ -11,9 +10,7 @@ import {
isString,
isObject,
isArray,
EMPTY_OBJ,
capitalize,
camelize
EMPTY_OBJ
} from '@vue/shared'
import { computed } from './apiReactivity'
import { watch, WatchOptions, CleanupRegistrator } from './apiWatch'
@@ -29,12 +26,10 @@ import {
onUnmounted
} from './apiLifecycle'
import { DebuggerEvent, reactive } from '@vue/reactivity'
import { warn } from './warning'
import { ComponentPropsOptions, ExtractPropTypes } from './componentProps'
import { Directive } from './directives'
import { VNodeChild } from './vnode'
import { ComponentPublicInstance } from './componentPublicInstanceProxy'
import { currentRenderingInstance } from './componentRenderUtils'
interface ComponentOptionsBase<
Props,
@@ -387,32 +382,3 @@ function applyMixins(
applyOptions(instance, mixins[i], true)
}
}
export function resolveComponent(name: string): Component | undefined {
return resolveAsset('components', name) as any
}
export function resolveDirective(name: string): Directive | undefined {
return resolveAsset('directives', name) as any
}
function resolveAsset(type: 'components' | 'directives', name: string) {
const instance = currentRenderingInstance || currentInstance
if (instance) {
let camelized
const registry = instance[type]
const res =
registry[name] ||
registry[(camelized = camelize(name))] ||
registry[capitalize(camelized)]
if (__DEV__ && !res) {
warn(`Failed to resolve ${type.slice(0, -1)}: ${name}`)
}
return res
} else if (__DEV__) {
warn(
`resolve${capitalize(type.slice(0, -1))} ` +
`can only be used in render() or setup().`
)
}
}

View File

@@ -40,6 +40,7 @@ export const PublicInstanceProxyHandlers = {
// return the value from propsProxy for ref unwrapping and readonly
return (propsProxy as any)[key]
} else {
// TODO simplify this?
switch (key) {
case '$data':
return data
@@ -79,6 +80,7 @@ export const PublicInstanceProxyHandlers = {
},
has(target: ComponentInternalInstance, key: string): boolean {
const { renderContext, data, props } = target
// TODO handle $xxx properties
return (
(data !== EMPTY_OBJ && hasOwn(data, key)) ||
hasOwn(renderContext, key) ||

View File

@@ -0,0 +1,2 @@
// TODO
export function renderList() {}

View File

@@ -0,0 +1,34 @@
import { currentRenderingInstance } from '../componentRenderUtils'
import { currentInstance, Component } from '../component'
import { Directive } from '../directives'
import { camelize, capitalize } from '@vue/shared'
import { warn } from '../warning'
export function resolveComponent(name: string): Component | undefined {
return resolveAsset('components', name) as any
}
export function resolveDirective(name: string): Directive | undefined {
return resolveAsset('directives', name) as any
}
function resolveAsset(type: 'components' | 'directives', name: string) {
const instance = currentRenderingInstance || currentInstance
if (instance) {
let camelized
const registry = instance[type]
const res =
registry[name] ||
registry[(camelized = camelize(name))] ||
registry[capitalize(camelized)]
if (__DEV__ && !res) {
warn(`Failed to resolve ${type.slice(0, -1)}: ${name}`)
}
return res
} else if (__DEV__) {
warn(
`resolve${capitalize(type.slice(0, -1))} ` +
`can only be used in render() or setup().`
)
}
}

View File

@@ -37,7 +37,8 @@ export {
// Internal, for compiler generated code
export { applyDirectives } from './directives'
export { resolveComponent, resolveDirective } from './componentOptions'
export { resolveComponent, resolveDirective } from './helpers/resolveAssets'
export { renderList } from './helpers/renderList'
// Internal, for integration with runtime compiler
export { registerRuntimeCompiler } from './component'