forked from domvm/domvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
294 lines (244 loc) · 7.43 KB
/
build.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
const rollup = require('rollup').rollup;
const replace = require('rollup-plugin-replace');
const buble = require('rollup-plugin-buble');
const fs = require('fs');
const exec = require('child_process').exec;
const execSync = require('child_process').execSync;
const zlib = require('zlib');
const Terser = require('terser');
const AVAIL_FEATS = [
"FLUENT_API",
"LAZY_LIST",
"PARSE_TAG",
"STATIC_CLASS",
"AUTO_KEY",
"SPL_ATTRS",
"PROP_ATTRS",
"EVENT_DELEG",
"ONEVENT",
"DIFF_CMP",
"FOREIGN_ELEMS",
"RAF_REDRAW",
"AUTO_PX",
"EMIT",
"STREAM",
/*
"HOOKS", // deep_notify_remove, etc
"EVENT_ATTRS", // EVENT_PARAMS
"REFS",
"PREPROC_FLATTEN",
"PREPROC_MERGE_TEXT",
"PREPROC_REMOVE_FALSEY",
"DIFF_CMP", // else diff just returns true/false
"SPREAD_BODY",
"PARTIAL_KEYS"
// exhaustive donor search
// styles, hooks, attrs, events
*/
];
function getBuilds(name) {
const pico = {
build: "pico",
contents: "fluent api<br>dom recycling<br>lifecycle hooks<br>parameterized handlers<br>sub-views<br>element injection<br>innerHTML<br>vnode refs<br>css objects<br>svg<br>diff<br>lazy list<br>",
descr: "view core<br><br>**This build is unstable by design; features that get decoupled<br>can move to nano+ builds at any commit!**",
feats: ["FLUENT_API","LAZY_LIST"],
};
const nano = {
build: "nano",
contents: "- fluent api<br>+ special attrs<br>+ prop attrs<br>+ tag parsing<br>+ vnode patching<br>+ class merging<br>+ auto keying<br>+ global onevent<br>+ object/array diff<br>+ foreign elem skipping<br>+ raf-debounced redraw",
descr: "`\"input[type=checkbox].some-class\"`<br>`vnode.patch({class: ..., style...})`",
feats: [
"LAZY_LIST",
"PARSE_TAG",
"STATIC_CLASS",
"AUTO_KEY",
"SPL_ATTRS",
"PROP_ATTRS",
"ONEVENT",
"DIFF_CMP",
"FOREIGN_ELEMS",
"RAF_REDRAW",
],
};
const micro = {
build: "micro",
contents: "+ `emit`<br> + `body`<br> + `autoPx`<br> + `defineElementSpread`<br> + `defineSvgElementSpread`<br>",
descr: "`vm.emit('myNotif', arg1, arg2...)`<br>`vm.body()`<br>`{style: {width: 20}}`",
feats: nano.feats.concat("AUTO_PX","EMIT"),
};
const mini = {
build: "mini",
contents: "+ `stream`<br>",
descr: "view reactivity",
feats: micro.feats.concat("STREAM"),
};
const client = {
build: "client",
contents: "`mini`<br> + `attach`<br>",
descr: "SSR hydration",
feats: mini.feats,
};
const server = {
build: "server",
contents: "`mini`<br> + `html`<br>",
descr: "SSR rendering",
feats: mini.feats,
};
const full = {
build: "full",
contents: "`mini`<br> + `attach`<br> + `html`<br>",
descr: "all the bells and whistles",
feats: mini.feats,
};
const dev = {
build: "dev",
contents: "`full`<br> + warnings<br>",
descr: "use this build for development; it contains detection of some<br>anti-patterns that may cause slowness, confusion, errors or<br>undesirable behavior",
feats: mini.feats,
};
return [pico, nano, micro, mini, client, server, full, dev].filter(b => name != null ? b.build === name : true);
}
var args = process.argv.slice(2);
var buildName = args[0];
builds = buildName == null ? getBuilds().map(b => b.build) : [buildName];
builds.forEach(b => compile(b));
function getCurBranch() {
var branches = execSync("git branch", {encoding: 'utf8'});
return branches.match(/^\*.*$/gm)[0].substr(2);
}
function compile(buildName) {
var start = +new Date;
var buildFile = './src/builds/' + buildName + '.js';
var buildCfg = getBuilds(buildName)[0];
var feats = buildCfg.feats;
var repls = {
_DEVMODE: buildName === "dev",
};
AVAIL_FEATS.forEach(function(f) {
repls["FEAT_" + f] = feats.indexOf(f) != -1;
});
rollup({
input: buildFile,
onwarn: (warning, warn) => {
if (warning.code === 'CIRCULAR_DEPENDENCY')
return;
warn(warning)
},
plugins: [
replace(repls),
buble(),
],
})
.then(function(bundle) {
var pkg = JSON.parse(fs.readFileSync('./package.json', 'utf8'));
var branch = getCurBranch();
var ver = branch.indexOf("-dev") != -1 ? branch : "v" + pkg.version;
var preserve = "https://github.com/domvm/domvm (" + ver + ", " + buildName + " build)";
var banner = [
"/**",
"* Copyright (c) " + new Date().getFullYear() + ", Leon Sorokin",
"* All rights reserved. (MIT Licensed)",
"*",
"* domvm.js (DOM ViewModel)",
"* A thin, fast, dependency-free vdom view layer",
"* @preserve " + preserve,
"*/",
"",
].join("\n");
bundle.write({
banner: banner,
name: "domvm",
format: "es", // output format - 'amd', 'cjs', 'es', 'iife', 'umd'
esModule: false,
// sourcemap: true,
file: "./dist/" + buildName + "/domvm." + buildName + ".es.js"
});
bundle.write({
banner: banner,
name: "domvm",
format: "cjs", // output format - 'amd', 'cjs', 'es', 'iife', 'umd'
esModule: false,
sourcemap: buildName == 'full',
file: "./dist/" + buildName + "/domvm." + buildName + ".cjs.js"
});
bundle.write({
banner: banner,
name: "domvm",
format: "iife", // output format - 'amd', 'cjs', 'es', 'iife', 'umd'
esModule: false,
sourcemap: buildName == 'full',
file: "./dist/" + buildName + "/domvm." + buildName + ".iife.js"
}).then(b => {
console.log((+new Date - start) + "ms: Rollup + Buble done (build: " + buildName + ")");
squish(buildName, preserve, start);
});
}).catch(function(err) {
console.log(err);
})
}
function squish(buildName, preserve, start) {
var src = "dist/" + buildName + "/domvm." + buildName + ".iife.js";
var dst = "dist/" + buildName + "/domvm." + buildName + ".iife.min.js";
const opts = {
compress: {
inline: 0,
passes: 2,
keep_fargs: false,
pure_getters: true,
unsafe: true,
unsafe_comps: true,
unsafe_math: true,
unsafe_undefined: true,
},
output: {
comments: /^!/,
}
};
const compiled = Terser.minify(fs.readFileSync(src, 'utf8'), opts).code;
fs.writeFileSync(dst, "// " + preserve + "\n" + compiled, 'utf8');
buildDistTable();
console.log((+new Date - start) + "ms: Terser done (build: " + buildName + ")");
}
function padRight(str, padStr, len) {
return str + padStr.repeat(len - str.length);
}
// builds markdown table
function buildDistTable() {
var builds = getBuilds();
var branch = getCurBranch();
var colWidths = {
build: 0,
"min / gz": 0,
contents: 0,
descr: 0,
};
var appendix = [];
builds.forEach(function(build, i) {
var buildName = build.build;
var path = "dist/" + buildName + "/domvm." + buildName + ".iife.min.js";
appendix.push("["+(i+1)+"]: https://github.com/domvm/domvm/blob/" + branch + "/" + path);
var minified = fs.readFileSync("./" + path, 'utf8');
var gzipped = zlib.gzipSync(minified, {level: 6});
var minLen = (minified.length / 1024).toFixed(1);
var gzLen = (gzipped.length / 1024).toFixed(1);
build["min / gz"] = minLen + "k / " + gzLen + "k";
build.build = "[" + buildName + "][" + (i+1) + "]";
for (var colName in colWidths)
colWidths[colName] = Math.max(colWidths[colName], build[colName].length);
});
var table = '';
for (var colName in colWidths)
table += "| " + padRight(colName, " ", colWidths[colName] + 1);
table += "|\n";
for (var colName in colWidths)
table += "| " + padRight("", "-", colWidths[colName]) + " ";
table += "|\n";
builds.forEach(function(build, i) {
for (var colName in colWidths)
table += "| " + padRight(build[colName], " ", colWidths[colName] + 1);
table += "|\n";
});
table += "\n" + appendix.join("\n");
fs.writeFileSync("./dist/README.md", table, 'utf8');
}
module.exports.compile = compile;