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, genCacheKey, extend, generateCodeFrame } 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 = Object.create(null);
|
22 | function compileToFunction(template, options) {
|
23 | if (!isString(template)) {
|
24 | if (template.nodeType) {
|
25 | template = template.innerHTML;
|
26 | } else {
|
27 | !!(process.env.NODE_ENV !== "production") && warn(`invalid template option: `, template);
|
28 | return NOOP;
|
29 | }
|
30 | }
|
31 | const key = genCacheKey(template, options);
|
32 | const cached = compileCache[key];
|
33 | if (cached) {
|
34 | return cached;
|
35 | }
|
36 | if (template[0] === "#") {
|
37 | const el = document.querySelector(template);
|
38 | if (!!(process.env.NODE_ENV !== "production") && !el) {
|
39 | warn(`Template element not found or is empty: ${template}`);
|
40 | }
|
41 | template = el ? el.innerHTML : ``;
|
42 | }
|
43 | const opts = extend(
|
44 | {
|
45 | hoistStatic: true,
|
46 | onError: !!(process.env.NODE_ENV !== "production") ? onError : void 0,
|
47 | onWarn: !!(process.env.NODE_ENV !== "production") ? (e) => onError(e, true) : NOOP
|
48 | },
|
49 | options
|
50 | );
|
51 | if (!opts.isCustomElement && typeof customElements !== "undefined") {
|
52 | opts.isCustomElement = (tag) => !!customElements.get(tag);
|
53 | }
|
54 | const { code } = compile(template, opts);
|
55 | function onError(err, asWarning = false) {
|
56 | const message = asWarning ? err.message : `Template compilation error: ${err.message}`;
|
57 | const codeFrame = err.loc && generateCodeFrame(
|
58 | template,
|
59 | err.loc.start.offset,
|
60 | err.loc.end.offset
|
61 | );
|
62 | warn(codeFrame ? `${message}
|
63 | ${codeFrame}` : message);
|
64 | }
|
65 | const render = new Function("Vue", code)(runtimeDom);
|
66 | render._rc = true;
|
67 | return compileCache[key] = render;
|
68 | }
|
69 | registerRuntimeCompiler(compileToFunction);
|
70 |
|
71 | export { compileToFunction as compile };
|