-
Notifications
You must be signed in to change notification settings - Fork 0
/
vue.to.js
executable file
·119 lines (106 loc) · 3.21 KB
/
vue.to.js
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
#!/usr/bin/env node
/*
* Vue template compiler
*
* This file is part of iDom-fe.
*
* Copyright (c) 2019, 2023 Aleksander Mazur
*
* iDom-fe is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* iDom-fe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with iDom-fe. If not, see <https://www.gnu.org/licenses/>.
*/
import fs from 'fs'
import { glob } from 'glob'
import compiler from 'vue-template-compiler'
import transpile from 'vue-template-es2015-compiler'
const SRC = process.argv[2]
const OUT = process.argv[3]
if (!SRC || !OUT) {
console.error('Usage: ' + process.argv[1].split('/').pop() + ' <srcdir> <outdir>')
process.exit(2)
}
function print_array(array, template) {
for (let i = 0, len = array.length; i < len; i++) {
console.error(array[i].msg)
const end = array[i].end || array[i].start + 1
console.error(compiler.generateCodeFrame(template, array[i].start, end))
}
}
function to_function(code) {
return 'function() { ' + code + ' }'
}
/*
function beautify(code) {
const beautiful = UglifyJS.minify(code, {
output: {
beautify: true,
semicolons: false,
},
})
if (beautiful.error) {
console.warn(beautiful.error)
return code
}
return beautiful.code
}
*/
glob(SRC + '/**/*.vue')
.then((files) => {
for (let i = 0, len = files.length; i < len; i++) {
let parts = files[i].split('/')
if (parts.shift() != SRC) {
console.error(files[i] + ': unexpected file matched')
process.exitCode = 1
return
}
const output = OUT + '/' + parts.join('/') + '.js'
const origname = parts.pop()
console.log(files[i] + ' -> ' + output)
const template = fs.readFileSync(files[i], 'utf8')
const compiled = compiler.compile(template, {
outputSourceRange: true,
whitespace: 'condense',
})
if (compiled.staticRenderFns && !compiled.staticRenderFns.length)
delete compiled.staticRenderFns
if (compiled.tips) {
print_array(compiled.tips, template)
}
if (compiled.errors && compiled.errors.length) {
print_array(compiled.errors, template)
process.exitCode = 1
continue
}
let module = ''
module += 'const template = {\n'
if (compiled.render)
module += '\trender: ' + to_function(compiled.render) + ',\n'
if (compiled.staticRenderFns) {
module += '\tstaticRenderFns: [\n'
for (let j = 0, jlen = compiled.staticRenderFns.length; j < jlen; j++)
module += '\t\t' + to_function(compiled.staticRenderFns[j]) + ',\n'
module += '\t],\n'
}
module += '}\n'
module = transpile(module)
//module = beautify(module)
module = '// This file is automatically generated from "' + origname + '", please do not edit\n\n' + module + '\nexport default template\n'
parts = output.split('/')
parts.pop()
if (parts.length)
fs.mkdirSync(parts.join('/'), {
recursive: true,
})
fs.writeFileSync(output, module, 'utf8')
}
})