UNPKG

2.65 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(err) {
54 {
55 const message = `Template compilation error: ${err.message}`;
56 const codeFrame = err.loc &&
57 shared.generateCodeFrame(template, err.loc.start.offset, err.loc.end.offset);
58 runtimeDom.warn(codeFrame ? `${message}\n${codeFrame}` : message);
59 }
60 }
61 }, options));
62 // The wildcard import results in a huge object with every export
63 // with keys that cannot be mangled, and can be quite heavy size-wise.
64 // In the global build we know `Vue` is available globally so we can avoid
65 // the wildcard object.
66 const render = (new Function('Vue', code)(runtimeDom__namespace));
67 render._rc = true;
68 return (compileCache[key] = render);
69}
70runtimeDom.registerRuntimeCompiler(compileToFunction);
71
72Object.keys(runtimeDom).forEach(function (k) {
73 if (k !== 'default') exports[k] = runtimeDom[k];
74});
75exports.compile = compileToFunction;