-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkastro.js
252 lines (232 loc) Β· 9.05 KB
/
kastro.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
import { plugin } from "bun";
import { cp, readFile } from "node:fs/promises";
import { createServer } from "vite";
import { transpileCss } from "../kdjs/cssParser";
import { transpileJsx } from "../kdjs/jsxParser";
import { Blue, Clear, parseArgs } from "../util/cli";
import { combine, getDir, getExt } from "../util/paths";
import compiler from "./compiler/compiler";
import crates from "./compiler/crates";
import { ttfTarget, woff2Target } from "./compiler/font";
import {
inlineSvgTarget,
jsxSvgTarget,
pngTarget,
svgTarget,
webpTarget
} from "./compiler/image";
import { pageTarget } from "./compiler/page";
import { getDomIdMapper, getGlobals, setDomIdMapper } from "./compiler/pageGlobals";
import { scriptTarget } from "./compiler/script";
import { styleSheetTarget } from "./compiler/styleSheet";
import { registerTargetFunction } from "./compiler/targetRegistry";
import { CompressedMimes } from "./workers/mimes";
const setupKastro = (buildMode) => {
setDomIdMapper(buildMode);
registerTargetFunction(".html", pageTarget);
registerTargetFunction(".inl.svg", inlineSvgTarget);
registerTargetFunction(".png", pngTarget);
registerTargetFunction(".svg", svgTarget);
registerTargetFunction(".jsx.svg", jsxSvgTarget);
registerTargetFunction(".css", styleSheetTarget);
registerTargetFunction(".webp", webpTarget);
registerTargetFunction(".ttf", ttfTarget);
registerTargetFunction(".woff2", woff2Target);
registerTargetFunction(".js", scriptTarget);
registerTargetFunction(".jsx", scriptTarget);
plugin({
name: "kastro-js",
async setup(build) {
const cwdLen = process.cwd().length + 1;
const path = (args) => args.path.slice(cwdLen);
build.onLoad({ filter: /\.(svg|png|webp)$/ }, (args) => {
const code = `import { Image } from "@kimlikdao/lib/kastro/image";\n` +
`export default (props) => Image({...props, src: "${path(args)}" });`;
return { contents: code, loader: "js" };
});
build.onLoad({ filter: /\.css$/ }, (args) => {
const code = `import { makeStyleSheet } from "@kimlikdao/lib/kastro/stylesheet";\n` +
`import { readFileSync } from "node:fs";\n` +
`export default makeStyleSheet("${path(args)}", readFileSync("${path(args)}", "utf-8"));`;
return { contents: code, loader: "js" };
});
build.onLoad({ filter: /\.ttf$/ }, (args) => {
const code = `import { TtfFont } from "@kimlikdao/lib/kastro/font";\n` +
`export default (props) => TtfFont({...props, href: "${path(args)}" });`;
return { contents: code, loader: "js" };
});
build.onResolve({ filter: /./, namespace: "kastro" }, ({ path, importer }) => ({
path: path.startsWith(".") ? "/" + combine(getDir(importer.replace("kastro:", "")), path) : path,
namespace: "kastro"
}));
build.onLoad({ filter: /.svg.jsx$/, namespace: "kastro" }, (args) => {
const code = `import { SvgJsxImage } from "@kimlikdao/lib/kastro/image";\n` +
`export default (props) => SvgJsxImage({...props, src: "${path(args)}" });`;
return { contents: code, loader: "js" };
});
build.onLoad({ filter: /.jsx$/, namespace: "kastro" }, (args) => {
const code = `import { Script } from "@kimlikdao/lib/kastro/script";\n` +
`export default (props) => Script({...props, src: "${path(args)}" });`;
return { contents: code, loader: "js" };
});
},
});
class SinkElement {
constructor(name) {
this.name = name;
}
get children() { return [this, this, this, this, this]; }
get firstElementChild() { return this; }
get nextSibling() { return this; }
get parentElement() { return this; }
get parentNode() { return this; }
get previousElementSibling() { return this; }
get previousSibling() { return this; }
cloneNode(deep) { return this; }
replaceChild(newChild, oldChild) { return this; }
appendChild() { return this; }
closest() { return this; }
get classList() {
return {
add: () => { },
remove: () => { },
contains: () => false,
toggle: () => false
};
}
get style() {
return new Proxy({}, {
get: () => "",
set: () => true
});
}
}
const window = {
ethereum: {
isRabby: false
},
}
globalThis.GEN = true;
globalThis.window = window;
globalThis.document = {
cookie: "",
getElementById: (id) => {
console.warn(`use typed dom.elem() methods instead! ${id}`);
return new SinkElement("");
},
};
globalThis.document.createElement = (name) => new SinkElement(name);
}
const serveCrate = async (crateName, buildMode) => {
setupKastro(buildMode);
const crate = await import(crateName);
/** @const {!Object<string, PageTarget>} */
const map = crates.getPageTargets(crate, buildMode);
/** @const {!DomIdMapper} */
const domIdMapper = getDomIdMapper();
let currentPageProps;
let currentPageGlobalsPattern;
createServer({
appType: "mpa",
publicDir: buildMode == compiler.BuildMode.Dev ? "" : "build/bundle",
plugins: [{
name: "kastro-js",
enforce: "pre",
configureServer(server) {
server.middlewares.use(async (req, res, next) => {
if (req.originalUrl in map) {
res.setHeader("content-type", "text/html;charset=utf-8");
server.moduleGraph.invalidateAll();
currentPageProps = map[req.originalUrl];
compiler.forceBuildTarget(currentPageProps.targetName, currentPageProps)
.then((content) => {
const globals = getGlobals();
globals.GEN = false;
currentPageGlobalsPattern = new RegExp(Object.keys(globals)
.map((key) => `/\\*\\* @define \\{[^}]*\\} \\*/\\s*const ${key} =.*?;`)
.join("|"), "g");
res.end(content)
});
} else next();
})
},
resolveId(source, importer) {
if (source.endsWith(".css"))
return importer.slice(0, importer.lastIndexOf("/") + 1) + source + ".js";
},
load(id) {
if (id.endsWith(".css.js"))
return readFile(id.slice(0, -3), "utf8")
.then((css) => transpileCss(id.slice(0, -3), css, domIdMapper));
},
transform(code, id) {
if (id.endsWith(".jsx"))
code = transpileJsx(code);
const globals = getGlobals();
return code.replace(currentPageGlobalsPattern, (match) => {
const constIdx = match.indexOf("const");
const varName = match.slice(match.indexOf("\nconst") + 6, match.indexOf("=", constIdx)).trim();
return `\nconst ${varName} = ${JSON.stringify(globals[varName])};`
});
}
}]
}).then((vite) => vite.listen(8787))
.then(console.log("Dev server running at http://localhost:8787"));
}
/**
* @param {string} crateName
* @param {compiler.BuildMode} buildMode
* @param {?LangCode} lang
*/
const buildCrate = async (crateName, buildMode, lang) => {
const crate = await import(crateName);
if (!lang) {
// If no language is specified, spawn a bun instance for each language.
// For now we do this sequentially since each page saturates the CPUs.
const langs = crates.getLanguages(crate);
for (const lang of langs) {
console.info(`${Blue}[Building]${Clear} MPA ${lang}`);
await Bun.spawn(["bun", "lib/kastro/kastro.js", "build", "--lang", lang], {
env: { NODE_ENV: "production" },
stdio: ["inherit", "inherit", "inherit"]
}).exited;
}
if (crate.Aliases) {
const tasks = [];
for (const alias in crate.Aliases) {
const exts = CompressedMimes[getExt(alias)] ? [""] : ["", ".br", ".gz"];
for (const ext of exts)
tasks.push(cp(`build/bundle/${crate.Aliases[alias]}${ext}`, `build/bundle/${alias}${ext}`));
}
await Promise.all(tasks);
}
return;
}
// If a language is specified, build each page for that language.
setupKastro(buildMode);
/** @const {!Object<string, PageTarget>} */
const map = crates.getPageTargets(crate, buildMode, lang);
for (const page of Object.values(map)) {
console.info(`${Blue}[Building]${Clear} Page ${page.bundleName}`);
await compiler.bundleTarget(page.targetName, page);
}
}
/**
* @param {string} crateName
* @param {string} target
*/
const deployCrate = (crateName, target) => Promise.all([
buildCrate(crateName, compiler.BuildMode.Compiled),
import(`${process.cwd()}/.secrets.js`),
import(`./${target}/crate.js`)
])
.then(([_, secrets, crateDeployer]) => crateDeployer.deploy(crateName, secrets, compiler.getNamedAssets()));
const args = parseArgs(process.argv.slice(2), "command");
/** @const {string} */
const crateName = (Array.isArray(args["command"]) ? args["command"][1] : "") + "/crate.js";
if (args["command"] == "serve")
serveCrate(crateName, args["compiled"] ? compiler.BuildMode.Compiled : compiler.BuildMode.Dev);
else if (args["command"] == "build")
buildCrate(crateName, compiler.BuildMode.Release, args["lang"]);
else if (args["command"] == "deploy")
deployCrate(crateName, args["target"] || "cloudflare");