Skip to content

Commit 249b46b

Browse files
committed
feat: measure time used by each extension
1 parent 02aba3a commit 249b46b

File tree

8 files changed

+26
-12
lines changed

8 files changed

+26
-12
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
"test": "jest --config jestconfig.json --coverage",
1717
"prepare": "npm run build",
1818
"dev": "wrangler dev",
19-
"build:worker": "esbuild src/cloudflare-worker/index.ts --outfile=dist/worker.js --bundle --minify --format=esm --loader:.html=text --external:events",
20-
"build:package": "esbuild src/core/index.ts --outfile=lib/index.js --bundle --format=esm --external:events",
19+
"build:worker": "esbuild src/cloudflare-worker/index.ts --outfile=dist/worker.js --bundle --minify --format=esm --loader:.html=text --external:events --keep-names",
20+
"build:package": "esbuild src/core/index.ts --outfile=lib/index.js --bundle --format=esm --external:events --keep-names",
2121
"build": "npm run build:worker && npm run build:package",
2222
"format": "prettier --write '**/*.{js,ts,jsx,tsx,json,yml,yaml,md,html}' '!dist' '!coverage'",
2323
"lint": "eslint .",

src/core/card.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,23 @@ export class Generator {
2525

2626
this.config = config;
2727

28-
const extensions = this.config.extensions.map((init) => init(this)) ?? [];
29-
const data = this.fetch(config.username, config.site);
28+
const extensions =
29+
this.config.extensions.map(async (init) => {
30+
const start = Date.now();
31+
const ext = await init(this);
32+
this.log(`extension "${ext.name}" initialized in ${Date.now() - start} ms`);
33+
return ext;
34+
}) ?? [];
35+
const data = (async () => {
36+
const start = Date.now();
37+
const data = await this.fetch(config.username, config.site);
38+
this.log(`user data fetched in ${Date.now() - start} ms`);
39+
return data;
40+
})();
3041
const body = this.body();
3142

3243
const result = await this.hydrate(await data, body, await Promise.all(extensions));
33-
this.log(`card generated in ${Date.now() - start_time}ms`);
44+
this.log(`card generated in ${Date.now() - start_time} ms`);
3445
return result;
3546
}
3647

@@ -116,9 +127,11 @@ export class Generator {
116127

117128
for (const extension of extensions) {
118129
try {
130+
const start = Date.now();
119131
await extension(this, data, body, ext_styles);
132+
this.log(`extension "${extension.name}" hydrated in ${Date.now() - start} ms`);
120133
} catch (err) {
121-
this.log("Extension Failed", err);
134+
this.log(`extension "${extension.name}" failed`, err);
122135
}
123136
}
124137

src/core/exts/activity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const langs: Record<string, string> = {
3838
};
3939

4040
export function ActivityExtension(generator: Generator): Extension {
41-
return async (generator, data, body, styles) => {
41+
return async function Activity(generator, data, body, styles) {
4242
if (generator.config.height < 400) {
4343
generator.config.height = 400;
4444
}

src/core/exts/animation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const order: typeof selectors[number][] = [
2626
];
2727

2828
export function AnimationExtension(generator: Generator): Extension {
29-
return async (generator, data, body, styles) => {
29+
return async function Animation(generator, data, body, styles) {
3030
if (generator.config.animation === false) {
3131
return;
3232
}

src/core/exts/font.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export async function FontExtension(generator: Generator): Promise<Extension> {
3838
if (res.ok) {
3939
const data = await res.json<{ name: string; base64: string }>();
4040
supported[name.toLowerCase()] = { name, base64: data.base64 };
41-
generator.log(`Loaded remote font ${name}`);
41+
generator.log(`loaded remote font "${name}"`);
4242
generator.cache.put(url, data);
4343
} else {
4444
return;
@@ -48,7 +48,7 @@ export async function FontExtension(generator: Generator): Promise<Extension> {
4848
}),
4949
);
5050

51-
return async (generator, data, body, styles) => {
51+
return async function Font(generator, data, body, styles) {
5252
if (Array.isArray(config.fonts)) {
5353
const list = config.fonts.map((font: string) => {
5454
if (supported[font.toLowerCase()]) {

src/core/exts/remote-style.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function RemoteStyleExtension(generator: Generator): Extension {
2727
);
2828
}
2929

30-
return async (generator, data, body, styles) => {
30+
return async function RemoteStyle(generator, data, body, styles) {
3131
for (const css of externals) {
3232
styles.push(await css);
3333
}

src/core/exts/theme.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const supported: Record<string, Theme> = {
1818
};
1919

2020
export function ThemeExtension(generator: Generator): Extension {
21-
return async (generator, data, body, styles) => {
21+
return async function Theme(generator, data, body, styles) {
2222
if (!generator.config?.theme) {
2323
return;
2424
}

wrangler.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ account_id = "b1c3d1b89f9131a84a0f1f6a973232f1"
44
workers_dev = true
55
compatibility_date = "2022-05-26"
66
kv_namespaces = []
7+
node_compat = true
78

89
[build]
910
command = "npm run -s build:worker"

0 commit comments

Comments
 (0)