vue3-yuanma/packages/vue/src/index.ts

53 lines
1.6 KiB
TypeScript
Raw Normal View History

2019-09-16 19:06:45 +00:00
// This package is the "full-build" that includes both the runtime
2019-09-20 04:24:16 +00:00
// and the compiler, and supports on-the-fly compilation of the template option.
import { compile, CompilerOptions } from '@vue/compiler-dom'
import { registerRuntimeCompiler, RenderFunction, warn } from '@vue/runtime-dom'
import * as runtimeDom from '@vue/runtime-dom'
import { isString, NOOP } from '@vue/shared'
const idToTemplateCache = Object.create(null)
2019-09-20 04:12:37 +00:00
function compileToFunction(
template: string | HTMLElement,
options?: CompilerOptions
): RenderFunction {
if (isString(template)) {
if (template[0] === '#') {
if (template in idToTemplateCache) {
template = idToTemplateCache[template]
} else {
const el = document.querySelector(template)
if (__DEV__ && !el) {
warn(`Template element not found or is empty: ${template}`)
}
template = idToTemplateCache[template] = el ? el.innerHTML : ``
}
}
} else if (template.nodeType) {
template = template.innerHTML
} else {
__DEV__ && warn(`invalid template option: `, template)
return NOOP
}
const { code } = compile(template as string, {
2019-10-04 13:03:00 +00:00
hoistStatic: true,
cacheHandlers: true,
2019-10-04 03:30:25 +00:00
...options
})
return new Function('Vue', code)(runtimeDom) as RenderFunction
2019-09-20 04:12:37 +00:00
}
registerRuntimeCompiler(compileToFunction)
2019-09-20 04:24:16 +00:00
export { compileToFunction as compile }
2018-10-26 19:44:50 +00:00
export * from '@vue/runtime-dom'
2019-09-04 00:51:42 +00:00
if (__BROWSER__ && __DEV__) {
2019-09-04 00:51:42 +00:00
console[console.info ? 'info' : 'log'](
`You are running a development build of Vue.\n` +
`Make sure to use the production build (*.prod.js) when deploying for production.`
)
}