1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import * as runtimeDom from '@vue/runtime-dom';
|
7 | import { initCustomFormatter, registerRuntimeCompiler, warn } from '@vue/runtime-dom';
|
8 | export * from '@vue/runtime-dom';
|
9 | import { compile } from '@vue/compiler-dom';
|
10 | import { isString, NOOP, extend, generateCodeFrame, EMPTY_OBJ } from '@vue/shared';
|
11 |
|
12 | function initDev() {
|
13 | {
|
14 | initCustomFormatter();
|
15 | }
|
16 | }
|
17 |
|
18 | if (!!(process.env.NODE_ENV !== "production")) {
|
19 | initDev();
|
20 | }
|
21 | const compileCache = new WeakMap();
|
22 | function getCache(options) {
|
23 | let c = compileCache.get(options != null ? options : EMPTY_OBJ);
|
24 | if (!c) {
|
25 | c = Object.create(null);
|
26 | compileCache.set(options != null ? options : EMPTY_OBJ, c);
|
27 | }
|
28 | return c;
|
29 | }
|
30 | function 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 | }
|
78 | registerRuntimeCompiler(compileToFunction);
|
79 |
|
80 | export { compileToFunction as compile };
|