index.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // This entry is the "full-build" that includes both the runtime
  2. // and the compiler, and supports on-the-fly compilation of the template option.
  3. import { initDev } from './dev'
  4. import {
  5. type CompilerError,
  6. type CompilerOptions,
  7. compile,
  8. } from '@vue/compiler-dom'
  9. import {
  10. type RenderFunction,
  11. registerRuntimeCompiler,
  12. warn,
  13. } from '@vue/runtime-dom'
  14. import * as runtimeDom from '@vue/runtime-dom'
  15. import {
  16. EMPTY_OBJ,
  17. NOOP,
  18. extend,
  19. generateCodeFrame,
  20. isString,
  21. } from '@vue/shared'
  22. import type { InternalRenderFunction } from 'packages/runtime-core/src/component'
  23. if (__DEV__) {
  24. initDev()
  25. }
  26. const compileCache = new WeakMap<
  27. CompilerOptions,
  28. Record<string, RenderFunction>
  29. >()
  30. function getCache(options?: CompilerOptions) {
  31. let c = compileCache.get(options ?? EMPTY_OBJ)
  32. if (!c) {
  33. c = Object.create(null) as Record<string, RenderFunction>
  34. compileCache.set(options ?? EMPTY_OBJ, c)
  35. }
  36. return c
  37. }
  38. function compileToFunction(
  39. template: string | HTMLElement,
  40. options?: CompilerOptions,
  41. ): RenderFunction {
  42. if (!isString(template)) {
  43. if (template.nodeType) {
  44. template = template.innerHTML
  45. } else {
  46. __DEV__ && warn(`invalid template option: `, template)
  47. return NOOP
  48. }
  49. }
  50. const key = template
  51. const cache = getCache(options)
  52. const cached = cache[key]
  53. if (cached) {
  54. return cached
  55. }
  56. if (template[0] === '#') {
  57. const el = document.querySelector(template)
  58. if (__DEV__ && !el) {
  59. warn(`Template element not found or is empty: ${template}`)
  60. }
  61. // __UNSAFE__
  62. // Reason: potential execution of JS expressions in in-DOM template.
  63. // The user must make sure the in-DOM template is trusted. If it's rendered
  64. // by the server, the template should not contain any user data.
  65. template = el ? el.innerHTML : ``
  66. }
  67. const opts = extend(
  68. {
  69. hoistStatic: true,
  70. onError: __DEV__ ? onError : undefined,
  71. onWarn: __DEV__ ? e => onError(e, true) : NOOP,
  72. } as CompilerOptions,
  73. options,
  74. )
  75. if (!opts.isCustomElement && typeof customElements !== 'undefined') {
  76. opts.isCustomElement = tag => !!customElements.get(tag)
  77. }
  78. const { code } = compile(template, opts)
  79. function onError(err: CompilerError, asWarning = false) {
  80. const message = asWarning
  81. ? err.message
  82. : `Template compilation error: ${err.message}`
  83. const codeFrame =
  84. err.loc &&
  85. generateCodeFrame(
  86. template as string,
  87. err.loc.start.offset,
  88. err.loc.end.offset,
  89. )
  90. warn(codeFrame ? `${message}\n${codeFrame}` : message)
  91. }
  92. // The wildcard import results in a huge object with every export
  93. // with keys that cannot be mangled, and can be quite heavy size-wise.
  94. // In the global build we know `Vue` is available globally so we can avoid
  95. // the wildcard object.
  96. const render = (
  97. __GLOBAL__ ? new Function(code)() : new Function('Vue', code)(runtimeDom)
  98. ) as RenderFunction
  99. // mark the function as runtime compiled
  100. ;(render as InternalRenderFunction)._rc = true
  101. return (cache[key] = render)
  102. }
  103. registerRuntimeCompiler(compileToFunction)
  104. export { compileToFunction as compile }
  105. export * from '@vue/runtime-dom'