@akanjs/devkit 3.2.0 → 3.2.1-rc.0
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/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
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
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import { mkdir, rm, stat, writeFile } from "node:fs/promises";
|
|
2
|
+
import { chmod, mkdir, rm, stat, utimes, writeFile } from "node:fs/promises";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import type { App } from "../commandDecorators";
|
|
5
5
|
import { tempDirs } from "../test/testHelpers";
|
|
@@ -17,15 +17,17 @@ const makeApp = async (layoutSource: string) => {
|
|
|
17
17
|
await writeFile(path.join(cwdPath, "page/_layout.tsx"), layoutSource);
|
|
18
18
|
await Bun.write(path.join(cwdPath, "public/fonts/Assistant-Regular.woff2"), Bun.file(SOURCE_FONT));
|
|
19
19
|
const warnings: string[] = [];
|
|
20
|
+
const verboses: string[] = [];
|
|
20
21
|
const app = {
|
|
21
22
|
cwdPath,
|
|
22
23
|
dist: { cwdPath: path.join(root, "dist/apps/demo") },
|
|
23
24
|
workspace: { workspaceRoot: root },
|
|
24
25
|
getPageKeys: async () => ["./_layout.tsx"],
|
|
25
|
-
verbose: () =>
|
|
26
|
+
verbose: (message: string) => verboses.push(message),
|
|
26
27
|
logger: { warn: (message: string) => warnings.push(message) },
|
|
27
28
|
} as unknown as App;
|
|
28
|
-
|
|
29
|
+
const reusedCount = () => verboses.filter((message) => message.startsWith("[font] reused")).length;
|
|
30
|
+
return { app, cwdPath, root, warnings, reusedCount };
|
|
29
31
|
};
|
|
30
32
|
|
|
31
33
|
const layoutWith = (extra = "") => `
|
|
@@ -125,6 +127,145 @@ describe("FontOptimizer cache", () => {
|
|
|
125
127
|
expect(await Bun.file(second.files[0]).exists()).toBe(true);
|
|
126
128
|
});
|
|
127
129
|
|
|
130
|
+
test("reuses the subset on the next akan start, which wipes the artifact folder", async () => {
|
|
131
|
+
const { app, cwdPath, reusedCount } = await makeApp(layoutWith());
|
|
132
|
+
const first = await optimize(app);
|
|
133
|
+
const firstBytes = await Bun.file(first.files[0]).bytes();
|
|
134
|
+
await rm(path.join(cwdPath, ".akan/artifact"), { recursive: true, force: true });
|
|
135
|
+
|
|
136
|
+
const second = await optimize(app);
|
|
137
|
+
expect(reusedCount()).toBe(1);
|
|
138
|
+
expect(second.files).toEqual(first.files);
|
|
139
|
+
expect(second.css).toBe(first.css);
|
|
140
|
+
expect(await Bun.file(second.files[0]).bytes()).toEqual(firstBytes);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test.each(["akanjs", "bun", "code"])("resubsets when the cache was written by another %s version", async (field) => {
|
|
144
|
+
const { app, cwdPath, reusedCount } = await makeApp(layoutWith());
|
|
145
|
+
await optimize(app);
|
|
146
|
+
const indexPath = path.join(cwdPath, ".akan/cache/fontCache.json");
|
|
147
|
+
const index = await Bun.file(indexPath).json();
|
|
148
|
+
await Bun.write(indexPath, JSON.stringify({ ...index, [field]: "0.0.1-other" }));
|
|
149
|
+
|
|
150
|
+
await optimize(app);
|
|
151
|
+
expect(reusedCount()).toBe(0);
|
|
152
|
+
expect((await Bun.file(indexPath).json())[field]).toBe(index[field]);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
test("resubsets when the lockfile changes, since a font tool version may have moved", async () => {
|
|
156
|
+
const { app, root, reusedCount } = await makeApp(layoutWith());
|
|
157
|
+
await writeFile(path.join(root, "bun.lock"), '{ "lockfileVersion": 1 }\n');
|
|
158
|
+
await optimize(app);
|
|
159
|
+
await optimize(app);
|
|
160
|
+
expect(reusedCount()).toBe(1);
|
|
161
|
+
|
|
162
|
+
await writeFile(path.join(root, "bun.lock"), '{ "lockfileVersion": 1, "packages": {} }\n');
|
|
163
|
+
await optimize(app);
|
|
164
|
+
expect(reusedCount()).toBe(1);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test("rebuilds a cached font file that was cut short", async () => {
|
|
168
|
+
const { app, cwdPath, reusedCount } = await makeApp(layoutWith());
|
|
169
|
+
const first = await optimize(app);
|
|
170
|
+
const firstBytes = await Bun.file(first.files[0]).bytes();
|
|
171
|
+
const cachedPath = path.join(
|
|
172
|
+
cwdPath,
|
|
173
|
+
".akan/cache",
|
|
174
|
+
path.relative(path.join(cwdPath, ".akan/artifact"), first.files[0]),
|
|
175
|
+
);
|
|
176
|
+
await Bun.write(cachedPath, firstBytes.subarray(0, firstBytes.byteLength / 2));
|
|
177
|
+
await rm(path.join(cwdPath, ".akan/artifact"), { recursive: true, force: true });
|
|
178
|
+
|
|
179
|
+
const second = await optimize(app);
|
|
180
|
+
expect(reusedCount()).toBe(0);
|
|
181
|
+
expect(await Bun.file(second.files[0]).bytes()).toEqual(firstBytes);
|
|
182
|
+
expect(await Bun.file(cachedPath).bytes()).toEqual(firstBytes);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
test("replaces an artifact copy that was cut short with the cached font", async () => {
|
|
186
|
+
const { app, reusedCount } = await makeApp(layoutWith());
|
|
187
|
+
const first = await optimize(app);
|
|
188
|
+
const firstBytes = await Bun.file(first.files[0]).bytes();
|
|
189
|
+
await Bun.write(first.files[0], firstBytes.subarray(0, 16));
|
|
190
|
+
|
|
191
|
+
const second = await optimize(app);
|
|
192
|
+
expect(reusedCount()).toBe(1);
|
|
193
|
+
expect(await Bun.file(second.files[0]).bytes()).toEqual(firstBytes);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
test("rebuilds when the cache index was cut short", async () => {
|
|
197
|
+
const { app, cwdPath, reusedCount } = await makeApp(layoutWith());
|
|
198
|
+
const first = await optimize(app);
|
|
199
|
+
const indexPath = path.join(cwdPath, ".akan/cache/fontCache.json");
|
|
200
|
+
const index = await Bun.file(indexPath).text();
|
|
201
|
+
await Bun.write(indexPath, index.slice(0, index.length / 2));
|
|
202
|
+
|
|
203
|
+
const second = await optimize(app);
|
|
204
|
+
expect(reusedCount()).toBe(0);
|
|
205
|
+
expect(second.css).toBe(first.css);
|
|
206
|
+
expect(await Bun.file(indexPath).text()).toBe(index);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
test("drops cached font files the current entry no longer lists", async () => {
|
|
210
|
+
const { app, cwdPath } = await makeApp(layoutWith());
|
|
211
|
+
const first = await optimize(app);
|
|
212
|
+
const stalePath = path.join(cwdPath, ".akan/cache/fonts/Assistant-400-normal-stale.woff2");
|
|
213
|
+
await Bun.write(stalePath, "stale");
|
|
214
|
+
|
|
215
|
+
await writeFile(path.join(cwdPath, "page/_layout.tsx"), layoutWith(`\n className: "font-brand",`));
|
|
216
|
+
await optimize(app);
|
|
217
|
+
expect(await Bun.file(stalePath).exists()).toBe(false);
|
|
218
|
+
expect(
|
|
219
|
+
await Bun.file(
|
|
220
|
+
path.join(cwdPath, ".akan/cache", path.relative(path.join(cwdPath, ".akan/artifact"), first.files[0])),
|
|
221
|
+
).exists(),
|
|
222
|
+
).toBe(true);
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
test("still writes the fonts when the cache folder cannot be written", async () => {
|
|
226
|
+
const { app, cwdPath } = await makeApp(layoutWith());
|
|
227
|
+
const cacheDir = path.join(cwdPath, ".akan/cache");
|
|
228
|
+
await mkdir(cacheDir, { recursive: true });
|
|
229
|
+
await chmod(cacheDir, 0o500);
|
|
230
|
+
try {
|
|
231
|
+
const result = await optimize(app);
|
|
232
|
+
expect(result.files).toHaveLength(1);
|
|
233
|
+
expect(await Bun.file(result.files[0]).exists()).toBe(true);
|
|
234
|
+
} finally {
|
|
235
|
+
await chmod(cacheDir, 0o700);
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
test("resubsets when the source font is replaced by one of the same size and mtime", async () => {
|
|
240
|
+
const { app, cwdPath, reusedCount } = await makeApp(`
|
|
241
|
+
export const fonts = [{ name: "Gilda", paths: [{ src: "/fonts/Gilda.ttf", weight: 400 }] }];
|
|
242
|
+
export default function Layout() {
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
`);
|
|
246
|
+
const sourcePath = path.join(cwdPath, "public/fonts/Gilda.ttf");
|
|
247
|
+
const source = await Bun.file(path.resolve(path.dirname(SOURCE_FONT), "GildaDisplay-Regular.ttf")).bytes();
|
|
248
|
+
await Bun.write(sourcePath, source);
|
|
249
|
+
await optimize(app);
|
|
250
|
+
const { mtime } = await stat(sourcePath);
|
|
251
|
+
|
|
252
|
+
await Bun.write(sourcePath, renameSfntTable(source.slice(), "OS/2", "XS/2"));
|
|
253
|
+
await utimes(sourcePath, mtime, mtime);
|
|
254
|
+
await optimize(app);
|
|
255
|
+
expect(reusedCount()).toBe(0);
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
test("leaves a temporary file another start is still writing in the cache folder", async () => {
|
|
259
|
+
const { app, cwdPath } = await makeApp(layoutWith());
|
|
260
|
+
await optimize(app);
|
|
261
|
+
const tempPath = path.join(cwdPath, ".akan/cache/fonts/Assistant-400-normal-abc.woff2.123.abc.tmp");
|
|
262
|
+
await Bun.write(tempPath, "in progress");
|
|
263
|
+
|
|
264
|
+
await writeFile(path.join(cwdPath, "page/_layout.tsx"), layoutWith(`\n className: "font-brand",`));
|
|
265
|
+
await optimize(app);
|
|
266
|
+
expect(await Bun.file(tempPath).exists()).toBe(true);
|
|
267
|
+
});
|
|
268
|
+
|
|
128
269
|
test("skips route files that never mention fonts", async () => {
|
|
129
270
|
const { app } = await makeApp("export default function Layout() {\n return null;\n}\n");
|
|
130
271
|
const result = await optimize(app);
|