index.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 { compile, CompilerOptions, CompilerError } from '@vue/compiler-dom'
  5. import { registerRuntimeCompiler, RenderFunction, warn } from '@vue/runtime-dom'
  6. import * as runtimeDom from '@vue/runtime-dom'
  7. import { isString, NOOP, generateCodeFrame, extend } from '@vue/shared'
  8. import { InternalRenderFunction } from 'packages/runtime-core/src/component'
  9. if (__DEV__) {
  10. initDev()
  11. }
  12. const compileCache: Record<string, RenderFunction> = Object.create(null)
  13. function compileToFunction(
  14. template: string | HTMLElement,
  15. options?: CompilerOptions
  16. ): RenderFunction {
  17. if (!isString(template)) {
  18. if (template.nodeType) {
  19. template = template.innerHTML
  20. } else {
  21. __DEV__ && warn(`invalid template option: `, template)
  22. return NOOP
  23. }
  24. }
  25. const key = template
  26. const cached = compileCache[key]
  27. if (cached) {
  28. return cached
  29. }
  30. if (template[0] === '#') {
  31. const el = document.querySelector(template)
  32. if (__DEV__ && !el) {
  33. warn(`Template element not found or is empty: ${template}`)
  34. }
  35. // __UNSAFE__
  36. // Reason: potential execution of JS expressions in in-DOM template.
  37. // The user must make sure the in-DOM template is trusted. If it's rendered
  38. // by the server, the template should not contain any user data.
  39. template = el ? el.innerHTML : ``
  40. }
  41. const { code } = compile(
  42. template,
  43. extend(
  44. {
  45. hoistStatic: true,
  46. onError(err: CompilerError) {
  47. if (__DEV__) {
  48. const message = `Template compilation error: ${err.message}`
  49. const codeFrame =
  50. err.loc &&
  51. generateCodeFrame(
  52. template as string,
  53. err.loc.start.offset,
  54. err.loc.end.offset
  55. )
  56. warn(codeFrame ? `${message}\n${codeFrame}` : message)
  57. } else {
  58. /* istanbul ignore next */
  59. throw err
  60. }
  61. }
  62. },
  63. options
  64. )
  65. )
  66. // The wildcard import results in a huge object with every export
  67. // with keys that cannot be mangled, and can be quite heavy size-wise.
  68. // In the global build we know `Vue` is available globally so we can avoid
  69. // the wildcard object.
  70. const render = (__GLOBAL__
  71. ? new Function(code)()
  72. : new Function('Vue', code)(runtimeDom)) as RenderFunction
  73. // mark the function as runtime compiled
  74. ;(render as InternalRenderFunction)._rc = true
  75. return (compileCache[key] = render)
  76. }
  77. registerRuntimeCompiler(compileToFunction)
  78. export { compileToFunction as compile }
  79. export * from '@vue/runtime-dom'