Serve es5 bundle if exists
[k0tyk.git] / bundle.js
blob22c4a94ef2d0b0946af81f6cc868507972e5254d
1 #!/usr/bin/qjs -m
2 /* Build a page with its dependencies for ES5 browsers.
3  * ./bundle.js todov.js; cat .todov.js
4  * @license 0BSD 2020 k0tyk <k0tyk@pm.me> */
5 import * as os from "os";
6 import * as std from "std";
7 os.exec([
8   "tsc",
9   "--allowJs",
10   "--lib",
11   "DOM,ESNext",
12   "-m",
13   "system",
14   "--outFile",
15   `.${scriptArgs[1]}`,
16   "--target",
17   "ES5",
18   ".crank/index.js",
19   scriptArgs[1],
20 ]);
21 var out = std.loadFile(`.${scriptArgs[1]}`);
22 std.open(`.${scriptArgs[1]}`, "w").puts(`function System(x) {
23   if (!/^\\.\\//.test(x)) x = "./" + x;
24   if (!/\\.js$/.test(x)) x = x + ".js";
25   return x;
27 System.import = function (x) {
28   var s = System[(x = System(x))];
29   if (s.c) return s.v;
30   s.d.map(function (y, i) {
31     return s.m.setters[i](System.import(y));
32   });
33   return s.m.execute(), s.c++, s.v;
35 System.register = function (x, d, f) {
36   var s = { c: 0, d: d.map(System), m: {}, v: {} };
37   s.m = f(function (k, v) {
38     s.v[k] = v;
39   });
40   System[System(x)] = s;
42 ${out.replace(/^#!.*/, "")}`);