@hewel/zgui-build 0.0.0-stage → 0.1.2
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/LICENSE +202 -0
- package/NOTICE +95 -0
- package/README.md +86 -2
- package/THIRD_PARTY_NOTICES.md +37 -0
- package/package.json +64 -4
- package/sdk/Cargo.lock +6814 -0
- package/sdk/LICENSE +202 -0
- package/sdk/NOTICE +95 -0
- package/sdk/THIRD_PARTY_NOTICES.md +37 -0
- package/sdk/bun.lock +539 -0
- package/sdk/package.json +60 -0
- package/sdk/preview/fixtures/empty.json +6 -0
- package/sdk/preview/fonts/NotoSans-LICENSE.txt +94 -0
- package/sdk/preview/fonts/NotoSans-Regular.ttf +0 -0
- package/sdk/preview/fonts/NotoSansCJK-LICENSE.txt +92 -0
- package/sdk/preview/fonts/NotoSansCJKsc-Regular.otf +0 -0
- package/sdk/preview/fonts/PROVENANCE.md +25 -0
- package/sdk/preview/fonts/fonts.json +60 -0
- package/sdk/preview/profiles/default.json +32 -0
- package/sdk/preview/scenarios/counter-capture.json +9 -0
- package/sdk/smoke/solid-smoke.ts +53 -0
- package/sdk/smoke/tsconfig.smoke.json +21 -0
- package/src/application.ts +70 -0
- package/src/build.ts +1097 -0
- package/src/check-smoke.ts +8 -0
- package/src/compiler.ts +701 -0
- package/src/css.ts +337 -0
- package/src/dev.ts +164 -0
- package/src/host.ts +578 -0
- package/src/identity.ts +60 -0
- package/src/index.ts +27 -0
- package/src/internal.ts +20 -0
- package/src/native-process.ts +178 -0
- package/src/package-app.ts +178 -0
- package/src/preview-check.ts +267 -0
- package/src/preview.ts +69 -0
- package/src/public-api.d.ts +265 -0
- package/src/publication.ts +108 -0
- package/src/sdk-packages.ts +376 -0
- package/src/sdk.ts +11 -0
- package/src/smoke.ts +99 -0
- package/src/sync-ui-resources.ts +171 -0
- package/src/typecheck.ts +177 -0
- package/src/validate.ts +1085 -0
- package/src/watch.ts +531 -0
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { existsSync, type Dirent } from "node:fs";
|
|
3
|
+
import {
|
|
4
|
+
copyFile,
|
|
5
|
+
mkdir,
|
|
6
|
+
open,
|
|
7
|
+
readdir,
|
|
8
|
+
readFile,
|
|
9
|
+
rename,
|
|
10
|
+
rm,
|
|
11
|
+
rmdir,
|
|
12
|
+
stat,
|
|
13
|
+
utimes,
|
|
14
|
+
writeFile,
|
|
15
|
+
} from "node:fs/promises";
|
|
16
|
+
import { createRequire } from "node:module";
|
|
17
|
+
import os from "node:os";
|
|
18
|
+
import path from "node:path";
|
|
19
|
+
import { extractStaticComponentSheet } from "./compiler.ts";
|
|
20
|
+
import { packagedSdkRoot, workspaceRoot } from "./sdk.ts";
|
|
21
|
+
|
|
22
|
+
export type SdkPackageName = "ui" | "services";
|
|
23
|
+
|
|
24
|
+
interface SdkPackageSpec {
|
|
25
|
+
readonly name: SdkPackageName;
|
|
26
|
+
readonly dir: string;
|
|
27
|
+
readonly handwrittenDts: readonly string[];
|
|
28
|
+
readonly verifyGeneratedStyles: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const SDK_PACKAGE_SPECS: Readonly<Record<SdkPackageName, SdkPackageSpec>> = {
|
|
32
|
+
ui: {
|
|
33
|
+
name: "ui",
|
|
34
|
+
dir: "packages/ui",
|
|
35
|
+
handwrittenDts: ["solid-native.d.ts", "events.d.ts", "styles.d.ts"],
|
|
36
|
+
verifyGeneratedStyles: true,
|
|
37
|
+
},
|
|
38
|
+
services: {
|
|
39
|
+
name: "services",
|
|
40
|
+
dir: "packages/services",
|
|
41
|
+
handwrittenDts: ["solid.d.ts"],
|
|
42
|
+
verifyGeneratedStyles: false,
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const ALL_SDK_PACKAGES: readonly SdkPackageName[] = ["ui", "services"];
|
|
47
|
+
|
|
48
|
+
const compilerCli = path.join(
|
|
49
|
+
path.dirname(createRequire(import.meta.url).resolve("tsrs/package.json")),
|
|
50
|
+
"bin/tsrs",
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
const inFlight = new Map<string, Promise<void>>();
|
|
54
|
+
|
|
55
|
+
export function hasCheckoutSdkSources(root: string): boolean {
|
|
56
|
+
const resolved = path.resolve(root);
|
|
57
|
+
if (resolved === packagedSdkRoot) return false;
|
|
58
|
+
return ALL_SDK_PACKAGES.every(pkg =>
|
|
59
|
+
existsSync(path.join(resolved, SDK_PACKAGE_SPECS[pkg].dir, "tsconfig.build.json")),
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async function listFilesRecursive(dir: string, prefix = ""): Promise<string[]> {
|
|
64
|
+
let entries: Dirent[];
|
|
65
|
+
try {
|
|
66
|
+
entries = await readdir(dir, { withFileTypes: true });
|
|
67
|
+
} catch (error) {
|
|
68
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return [];
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
const files: string[] = [];
|
|
72
|
+
for (const entry of entries) {
|
|
73
|
+
const rel = prefix ? `${prefix}/${entry.name}` : entry.name;
|
|
74
|
+
const full = path.join(dir, entry.name);
|
|
75
|
+
if (entry.isDirectory()) {
|
|
76
|
+
files.push(...(await listFilesRecursive(full, rel)));
|
|
77
|
+
} else if (entry.isFile()) {
|
|
78
|
+
files.push(rel);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return files;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async function newestInputMtimeMs(pkgDir: string): Promise<number> {
|
|
85
|
+
let newest = 0;
|
|
86
|
+
for (const cfg of ["tsconfig.json", "tsconfig.build.json"]) {
|
|
87
|
+
const full = path.join(pkgDir, cfg);
|
|
88
|
+
if (existsSync(full)) {
|
|
89
|
+
const info = await stat(full);
|
|
90
|
+
if (info.mtimeMs > newest) newest = info.mtimeMs;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const srcDir = path.join(pkgDir, "src");
|
|
94
|
+
for (const rel of await listFilesRecursive(srcDir)) {
|
|
95
|
+
const info = await stat(path.join(srcDir, rel));
|
|
96
|
+
if (info.mtimeMs > newest) newest = info.mtimeMs;
|
|
97
|
+
}
|
|
98
|
+
return newest;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async function isPackageDistFresh(pkgDir: string, spec: SdkPackageSpec): Promise<boolean> {
|
|
102
|
+
const distDir = path.join(pkgDir, "dist");
|
|
103
|
+
const required = ["index.js", "index.d.ts", ...spec.handwrittenDts];
|
|
104
|
+
let stampMtime = Number.POSITIVE_INFINITY;
|
|
105
|
+
for (const rel of required) {
|
|
106
|
+
const full = path.join(distDir, rel);
|
|
107
|
+
try {
|
|
108
|
+
const info = await stat(full);
|
|
109
|
+
if (rel === "index.js" || rel === "index.d.ts") {
|
|
110
|
+
stampMtime = Math.min(stampMtime, info.mtimeMs);
|
|
111
|
+
}
|
|
112
|
+
} catch {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if (!Number.isFinite(stampMtime)) return false;
|
|
117
|
+
|
|
118
|
+
if (spec.verifyGeneratedStyles) {
|
|
119
|
+
const srcGenDir = path.join(pkgDir, "src/components/generated");
|
|
120
|
+
const distGenDir = path.join(distDir, "components/generated");
|
|
121
|
+
try {
|
|
122
|
+
const srcStyles = (await readdir(srcGenDir)).filter(name => name.endsWith("Style.ts"));
|
|
123
|
+
const distStyles = (await readdir(distGenDir)).filter(name => name.endsWith("Style.js"));
|
|
124
|
+
if (srcStyles.length === 0 || distStyles.length < srcStyles.length) return false;
|
|
125
|
+
} catch {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const newestInput = await newestInputMtimeMs(pkgDir);
|
|
131
|
+
return stampMtime >= newestInput;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async function removeEmptyDirs(dir: string): Promise<void> {
|
|
135
|
+
let entries: Dirent[];
|
|
136
|
+
try {
|
|
137
|
+
entries = await readdir(dir, { withFileTypes: true });
|
|
138
|
+
} catch {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
for (const entry of entries) {
|
|
142
|
+
if (!entry.isDirectory()) continue;
|
|
143
|
+
const child = path.join(dir, entry.name);
|
|
144
|
+
await removeEmptyDirs(child);
|
|
145
|
+
try {
|
|
146
|
+
const remaining = await readdir(child);
|
|
147
|
+
if (remaining.length === 0) await rmdir(child);
|
|
148
|
+
} catch {
|
|
149
|
+
// Ignore concurrent deletion.
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async function syncDirectoryInPlace(stagingDir: string, distDir: string): Promise<void> {
|
|
155
|
+
await mkdir(distDir, { recursive: true });
|
|
156
|
+
const stagedFiles = await listFilesRecursive(stagingDir);
|
|
157
|
+
const stagedSet = new Set(stagedFiles);
|
|
158
|
+
|
|
159
|
+
for (const rel of stagedFiles) {
|
|
160
|
+
const srcPath = path.join(stagingDir, rel);
|
|
161
|
+
const destPath = path.join(distDir, rel);
|
|
162
|
+
const nextBytes = await readFile(srcPath);
|
|
163
|
+
let unchanged = false;
|
|
164
|
+
try {
|
|
165
|
+
const currentBytes = await readFile(destPath);
|
|
166
|
+
unchanged = currentBytes.equals(nextBytes);
|
|
167
|
+
} catch {
|
|
168
|
+
unchanged = false;
|
|
169
|
+
}
|
|
170
|
+
if (unchanged) continue;
|
|
171
|
+
await mkdir(path.dirname(destPath), { recursive: true });
|
|
172
|
+
const tmpPath = `${destPath}.tmp-${process.pid}-${randomUUID()}`;
|
|
173
|
+
await writeFile(tmpPath, nextBytes);
|
|
174
|
+
await rename(tmpPath, destPath);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const existingFiles = await listFilesRecursive(distDir);
|
|
178
|
+
for (const rel of existingFiles) {
|
|
179
|
+
if (stagedSet.has(rel)) continue;
|
|
180
|
+
if (path.basename(rel).includes(".tmp-")) continue;
|
|
181
|
+
await rm(path.join(distDir, rel), { force: true });
|
|
182
|
+
}
|
|
183
|
+
await removeEmptyDirs(distDir);
|
|
184
|
+
|
|
185
|
+
const now = new Date();
|
|
186
|
+
for (const sentinel of ["index.js", "index.d.ts"]) {
|
|
187
|
+
const target = path.join(distDir, sentinel);
|
|
188
|
+
if (existsSync(target)) {
|
|
189
|
+
await utimes(target, now, now).catch(() => {});
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async function verifyGeneratedStyleSheets(stagingDir: string): Promise<number> {
|
|
195
|
+
const genDir = path.join(stagingDir, "components/generated");
|
|
196
|
+
const entries = (await readdir(genDir)).filter(name => name.endsWith("Style.js")).sort();
|
|
197
|
+
if (entries.length === 0) {
|
|
198
|
+
throw new Error(`No generated component style modules emitted under ${genDir}`);
|
|
199
|
+
}
|
|
200
|
+
for (const filename of entries) {
|
|
201
|
+
const fullPath = path.join(genDir, filename);
|
|
202
|
+
const source = await readFile(fullPath, "utf8");
|
|
203
|
+
const sheet = extractStaticComponentSheet(source, fullPath);
|
|
204
|
+
if (!sheet) {
|
|
205
|
+
throw new Error(
|
|
206
|
+
`Generated component style ${filename} failed single-statement static sheet hoisting verification`,
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return entries.length;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
async function withSdkBuildLock<T>(root: string, fn: () => Promise<T>): Promise<T> {
|
|
214
|
+
const key = createHash("sha256").update(path.resolve(root)).digest("hex").slice(0, 16);
|
|
215
|
+
const lockPath = path.join(os.tmpdir(), `zgui-sdk-build-${key}.lock`);
|
|
216
|
+
const deadline = Date.now() + 60_000;
|
|
217
|
+
let handle: Awaited<ReturnType<typeof open>> | undefined;
|
|
218
|
+
|
|
219
|
+
while (!handle) {
|
|
220
|
+
try {
|
|
221
|
+
handle = await open(lockPath, "wx");
|
|
222
|
+
await handle.writeFile(`${process.pid}\n`);
|
|
223
|
+
} catch (error) {
|
|
224
|
+
if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error;
|
|
225
|
+
try {
|
|
226
|
+
const info = await stat(lockPath);
|
|
227
|
+
const ownerPid = Number.parseInt((await readFile(lockPath, "utf8")).trim(), 10);
|
|
228
|
+
let ownerAlive = false;
|
|
229
|
+
if (Number.isInteger(ownerPid) && ownerPid > 0) {
|
|
230
|
+
try {
|
|
231
|
+
process.kill(ownerPid, 0);
|
|
232
|
+
ownerAlive = true;
|
|
233
|
+
} catch {
|
|
234
|
+
ownerAlive = false;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
if (!ownerAlive || Date.now() - info.mtimeMs > 60_000) {
|
|
238
|
+
await rm(lockPath, { force: true });
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
} catch {
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
if (Date.now() > deadline) {
|
|
245
|
+
throw new Error(`Timed out waiting for SDK build lock ${lockPath}`);
|
|
246
|
+
}
|
|
247
|
+
await Bun.sleep(25);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
try {
|
|
252
|
+
return await fn();
|
|
253
|
+
} finally {
|
|
254
|
+
await handle.close().catch(() => {});
|
|
255
|
+
await rm(lockPath, { force: true }).catch(() => {});
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
async function compileSinglePackage(
|
|
260
|
+
root: string,
|
|
261
|
+
spec: SdkPackageSpec,
|
|
262
|
+
{ silent = false }: { silent?: boolean } = {},
|
|
263
|
+
): Promise<void> {
|
|
264
|
+
const pkgDir = path.join(root, spec.dir);
|
|
265
|
+
const tsconfigPath = path.join(pkgDir, "tsconfig.build.json");
|
|
266
|
+
if (!existsSync(tsconfigPath)) {
|
|
267
|
+
throw new Error(`Missing SDK build tsconfig: ${tsconfigPath}`);
|
|
268
|
+
}
|
|
269
|
+
const stagingDir = path.join(pkgDir, `.dist-staging-${process.pid}-${randomUUID()}`);
|
|
270
|
+
try {
|
|
271
|
+
const child = Bun.spawn(
|
|
272
|
+
[
|
|
273
|
+
process.execPath,
|
|
274
|
+
compilerCli,
|
|
275
|
+
"--project",
|
|
276
|
+
tsconfigPath,
|
|
277
|
+
"--outDir",
|
|
278
|
+
stagingDir,
|
|
279
|
+
"--pretty",
|
|
280
|
+
"false",
|
|
281
|
+
],
|
|
282
|
+
{
|
|
283
|
+
cwd: pkgDir,
|
|
284
|
+
stdin: "ignore",
|
|
285
|
+
stdout: "pipe",
|
|
286
|
+
stderr: "pipe",
|
|
287
|
+
},
|
|
288
|
+
);
|
|
289
|
+
const [code, stdout, stderr] = await Promise.all([
|
|
290
|
+
child.exited,
|
|
291
|
+
new Response(child.stdout).text(),
|
|
292
|
+
new Response(child.stderr).text(),
|
|
293
|
+
]);
|
|
294
|
+
if (code !== 0 || child.signalCode) {
|
|
295
|
+
const detail = `${stdout}${stderr}`.trim();
|
|
296
|
+
throw new Error(
|
|
297
|
+
`SDK build failed for ${spec.dir} (exit ${child.signalCode ?? code})${detail ? `:\n${detail}` : ""}`,
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
for (const rel of spec.handwrittenDts) {
|
|
302
|
+
const srcPath = path.join(pkgDir, "src", rel);
|
|
303
|
+
const destPath = path.join(stagingDir, rel);
|
|
304
|
+
await mkdir(path.dirname(destPath), { recursive: true });
|
|
305
|
+
await copyFile(srcPath, destPath);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
let verifiedSheets = 0;
|
|
309
|
+
if (spec.verifyGeneratedStyles) {
|
|
310
|
+
verifiedSheets = await verifyGeneratedStyleSheets(stagingDir);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
await syncDirectoryInPlace(stagingDir, path.join(pkgDir, "dist"));
|
|
314
|
+
if (!silent) {
|
|
315
|
+
const detail = verifiedSheets > 0 ? ` (${verifiedSheets} static style sheets verified)` : "";
|
|
316
|
+
console.log(`Built ${spec.dir}/dist${detail}`);
|
|
317
|
+
}
|
|
318
|
+
} finally {
|
|
319
|
+
await rm(stagingDir, { recursive: true, force: true }).catch(() => {});
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
export async function buildSdkPackages({
|
|
324
|
+
root = workspaceRoot,
|
|
325
|
+
packages = ALL_SDK_PACKAGES,
|
|
326
|
+
silent = false,
|
|
327
|
+
}: {
|
|
328
|
+
root?: string;
|
|
329
|
+
packages?: readonly SdkPackageName[];
|
|
330
|
+
silent?: boolean;
|
|
331
|
+
} = {}): Promise<void> {
|
|
332
|
+
const resolvedRoot = path.resolve(root);
|
|
333
|
+
const uniquePackages = [...new Set(packages)];
|
|
334
|
+
await withSdkBuildLock(resolvedRoot, async () => {
|
|
335
|
+
for (const name of uniquePackages) {
|
|
336
|
+
await compileSinglePackage(resolvedRoot, SDK_PACKAGE_SPECS[name], { silent });
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
export async function ensureSdkPackagesBuilt(
|
|
342
|
+
root: string = workspaceRoot,
|
|
343
|
+
{ silent = true }: { silent?: boolean } = {},
|
|
344
|
+
): Promise<void> {
|
|
345
|
+
const resolvedRoot = path.resolve(root);
|
|
346
|
+
if (!hasCheckoutSdkSources(resolvedRoot)) return;
|
|
347
|
+
|
|
348
|
+
const existing = inFlight.get(resolvedRoot);
|
|
349
|
+
if (existing) return existing;
|
|
350
|
+
|
|
351
|
+
const run = (async () => {
|
|
352
|
+
const stale: SdkPackageName[] = [];
|
|
353
|
+
for (const name of ALL_SDK_PACKAGES) {
|
|
354
|
+
const spec = SDK_PACKAGE_SPECS[name];
|
|
355
|
+
if (!(await isPackageDistFresh(path.join(resolvedRoot, spec.dir), spec))) {
|
|
356
|
+
stale.push(name);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
if (stale.length === 0) return;
|
|
360
|
+
|
|
361
|
+
await withSdkBuildLock(resolvedRoot, async () => {
|
|
362
|
+
for (const name of stale) {
|
|
363
|
+
const spec = SDK_PACKAGE_SPECS[name];
|
|
364
|
+
if (await isPackageDistFresh(path.join(resolvedRoot, spec.dir), spec)) continue;
|
|
365
|
+
await compileSinglePackage(resolvedRoot, spec, { silent });
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
})();
|
|
369
|
+
|
|
370
|
+
inFlight.set(resolvedRoot, run);
|
|
371
|
+
try {
|
|
372
|
+
await run;
|
|
373
|
+
} finally {
|
|
374
|
+
if (inFlight.get(resolvedRoot) === run) inFlight.delete(resolvedRoot);
|
|
375
|
+
}
|
|
376
|
+
}
|
package/src/sdk.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
|
|
5
|
+
const checkoutRoot = fileURLToPath(new URL("../../../", import.meta.url));
|
|
6
|
+
export const packagedSdkRoot = fileURLToPath(new URL("../sdk", import.meta.url));
|
|
7
|
+
export const workspaceRoot =
|
|
8
|
+
import.meta.dir === path.join(checkoutRoot, "packages/build/src") &&
|
|
9
|
+
existsSync(path.join(checkoutRoot, "Cargo.toml"))
|
|
10
|
+
? checkoutRoot
|
|
11
|
+
: packagedSdkRoot;
|
package/src/smoke.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { realpathSync } from "node:fs";
|
|
2
|
+
import { mkdir, mkdtemp } from "node:fs/promises";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { bundle, dependencyVersions, workspaceRoot as root } from "./build.ts";
|
|
6
|
+
import { sha256 } from "./identity.ts";
|
|
7
|
+
import { packagedSdkRoot } from "./sdk.ts";
|
|
8
|
+
|
|
9
|
+
export function resolveSmokeTsconfigPath(sdkRoot: string = root): string {
|
|
10
|
+
const resolved = path.resolve(sdkRoot);
|
|
11
|
+
if (resolved === packagedSdkRoot) {
|
|
12
|
+
return path.join(packagedSdkRoot, "smoke/tsconfig.smoke.json");
|
|
13
|
+
}
|
|
14
|
+
return path.join(resolved, "packages/build/tsconfig.smoke.json");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function buildSmoke({
|
|
18
|
+
outDir,
|
|
19
|
+
}: {
|
|
20
|
+
outDir?: string;
|
|
21
|
+
} = {}): Promise<{ outDir: string; bundlePath: string }> {
|
|
22
|
+
const installed = path.resolve(root) === packagedSdkRoot;
|
|
23
|
+
const fixturePath = installed
|
|
24
|
+
? path.join(packagedSdkRoot, "smoke/solid-smoke.ts")
|
|
25
|
+
: path.join(root, "tests/fixtures/solid-smoke.ts");
|
|
26
|
+
const appDirectory = installed ? path.resolve(packagedSdkRoot, "..") : root;
|
|
27
|
+
const outfile = "solid-smoke.mjs";
|
|
28
|
+
const result = await bundle(fixturePath, {
|
|
29
|
+
root,
|
|
30
|
+
appDirectory,
|
|
31
|
+
hostEntry: false,
|
|
32
|
+
filename: outfile,
|
|
33
|
+
});
|
|
34
|
+
const realFixture = realpathSync(fixturePath);
|
|
35
|
+
const inputs = result.graph.modules.map(filename => path.relative(root, filename));
|
|
36
|
+
const resolvedModules = result.graph.modules.map(filename => {
|
|
37
|
+
try {
|
|
38
|
+
return realpathSync(filename);
|
|
39
|
+
} catch {
|
|
40
|
+
return path.resolve(filename);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
// The smoke fixture must contain only itself and the production Solid/Signals runtime modules.
|
|
44
|
+
if (
|
|
45
|
+
!resolvedModules.includes(realFixture) ||
|
|
46
|
+
!resolvedModules.every(
|
|
47
|
+
modulePath =>
|
|
48
|
+
modulePath === realFixture ||
|
|
49
|
+
modulePath.includes("/solid-js/dist/") ||
|
|
50
|
+
modulePath.includes("/@solidjs/signals/dist/prod/"),
|
|
51
|
+
) ||
|
|
52
|
+
!result.solidRuntime
|
|
53
|
+
) {
|
|
54
|
+
throw new Error(`Unexpected smoke module graph: ${JSON.stringify(inputs)}`);
|
|
55
|
+
}
|
|
56
|
+
const output = result.graph.chunks[0];
|
|
57
|
+
if (!output || output.exports.join(",") !== "runSmoke") {
|
|
58
|
+
throw new Error("Smoke must be one self-contained ESM module exporting runSmoke");
|
|
59
|
+
}
|
|
60
|
+
const packageJson = (await Bun.file(path.join(root, "package.json")).json()) as {
|
|
61
|
+
packageManager: string;
|
|
62
|
+
};
|
|
63
|
+
const metadata = {
|
|
64
|
+
bun: Bun.version,
|
|
65
|
+
packageManager: packageJson.packageManager,
|
|
66
|
+
packages: dependencyVersions({ root, appDirectory, modules: result.graph.modules }),
|
|
67
|
+
conditions: ["browser", "production"],
|
|
68
|
+
target: "es2022",
|
|
69
|
+
lockfile: {
|
|
70
|
+
path: "bun.lock",
|
|
71
|
+
digest: sha256(await Bun.file(path.join(root, "bun.lock")).bytes()),
|
|
72
|
+
},
|
|
73
|
+
solidRuntime: result.solidRuntime,
|
|
74
|
+
inputs,
|
|
75
|
+
exports: output.exports,
|
|
76
|
+
imports: output.imports,
|
|
77
|
+
};
|
|
78
|
+
const targetDir = outDir
|
|
79
|
+
? path.resolve(outDir)
|
|
80
|
+
: installed
|
|
81
|
+
? await mkdtemp(path.join(os.tmpdir(), "zgui-smoke-"))
|
|
82
|
+
: path.join(root, "target");
|
|
83
|
+
await mkdir(targetDir, { recursive: true });
|
|
84
|
+
for (const [filename, bytes] of result.files) {
|
|
85
|
+
await Bun.write(path.join(targetDir, filename), bytes);
|
|
86
|
+
}
|
|
87
|
+
await Bun.write(
|
|
88
|
+
path.join(targetDir, "solid-smoke.meta.json"),
|
|
89
|
+
JSON.stringify(result.graph, null, 2) + "\n",
|
|
90
|
+
);
|
|
91
|
+
await Bun.write(
|
|
92
|
+
path.join(targetDir, "solid-smoke.build.json"),
|
|
93
|
+
JSON.stringify(metadata, null, 2) + "\n",
|
|
94
|
+
);
|
|
95
|
+
const displayTarget = installed ? path.join(targetDir, outfile) : `target/${outfile}`;
|
|
96
|
+
console.log(`Built ${displayTarget}; one reactive Solid runtime; no external imports`);
|
|
97
|
+
console.log(JSON.stringify(metadata, null, 2));
|
|
98
|
+
return { outDir: targetDir, bundlePath: path.join(targetDir, outfile) };
|
|
99
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { mkdir } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
|
|
5
|
+
const REVISION = "ee29c7dc66206eb04ba7bcf514058a278c9070ba";
|
|
6
|
+
const workspaceRoot = fileURLToPath(new URL("../../../", import.meta.url));
|
|
7
|
+
|
|
8
|
+
export interface ResourceSnapshot {
|
|
9
|
+
revision: string;
|
|
10
|
+
styles: Record<string, { className: string; css: string }>;
|
|
11
|
+
themes: Record<string, { light: string; dark: string }>;
|
|
12
|
+
tokens: string[];
|
|
13
|
+
icons: Record<string, { name: string; d: string; viewBox: number }>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function assertRecord(value: unknown, label: string): Record<string, unknown> {
|
|
17
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
18
|
+
throw new Error(`Snapshot ${label} must be an object`);
|
|
19
|
+
}
|
|
20
|
+
return value as Record<string, unknown>;
|
|
21
|
+
}
|
|
22
|
+
function assertString(value: unknown, label: string): string {
|
|
23
|
+
if (typeof value !== "string") throw new Error(`Snapshot ${label} must be a string`);
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
function validateSnapshot(value: unknown): asserts value is ResourceSnapshot {
|
|
27
|
+
const snapshot = value as Record<string, unknown>;
|
|
28
|
+
if (snapshot.revision !== REVISION) {
|
|
29
|
+
throw new Error(
|
|
30
|
+
`Snapshot revision ${String(snapshot.revision)} does not match pinned ${REVISION}`,
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
const styles = assertRecord(snapshot.styles, "styles");
|
|
34
|
+
const themes = assertRecord(snapshot.themes, "themes");
|
|
35
|
+
if (
|
|
36
|
+
!Array.isArray(snapshot.tokens) ||
|
|
37
|
+
snapshot.tokens.some((token: unknown) => typeof token !== "string")
|
|
38
|
+
) {
|
|
39
|
+
throw new Error("Snapshot tokens must be a string array");
|
|
40
|
+
}
|
|
41
|
+
const icons = assertRecord(snapshot.icons, "icons");
|
|
42
|
+
for (const [name, value] of Object.entries(styles)) {
|
|
43
|
+
if (!/^[A-Z][A-Za-z0-9]*$/.test(name)) throw new Error(`Unexpected style export name: ${name}`);
|
|
44
|
+
const sheet = assertRecord(value, `styles.${name}`);
|
|
45
|
+
assertString(sheet.className, `styles.${name}.className`);
|
|
46
|
+
assertString(sheet.css, `styles.${name}.css`);
|
|
47
|
+
}
|
|
48
|
+
for (const [name, value] of Object.entries(themes)) {
|
|
49
|
+
const theme = assertRecord(value, `themes.${name}`);
|
|
50
|
+
assertString(theme.light, `themes.${name}.light`);
|
|
51
|
+
assertString(theme.dark, `themes.${name}.dark`);
|
|
52
|
+
}
|
|
53
|
+
for (const [name, icon] of Object.entries(icons)) {
|
|
54
|
+
if (!/^[A-Z][A-Z0-9_]*$/.test(name)) throw new Error(`Unexpected icon export name: ${name}`);
|
|
55
|
+
const data = assertRecord(icon, `icons.${name}`);
|
|
56
|
+
assertString(data.name, `icons.${name}.name`);
|
|
57
|
+
assertString(data.d, `icons.${name}.d`);
|
|
58
|
+
if (typeof data.viewBox !== "number" || !Number.isFinite(data.viewBox) || data.viewBox <= 0) {
|
|
59
|
+
throw new Error(`Snapshot icons.${name}.viewBox must be a positive number`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function adaptUpstreamCss(name: string, className: string, css: string): string {
|
|
65
|
+
switch (name) {
|
|
66
|
+
case "TextareaStyle":
|
|
67
|
+
return css.replace("overflow: hidden;", "overflow-x: hidden;\n overflow-y: auto;");
|
|
68
|
+
case "TabsStyle":
|
|
69
|
+
return css
|
|
70
|
+
.replaceAll(
|
|
71
|
+
'.zui-tabs__list[data-variant="default"] .zui-tabs__trigger',
|
|
72
|
+
'.zui-tabs__trigger[data-variant="default"]',
|
|
73
|
+
)
|
|
74
|
+
.replaceAll(
|
|
75
|
+
'.zui-tabs__list[data-variant="line"] .zui-tabs__trigger',
|
|
76
|
+
'.zui-tabs__trigger[data-variant="line"]',
|
|
77
|
+
)
|
|
78
|
+
.replaceAll(
|
|
79
|
+
`.${className}[data-orientation="vertical"] .zui-tabs__list`,
|
|
80
|
+
'.zui-tabs__list[data-orientation="vertical"]',
|
|
81
|
+
)
|
|
82
|
+
.replaceAll(
|
|
83
|
+
`.${className}[data-orientation="vertical"] .zui-tabs__trigger`,
|
|
84
|
+
'.zui-tabs__trigger[data-orientation="vertical"]',
|
|
85
|
+
)
|
|
86
|
+
.replaceAll(
|
|
87
|
+
`.${className}[data-orientation="horizontal"] .zui-tabs__trigger`,
|
|
88
|
+
'.zui-tabs__trigger[data-orientation="horizontal"]',
|
|
89
|
+
);
|
|
90
|
+
case "AccordionStyle":
|
|
91
|
+
return css
|
|
92
|
+
.replaceAll(".zui-accordion__content .zui-collapsible__measure", ".zui-accordion__measure")
|
|
93
|
+
.replaceAll(
|
|
94
|
+
'.zui-accordion__trigger[data-state="open"] .zui-accordion__chevron',
|
|
95
|
+
'.zui-accordion__chevron[data-state="open"]',
|
|
96
|
+
);
|
|
97
|
+
case "CollapsibleStyle":
|
|
98
|
+
return `${css}\n.zui-collapsible__content[data-collapsed] { visibility: hidden; }`;
|
|
99
|
+
default:
|
|
100
|
+
return css;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export async function syncUiResources(
|
|
105
|
+
snapshotPath: string,
|
|
106
|
+
outDir = path.join(workspaceRoot, "packages/ui/src/components/generated"),
|
|
107
|
+
): Promise<number> {
|
|
108
|
+
const snapshot: unknown = await Bun.file(path.resolve(snapshotPath)).json();
|
|
109
|
+
validateSnapshot(snapshot);
|
|
110
|
+
await mkdir(outDir, { recursive: true });
|
|
111
|
+
|
|
112
|
+
const literal = (value: string) => JSON.stringify(value);
|
|
113
|
+
const outputs = new Map<string, string>();
|
|
114
|
+
for (const name of Object.keys(snapshot.styles).sort()) {
|
|
115
|
+
const sheet = snapshot.styles[name];
|
|
116
|
+
const adaptedCss = adaptUpstreamCss(name, sheet.className, sheet.css);
|
|
117
|
+
outputs.set(
|
|
118
|
+
`${name}.ts`,
|
|
119
|
+
`export const sheet = { name: ${literal(name)}, className: ${literal(sheet.className)}, css: ${literal(adaptedCss)} } as const;\n`,
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
const themeEntries = Object.keys(snapshot.themes)
|
|
123
|
+
.sort()
|
|
124
|
+
.map(name => `${JSON.stringify(name)}: ${JSON.stringify(snapshot.themes[name])}`)
|
|
125
|
+
.join(", ");
|
|
126
|
+
outputs.set(
|
|
127
|
+
"themes.ts",
|
|
128
|
+
[
|
|
129
|
+
`export const upstreamRevision = ${literal(REVISION)} as const;`,
|
|
130
|
+
`export const themes = { ${themeEntries} } as const;`,
|
|
131
|
+
`export const themeTokens = ${JSON.stringify(snapshot.tokens)} as const;`,
|
|
132
|
+
"",
|
|
133
|
+
].join("\n"),
|
|
134
|
+
);
|
|
135
|
+
const iconExports = Object.keys(snapshot.icons)
|
|
136
|
+
.sort()
|
|
137
|
+
.map(name => {
|
|
138
|
+
const icon = snapshot.icons[name];
|
|
139
|
+
return `export const ${name} = { name: ${literal(icon.name)}, viewBox: ${icon.viewBox}, d: ${literal(icon.d)} } as const;`;
|
|
140
|
+
})
|
|
141
|
+
.join("\n");
|
|
142
|
+
outputs.set("icons.ts", `${iconExports}\n`);
|
|
143
|
+
|
|
144
|
+
for (const [filename, content] of [...outputs].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))) {
|
|
145
|
+
await Bun.write(path.join(outDir, filename), content);
|
|
146
|
+
}
|
|
147
|
+
return outputs.size;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (import.meta.main) {
|
|
151
|
+
const args = process.argv.slice(2);
|
|
152
|
+
let snapshotPath: string | undefined;
|
|
153
|
+
let outDir: string | undefined;
|
|
154
|
+
for (let i = 0; i < args.length; i++) {
|
|
155
|
+
if (args[i] === "--out") {
|
|
156
|
+
outDir = args[++i];
|
|
157
|
+
if (!outDir) throw new Error("--out requires a directory");
|
|
158
|
+
} else if (!snapshotPath) {
|
|
159
|
+
snapshotPath = args[i];
|
|
160
|
+
} else {
|
|
161
|
+
throw new Error(`Unexpected argument: ${args[i]}`);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
if (!snapshotPath) {
|
|
165
|
+
throw new Error(
|
|
166
|
+
"Usage: bun packages/build/src/sync-ui-resources.ts <snapshot.json> [--out DIR]",
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
const count = await syncUiResources(snapshotPath, outDir ? path.resolve(outDir) : undefined);
|
|
170
|
+
console.log(`Wrote ${count} generated files from ${snapshotPath}`);
|
|
171
|
+
}
|