@akanjs/devkit 3.2.0 → 3.2.1-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +94 -0
- package/akanApp/akanApp.host.test.ts +21 -4
- package/akanApp/akanApp.host.ts +46 -9
- package/akanApp/akanApp.host.wiring.test.ts +206 -2
- package/akanApp/devBackend.ts +3 -1
- package/akanApp/devBuilder.ts +5 -1
- package/akanApp/devStartTimeline.test.ts +115 -0
- package/akanApp/devStartTimeline.ts +69 -0
- package/executors/appExecutor.ts +19 -4
- package/executors/executors.test.ts +30 -3
- package/frontendBuild/buildCacheStamp.test.ts +42 -0
- package/frontendBuild/buildCacheStamp.ts +23 -0
- package/frontendBuild/cssCandidateCache.test.ts +70 -1
- package/frontendBuild/cssCandidateCache.ts +17 -10
- package/frontendBuild/fontOptimizer.test.ts +144 -3
- package/frontendBuild/fontOptimizer.ts +103 -18
- package/frontendBuild/ssrBaseArtifactBuilder.test.ts +30 -1
- package/frontendBuild/ssrBaseArtifactBuilder.ts +40 -12
- package/incrementalBuilder/buildBatch.proc.ts +2 -1
- package/incrementalBuilder/buildBatchProtocol.ts +1 -0
- package/incrementalBuilder/builderBoot.ts +9 -2
- package/incrementalBuilder/builderChannel.ts +8 -2
- package/incrementalBuilder/incrementalBuilder.host.test.ts +16 -0
- package/incrementalBuilder/incrementalBuilder.host.ts +7 -1
- package/incrementalBuilder/incrementalBuilder.proc.test.ts +41 -0
- package/incrementalBuilder/incrementalBuilder.proc.ts +1 -0
- package/mobile/nativePublicEnv.ts +2 -1
- package/package.json +3 -3
- package/util/fileSys.test.ts +13 -0
- package/util/fileSys.ts +2 -2
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import type { Logger } from "akanjs/common";
|
|
3
|
+
import { DevStartTimeline } from "./devStartTimeline";
|
|
4
|
+
|
|
5
|
+
const timeline = () => {
|
|
6
|
+
const lines: string[] = [];
|
|
7
|
+
const clock = { at: 0 };
|
|
8
|
+
const logger = { info: (line: string) => void lines.push(line) } as unknown as Logger;
|
|
9
|
+
return { lines, clock, timeline: new DevStartTimeline(logger, () => clock.at) };
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const phases = { runtime: 70, routes: 200, styles: 900, dictionary: 6, pages: 500, total: 1700 };
|
|
13
|
+
|
|
14
|
+
describe("DevStartTimeline", () => {
|
|
15
|
+
test("names every phase of a start in one line, once the builder and the backend are both ready", () => {
|
|
16
|
+
const { lines, clock, timeline: start } = timeline();
|
|
17
|
+
clock.at = 800;
|
|
18
|
+
start.begin();
|
|
19
|
+
clock.at = 2700;
|
|
20
|
+
start.baseBuilt(phases);
|
|
21
|
+
start.mark("backendSpawned");
|
|
22
|
+
clock.at = 2900;
|
|
23
|
+
start.mark("builderReady");
|
|
24
|
+
expect(lines).toEqual([]);
|
|
25
|
+
clock.at = 3200;
|
|
26
|
+
start.mark("backendReady");
|
|
27
|
+
|
|
28
|
+
expect(lines).toEqual([
|
|
29
|
+
"ready in 3200ms (prepare 800 · builder 2100: spawn 200 + base build 1700 [runtime 70 · routes 200 · styles 900 · dictionary 6 · pages 500] + watch 200 · backend 500 · first artifact at 2700)",
|
|
30
|
+
]);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test("says what a backend spawned beside the build took once the artifact was there", () => {
|
|
34
|
+
const { lines, clock, timeline: start } = timeline();
|
|
35
|
+
clock.at = 800;
|
|
36
|
+
start.begin();
|
|
37
|
+
clock.at = 850;
|
|
38
|
+
start.mark("backendSpawned");
|
|
39
|
+
clock.at = 2700;
|
|
40
|
+
start.baseBuilt(phases);
|
|
41
|
+
clock.at = 2730;
|
|
42
|
+
start.mark("builderReady");
|
|
43
|
+
clock.at = 2950;
|
|
44
|
+
start.mark("backendReady");
|
|
45
|
+
|
|
46
|
+
expect(lines).toHaveLength(1);
|
|
47
|
+
expect(lines[0]).toStartWith("ready in 2950ms (prepare 800 · builder 1930: spawn 200 + base build 1700 [");
|
|
48
|
+
expect(lines[0]).toEndWith("+ watch 30 · backend 2100 (250 after the artifact) · first artifact at 2700)");
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("reports at the builder's ready when the backend was ready first", () => {
|
|
52
|
+
const { lines, clock, timeline: start } = timeline();
|
|
53
|
+
start.begin();
|
|
54
|
+
clock.at = 100;
|
|
55
|
+
start.mark("backendSpawned");
|
|
56
|
+
clock.at = 400;
|
|
57
|
+
start.mark("backendReady");
|
|
58
|
+
expect(lines).toEqual([]);
|
|
59
|
+
clock.at = 2000;
|
|
60
|
+
start.baseBuilt(phases);
|
|
61
|
+
clock.at = 2100;
|
|
62
|
+
start.mark("builderReady");
|
|
63
|
+
|
|
64
|
+
expect(lines).toHaveLength(1);
|
|
65
|
+
expect(lines[0]).toStartWith("ready in 2100ms (prepare 0 · builder 2100: spawn 300 + base build 1700 [");
|
|
66
|
+
expect(lines[0]).toEndWith("+ watch 100 · backend 300 · first artifact at 2000)");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("leaves the build's parts out when the boot build wrote no artifact", () => {
|
|
70
|
+
const { lines, clock, timeline: start } = timeline();
|
|
71
|
+
start.begin();
|
|
72
|
+
clock.at = 900;
|
|
73
|
+
start.mark("builderReady");
|
|
74
|
+
start.mark("backendSpawned");
|
|
75
|
+
clock.at = 1300;
|
|
76
|
+
start.mark("backendReady");
|
|
77
|
+
|
|
78
|
+
expect(lines).toEqual(["ready in 1300ms (prepare 0 · builder 900 · backend 400)"]);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test("times a backend replaced before it was ready from its last spawn", () => {
|
|
82
|
+
const { lines, clock, timeline: start } = timeline();
|
|
83
|
+
start.begin();
|
|
84
|
+
start.mark("builderReady");
|
|
85
|
+
start.mark("backendSpawned");
|
|
86
|
+
clock.at = 700;
|
|
87
|
+
start.mark("backendSpawned");
|
|
88
|
+
clock.at = 1000;
|
|
89
|
+
start.mark("backendReady");
|
|
90
|
+
|
|
91
|
+
expect(lines).toEqual(["ready in 1000ms (prepare 0 · builder 0 · backend 300)"]);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("says nothing after its one line until the next start, and nothing before the first", () => {
|
|
95
|
+
const { lines, clock, timeline: start } = timeline();
|
|
96
|
+
start.mark("builderReady");
|
|
97
|
+
start.mark("backendReady");
|
|
98
|
+
expect(lines).toEqual([]);
|
|
99
|
+
|
|
100
|
+
start.begin();
|
|
101
|
+
start.mark("builderReady");
|
|
102
|
+
start.mark("backendReady");
|
|
103
|
+
clock.at = 50;
|
|
104
|
+
start.mark("backendSpawned");
|
|
105
|
+
start.mark("backendReady");
|
|
106
|
+
expect(lines).toHaveLength(1);
|
|
107
|
+
|
|
108
|
+
start.begin();
|
|
109
|
+
start.mark("builderReady");
|
|
110
|
+
clock.at = 80;
|
|
111
|
+
start.mark("backendReady");
|
|
112
|
+
expect(lines).toHaveLength(2);
|
|
113
|
+
expect(lines[1]).toBe("ready in 80ms (prepare 50 · builder 0)");
|
|
114
|
+
});
|
|
115
|
+
});
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { Logger } from "akanjs/common";
|
|
2
|
+
|
|
3
|
+
type StartMark = "started" | "baseBuilt" | "builderReady" | "backendSpawned" | "backendReady";
|
|
4
|
+
|
|
5
|
+
const BASE_BUILD_PARTS = ["runtime", "routes", "styles", "dictionary", "pages"];
|
|
6
|
+
|
|
7
|
+
export class DevStartTimeline {
|
|
8
|
+
readonly #logger: Logger;
|
|
9
|
+
readonly #now: () => number;
|
|
10
|
+
#marks: Partial<Record<StartMark, number>> = {};
|
|
11
|
+
#basePhaseMs: Partial<Record<string, number>> = {};
|
|
12
|
+
#reported = true;
|
|
13
|
+
|
|
14
|
+
constructor(logger: Logger, now: () => number = () => performance.now()) {
|
|
15
|
+
this.#logger = logger;
|
|
16
|
+
this.#now = now;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
begin(): void {
|
|
20
|
+
this.#marks = { started: this.#now() };
|
|
21
|
+
this.#basePhaseMs = {};
|
|
22
|
+
this.#reported = false;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
baseBuilt(phaseMs: Record<string, number>): void {
|
|
26
|
+
if (this.#reported || this.#marks.baseBuilt !== undefined) return;
|
|
27
|
+
this.#basePhaseMs = phaseMs;
|
|
28
|
+
this.#marks.baseBuilt = this.#now();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
mark(mark: Exclude<StartMark, "started" | "baseBuilt">): void {
|
|
32
|
+
if (this.#reported) return;
|
|
33
|
+
if (mark === "backendSpawned") this.#marks.backendSpawned = this.#now();
|
|
34
|
+
else this.#marks[mark] ??= this.#now();
|
|
35
|
+
this.#report();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
#report(): void {
|
|
39
|
+
const { started, baseBuilt, builderReady, backendSpawned, backendReady } = this.#marks;
|
|
40
|
+
if (started === undefined || builderReady === undefined || backendReady === undefined) return;
|
|
41
|
+
this.#reported = true;
|
|
42
|
+
const ms = (value: number) => Math.round(value).toString();
|
|
43
|
+
//? The clock counts from the start of the process, so the first mark is what the CLI spent before the host.
|
|
44
|
+
const parts = [`prepare ${ms(started)}`, `builder ${ms(builderReady - started)}${this.#builderParts()}`];
|
|
45
|
+
if (backendSpawned !== undefined) {
|
|
46
|
+
//? Spawned beside the build, most of its time is the wait for the artifact: what it took after that is its own.
|
|
47
|
+
const afterArtifact =
|
|
48
|
+
baseBuilt !== undefined && backendSpawned < baseBuilt && baseBuilt <= backendReady
|
|
49
|
+
? ` (${ms(backendReady - baseBuilt)} after the artifact)`
|
|
50
|
+
: "";
|
|
51
|
+
parts.push(`backend ${ms(backendReady - backendSpawned)}${afterArtifact}`);
|
|
52
|
+
}
|
|
53
|
+
if (baseBuilt !== undefined) parts.push(`first artifact at ${ms(baseBuilt)}`);
|
|
54
|
+
this.#logger.info(`ready in ${ms(Math.max(builderReady, backendReady))}ms (${parts.join(" · ")})`);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
#builderParts(): string {
|
|
58
|
+
const { started, baseBuilt, builderReady } = this.#marks;
|
|
59
|
+
if (started === undefined || baseBuilt === undefined || builderReady === undefined) return "";
|
|
60
|
+
const total = this.#basePhaseMs.total ?? 0;
|
|
61
|
+
const base = BASE_BUILD_PARTS.flatMap((part) => {
|
|
62
|
+
const partMs = this.#basePhaseMs[part];
|
|
63
|
+
return partMs === undefined ? [] : [`${part} ${Math.round(partMs)}`];
|
|
64
|
+
});
|
|
65
|
+
const spawn = Math.max(0, Math.round(baseBuilt - started - total));
|
|
66
|
+
const watch = Math.max(0, Math.round(builderReady - baseBuilt));
|
|
67
|
+
return `: spawn ${spawn} + base build ${Math.round(total)} [${base.join(" · ")}] + watch ${watch}`;
|
|
68
|
+
}
|
|
69
|
+
}
|
package/executors/appExecutor.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { cp as cpEntry, lstat, mkdir, readdir as readDirEntries, realpath, symlink } from "node:fs/promises";
|
|
1
|
+
import { cp as cpEntry, lstat, mkdir, readdir as readDirEntries, realpath, rm, stat, symlink } from "node:fs/promises";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { pathToFileURL } from "node:url";
|
|
4
4
|
import type { AkanPlugin, AkanSyncContext } from "akanjs";
|
|
@@ -44,8 +44,8 @@ export class AppExecutor extends SysExecutor {
|
|
|
44
44
|
static from(executor: SysExecutor | WorkspaceExecutor, name: string) {
|
|
45
45
|
return new AppExecutor({ workspace: executor instanceof WorkspaceExecutor ? executor : executor.workspace, name });
|
|
46
46
|
}
|
|
47
|
-
//* Not dev output: the downloaded `bin` sources.
|
|
48
|
-
static readonly #keptOnStart = [path.join(".akan", "cache"
|
|
47
|
+
//* Not dev output: the downloaded `bin` sources, and caches whose entries name the akanjs and Bun that wrote them.
|
|
48
|
+
static readonly #keptOnStart = [path.join(".akan", "cache")];
|
|
49
49
|
//* Native builds moved to dist/native/<app>; the old folder may hold a signed release nobody uploaded yet.
|
|
50
50
|
static readonly #legacyNative = path.join(".akan", "native");
|
|
51
51
|
async #removeDevOutput(dir = ".akan") {
|
|
@@ -59,13 +59,28 @@ export class AppExecutor extends SysExecutor {
|
|
|
59
59
|
);
|
|
60
60
|
return;
|
|
61
61
|
}
|
|
62
|
-
if (AppExecutor.#keptOnStart.includes(entry)) return;
|
|
62
|
+
if (AppExecutor.#keptOnStart.includes(entry)) return await this.#removeStaleTemps(entry);
|
|
63
63
|
if (AppExecutor.#keptOnStart.some((kept) => kept.startsWith(`${entry}${path.sep}`)))
|
|
64
64
|
return await this.#removeDevOutput(entry);
|
|
65
65
|
await this.removeDir(entry);
|
|
66
66
|
}),
|
|
67
67
|
);
|
|
68
68
|
}
|
|
69
|
+
//* A start killed between an atomic write and its rename leaves the `.tmp`; a younger one may be another start's.
|
|
70
|
+
static readonly #staleTempMs = 60_000;
|
|
71
|
+
async #removeStaleTemps(dir: string) {
|
|
72
|
+
const root = this.getPath(dir);
|
|
73
|
+
const names = await readDirEntries(root, { recursive: true }).catch(() => [] as string[]);
|
|
74
|
+
await Promise.all(
|
|
75
|
+
names
|
|
76
|
+
.filter((name) => name.endsWith(".tmp"))
|
|
77
|
+
.map(async (name) => {
|
|
78
|
+
const file = path.join(root, name);
|
|
79
|
+
const stats = await stat(file).catch(() => null);
|
|
80
|
+
if (stats?.isFile() && Date.now() - stats.mtimeMs > AppExecutor.#staleTempMs) await rm(file, { force: true });
|
|
81
|
+
}),
|
|
82
|
+
);
|
|
83
|
+
}
|
|
69
84
|
getEnv() {
|
|
70
85
|
return WorkspaceExecutor.getBaseDevEnv().env;
|
|
71
86
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { describe, expect, spyOn, test } from "bun:test";
|
|
2
2
|
import { existsSync } from "node:fs";
|
|
3
|
-
import { lstat, mkdir, readFile, readlink, rm, stat, writeFile } from "node:fs/promises";
|
|
3
|
+
import { lstat, mkdir, readFile, readlink, rm, stat, utimes, writeFile } from "node:fs/promises";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import { AkanAppConfig } from "../akanConfig";
|
|
6
6
|
import { DevGeneratedIndexSync } from "../frontendBuild/devGeneratedIndexSync";
|
|
@@ -612,7 +612,7 @@ describe("Workspace and app executor environment contracts", () => {
|
|
|
612
612
|
expect(app.getCommandEnv().AKAN_PUBLIC_SERVE_DOMAIN).toBe("example.com");
|
|
613
613
|
});
|
|
614
614
|
|
|
615
|
-
test("akan start clears the dev output and keeps the
|
|
615
|
+
test("akan start clears the dev output and keeps the caches and a native folder from before dist/native", async () => {
|
|
616
616
|
const root = await makeTempRoot();
|
|
617
617
|
process.env.AKAN_PUBLIC_REPO_NAME = "repo";
|
|
618
618
|
process.env.AKAN_PUBLIC_SERVE_DOMAIN = "example.com";
|
|
@@ -626,11 +626,14 @@ describe("Workspace and app executor environment contracts", () => {
|
|
|
626
626
|
"native/desktop/updates/macos-arm64/main.json",
|
|
627
627
|
"native/desktop/build/macos/App.app",
|
|
628
628
|
"cache/bin/ffmpeg",
|
|
629
|
+
"cache/cssCandidates.json",
|
|
630
|
+
"cache/fontCache.json",
|
|
631
|
+
"cache/fonts/Assistant-400-normal-abc.woff2",
|
|
629
632
|
];
|
|
630
633
|
const removed = [
|
|
631
634
|
"artifact/client/app.js",
|
|
635
|
+
"artifact/fontCache.json",
|
|
632
636
|
"generated/dict/index.ts",
|
|
633
|
-
"cache/cssCandidates.json",
|
|
634
637
|
"desktop/server/main.js",
|
|
635
638
|
"mobile/desktop/native/macos/App.app",
|
|
636
639
|
];
|
|
@@ -644,6 +647,30 @@ describe("Workspace and app executor environment contracts", () => {
|
|
|
644
647
|
expect(existsSync(path.join(akan, file))).toBe(false);
|
|
645
648
|
});
|
|
646
649
|
|
|
650
|
+
test("akan start removes a cache temp file a killed start left, and keeps one a running start is writing", async () => {
|
|
651
|
+
const root = await makeTempRoot();
|
|
652
|
+
process.env.AKAN_PUBLIC_REPO_NAME = "repo";
|
|
653
|
+
process.env.AKAN_PUBLIC_SERVE_DOMAIN = "example.com";
|
|
654
|
+
process.env.AKAN_PUBLIC_ENV = "local";
|
|
655
|
+
process.env.PORT_OFFSET = "0";
|
|
656
|
+
await writeJson(path.join(root, "package.json"), rootPackageJson());
|
|
657
|
+
await mkdir(path.join(root, "apps/starttemps/page"), { recursive: true });
|
|
658
|
+
await writeFile(path.join(root, "apps/starttemps/akan.config.ts"), "export default {};\n");
|
|
659
|
+
const cache = path.join(root, "apps/starttemps/.akan/cache");
|
|
660
|
+
const stale = ["cssCandidates.json.1.a.tmp", "fonts/x.woff2.1.b.tmp"];
|
|
661
|
+
const fresh = ["fontCache.json.2.c.tmp", "fonts/x.woff2.2.d.tmp"];
|
|
662
|
+
const kept = [...fresh, "cssCandidates.json", "fonts/x.woff2"];
|
|
663
|
+
for (const file of [...stale, ...kept]) await writeText(path.join(cache, file), "x");
|
|
664
|
+
const twoMinutesAgo = new Date(Date.now() - 120_000);
|
|
665
|
+
for (const file of stale) await utimes(path.join(cache, file), twoMinutesAgo, twoMinutesAgo);
|
|
666
|
+
|
|
667
|
+
const app = AppExecutor.from(new WorkspaceExecutor({ workspaceRoot: root, repoName: "repo" }), "starttemps");
|
|
668
|
+
await app.prepareCommand("start");
|
|
669
|
+
|
|
670
|
+
for (const file of stale) expect(existsSync(path.join(cache, file))).toBe(false);
|
|
671
|
+
for (const file of kept) expect(existsSync(path.join(cache, file))).toBe(true);
|
|
672
|
+
});
|
|
673
|
+
|
|
647
674
|
describe("syncPages", () => {
|
|
648
675
|
// `AppExecutor.from` memoises by name, so each test needs a name no other test has used.
|
|
649
676
|
const makeAppWithLibPages = async (
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { appendFile, utimes, writeFile } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { tempDirs } from "../test/testHelpers";
|
|
5
|
+
import { buildCacheStamp, isBuildCacheStamp } from "./buildCacheStamp";
|
|
6
|
+
|
|
7
|
+
const tempRoot = tempDirs("akan-devkit-stamp-");
|
|
8
|
+
|
|
9
|
+
const makeProducer = async () => {
|
|
10
|
+
const file = path.join(await tempRoot(), "producer.ts");
|
|
11
|
+
await writeFile(file, "export const regex = /x/;\n");
|
|
12
|
+
await utimes(file, new Date(1_700_000_000_000), new Date(1_700_000_000_000));
|
|
13
|
+
return file;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
describe("buildCacheStamp", () => {
|
|
17
|
+
test("matches a stamp taken from the same producing file", async () => {
|
|
18
|
+
const file = await makeProducer();
|
|
19
|
+
expect(isBuildCacheStamp(buildCacheStamp([file]), buildCacheStamp([file]))).toBe(true);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("misses when the producing file's mtime moves", async () => {
|
|
23
|
+
const file = await makeProducer();
|
|
24
|
+
const before = buildCacheStamp([file]);
|
|
25
|
+
await utimes(file, new Date(1_700_000_001_000), new Date(1_700_000_001_000));
|
|
26
|
+
expect(isBuildCacheStamp(before, buildCacheStamp([file]))).toBe(false);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("misses when the producing file's size changes under the same mtime", async () => {
|
|
30
|
+
const file = await makeProducer();
|
|
31
|
+
const before = buildCacheStamp([file]);
|
|
32
|
+
await appendFile(file, "export const more = 1;\n");
|
|
33
|
+
await utimes(file, new Date(1_700_000_000_000), new Date(1_700_000_000_000));
|
|
34
|
+
expect(isBuildCacheStamp(before, buildCacheStamp([file]))).toBe(false);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("misses an entry written before the stamp named its producing files", async () => {
|
|
38
|
+
const file = await makeProducer();
|
|
39
|
+
const { code: _, ...older } = buildCacheStamp([file]);
|
|
40
|
+
expect(isBuildCacheStamp(older, buildCacheStamp([file]))).toBe(false);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { statSync } from "node:fs";
|
|
2
|
+
import { AkanAppConfig } from "../akanConfig/akanConfig";
|
|
3
|
+
|
|
4
|
+
export interface BuildCacheStamp {
|
|
5
|
+
akanjs: string;
|
|
6
|
+
bun: string;
|
|
7
|
+
code: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
//? Kept across starts, so another akanjs, Bun or edit of the producing module (run from source) must read as a miss.
|
|
11
|
+
export const buildCacheStamp = (producers: string[]): BuildCacheStamp => ({
|
|
12
|
+
akanjs: AkanAppConfig.akanVersion,
|
|
13
|
+
bun: `${Bun.version}+${Bun.revision}`,
|
|
14
|
+
code: producers
|
|
15
|
+
.map((file) => {
|
|
16
|
+
const stats = statSync(file, { throwIfNoEntry: false });
|
|
17
|
+
return stats ? `${stats.mtimeMs}:${stats.size}` : "missing";
|
|
18
|
+
})
|
|
19
|
+
.join(","),
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
export const isBuildCacheStamp = (value: Partial<BuildCacheStamp> | null | undefined, stamp: BuildCacheStamp) =>
|
|
23
|
+
value?.akanjs === stamp.akanjs && value.bun === stamp.bun && value.code === stamp.code;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import { mkdtemp, writeFile } from "node:fs/promises";
|
|
2
|
+
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
3
3
|
import { tmpdir } from "node:os";
|
|
4
4
|
import path from "node:path";
|
|
5
|
+
import type { BuildCacheStamp } from "./buildCacheStamp";
|
|
5
6
|
import { CssCandidateCache } from "./cssCandidateCache";
|
|
6
7
|
|
|
7
8
|
const scan = async (source: string) => {
|
|
@@ -56,3 +57,71 @@ describe("CssCandidateCache token extraction", () => {
|
|
|
56
57
|
expect(candidates.filter((candidate) => candidate.startsWith("["))).toEqual([]);
|
|
57
58
|
});
|
|
58
59
|
});
|
|
60
|
+
|
|
61
|
+
describe("CssCandidateCache persistence", () => {
|
|
62
|
+
const writeCache = async (stamp: BuildCacheStamp) => {
|
|
63
|
+
const dir = await mkdtemp(path.join(tmpdir(), "css-candidate-"));
|
|
64
|
+
const file = path.join(dir, "probe.tsx");
|
|
65
|
+
const cachePath = path.join(dir, "cache.json");
|
|
66
|
+
await writeFile(file, `const c = "w-full";`);
|
|
67
|
+
const cache = await new CssCandidateCache(cachePath, stamp).load();
|
|
68
|
+
await cache.candidatesFor(file);
|
|
69
|
+
await cache.save(new Set([file]));
|
|
70
|
+
return { file, cachePath };
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
test("reuses an entry the same akanjs and Bun wrote", async () => {
|
|
74
|
+
const { file, cachePath } = await writeCache({ akanjs: "1.0.0", bun: "1.0.0+a", code: "1:1" });
|
|
75
|
+
const cache = await new CssCandidateCache(cachePath, { akanjs: "1.0.0", bun: "1.0.0+a", code: "1:1" }).load();
|
|
76
|
+
expect(await cache.candidatesFor(file)).toContain("w-full");
|
|
77
|
+
expect([cache.reused, cache.rescanned]).toEqual([1, 0]);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test.each([
|
|
81
|
+
["akanjs", { akanjs: "1.0.1", bun: "1.0.0+a", code: "1:1" }],
|
|
82
|
+
["Bun", { akanjs: "1.0.0", bun: "1.0.0+b", code: "1:1" }],
|
|
83
|
+
["scanner build", { akanjs: "1.0.0", bun: "1.0.0+a", code: "2:1" }],
|
|
84
|
+
])("rescans an entry another %s wrote", async (_, stamp) => {
|
|
85
|
+
const { file, cachePath } = await writeCache({ akanjs: "1.0.0", bun: "1.0.0+a", code: "1:1" });
|
|
86
|
+
const cache = await new CssCandidateCache(cachePath, stamp).load();
|
|
87
|
+
expect(await cache.candidatesFor(file)).toContain("w-full");
|
|
88
|
+
expect([cache.reused, cache.rescanned]).toEqual([0, 1]);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test("keeps another base path's entries, so each base path reuses its scan on the next start", async () => {
|
|
92
|
+
const stamp = { akanjs: "1.0.0", bun: "1.0.0+a", code: "1:1" };
|
|
93
|
+
const dir = await mkdtemp(path.join(tmpdir(), "css-candidate-"));
|
|
94
|
+
const cachePath = path.join(dir, "cache.json");
|
|
95
|
+
const [rootFile, adminFile] = [path.join(dir, "root.tsx"), path.join(dir, "admin.tsx")];
|
|
96
|
+
await writeFile(rootFile, `const c = "w-full";`);
|
|
97
|
+
await writeFile(adminFile, `const c = "h-full";`);
|
|
98
|
+
const scanBasePath = async (file: string) => {
|
|
99
|
+
const cache = await new CssCandidateCache(cachePath, stamp).load();
|
|
100
|
+
await cache.candidatesFor(file);
|
|
101
|
+
await cache.save(new Set([file]));
|
|
102
|
+
return cache;
|
|
103
|
+
};
|
|
104
|
+
await scanBasePath(rootFile);
|
|
105
|
+
await scanBasePath(adminFile);
|
|
106
|
+
|
|
107
|
+
const [root, admin] = [await scanBasePath(rootFile), await scanBasePath(adminFile)];
|
|
108
|
+
expect([root.reused, admin.reused]).toEqual([1, 1]);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test("drops the entry of a source file that was deleted", async () => {
|
|
112
|
+
const stamp = { akanjs: "1.0.0", bun: "1.0.0+a", code: "1:1" };
|
|
113
|
+
const { file, cachePath } = await writeCache(stamp);
|
|
114
|
+
await rm(file);
|
|
115
|
+
await new CssCandidateCache(cachePath, stamp).load().then((cache) => cache.save(new Set()));
|
|
116
|
+
expect(Object.keys((await Bun.file(cachePath).json()).files)).toEqual([]);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test("rescans when the cache file was cut short", async () => {
|
|
120
|
+
const { file, cachePath } = await writeCache({ akanjs: "1.0.0", bun: "1.0.0+a", code: "1:1" });
|
|
121
|
+
const text = await Bun.file(cachePath).text();
|
|
122
|
+
await writeFile(cachePath, text.slice(0, text.length / 2));
|
|
123
|
+
const cache = await new CssCandidateCache(cachePath, { akanjs: "1.0.0", bun: "1.0.0+a", code: "1:1" }).load();
|
|
124
|
+
expect(await cache.candidatesFor(file)).toContain("w-full");
|
|
125
|
+
expect([cache.reused, cache.rescanned]).toEqual([0, 1]);
|
|
126
|
+
});
|
|
127
|
+
});
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { stat } from "node:fs/promises";
|
|
2
|
+
import { FileSys } from "../util/fileSys";
|
|
3
|
+
import { type BuildCacheStamp, buildCacheStamp, isBuildCacheStamp } from "./buildCacheStamp";
|
|
2
4
|
|
|
3
5
|
// The leading `[` alternation keeps arbitrary variants (`[&_td]:px-3`) whole, and a bracket may hold one nested
|
|
4
6
|
// bracket (`[&_[data-slot=x]]:p-2`, `has-[[aria-expanded=true]]:x`), or the candidate is cut at the inner `]` and the
|
|
@@ -14,7 +16,7 @@ interface CachedFile {
|
|
|
14
16
|
candidates: string[];
|
|
15
17
|
}
|
|
16
18
|
|
|
17
|
-
interface CacheFile {
|
|
19
|
+
interface CacheFile extends BuildCacheStamp {
|
|
18
20
|
version: number;
|
|
19
21
|
files: Record<string, CachedFile>;
|
|
20
22
|
}
|
|
@@ -22,15 +24,17 @@ interface CacheFile {
|
|
|
22
24
|
/** Tailwind candidates per source file, on disk: the batch worker that compiles CSS exits before the next save. */
|
|
23
25
|
export class CssCandidateCache {
|
|
24
26
|
// Bump when the token regex or the entry shape changes, so stale extractions are not reused.
|
|
25
|
-
static readonly #version =
|
|
27
|
+
static readonly #version = 6;
|
|
26
28
|
readonly #path: string;
|
|
29
|
+
readonly #stamp: BuildCacheStamp;
|
|
27
30
|
readonly #entries = new Map<string, CachedFile>();
|
|
28
31
|
#dirty = false;
|
|
29
32
|
#reused = 0;
|
|
30
33
|
#rescanned = 0;
|
|
31
34
|
|
|
32
|
-
constructor(cachePath: string) {
|
|
35
|
+
constructor(cachePath: string, stamp = buildCacheStamp([import.meta.path])) {
|
|
33
36
|
this.#path = cachePath;
|
|
37
|
+
this.#stamp = stamp;
|
|
34
38
|
}
|
|
35
39
|
|
|
36
40
|
get reused(): number {
|
|
@@ -44,8 +48,8 @@ export class CssCandidateCache {
|
|
|
44
48
|
async load(): Promise<this> {
|
|
45
49
|
const raw = (await Bun.file(this.#path)
|
|
46
50
|
.json()
|
|
47
|
-
.catch(() => null)) as CacheFile | null;
|
|
48
|
-
if (
|
|
51
|
+
.catch(() => null)) as Partial<CacheFile> | null;
|
|
52
|
+
if (raw?.version !== CssCandidateCache.#version || !isBuildCacheStamp(raw, this.#stamp) || !raw.files) return this;
|
|
49
53
|
for (const [file, entry] of Object.entries(raw.files)) {
|
|
50
54
|
if (typeof entry?.mtimeMs !== "number" || !Array.isArray(entry.candidates)) continue;
|
|
51
55
|
this.#entries.set(file, entry);
|
|
@@ -71,20 +75,23 @@ export class CssCandidateCache {
|
|
|
71
75
|
return candidates;
|
|
72
76
|
}
|
|
73
77
|
|
|
78
|
+
// Each base path scans its own files into the same cache, so only an entry whose file is gone is dropped.
|
|
74
79
|
async save(present: Set<string>): Promise<void> {
|
|
75
|
-
|
|
76
|
-
|
|
80
|
+
const absent = [...this.#entries.keys()].filter((file) => !present.has(file));
|
|
81
|
+
const exists = await Promise.all(absent.map((file) => Bun.file(file).exists()));
|
|
82
|
+
absent.forEach((file, index) => {
|
|
83
|
+
if (exists[index]) return;
|
|
77
84
|
this.#entries.delete(file);
|
|
78
85
|
this.#dirty = true;
|
|
79
|
-
}
|
|
86
|
+
});
|
|
80
87
|
if (!this.#dirty) return;
|
|
81
88
|
this.#dirty = false;
|
|
82
89
|
const files: Record<string, CachedFile> = {};
|
|
83
90
|
for (const [file, entry] of this.#entries) files[file] = entry;
|
|
84
91
|
// An unwritable cache is a slow build, not a failed one: a read-only checkout must still compile.
|
|
85
|
-
await
|
|
92
|
+
await FileSys.writeTextAtomic(
|
|
86
93
|
this.#path,
|
|
87
|
-
JSON.stringify({ version: CssCandidateCache.#version, files } satisfies CacheFile),
|
|
94
|
+
JSON.stringify({ version: CssCandidateCache.#version, ...this.#stamp, files } satisfies CacheFile),
|
|
88
95
|
).catch(() => undefined);
|
|
89
96
|
}
|
|
90
97
|
}
|