保誠-保戶業務員媒合平台
HelenHuang
2022-06-09 23b60dc1975db38c280d8a123aff97544d1673e0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import Vue, { VNode } from "vue";
import {
  compile,
  compileToFunctions,
  ssrCompile,
  ssrCompileToFunctions,
  parseComponent,
  generateCodeFrame
} from "./";
 
// check compile options
const compiled = compile("<div>hi</div>", {
  outputSourceRange: true,
  preserveWhitespace: false,
  whitespace: 'condense',
  modules: [
    {
      preTransformNode: el => el,
      transformNode: el => el,
      postTransformNode: el => {
        el.tag = "p";
      },
      genData: el => el.tag,
      transformCode: (el, code) => code,
      staticKeys: ["test"]
    }
  ],
  directives: {
    test: (node, directiveMeta) => {
      node.tag;
      directiveMeta.value;
    }
  }
});
 
// can be passed to function constructor
new Function(compiled.render);
compiled.staticRenderFns.map(fn => new Function(fn));
 
// with outputSourceRange: true
// errors should be objects with range
compiled.errors.forEach(e => {
  console.log(e.msg)
})
 
// without option or without outputSourceRange: true, should be strings
const { errors } = compile(`foo`)
errors.forEach(e => {
  console.log(e.length)
})
 
const { errors: errors2 } = compile(`foo`, {})
errors2.forEach(e => {
  console.log(e.length)
})
 
const { errors: errors3 } = compile(`foo`, {
  outputSourceRange: false
})
errors3.forEach(e => {
  console.log(e.length)
})
 
const compiledFns = compileToFunctions("<div>hi</div>");
 
// can be passed to component render / staticRenderFns options
const vm = new Vue({
  data() {
    return {
      test: "Test"
    };
  },
  render: compiledFns.render,
  staticRenderFns: compiledFns.staticRenderFns
});
 
// can be called with component instance
const vnode: VNode = compiledFns.render.call(vm);
 
// check SFC parser
const desc = parseComponent("<template></template>", {
  pad: "space",
  deindent: false
});
 
const templateContent: string = desc.template!.content;
const scriptContent: string = desc.script!.content;
const styleContent: string = desc.styles.map(s => s.content).join("\n");
 
const codeframe: string = generateCodeFrame(`foobar`, 0, 4)