MCPcopy
hub / github.com/vuejs/core / compileToFunction

Function compileToFunction

packages/vue-compat/src/index.ts:31–105  ·  view source on GitHub ↗
(
  template: string | HTMLElement,
  options?: CompilerOptions,
)

Source from the content-addressed store, hash-verified

29const compileCache: Record<string, RenderFunction> = Object.create(null)
30
31function 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,

Callers

nothing calls this directly

Calls 7

isStringFunction · 0.90
genCacheKeyFunction · 0.90
warnDeprecationFunction · 0.90
compileFunction · 0.90
querySelectorMethod · 0.80
onErrorFunction · 0.70
warnFunction · 0.50

Tested by

no test coverage detected