保誠-保戶業務員媒合平台
HelenHuang
2022-06-09 9bdb95c9e34cef640534e5e5a1e2225a80442000
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
var chalk = require('chalk')
var vueCompiler = require('vue-template-compiler')
var transpile = require('vue-template-es2015-compiler')
var compilePug = require('./compilers/pug-compiler')
var compileJade = require('./compilers/jade-compiler')
var compileHaml = require('./compilers/haml-compiler')
const throwError = require('./throw-error')
 
function getTemplateContent (templatePart, config) {
  if (templatePart.lang === 'pug') {
    return compilePug(templatePart, config)
  }
  if (templatePart.lang === 'jade') {
    return compileJade(templatePart.content)
  }
  if (templatePart.lang === 'haml') {
    return compileHaml(templatePart.content)
  }
  return templatePart.content
}
 
module.exports = function compileTemplate (templatePart, config) {
  var templateContent = getTemplateContent(templatePart, config)
  var compiled = vueCompiler.compile(templateContent)
  if (compiled.errors.length) {
    compiled.errors.forEach(function (msg) {
      console.error('\n' + chalk.red(msg) + '\n')
    })
    throwError('Vue template compilation failed')
  } else {
    return compile(compiled, templatePart.attrs.functional)
  }
}
 
function compile (compiled, isFunctional) {
  function toFunction (code) {
    var renderArgs = isFunctional ? '_h, _vm' : ''
    return transpile('function render (' + renderArgs + ') {' + code + '}', {
      transforms: { stripWithFunctional: isFunctional }
    })
  }
 
  return {
    render: toFunction(compiled.render),
    staticRenderFns: '[' + compiled.staticRenderFns.map(toFunction).join(',') + ']'
  }
}