UNPKG

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