UNPKG

2.8 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var compilerDom = require('@vue/compiler-dom');
6var runtimeDom = require('@vue/runtime-dom');
7var shared = require('@vue/shared');
8
9function _interopNamespaceDefault(e) {
10 var n = Object.create(null);
11 if (e) {
12 for (var k in e) {
13 n[k] = e[k];
14 }
15 }
16 n.default = e;
17 return Object.freeze(n);
18}
19
20var runtimeDom__namespace = /*#__PURE__*/_interopNamespaceDefault(runtimeDom);
21
22// This entry is the "full-build" that includes both the runtime
23const compileCache = Object.create(null);
24function compileToFunction(template, options) {
25 if (!shared.isString(template)) {
26 if (template.nodeType) {
27 template = template.innerHTML;
28 }
29 else {
30 runtimeDom.warn(`invalid template option: `, template);
31 return shared.NOOP;
32 }
33 }
34 const key = template;
35 const cached = compileCache[key];
36 if (cached) {
37 return cached;
38 }
39 if (template[0] === '#') {
40 const el = document.querySelector(template);
41 if (!el) {
42 runtimeDom.warn(`Template element not found or is empty: ${template}`);
43 }
44 // __UNSAFE__
45 // Reason: potential execution of JS expressions in in-DOM template.
46 // The user must make sure the in-DOM template is trusted. If it's rendered
47 // by the server, the template should not contain any user data.
48 template = el ? el.innerHTML : ``;
49 }
50 const opts = shared.extend({
51 hoistStatic: true,
52 onError: onError ,
53 onWarn: e => onError(e, true)
54 }, options);
55 if (!opts.isCustomElement && typeof customElements !== 'undefined') {
56 opts.isCustomElement = tag => !!customElements.get(tag);
57 }
58 const { code } = compilerDom.compile(template, opts);
59 function onError(err, asWarning = false) {
60 const message = asWarning
61 ? err.message
62 : `Template compilation error: ${err.message}`;
63 const codeFrame = err.loc &&
64 shared.generateCodeFrame(template, err.loc.start.offset, err.loc.end.offset);
65 runtimeDom.warn(codeFrame ? `${message}\n${codeFrame}` : message);
66 }
67 // The wildcard import results in a huge object with every export
68 // with keys that cannot be mangled, and can be quite heavy size-wise.
69 // In the global build we know `Vue` is available globally so we can avoid
70 // the wildcard object.
71 const render = (new Function('Vue', code)(runtimeDom__namespace));
72 render._rc = true;
73 return (compileCache[key] = render);
74}
75runtimeDom.registerRuntimeCompiler(compileToFunction);
76
77exports.compile = compileToFunction;
78Object.keys(runtimeDom).forEach(function(k) {
79 if (k !== 'default') exports[k] = runtimeDom[k];
80});