Skip to content

Commit 5378842

Browse files
committed
bundling update
1 parent 9faa0ec commit 5378842

File tree

7 files changed

+111
-41
lines changed

7 files changed

+111
-41
lines changed

build.ts

Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,95 @@ import child_process from "child_process";
66

77
const __filename = fileURLToPath(import.meta.url);
88
const __dirname = path.dirname(__filename);
9-
async function moveFiles(curPath: string, newPath: string) {
9+
async function moveFiles(curPath: string, newPath: string, first = true) {
1010
async function processFile(file: string) {
1111
const Prom: Promise<unknown>[] = [];
1212
if ((await fs.stat(path.join(curPath, file))).isDirectory()) {
1313
await fs.mkdir(path.join(newPath, file));
14-
Prom.push(moveFiles(path.join(curPath, file), path.join(newPath, file)));
14+
Prom.push(moveFiles(path.join(curPath, file), path.join(newPath, file), false));
1515
} else {
1616
if (!file.endsWith(".ts")) {
17+
if (file.endsWith(".html")) {
18+
for (const [, match] of (await fs.readFile(path.join(curPath, file)))
19+
.toString()
20+
.matchAll(/<script.*?src="(.*?)"/gm)) {
21+
const sPathTemp = path.format({
22+
root: path.join(__dirname, "src", "webpage"),
23+
base: match,
24+
});
25+
const temp2 = path.parse(sPathTemp);
26+
//@ts-ignore
27+
delete temp2.base;
28+
temp2.ext = "ts";
29+
const sPath = path.format(temp2);
30+
let mod = await swc.bundle({
31+
entry: sPath,
32+
target: "browser",
33+
output: {
34+
path: path.parse(sPath).dir,
35+
name: path.parse(sPath).base,
36+
},
37+
module: {
38+
minify: true,
39+
sourceMaps: true,
40+
isModule: true,
41+
jsc: {
42+
minify: {
43+
mangle: !process.argv.includes("watch"),
44+
},
45+
},
46+
},
47+
options: {
48+
minify: !process.argv.includes("watch"),
49+
jsc: {
50+
minify: {
51+
mangle: !process.argv.includes("watch"),
52+
},
53+
},
54+
},
55+
});
56+
const code = mod[path.parse(sPath).base] || mod;
57+
const newfileDir = path.join(newPath, path.parse(sPath).name);
58+
if (code.map) console.log("map");
59+
await Promise.all([
60+
fs.writeFile(
61+
newfileDir + ".js",
62+
code.code + "\n" + `//# sourceMappingURL= ${path.parse(sPath).name}.js.map`,
63+
),
64+
code.map ? fs.writeFile(newfileDir + ".js.map", code.map as string) : null,
65+
]);
66+
}
67+
}
1768
await fs.copyFile(path.join(curPath, file), path.join(newPath, file));
1869
} else {
1970
const plainname = file.split(".ts")[0];
2071
const newfileDir = path.join(newPath, plainname);
21-
const mod = await swc.transformFile(path.join(curPath, file), {
22-
minify: true,
23-
sourceMaps: true,
24-
isModule: true,
25-
jsc: {
26-
minify: {
27-
mangle: !process.argv.includes("watch"),
28-
},
29-
},
30-
});
31-
await Promise.all([
32-
fs.writeFile(
33-
newfileDir + ".js",
34-
mod.code + "\n" + `//# sourceMappingURL= ${plainname}.js.map`,
35-
),
36-
fs.writeFile(newfileDir + ".js.map", mod.map as string),
37-
]);
72+
let mod = first
73+
? await swc.transformFile(path.join(curPath, file), {
74+
minify: true,
75+
sourceMaps: true,
76+
isModule: true,
77+
jsc: {
78+
minify: {
79+
mangle: !process.argv.includes("watch"),
80+
},
81+
},
82+
})
83+
: false;
84+
if (mod) {
85+
await Promise.all([
86+
fs.writeFile(
87+
newfileDir + ".js",
88+
mod.code + "\n" + `//# sourceMappingURL= ${plainname}.js.map`,
89+
),
90+
mod.map ? fs.writeFile(newfileDir + ".js.map", mod.map as string) : null,
91+
]);
92+
}
3893
}
3994
}
4095
await Promise.all(Prom);
4196
}
42-
await Promise.all((await fs.readdir(curPath)).map(processFile));
97+
await Promise.all((await fs.readdir(curPath)).map((_) => processFile(_)));
4398
}
4499
async function build() {
45100
console.time("build");

src/webpage/guild.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {webhookMenu} from "./webhooks.js";
2424
import {createImg} from "./utils/utils.js";
2525
import {Sticker} from "./sticker.js";
2626
import {ProgessiveDecodeJSON} from "./utils/progessiveLoad.js";
27+
import {MarkDown} from "./markdown.js";
2728
export async function makeInviteMenu(inviteMenu: Options, guild: Guild, url: string) {
2829
const invDiv = document.createElement("div");
2930
const bansp = ProgessiveDecodeJSON<invitejson[]>(url, {
@@ -47,11 +48,14 @@ export async function makeInviteMenu(inviteMenu: Options, guild: Guild, url: str
4748
const inviter = new User(invite.inviter, guild.localuser);
4849

4950
opt.addMDText(
50-
window.location.origin +
51-
"/invite/" +
52-
invite.code +
53-
"?" +
54-
new URLSearchParams([["instance", guild.info.wellknown]]),
51+
new MarkDown(
52+
window.location.origin +
53+
"/invite/" +
54+
invite.code +
55+
"?" +
56+
new URLSearchParams([["instance", guild.info.wellknown]]),
57+
undefined,
58+
),
5559
);
5660

5761
opt.addText(I18n.invite.used(invite.uses + ""));
@@ -755,8 +759,11 @@ class Guild extends SnowFlake {
755759
);
756760
const search = new URLSearchParams([["instance", this.info.wellknown]]);
757761
form.addMDText(
758-
I18n.guild.templateURL(
759-
window.location.origin + "/template/" + temp.code + "?" + search,
762+
new MarkDown(
763+
I18n.guild.templateURL(
764+
window.location.origin + "/template/" + temp.code + "?" + search,
765+
),
766+
undefined,
760767
),
761768
);
762769

src/webpage/localuser.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2279,7 +2279,12 @@ class Localuser {
22792279
img.width = 128;
22802280
img.height = 128;
22812281
const ver = await (await fetch("/getupdates")).text();
2282-
jankInfo.addMDText(I18n.clientDesc(ver, window.location.origin, this.rights.allow + ""));
2282+
jankInfo.addMDText(
2283+
new MarkDown(
2284+
I18n.clientDesc(ver, window.location.origin, this.rights.allow + ""),
2285+
undefined,
2286+
),
2287+
);
22832288
jankInfo.addButtonInput("", I18n.instInfo(), () => {
22842289
this.instanceStats();
22852290
});

src/webpage/settings.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import {Emoji} from "./emoji.js";
99
import {I18n} from "./i18n.js";
1010
import {Localuser} from "./localuser.js";
11+
import {MarkDown} from "./markdown.js";
1112

1213
interface OptionsElement<x> {
1314
//
@@ -188,13 +189,12 @@ class DateInput extends TextInput {
188189
return div;
189190
}
190191
}
191-
const mdProm = import("./markdown.js");
192192
class SettingsMDText implements OptionsElement<void> {
193193
readonly onSubmit!: (str: string) => void;
194194
value!: void;
195-
text: string;
195+
text: MarkDown;
196196
elm!: WeakRef<HTMLSpanElement>;
197-
constructor(text: string) {
197+
constructor(text: MarkDown) {
198198
this.text = text;
199199
}
200200
generateHTML(): HTMLSpanElement {
@@ -203,15 +203,14 @@ class SettingsMDText implements OptionsElement<void> {
203203
this.setText(this.text);
204204
return span;
205205
}
206-
setText(text: string) {
206+
setText(text: MarkDown) {
207207
this.text = text;
208208
if (this.elm) {
209209
const span = this.elm.deref();
210210
if (span) {
211211
span.innerHTML = "";
212-
mdProm.then((e) => {
213-
span.append(new e.MarkDown(text, undefined).makeHTML());
214-
});
212+
213+
span.append(text.makeHTML());
215214
}
216215
}
217216
}
@@ -1209,7 +1208,7 @@ class Options implements OptionsElement<void> {
12091208
this.generate(text);
12101209
return text;
12111210
}
1212-
addMDText(str: string) {
1211+
addMDText(str: MarkDown) {
12131212
const text = new SettingsMDText(str);
12141213
this.options.push(text);
12151214
this.generate(text);
@@ -1771,7 +1770,7 @@ class Form implements OptionsElement<object> {
17711770
addText(str: string) {
17721771
return this.options.addText(str);
17731772
}
1774-
addMDText(str: string) {
1773+
addMDText(str: MarkDown) {
17751774
return this.options.addMDText(str);
17761775
}
17771776
addHR() {

src/webpage/utils/utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {I18n} from "../i18n.js";
2+
import {MarkDown} from "../markdown.js";
23
import {Dialog} from "../settings.js";
34
let instances:
45
| {
@@ -496,7 +497,7 @@ export async function getapiurls(str: string): Promise<
496497
>((res) => {
497498
const menu = new Dialog("");
498499
const options = menu.float.options;
499-
options.addMDText(I18n.incorrectURLS());
500+
options.addMDText(new MarkDown(I18n.incorrectURLS(), undefined));
500501
const opt = options.addOptions("", {ltr: true});
501502
let clicked = false;
502503
opt.addButtonInput("", I18n.yes(), async () => {
@@ -552,6 +553,8 @@ export async function getapiurls(str: string): Promise<
552553
if (clicked) return;
553554
clicked = true;
554555
try {
556+
//TODO make this a promise race for when the server just never responds
557+
//TODO maybe try to strip ports as another way to fix it
555558
if (!(await fetch(urls.api + "/ping")).ok) {
556559
res(false);
557560
menu.hide();

src/webpage/webhooks.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {Guild} from "./guild.js";
22
import {I18n} from "./i18n.js";
33
import {webhookType} from "./jsontypes.js";
4+
import {MarkDown} from "./markdown.js";
45
import {Member} from "./member.js";
56
import {Dialog, Options} from "./settings.js";
67
import {SnowFlake} from "./snowflake.js";
@@ -136,8 +137,8 @@ async function webhookMenu(
136137
moveChannels.map((_) => _.id),
137138
);
138139

139-
form.addMDText(I18n.webhooks.token(hook.token));
140-
form.addMDText(I18n.webhooks.url(hook.url));
140+
form.addMDText(new MarkDown(I18n.webhooks.token(hook.token), undefined));
141+
form.addMDText(new MarkDown(I18n.webhooks.url(hook.url), undefined));
141142
form.addText(I18n.webhooks.type(typeText));
142143
form.addButtonInput("", I18n.webhooks.copyURL(), () => {
143144
navigator.clipboard.writeText(hook.url);

translations/en.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@
627627
"incorrectURLS": "## This instance has likely sent the incorrect URLs.\n### If you're the instance owner please see [here](https://docs.spacebar.chat/setup/server/) under *Connecting from remote machines* to correct the issue.\n Would you like Fermi to automatically try to fix this error to let you connect to the instance?",
628628
"jankInfo": "Client Information",
629629
"instInfo": "Instance Information",
630-
"clientDesc": "Client version: $1\n\n[Join the official Fermi guild]($2/invite/USgYJo?instance=https%3A%2F%2Fspacebar.chat)\n\n[Help translate Fermi](https://translatewiki.net/wiki/Translating:JankClient#sortable:3=desc) \n\n[Help create Fermi](https://github.com/MathMan05/Fermi)\n\n[Help maintain the server Fermi relies on](https://github.com/spacebarchat/server)\n\nCalculated rights: $3",
630+
"clientDesc": "Client version: $1\n\n[Join the official Fermi guild]($2/invite/USgYJo?instance=https%3A%2F%2Fspacebar.chat)\n\n[Help translate Fermi](https://translatewiki.net/wiki/Translating:JankClient#sortable:3=desc) \n\n[Help create Fermi](https://github.com/MathMan05/Fermi)\n\n[Help maintain the server Fermi relies on](https://github.com/spacebarchat/server)\n\n[Read the blog](https://blog.fermi.chat/)\n\nCalculated rights: $3",
631631
"uploadFilesText": "Upload your files here!",
632632
"errorReconnect": "Unable to connect to the server, retrying in **$1** seconds...",
633633
"retrying": "Retrying...",

0 commit comments

Comments
 (0)