-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.js
More file actions
202 lines (175 loc) · 4.26 KB
/
build.js
File metadata and controls
202 lines (175 loc) · 4.26 KB
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
#!/bin/env node
/* eslint-disable no-undef */
const flags = process.argv[2] || '';
import * as fs from 'fs';
import {exec as child_process_exec} from 'child_process';
import {hostname} from 'os';
const {version} = JSON.parse(
fs.readFileSync('./package.json', 'utf-8')
);
const exec = cmd => (
new Promise(resolve =>
child_process_exec(
cmd,
(...args) => resolve(args)
)
)
)
function flags_set(debug, verbose, legacy, rjs, extended, noeval) {
fs.writeFileSync(
'./src/flags.js',
`export const DEBUG = ${debug};
export const VERBOSE = ${verbose};
export const LEGACY = ${legacy};
export const RJS = ${rjs};
export const EXTENDED = ${extended};
export const NOEVAL = ${noeval};
`,
'utf8'
);
}
async function build(prod, legacy, rjs, extended, noeval) {
flags_set(!prod, false, legacy, rjs, extended, noeval);
const filename = `lui${
extended ? 'x' : ''
}${
rjs ? '.r' : ''
}${
noeval ? '.noeval' : ''
}${
legacy ? '.legacy' : ''
}${
prod ? '' : '.dev'
}.js`;
const file = './dist/' + filename;
const stderr = (await exec(
'google-closure-compiler --' +
[
'assume_function_wrapper',
'charset UTF-8',
'compilation_level ADVANCED',
'dependency_mode PRUNE',
'entry_point ./src/luirt.js',
'js ./src',
'js_output_file ' + file,
'language_in ECMASCRIPT_NEXT',
`language_out ECMASCRIPT${
legacy ? '3'
: rjs ? '5_STRICT'
: '_2020'
}`,
'strict_mode_input',
'emit_use_strict',
'module_resolution WEBPACK',
'rewrite_polyfills false',
'use_types_for_optimization',
'warning_level VERBOSE'
]
.join(' --')
))[2].trim();
if (stderr) {
console.log(stderr);
process.exit(1);
}
const wrap_fn = legacy || rjs;
let code_js = (
fs.readFileSync(file, 'ascii')
.trim()
);
if (!code_js.startsWith("'use strict';")) {
throw new Error('closure compiler output signature mismatch');
}
// cut off use strict
code_js = code_js.slice(13);
// extract closure compiler helpers added to the beginning
let cc_helpers = '';
const cc_helpers_length = code_js.indexOf('/*');
if (cc_helpers_length > 0) {
cc_helpers = code_js.slice(0, cc_helpers_length);
code_js = code_js.slice(cc_helpers_length);
}
code_js = (
'"use strict";' +
code_js
// insert version into file header
.replace('lui.js web framework', 'lui.js web framework ' + version)
.replace('*/\n',
'*/\n' +
(wrap_fn ? '(function()' : '') +
'{' +
cc_helpers
)
// remove last ;
.slice(0, -1) +
'}' +
(wrap_fn ? ')()' : '') +
'\n'
);
fs.writeFileSync(file, code_js, 'ascii');
let size_before = parseInt(
(
await exec(`git cat-file -s origin/dist:${filename}`)
)[1]
);
if (isNaN(size_before)) size_before = 0;
let diff = String(code_js.length - size_before);
if (diff === '0') diff = '';
else {
if (!diff.startsWith('-')) diff = ' +' + diff;
else diff = ' ' + diff;
}
console.log(`${
filename.padEnd(14, ' ')
} ${
String(code_js.length).padStart(5, ' ')
}${
diff
}`);
}
(async () => {
if(
!(
await exec('google-closure-compiler --version')
)[1].includes('Version: v202')
) {
console.log('newer closure compiler version required!');
return;
}
await exec('mkdir -p ./dist');
//await exec('rm ./dist/*.old.js;rename.ul .js .old.js ./dist/*.js');
await exec('rm ./dist/*');
console.log(`build ${version}...`);
//await build(true, false, false, false);
for (const extended of [false, true]) {
for (const prod of [false, true])
for (const rjs of [false, true])
await build(prod, false, rjs, extended, false);
// legacy
await build(true, true, false, extended, false);
// noeval
await build(true, false, false, extended, true);
await build(true, false, true, extended, true);
}
if (
flags.includes('d') &&
hostname() === 'l3p3-rk5'
) {
try {
console.log('compress...');
console.log((await exec('parallel zopfli --i10000 dist/{/} ::: dist/lui*'))[2]);
console.log(`compression: ${
fs.statSync('./dist/lui.js').size
} -> ${
fs.statSync('./dist/lui.js.gz').size
}`);
}
catch (error) {}
console.log('deploy...');
const version_m = version.split('.')[0];
await exec(`mkdir -p /media/Archiv/Anbieter/www/shr/lui/${version_m};cp ./dist/lui* /media/Archiv/Anbieter/www/shr/lui/${version_m}/`);
}
})()
.catch(console.log)
.finally(() => {
flags_set(true, false, false, false, true, false);
});