( template: string | HTMLElement, options?: CompilerOptions, )
| 28 | const compileCache: Record<string, RenderFunction> = Object.create(null) |
| 29 | |
| 30 | function compileToFunction( |
| 31 | template: string | HTMLElement, |
| 32 | options?: CompilerOptions, |
| 33 | ): RenderFunction { |
| 34 | if (!isString(template)) { |
| 35 | if (template.nodeType) { |
| 36 | template = template.innerHTML |
| 37 | } else { |
| 38 | __DEV__ && warn(`invalid template option: `, template) |
| 39 | return NOOP |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | const key = genCacheKey(template, options) |
| 44 | const cached = compileCache[key] |
| 45 | if (cached) { |
| 46 | return cached |
| 47 | } |
| 48 | |
| 49 | if (template[0] === '#') { |
| 50 | const el = document.querySelector(template) |
| 51 | if (__DEV__ && !el) { |
| 52 | warn(`Template element not found or is empty: ${template}`) |
| 53 | } |
| 54 | // __UNSAFE__ |
| 55 | // Reason: potential execution of JS expressions in in-DOM template. |
| 56 | // The user must make sure the in-DOM template is trusted. If it's rendered |
| 57 | // by the server, the template should not contain any user data. |
| 58 | template = el ? el.innerHTML : `` |
| 59 | } |
| 60 | |
| 61 | const opts = extend( |
| 62 | { |
| 63 | hoistStatic: true, |
| 64 | onError: __DEV__ ? onError : undefined, |
| 65 | onWarn: __DEV__ ? e => onError(e, true) : NOOP, |
| 66 | } as CompilerOptions, |
| 67 | options, |
| 68 | ) |
| 69 | |
| 70 | if (!opts.isCustomElement && typeof customElements !== 'undefined') { |
| 71 | opts.isCustomElement = tag => !!customElements.get(tag) |
| 72 | } |
| 73 | |
| 74 | const { code } = compile(template, opts) |
| 75 | |
| 76 | function onError(err: CompilerError, asWarning = false) { |
| 77 | const message = asWarning |
| 78 | ? err.message |
| 79 | : `Template compilation error: ${err.message}` |
| 80 | const codeFrame = |
| 81 | err.loc && |
| 82 | generateCodeFrame( |
| 83 | template as string, |
| 84 | err.loc.start.offset, |
| 85 | err.loc.end.offset, |
| 86 | ) |
| 87 | warn(codeFrame ? `${message}\n${codeFrame}` : message) |
nothing calls this directly
no test coverage detected