Blame view

node_modules/vue-baidu-map/build/compiler.js 3.21 KB
290144e9   易尊强   第一次
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  /**
   * @file compile & output regular vue component files
   */
  const compiler = require('vue-template-compiler')
  const path = require('path')
  const fs = require('fs')
  const pug = require('pug')
  const babel = require('babel-core')
  const rmdir = require('rmdir')
  
  const rootPath = path.resolve(__dirname, '../src')
  
  const isArray = Array.isArray
  
  function attrsToString (attrs) {
    let ret = ''
    for (const name in attrs) {
      const value = attrs[name]
      if (value === true) {
        ret += `${name} `
      } else if (value) {
        ret += `${name}="${value}" `
      }
    }
    return ret
  }
  
  function tagToString (tag) {
    return tag ? `<${tag.type} ${attrsToString(tag.attrs)}>${tag.content}</${tag.type}>` : ''
  }
  
  function reverseToComponent (componentObj) {
    let ret = ''
    for (const key in componentObj) {
      const tag = componentObj[key]
      if (isArray(tag)) {
        ret += tag.map(tagToString).join('')
      } else {
        ret += tagToString(tag)
      }
    }
    return ret
  }
  
  function transformAlias (content, level) {
    return babel.transform(content, {
      plugins: [
        [
          'module-alias', [
            {src: '../'.repeat(level), expose: '@'}
          ]
        ]
      ]
    }).code
  }
  
  function transformES (content) {
    return babel.transform(content, {
      presets: [
        'es2015'
      ]
    }).code
  }
  
  function walkComponents (rootPath, level = 0, pathName = '.', result = []) {
    const files = fs.readdirSync(rootPath)
    files.forEach(file => {
      const filePath = path.resolve(rootPath, file)
      const extName = path.extname(filePath)
      const isDirectory = fs.lstatSync(filePath).isDirectory()
      if (isDirectory) {
        walkComponents(filePath, level + 1, path.join(pathName, file), result)
      } else if (extName === '.vue') {
        const componentFile = fs.readFileSync(filePath).toString()
        const component = compiler.parseComponent(componentFile)
        const baseName = path.basename(filePath)
        if (component.template) {
          component.template.content = pug.render(component.template.content)
          if (component.template.attrs) {
            component.template.attrs.lang = false
          }
        }
        if (component.script) {
          component.script.content = transformAlias(component.script.content, level)
        }
        result.push({
          content: reverseToComponent(component),
          path: pathName,
          name: baseName
        })
      } else if (extName === '.js') {
        const content = fs.readFileSync(filePath).toString()
        const baseName = path.basename(filePath)
        result.push({
          content: transformES(transformAlias(content, level)),
          path: pathName,
          name: baseName
        })
      }
    })
    return result
  }
  
  function mkdirp (filepath) {
    var dirname = path.dirname(filepath)
  
    if (!fs.existsSync(dirname)) {
      mkdirp(dirname)
    }
    if (!fs.existsSync(filepath)) {
      fs.mkdirSync(filepath)
    }
  }
  
  const compiledFiles = walkComponents(rootPath)
  
  const componentDir = path.resolve(__dirname, '../components')
  rmdir(componentDir, () => {
    mkdirp(componentDir)
    compiledFiles.forEach(file => {
      const pathName = path.resolve(componentDir, file.path)
      mkdirp(pathName)
      const fd = fs.openSync(path.resolve(pathName, file.name), 'w')
      fs.writeSync(fd, file.content)
      fs.closeSync(fd)
    })
  })