UNPKG

2.41 kBJavaScriptView Raw
1/**
2* vue v3.4.25
3* (c) 2018-present Yuxi (Evan) You and Vue contributors
4* @license MIT
5**/
6import * as runtimeDom from '@vue/runtime-dom';
7import { initCustomFormatter, registerRuntimeCompiler, warn } from '@vue/runtime-dom';
8export * from '@vue/runtime-dom';
9import { compile } from '@vue/compiler-dom';
10import { isString, NOOP, extend, generateCodeFrame, EMPTY_OBJ } from '@vue/shared';
11
12function initDev() {
13 {
14 initCustomFormatter();
15 }
16}
17
18if (!!(process.env.NODE_ENV !== "production")) {
19 initDev();
20}
21const compileCache = /* @__PURE__ */ new WeakMap();
22function getCache(options) {
23 let c = compileCache.get(options != null ? options : EMPTY_OBJ);
24 if (!c) {
25 c = /* @__PURE__ */ Object.create(null);
26 compileCache.set(options != null ? options : EMPTY_OBJ, c);
27 }
28 return c;
29}
30function compileToFunction(template, options) {
31 if (!isString(template)) {
32 if (template.nodeType) {
33 template = template.innerHTML;
34 } else {
35 !!(process.env.NODE_ENV !== "production") && warn(`invalid template option: `, template);
36 return NOOP;
37 }
38 }
39 const key = template;
40 const cache = getCache(options);
41 const cached = cache[key];
42 if (cached) {
43 return cached;
44 }
45 if (template[0] === "#") {
46 const el = document.querySelector(template);
47 if (!!(process.env.NODE_ENV !== "production") && !el) {
48 warn(`Template element not found or is empty: ${template}`);
49 }
50 template = el ? el.innerHTML : ``;
51 }
52 const opts = extend(
53 {
54 hoistStatic: true,
55 onError: !!(process.env.NODE_ENV !== "production") ? onError : void 0,
56 onWarn: !!(process.env.NODE_ENV !== "production") ? (e) => onError(e, true) : NOOP
57 },
58 options
59 );
60 if (!opts.isCustomElement && typeof customElements !== "undefined") {
61 opts.isCustomElement = (tag) => !!customElements.get(tag);
62 }
63 const { code } = compile(template, opts);
64 function onError(err, asWarning = false) {
65 const message = asWarning ? err.message : `Template compilation error: ${err.message}`;
66 const codeFrame = err.loc && generateCodeFrame(
67 template,
68 err.loc.start.offset,
69 err.loc.end.offset
70 );
71 warn(codeFrame ? `${message}
72${codeFrame}` : message);
73 }
74 const render = new Function("Vue", code)(runtimeDom);
75 render._rc = true;
76 return cache[key] = render;
77}
78registerRuntimeCompiler(compileToFunction);
79
80export { compileToFunction as compile };