1 | import * as runtimeDom from '@vue/runtime-dom';
|
2 | import { initCustomFormatter, warn, registerRuntimeCompiler } from '@vue/runtime-dom';
|
3 | export * from '@vue/runtime-dom';
|
4 | import { compile } from '@vue/compiler-dom';
|
5 | import { isString, NOOP, extend, generateCodeFrame } from '@vue/shared';
|
6 |
|
7 | function initDev() {
|
8 | {
|
9 | initCustomFormatter();
|
10 | }
|
11 | }
|
12 |
|
13 |
|
14 | if ((process.env.NODE_ENV !== 'production')) {
|
15 | initDev();
|
16 | }
|
17 | const compileCache = Object.create(null);
|
18 | function compileToFunction(template, options) {
|
19 | if (!isString(template)) {
|
20 | if (template.nodeType) {
|
21 | template = template.innerHTML;
|
22 | }
|
23 | else {
|
24 | (process.env.NODE_ENV !== 'production') && warn(`invalid template option: `, template);
|
25 | return NOOP;
|
26 | }
|
27 | }
|
28 | const key = template;
|
29 | const cached = compileCache[key];
|
30 | if (cached) {
|
31 | return cached;
|
32 | }
|
33 | if (template[0] === '#') {
|
34 | const el = document.querySelector(template);
|
35 | if ((process.env.NODE_ENV !== 'production') && !el) {
|
36 | warn(`Template element not found or is empty: ${template}`);
|
37 | }
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | template = el ? el.innerHTML : ``;
|
43 | }
|
44 | const { code } = compile(template, extend({
|
45 | hoistStatic: true,
|
46 | onError(err) {
|
47 | if ((process.env.NODE_ENV !== 'production')) {
|
48 | const message = `Template compilation error: ${err.message}`;
|
49 | const codeFrame = err.loc &&
|
50 | generateCodeFrame(template, err.loc.start.offset, err.loc.end.offset);
|
51 | warn(codeFrame ? `${message}\n${codeFrame}` : message);
|
52 | }
|
53 | else {
|
54 |
|
55 | throw err;
|
56 | }
|
57 | }
|
58 | }, options));
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | const render = (new Function('Vue', code)(runtimeDom));
|
64 | render._rc = true;
|
65 | return (compileCache[key] = render);
|
66 | }
|
67 | registerRuntimeCompiler(compileToFunction);
|
68 |
|
69 | export { compileToFunction as compile };
|