vue3-yuanma/packages/runtime-dom/src/index.ts

140 lines
3.6 KiB
TypeScript
Raw Normal View History

2019-11-01 15:32:53 +00:00
import {
createRenderer,
createHydrationRenderer,
2019-11-01 15:32:53 +00:00
warn,
RootRenderFunction,
CreateAppFunction,
Renderer,
HydrationRenderer,
App,
RootHydrateFunction
2019-11-01 15:32:53 +00:00
} from '@vue/runtime-core'
2019-06-20 13:28:37 +00:00
import { nodeOps } from './nodeOps'
import { patchProp } from './patchProp'
// Importing from the compiler, will be tree-shaken in prod
import { isFunction, isString, isHTMLTag, isSVGTag } from '@vue/shared'
2018-09-19 15:35:38 +00:00
declare module '@vue/reactivity' {
export interface RefUnwrapBailTypes {
// Note: if updating this, also update `types/refBail.d.ts`.
runtimeDOMBailTypes: Node | Window
}
}
const rendererOptions = {
2019-06-20 13:28:37 +00:00
patchProp,
...nodeOps
}
// lazy create the renderer - this makes core renderer logic tree-shakable
// in case the user only imports reactivity utilities from Vue.
let renderer: Renderer | HydrationRenderer
let enabledHydration = false
function ensureRenderer() {
return renderer || (renderer = createRenderer(rendererOptions))
}
function ensureHydrationRenderer() {
renderer = enabledHydration
? renderer
: createHydrationRenderer(rendererOptions)
enabledHydration = true
return renderer as HydrationRenderer
}
2019-09-02 20:09:34 +00:00
2019-11-01 15:32:53 +00:00
// use explicit type casts here to avoid import() calls in rolled-up d.ts
export const render = ((...args) => {
ensureRenderer().render(...args)
}) as RootRenderFunction<Element>
export const hydrate = ((...args) => {
ensureHydrationRenderer().hydrate(...args)
}) as RootHydrateFunction
2019-11-01 15:32:53 +00:00
export const createApp = ((...args) => {
const app = ensureRenderer().createApp(...args)
if (__DEV__) {
injectNativeTagCheck(app)
}
const { mount } = app
app.mount = (containerOrSelector: Element | string): any => {
const container = normalizeContainer(containerOrSelector)
if (!container) return
const component = app._component
if (!isFunction(component) && !component.render && !component.template) {
component.template = container.innerHTML
}
// clear content before mounting
container.innerHTML = ''
const proxy = mount(container)
container.removeAttribute('v-cloak')
return proxy
}
return app
}) as CreateAppFunction<Element>
export const createSSRApp = ((...args) => {
const app = ensureHydrationRenderer().createApp(...args)
if (__DEV__) {
injectNativeTagCheck(app)
}
const { mount } = app
app.mount = (containerOrSelector: Element | string): any => {
const container = normalizeContainer(containerOrSelector)
if (container) {
return mount(container, true)
2020-02-14 04:31:03 +00:00
}
}
return app
}) as CreateAppFunction<Element>
function injectNativeTagCheck(app: App) {
// Inject `isNativeTag`
// this is used for component name validation (dev only)
Object.defineProperty(app.config, 'isNativeTag', {
value: (tag: string) => isHTMLTag(tag) || isSVGTag(tag),
writable: false
})
}
function normalizeContainer(container: Element | string): Element | null {
if (isString(container)) {
const res = document.querySelector(container)
if (__DEV__ && !res) {
warn(`Failed to mount app: mount target selector returned null.`)
}
return res
}
return container
}
// DOM-only components
export { Transition, TransitionProps } from './components/Transition'
export {
TransitionGroup,
TransitionGroupProps
} from './components/TransitionGroup'
// **Internal** DOM-only runtime directive helpers
2019-10-10 22:02:51 +00:00
export {
vModelText,
vModelCheckbox,
vModelRadio,
vModelSelect,
vModelDynamic
} from './directives/vModel'
export { withModifiers, withKeys } from './directives/vOn'
2019-11-28 23:41:01 +00:00
export { vShow } from './directives/vShow'
// re-export everything from core
2019-08-20 13:58:10 +00:00
// h, Component, reactivity API, nextTick, flags & types
2018-10-26 19:44:50 +00:00
export * from '@vue/runtime-core'