( template: string | HTMLElement, options?: CompilerOptions, )
| 29 | const compileCache: Record<string, RenderFunction> = Object.create(null) |
| 30 | |
| 31 | function compileToFunction( |
| 32 | template: string | HTMLElement, |
| 33 | options?: CompilerOptions, |
| 34 | ): RenderFunction { |
| 35 | if (!isString(template)) { |
| 36 | if (template.nodeType) { |
| 37 | template = template.innerHTML |
| 38 | } else { |
| 39 | __DEV__ && warn(`invalid template option: `, template) |
| 40 | return NOOP |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | const key = genCacheKey(template, options) |
| 45 | const cached = compileCache[key] |
| 46 | if (cached) { |
| 47 | return cached |
| 48 | } |
| 49 | |
| 50 | if (template[0] === '#') { |
| 51 | const el = document.querySelector(template) |
| 52 | if (__DEV__ && !el) { |
| 53 | warn(`Template element not found or is empty: ${template}`) |
| 54 | } |
| 55 | // __UNSAFE__ |
| 56 | // Reason: potential execution of JS expressions in in-DOM template. |
| 57 | // The user must make sure the in-DOM template is trusted. If it's rendered |
| 58 | // by the server, the template should not contain any user data. |
| 59 | template = el ? el.innerHTML : `` |
| 60 | } |
| 61 | |
| 62 | if (__DEV__ && !__TEST__ && (!options || !options.whitespace)) { |
| 63 | warnDeprecation(DeprecationTypes.CONFIG_WHITESPACE, null) |
| 64 | } |
| 65 | |
| 66 | const { code } = compile( |
| 67 | template, |
| 68 | extend( |
| 69 | { |
| 70 | hoistStatic: true, |
| 71 | whitespace: 'preserve', |
| 72 | onError: __DEV__ ? onError : undefined, |
| 73 | onWarn: __DEV__ ? e => onError(e, true) : NOOP, |
| 74 | } as CompilerOptions, |
| 75 | options, |
| 76 | ), |
| 77 | ) |
| 78 | |
| 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, |
nothing calls this directly
no test coverage detected