-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
executable file
·460 lines (415 loc) · 15.9 KB
/
index.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#!/usr/bin/env node
const compiler = require('vue-template-compiler');
const prettier = require('prettier');
const csstree = require('css-tree');
const babelParser = require('@babel/parser');
const babelGenerator = require('@babel/generator').default;
const VISITOR_KEYS = require('@babel/types').VISITOR_KEYS;
const chalk = require('chalk');
const path = require('path');
const fs = require('fs');
// Funcs
const quoteBindedAttrs = true;
const useShortBinds = true;
function processTemplateExpression(expr) {
// to do: transform $event -> event
return expr; // for now
}
function processTemplateNode(node, output, depth, indent) {
switch (node.type) {
case 1: // Tag
if (node['for']) {
output.push(`${indent.repeat(depth)}{#each ${processTemplateExpression(node['for'])} as ${node.alias}${node.iterator1 ? ', ' + node.iterator1 : ''}}\n`);
depth++;
}
if (node['if']) {
output.push(`${indent.repeat(depth)}{#if ${processTemplateExpression(node['if'])}}\n`);
depth++;
} else
if (node['else']) {
output.push(`${indent.repeat(depth)}{:else}\n`);
depth++;
} else
if (node['elseif']) {
output.push(`${indent.repeat(depth)}{:else if ${processTemplateExpression(node['elseif'])}}\n`);
depth++;
}
output.push(`${indent.repeat(depth)}<${node.tag}`);
let attrs = [];
let vHtml = null;
for (let attr in node.attrsMap) {
if (['v-if', 'v-else', 'v-else-if', 'v-for'].includes(attr)) {
continue;
}
if (attr === 'v-html') {
vHtml = node.attrsMap[attr];
} else
if (attr[0] === ':' || attr.startsWith('v-bind:')) {
let key = attr.substr(attr[0] === ':' ? 1 : 7);
if (key === node.attrsMap[attr] && useShortBinds) {
attrs.push(`{${key}}`);
} else {
attrs.push(`${key}=${quoteBindedAttrs ? '"' : ''}{${
processTemplateExpression(node.attrsMap[attr])
}}${quoteBindedAttrs ? '"' : ''}`);
}
} else
if (attr[0] === '@' || attr.startsWith('v-on:')) {
let key = attr.substr(attr[0] === '@' ? 1 : 5);
attrs.push(`on:${key}=${quoteBindedAttrs ? '"' : ''}{${
processTemplateExpression(node.attrsMap[attr])
}}${quoteBindedAttrs ? '"' : ''}`);
} else {
attrs.push(`${attr}="${node.attrsMap[attr]}"`);
}
}
if (attrs.length) {
output.push(` ${attrs.join(`\n${indent.repeat(depth)}${' '.repeat(node.tag.length + 2)}`)}`);
}
if (!node.children.length && vHtml !== null) {
output.push('/>\n');
} else {
output.push('>\n');
if (vHtml !== null) {
output.push(`${indent.repeat(depth + 1)}{@html ${processTemplateExpression(vHtml)}}\n`);
if (node.children.length) {
console.log(chalk`{yellow Warning: found a node with children and} {green v-html} {yellow directive. Its children will be ignored.}`);
}
} else {
for (let child of node.children) {
processTemplateNode(child, output, depth + 1, indent);
}
}
output.push(`${indent.repeat(depth)}</${node.tag}>\n`);
}
if (node.ifConditions && node.ifConditions.length > 1) {
depth--;
for (let i = 1; i < node.ifConditions.length; i++) {
processTemplateNode(node.ifConditions[i].block, output, depth, indent);
}
depth++;
}
if (node['if']) {
depth--;
output.push(`${indent.repeat(depth)}{/if}\n`);
}
if (node['for']) {
depth--;
output.push(`${indent.repeat(depth)}{/each}\n`);
}
break;
case 2: // Dynamic text
output.push(indent.repeat(depth), node.tokens.map(token => {
if (typeof token === 'string') {
return token;
} else {
return `{${token['@binding']}}`;
}
}).join('').trim(), '\n');
break;
case 3: // Static text
const text = node.text.trim();
if (text.length) {
output.push(text);
}
break;
default:
console.log(chalk`{red Unknown node in template AST tree:}\n`, node);
process.exit();
}
}
function processScriptNode(node, toplevel) {
// TODO: convert $set to assignment
if (!node) {
return node;
}
if (node.type === 'MemberExpression' && node.object.type === 'ThisExpression') { // this.something
node = node.property;
}
if (node.type === 'CallExpression' &&
node.callee.type === 'MemberExpression' &&
node.callee.object.type === 'FunctionExpression' &&
node.callee.property.type === 'Identifier' &&
node.callee.property.name === 'bind' &&
node.arguments.length === 1 &&
node.arguments[0].type === 'ThisExpression') { // function() {}.bind(this)
// Remove bind and enter
node = node.callee.object;
} else
if (!toplevel && (node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression')) {
return node;
}
// Visit children
if (!(node.type in VISITOR_KEYS)) {
console.log(chalk`{yellow Warning: unknown node type } {greenBright ${node.type}}{yellow , unable to visit childs.}`);
return node;
}
for (let key of VISITOR_KEYS[node.type]) {
if (!(key in node)) {
continue;
}
const val = node[key];
if (Array.isArray(val)) {
for (let i = 0; i < val.length; i++) {
val[i] = processScriptNode(val[i]);
}
} else {
node[key] = processScriptNode(val);
}
}
return node;
}
function unwrapFunctionBody(node) {
if (node.body.type === 'BlockStatement') {
return node.body.body.map((child) => {
return babelGenerator(processScriptNode(child)).code
}).join('\n');
}
return babelGenerator(processScriptNode(prop.body)).code;
}
// Start
if (process.argv.length < 3) {
console.log(chalk`Please provide a file path to a SFC Vue component you want to convert.
{yellow Example:}
node ${process.argv[1]} Button.vue`);
process.exit();
}
const inputPath = process.argv[2];
let outputPath = process.argv[3] || ('./' + path.basename(inputPath, '.vue') + '.svelte');
let inputContent;
try {
var data = fs.readFileSync(inputPath, 'utf8');
inputContent = data.toString();
} catch(e) {
console.log(chalk`{red Error while reading file} {yellow ${inputPath}}{red :}\n`, e.stack);
process.exit();
}
const sfc = compiler.parseComponent(inputContent);
let output = [];
const HOOKS = {
mounted: 'onMount',
beforeUpdate: 'beforeUpdate',
updated: 'afterUpdate',
destroyed: 'onDestroy',
};
if (sfc.script) {
const script = babelParser.parse(sfc.script.content, {
sourceType: 'module',
});
const body = script.program.body;
let exportDef = null;
for (let i = 0; i < body.length; i++) {
if (body[i].type === 'ExportDefaultDeclaration') {
if (exportDef) {
console.log(chalk`{red Unable to parse script: multiple} {greenBright export default} {red declarations.}`);
process.exit();
}
exportDef = body[i];
body.splice(i, 1);
i--;
}
}
//console.log(script);
if (body.length > 0) {
output.push(`<script context="module">\n${babelGenerator(script).code}\n</script>\n\n`);
}
if (exportDef) {
const props = exportDef.declaration.properties;
let imports = [];
let params = [];
let data = [];
let computed = [];
let methods = [];
let beforeCreate = null;
let created = null;
for (let prop of props) {
if (prop.key.type !== 'Identifier') {
console.log(chalk`{yellow Warning: in default export,} {greenBright ${babelGenerator(prop.key).code}} {yellow is not an identifier, ignoring it.}`);
continue;
}
switch (prop.key.name) {
case 'name':
// Ignore for now
break;
case 'props':
if (prop.value.type === 'ObjectExpression') {
for (let param of prop.value.properties) {
if (param.value.type === 'Identifier') {
params.push(`export let ${babelGenerator(param.key).code}; // ${param.value.name}`);
} else
if (param.value.type === 'ObjectExpression') {
let props = {};
for (let prop of param.value.properties) {
props[prop.key.name] = prop.value;
}
let def = `export let ${babelGenerator(param.key).code}`;
if (props.default) {
def += ` = ${babelGenerator(props.default).code}`;
}
def += ';';
if (props.type) {
def += ` // ${babelGenerator(props.type).code}`;
}
if (props.required) {
def += props.type ? ', required' : ' // required';
}
if (props.validator) {
console.log(chalk`{yellow Warning: prop} {greenBright ${babelGenerator(param.key).code}} {yellow validator is ignored.}`);
}
params.push(def);
} else {
console.log(chalk`{yellow Warning: unexpected prop} {greenBright ${babelGenerator(param.key).code}} {yellow declaration:} {greenBright ${babelGenerator(param.value).code}}{yellow , ignoring it.}`);
continue;
}
}
} else
if (prop.value.type === 'ArrayExpression') {
for (let param of prop.value.elements) {
if (param.type === 'StringLiteral') {
params.push(`export let ${param.value};`);
} else {
console.log(chalk`{yellow Warning: unexpected prop} {greenBright ${babelGenerator(param).code}}{yellow , ignoring it.}`);
continue;
}
}
} else {
console.log(chalk`{yellow Warning: in default export,} {greenBright props} {yellow property is neither an array nor an object, ignoring it.}`);
continue;
}
break;
case 'beforeCreate':
if (prop.type !== 'ObjectMethod') {
console.log(chalk`{yellow Warning: in default export, } {greenBright ${prop.key.name}} is not a function, ignoring it.}`);
continue;
}
beforeCreate = unwrapFunctionBody(prop);
break;
case 'created':
if (prop.type !== 'ObjectMethod') {
console.log(chalk`{yellow Warning: in default export, } {greenBright ${prop.key.name}} is not a function, ignoring it.}`);
continue;
}
created = unwrapFunctionBody(prop);
break;
case 'mounted':
case 'beforeUpdate':
case 'updated':
case 'destroyed':
if (prop.type !== 'ObjectMethod') {
console.log(chalk`{yellow Warning: in default export, } {greenBright ${prop.key.name}} is not a function, ignoring it.}`);
continue;
}
prop.type = 'ArrowFunctionExpression';
imports.push(HOOKS[prop.key.name]);
methods.push(`${HOOKS[prop.key.name]}(${babelGenerator(processScriptNode(prop)).code});`);
break;
case 'data':
let obj;
if (prop.type === 'ObjectMethod') {
if (prop.body.type === 'BlockStatement' &&
prop.body.body.length === 1 &&
prop.body.body[0].type === 'ReturnStatement' &&
prop.body.body[0].argument.type === 'ObjectExpression') {
obj = prop.body.body[0].argument;
} else {
console.log(chalk`{yellow Warning: in default export,} {greenBright data} {yellow property contains too complex function, ignoring it.}`);
continue;
}
} else
if (prop.value.type === 'ObjectExpression') {
obj = prop.value;
} else {
console.log(chalk`{yellow Warning: in default export,} {greenBright data} {yellow property is not an object expression, ignoring it.}`);
continue;
}
for (let param of obj.properties) {
data.push(`let ${babelGenerator(param.key).code} = ${babelGenerator(processScriptNode(param.value)).code};`);
}
break;
// watch
case 'computed':
if (prop.value.type !== 'ObjectExpression') {
console.log(chalk`{yellow Warning: in default export,} {greenBright computed} {yellow property is not an object expression, ignoring it.}`);
continue;
}
// TODO: {get, set} format
// TODO: vm => vm.a * 2 format
for (let param of prop.value.properties) {
if (param.type === 'ObjectMethod') {
if (param.body.type === 'BlockStatement' &&
param.body.body.length === 1 &&
param.body.body[0].type === 'ReturnStatement') {
computed.push(`$: ${babelGenerator(param.key).code} = ${babelGenerator(processScriptNode(param.body.body[0].argument)).code};`);
} else {
param.type = 'ArrowFunctionExpression';
computed.push(`$: ${babelGenerator(param.key).code} = (${babelGenerator(processScriptNode(param)).code})();`);
}
} else {
console.log(chalk`{yellow Warning: unexpected computed} {greenBright ${babelGenerator(param.key).code}} {yellow declaration:} {greenBright ${babelGenerator(param).code}}{yellow , ignoring it.}`);
continue;
}
}
break;
case 'methods':
if (prop.value.type !== 'ObjectExpression') {
console.log(chalk`{yellow Warning: in default export,} {greenBright methods} {yellow property is not an object expression, ignoring it.}`);
continue;
}
for (let method of prop.value.properties) {
if (method.type !== 'ObjectMethod') {
console.log(chalk`{yellow Warning: in default export, method} {greenBright ${babelGenerator(method.key).code}} is not a function, ignoring it.}`);
continue;
}
method.type = 'FunctionDeclaration';
method.id = method.key;
methods.push(babelGenerator(processScriptNode(method, true)).code);
}
break;
default:
console.log(chalk`{yellow Warning: in default export,} {greenBright ${babelGenerator(prop.key).code}} {yellow is an unknown property, ignoring it.}`);
}
}
output.push(`<script>${imports.length ? '\nimport { ' + imports.join(', ') + ' } from \'svelte\';' : ''}
${params.concat(data, computed).join('\n')}
${beforeCreate ? '\n// beforeCreate:\n' + beforeCreate + '\n' : ''}${created ? '\n// created:\n' + created + '\n' : ''}
${methods.join('\n\n')}
</script>\n\n`);
}
}
for (let style of sfc.styles) {
let css = style.content;
if (!style.scoped) {
// TODO: move this to prettier, so the whitespace can be preserved
const ast = csstree.parse(css);
csstree.walk(ast, {
visit: 'Selector',
enter: function(node) {
//console.log(node, node.children.head);
const children = node.children;
node.children = new csstree.List();
node.children.appendData({
type: 'PseudoClassSelector',
loc: null,
name: 'global',
children,
});
}
});
css = csstree.generate(ast);
}
output.push('<style>\n', prettier.format(css, { parser: 'css' }), '</style>\n\n');
}
if (sfc.template) {
const template = compiler.compile(sfc.template.content);
if (template.errors.length) {
console.log(chalk`{red Error(s) while parsing template:}\n`);
for (let error of template.errors) {
console.log(error);
}
process.exit();
}
//console.log(template.ast);
processTemplateNode(template.ast, output, 0, ' '.repeat(2));
}
fs.writeFileSync(outputPath, output.join(''), 'utf8');
console.log(chalk`{greenBright Successfully converted} {yellow ${inputPath}} {greenBright to} {yellow ${outputPath}}{greenBright . Please note that some manual corrections are probably still required.}`);