-
Notifications
You must be signed in to change notification settings - Fork 0
/
liteqml.js
80 lines (66 loc) · 1.83 KB
/
liteqml.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
import * as IR from './ir.mjs';
import {Generator} from './es2015.mjs';
import {chainConnect, Binding} from './qmlcore.mjs'
import {dirname, polyfill, writeFile, basename} from './utils.mjs';
import {Getopt} from './getopt.mjs'
function run() {
let opt = new Getopt([
['', 'no-cache'],
['', 'env=ARG'],
['o', 'output-file=ARG'],
['h', 'help', 'help me']
]);
opt.bindHelp(`liteqml 2020-11-17
Usage:
${basename(process.argv[0])} liteqml.js [OPTIONS] entry.qml
Options:
--no-cache Force reparse
--env Set environment(browser|none)
-o, --output-file Output file
-h, --help help
`)
opt.parseSystem();
let inputFile = '';
if(!opt.argv.length) {
opt.showHelp();
return;
}
inputFile = opt.argv[0];
let outFile = inputFile.substr(0, inputFile.length-3)+'js';
if('o' in opt.options) {
outFile = opt.options.o;
}
let env = [];
if(opt.options.env === 'browser')
env = ['console', 'window', 'setTimeout', 'clearTimeout', 'document'];
else
env = ['console', 'setTimeout', 'clearTimeout'];
let path = dirname(inputFile);
let ir = new IR.ClassIR({
scriptPath: path,
noCache: opt.options['no-cache'],
env: env
});
ir.addImportPath('imports');
ir.load(inputFile);
let generator = new Generator(ir);
const code = generator.generate();
let sourceCode = `
${polyfill}
${Binding}
${chainConnect}
${code}
polyfill().then(() => {
if(!('window' in globalThis))
globalThis.window = {};
window.qml = {};
window.qml.rootObject = new ${ir.objName}();
});
`
console.info(`Outputing to ${outFile}`);
writeFile(outFile, sourceCode);
}
//globalThis.fs = fs;
polyfill().then(run).catch((err) => {
console.log(err+err.stack)
});