@octane-xplat/build 0.0.1 → 0.20.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/LICENSE +21 -0
- package/README.md +4 -2
- package/dist/add-worker.js +66 -0
- package/dist/build-worker.js +276 -0
- package/dist/compatibility-check.js +153 -0
- package/dist/compatibility-data/compatibility.js +106 -0
- package/dist/compatibility-data/compatibility.json +305 -0
- package/dist/compatibility.js +229 -0
- package/dist/css-layers.js +63 -0
- package/dist/css-platform.js +230 -0
- package/dist/css-platform.types.d.ts +11 -0
- package/dist/dev-inspector.web.js +125 -0
- package/dist/dev-log-client.web.js +79 -0
- package/dist/dev-log-server.js +13 -0
- package/dist/doctor-checks.js +317 -0
- package/dist/doctor-tool.js +27 -0
- package/dist/env.js +3 -0
- package/dist/fmt-plan.js +149 -0
- package/dist/fonts-worker.js +31 -0
- package/dist/fonts.js +614 -0
- package/dist/inline-stylesheets.js +37 -0
- package/dist/linux/config.js +43 -0
- package/dist/linux/host/bridge-selftest.linux.js +255 -0
- package/dist/linux/host/gjs-host.js +1412 -0
- package/dist/linux/package.js +176 -0
- package/dist/linux-gtk/vite.d.ts +23 -0
- package/dist/linux-gtk/vite.js +194 -0
- package/dist/macos/config.js +141 -0
- package/dist/macos/doctor-checks.js +58 -0
- package/dist/macos/executables.js +39 -0
- package/dist/macos/jsc-host/runtime.js +134 -0
- package/dist/macos/native.js +287 -0
- package/dist/macos/runtime-package.js +30 -0
- package/dist/macos/tab-item.js +62 -0
- package/dist/macos/vite.d.ts +21 -0
- package/dist/macos/vite.js +111 -0
- package/dist/markdown.js +11 -0
- package/dist/mason-engine.js +20 -0
- package/dist/mcp/contracts/actions.js +133 -0
- package/dist/mcp/dev-bridge.js +363 -0
- package/dist/mcp/dev-socket.js +161 -0
- package/dist/mcp/dev-stack.js +134 -0
- package/dist/mcp/inspection/dom.web.js +46 -0
- package/dist/native-regex.js +123 -0
- package/dist/package-manager.js +106 -0
- package/dist/package-root.js +48 -0
- package/dist/patches.js +356 -0
- package/dist/route-config.js +53 -0
- package/dist/route-data.js +42 -0
- package/dist/route-generator.js +514 -0
- package/dist/route-path.js +29 -0
- package/dist/route-source.js +67 -0
- package/dist/targets.js +38 -0
- package/dist/tsconfig-envs.js +287 -0
- package/dist/tsrx-leaf.js +57 -0
- package/dist/typecheck-globals/macos.d.ts +21 -0
- package/dist/typecheck-globals/native.d.ts +26 -0
- package/dist/ui/src/markdown/markdown-ast.js +33 -0
- package/dist/ui/src/markdown/markdown-character-references.js +79 -0
- package/dist/ui/src/markdown/markdown-parse.js +1930 -0
- package/dist/ui/src/markdown/markdown-plugins.js +564 -0
- package/dist/ui/src/markdown/markdown-url.js +17 -0
- package/dist/updates-worker.js +34 -0
- package/dist/upgrade-worker.js +63 -0
- package/dist/vite.d.ts +85 -0
- package/dist/vite.js +628 -0
- package/dist/workshop-worker.js +181 -0
- package/package.json +71 -9
package/dist/fonts.js
ADDED
|
@@ -0,0 +1,614 @@
|
|
|
1
|
+
import { packageManagerFor } from "./package-manager.js";
|
|
2
|
+
import { existsSync, lstatSync, mkdtempSync, readFileSync, readdirSync, realpathSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { basename, dirname, extname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
4
|
+
import { createHash } from "node:crypto";
|
|
5
|
+
import { spawnSync } from "node:child_process";
|
|
6
|
+
import { tmpdir } from "node:os";
|
|
7
|
+
import { inflateSync } from "node:zlib";
|
|
8
|
+
// ../cli/src/fonts.ts
|
|
9
|
+
var FONT_EXTENSIONS = /* @__PURE__ */ new Set([".ttf", ".otf"]);
|
|
10
|
+
var CONVERTIBLE_EXTENSIONS = /* @__PURE__ */ new Set([".woff", ".woff2"]);
|
|
11
|
+
var WEB_ENTRIES = [
|
|
12
|
+
"main.web.tsrx",
|
|
13
|
+
"main.web.tsx",
|
|
14
|
+
"main.web.ts",
|
|
15
|
+
"main.web.ts",
|
|
16
|
+
"main.web.js"
|
|
17
|
+
];
|
|
18
|
+
var MANIFEST = "fonts.json";
|
|
19
|
+
var GENERIC_FALLBACKS = {
|
|
20
|
+
sans: ["system-ui", "sans-serif"],
|
|
21
|
+
mono: ["ui-monospace", "monospace"],
|
|
22
|
+
serif: ["serif"]
|
|
23
|
+
};
|
|
24
|
+
var WEIGHT_UTILITY = {
|
|
25
|
+
100: "font-thin",
|
|
26
|
+
200: "font-extralight",
|
|
27
|
+
300: "font-light",
|
|
28
|
+
400: "font-normal",
|
|
29
|
+
500: "font-medium",
|
|
30
|
+
600: "font-semibold",
|
|
31
|
+
700: "font-bold",
|
|
32
|
+
800: "font-extrabold",
|
|
33
|
+
900: "font-black"
|
|
34
|
+
};
|
|
35
|
+
var managedStart = (token) => `/* xplat-fonts:${token}:start */`;
|
|
36
|
+
var managedEnd = (token) => `/* xplat-fonts:${token}:end */`;
|
|
37
|
+
var decodeName = (platformID, raw) => {
|
|
38
|
+
if (platformID === 0 || platformID === 3) return Buffer.from(raw).swap16().toString("utf16le");
|
|
39
|
+
return raw.toString("latin1");
|
|
40
|
+
};
|
|
41
|
+
var rankName = ({ platformID, languageID }) => {
|
|
42
|
+
if (platformID === 3 && languageID === 1033) return 0;
|
|
43
|
+
if (platformID === 3) return 1;
|
|
44
|
+
if (platformID === 0) return 2;
|
|
45
|
+
if (platformID === 1) return 3;
|
|
46
|
+
return 4;
|
|
47
|
+
};
|
|
48
|
+
function pickName(records, nameIDs) {
|
|
49
|
+
for (const nameID of nameIDs) {
|
|
50
|
+
const candidates = records.filter((r) => r.nameID === nameID && r.value);
|
|
51
|
+
if (candidates.length) {
|
|
52
|
+
candidates.sort((a, b) => rankName(a) - rankName(b));
|
|
53
|
+
return candidates[0].value;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function tableDirectory(buf) {
|
|
58
|
+
let dirOffset = 0;
|
|
59
|
+
if (buf.toString("latin1", 0, 4) === "ttcf") {
|
|
60
|
+
if (buf.length < 16) return null;
|
|
61
|
+
dirOffset = buf.readUInt32BE(12);
|
|
62
|
+
}
|
|
63
|
+
if (dirOffset + 4 > buf.length) return null;
|
|
64
|
+
const numTables = buf.readUInt16BE(dirOffset + 4);
|
|
65
|
+
const tables = /* @__PURE__ */ new Map();
|
|
66
|
+
for (let i = 0; i < numTables; i++) {
|
|
67
|
+
const rec = dirOffset + 12 + i * 16;
|
|
68
|
+
if (rec + 16 > buf.length) return null;
|
|
69
|
+
tables.set(buf.toString("latin1", rec, rec + 4), {
|
|
70
|
+
offset: buf.readUInt32BE(rec + 8),
|
|
71
|
+
length: buf.readUInt32BE(rec + 12)
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
return tables;
|
|
75
|
+
}
|
|
76
|
+
function readNameRecords(buf, table) {
|
|
77
|
+
const base = table.offset;
|
|
78
|
+
if (base + 6 > buf.length) return [];
|
|
79
|
+
const count = buf.readUInt16BE(base + 2);
|
|
80
|
+
const stringBase = base + buf.readUInt16BE(base + 4);
|
|
81
|
+
const records = [];
|
|
82
|
+
for (let i = 0; i < count; i++) {
|
|
83
|
+
const rec = base + 6 + i * 12;
|
|
84
|
+
if (rec + 12 > buf.length) break;
|
|
85
|
+
const platformID = buf.readUInt16BE(rec);
|
|
86
|
+
const encodingID = buf.readUInt16BE(rec + 2);
|
|
87
|
+
const languageID = buf.readUInt16BE(rec + 4);
|
|
88
|
+
const nameID = buf.readUInt16BE(rec + 6);
|
|
89
|
+
const length = buf.readUInt16BE(rec + 8);
|
|
90
|
+
const start = stringBase + buf.readUInt16BE(rec + 10);
|
|
91
|
+
if (start + length > buf.length) continue;
|
|
92
|
+
const value = decodeName(platformID, buf.subarray(start, start + length)).replaceAll("\0", "").trim();
|
|
93
|
+
records.push({
|
|
94
|
+
platformID,
|
|
95
|
+
encodingID,
|
|
96
|
+
languageID,
|
|
97
|
+
nameID,
|
|
98
|
+
value
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
return records;
|
|
102
|
+
}
|
|
103
|
+
var fixedToNumber = (v) => {
|
|
104
|
+
const n = v / 65536;
|
|
105
|
+
const rounded = Math.round(n);
|
|
106
|
+
return Math.abs(n - rounded) < .01 ? rounded : Math.round(n * 100) / 100;
|
|
107
|
+
};
|
|
108
|
+
function readWghtAxis(buf, tables) {
|
|
109
|
+
const fvar = tables.get("fvar");
|
|
110
|
+
if (!fvar || fvar.offset + 16 > buf.length) return null;
|
|
111
|
+
const axesOffset = fvar.offset + buf.readUInt16BE(fvar.offset + 4);
|
|
112
|
+
const axisCount = buf.readUInt16BE(fvar.offset + 8);
|
|
113
|
+
const axisSize = buf.readUInt16BE(fvar.offset + 10);
|
|
114
|
+
for (let i = 0; i < axisCount; i++) {
|
|
115
|
+
const rec = axesOffset + i * axisSize;
|
|
116
|
+
if (rec + 20 > buf.length || rec + 20 > fvar.offset + fvar.length) break;
|
|
117
|
+
if (buf.toString("latin1", rec, rec + 4) === "wght") return {
|
|
118
|
+
min: fixedToNumber(buf.readInt32BE(rec + 4)),
|
|
119
|
+
max: fixedToNumber(buf.readInt32BE(rec + 12))
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
function parseFontBuffer(buf) {
|
|
125
|
+
const tables = tableDirectory(buf);
|
|
126
|
+
if (!tables || !tables.has("name")) return {
|
|
127
|
+
family: void 0,
|
|
128
|
+
postscript: void 0,
|
|
129
|
+
wght: null,
|
|
130
|
+
weightClass: void 0
|
|
131
|
+
};
|
|
132
|
+
const records = readNameRecords(buf, tables.get("name"));
|
|
133
|
+
const os2 = tables.get("OS/2");
|
|
134
|
+
return {
|
|
135
|
+
family: pickName(records, [16, 1]),
|
|
136
|
+
postscript: pickName(records, [6]),
|
|
137
|
+
wght: readWghtAxis(buf, tables),
|
|
138
|
+
weightClass: os2 && os2.offset + 6 <= buf.length ? buf.readUInt16BE(os2.offset + 4) : void 0
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
var checksum = (buf) => {
|
|
142
|
+
let sum = 0;
|
|
143
|
+
for (let i = 0; i < buf.length; i += 4) sum = (sum + buf.readUInt32BE(i)) % 4294967296;
|
|
144
|
+
return sum;
|
|
145
|
+
};
|
|
146
|
+
function buildSfnt(flavor, tables) {
|
|
147
|
+
tables.sort((a, b) => a.tag < b.tag ? -1 : 1);
|
|
148
|
+
const n = tables.length;
|
|
149
|
+
const maxPow2 = 2 ** Math.floor(Math.log2(n));
|
|
150
|
+
const out = Buffer.alloc(12 + n * 16 + tables.reduce((s, t) => s + Math.ceil(t.data.length / 4) * 4, 0));
|
|
151
|
+
out.writeUInt32BE(flavor, 0);
|
|
152
|
+
out.writeUInt16BE(n, 4);
|
|
153
|
+
out.writeUInt16BE(maxPow2 * 16, 6);
|
|
154
|
+
out.writeUInt16BE(Math.log2(maxPow2), 8);
|
|
155
|
+
out.writeUInt16BE(n * 16 - maxPow2 * 16, 10);
|
|
156
|
+
let cursor = 12 + n * 16;
|
|
157
|
+
tables.forEach((t, i) => {
|
|
158
|
+
const rec = 12 + i * 16;
|
|
159
|
+
out.write(t.tag, rec, "latin1");
|
|
160
|
+
out.writeUInt32BE(checksum(t.data.length % 4 ? Buffer.concat([t.data, Buffer.alloc(4 - t.data.length % 4)]) : t.data), rec + 4);
|
|
161
|
+
out.writeUInt32BE(cursor, rec + 8);
|
|
162
|
+
out.writeUInt32BE(t.data.length, rec + 12);
|
|
163
|
+
t.data.copy(out, cursor);
|
|
164
|
+
cursor += Math.ceil(t.data.length / 4) * 4;
|
|
165
|
+
});
|
|
166
|
+
const headIdx = tables.findIndex((t) => t.tag === "head");
|
|
167
|
+
if (headIdx !== -1) {
|
|
168
|
+
const headOffset = out.readUInt32BE(12 + headIdx * 16 + 8);
|
|
169
|
+
out.writeUInt32BE(0, headOffset + 8);
|
|
170
|
+
out.writeUInt32BE(2981146554 - checksum(out) >>> 0, headOffset + 8);
|
|
171
|
+
}
|
|
172
|
+
return out;
|
|
173
|
+
}
|
|
174
|
+
function woffToSfnt(buf) {
|
|
175
|
+
if (buf.length < 44 || buf.toString("latin1", 0, 4) !== "wOFF") throw new Error("not a WOFF file");
|
|
176
|
+
const flavor = buf.readUInt32BE(4);
|
|
177
|
+
const numTables = buf.readUInt16BE(12);
|
|
178
|
+
const tables = [];
|
|
179
|
+
for (let i = 0; i < numTables; i++) {
|
|
180
|
+
const rec = 44 + i * 20;
|
|
181
|
+
if (rec + 20 > buf.length) throw new Error("truncated WOFF table directory");
|
|
182
|
+
const tag = buf.toString("latin1", rec, rec + 4);
|
|
183
|
+
const offset = buf.readUInt32BE(rec + 4);
|
|
184
|
+
const compLen = buf.readUInt32BE(rec + 8);
|
|
185
|
+
const origLen = buf.readUInt32BE(rec + 12);
|
|
186
|
+
const checkSum = buf.readUInt32BE(rec + 16);
|
|
187
|
+
if (offset + compLen > buf.length) throw new Error(`truncated WOFF table ${tag}`);
|
|
188
|
+
const packed = buf.subarray(offset, offset + compLen);
|
|
189
|
+
const data = compLen < origLen ? inflateSync(packed) : Buffer.from(packed);
|
|
190
|
+
if (data.length !== origLen) throw new Error(`WOFF table ${tag} inflated to ${data.length}, expected ${origLen}`);
|
|
191
|
+
tables.push({
|
|
192
|
+
tag,
|
|
193
|
+
checkSum,
|
|
194
|
+
data
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
return buildSfnt(flavor, tables.map(({ tag, data }) => ({
|
|
198
|
+
tag,
|
|
199
|
+
data
|
|
200
|
+
})));
|
|
201
|
+
}
|
|
202
|
+
async function woff2ToSfnt(buf) {
|
|
203
|
+
const mod = await import("wawoff2");
|
|
204
|
+
const decompress = mod.default?.decompress ?? mod.decompress;
|
|
205
|
+
return Buffer.from(await decompress(buf));
|
|
206
|
+
}
|
|
207
|
+
async function toSfnt(file) {
|
|
208
|
+
const buf = readFileSync(file);
|
|
209
|
+
const ext = extname(file).toLowerCase();
|
|
210
|
+
if (ext === ".woff") return woffToSfnt(buf);
|
|
211
|
+
if (ext === ".woff2") return woff2ToSfnt(buf);
|
|
212
|
+
throw new Error(`cannot convert ${ext}`);
|
|
213
|
+
}
|
|
214
|
+
var sanitizeFileBase = (name) => name.replace(/[^A-Za-z0-9._-]/g, "-");
|
|
215
|
+
async function stageAsTtf(sourcePath, stagingDir, destName) {
|
|
216
|
+
const sourceInfo = statSync(sourcePath);
|
|
217
|
+
if (!sourceInfo.isFile() || sourceInfo.size > 16777216) throw new Error("Font source must be an ordinary file within 16 MiB");
|
|
218
|
+
const ext = extname(sourcePath).toLowerCase();
|
|
219
|
+
const sfnt = FONT_EXTENSIONS.has(ext) ? readFileSync(sourcePath) : await toSfnt(sourcePath);
|
|
220
|
+
if (sfnt.length > 16777216) throw new Error("Converted font exceeds 16 MiB");
|
|
221
|
+
const info = parseFontBuffer(sfnt);
|
|
222
|
+
const name = destName ?? sanitizeFileBase(info.postscript ?? basename(sourcePath, ext)) + ".ttf";
|
|
223
|
+
const staged = join(stagingDir, name);
|
|
224
|
+
writeFileSync(staged, sfnt);
|
|
225
|
+
return {
|
|
226
|
+
staged,
|
|
227
|
+
destName: name,
|
|
228
|
+
info
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
var webFamilyFromCss = (cssPath) => {
|
|
232
|
+
if (!existsSync(cssPath)) return;
|
|
233
|
+
return readFileSync(cssPath, "utf8").match(/font-family:\s*'([^']+)'/)?.[1];
|
|
234
|
+
};
|
|
235
|
+
var isFontsourceSpec = (input) => /^@fontsource(-variable)?\/[a-z0-9][a-z0-9._-]*$/.test(input);
|
|
236
|
+
function fontsourceRoot(appDir, spec) {
|
|
237
|
+
for (let at = resolve(appDir);; at = dirname(at)) {
|
|
238
|
+
const path = join(at, "node_modules", spec);
|
|
239
|
+
let present = false;
|
|
240
|
+
try {
|
|
241
|
+
lstatSync(path);
|
|
242
|
+
present = true;
|
|
243
|
+
} catch (error) {
|
|
244
|
+
if (error.code !== "ENOENT") throw error;
|
|
245
|
+
}
|
|
246
|
+
if (present) return realpathSync(path);
|
|
247
|
+
if (dirname(at) === at) return null;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
async function prepareFontsource(appDir, spec, { subset = "latin", weights, install, stagingRoot } = {}) {
|
|
251
|
+
const variable = spec.startsWith("@fontsource-variable/");
|
|
252
|
+
if (!isFontsourceSpec(spec)) return { error: `unsupported package ${spec} — only @fontsource/* and @fontsource-variable/*` };
|
|
253
|
+
let pkgDir = fontsourceRoot(appDir, spec) ?? join(appDir, "node_modules", ...spec.split("/"));
|
|
254
|
+
let wasInstalled = false;
|
|
255
|
+
if (!existsSync(join(pkgDir, "package.json"))) {
|
|
256
|
+
if (!install) return { error: `${spec} is not installed — run \`bun add ${spec}\` or pass --install` };
|
|
257
|
+
if (spawnSync(packageManagerFor(appDir), ["add", spec], {
|
|
258
|
+
cwd: appDir,
|
|
259
|
+
stdio: "inherit",
|
|
260
|
+
shell: process.platform === "win32"
|
|
261
|
+
}).status !== 0) return { error: `bun add ${spec} failed` };
|
|
262
|
+
pkgDir = fontsourceRoot(appDir, spec) ?? pkgDir;
|
|
263
|
+
if (!existsSync(join(pkgDir, "package.json"))) return { error: `${spec} still not resolvable after install` };
|
|
264
|
+
wasInstalled = true;
|
|
265
|
+
}
|
|
266
|
+
const filesDir = join(pkgDir, "files");
|
|
267
|
+
if (!existsSync(filesDir)) return { error: `${spec} has no files/ directory — not a Fontsource layout` };
|
|
268
|
+
const base = spec.split("/").pop();
|
|
269
|
+
const parts = (f) => {
|
|
270
|
+
if (!f.startsWith(`${base}-`)) return null;
|
|
271
|
+
const segs = f.replace(/\.(woff2?|ttf|otf)$/, "").slice(base.length + 1).split("-");
|
|
272
|
+
return segs.length >= 3 ? {
|
|
273
|
+
subset: segs.slice(0, -2).join("-"),
|
|
274
|
+
key: segs.at(-2),
|
|
275
|
+
style: segs.at(-1),
|
|
276
|
+
file: f
|
|
277
|
+
} : null;
|
|
278
|
+
};
|
|
279
|
+
const listing = readdirSync(filesDir).map(parts).filter(Boolean);
|
|
280
|
+
const stagingDir = mkdtempSync(join(stagingRoot ?? tmpdir(), "xplat-fonts-"));
|
|
281
|
+
if (variable) {
|
|
282
|
+
const axisPreference = [
|
|
283
|
+
"wght",
|
|
284
|
+
"standard",
|
|
285
|
+
"full"
|
|
286
|
+
];
|
|
287
|
+
const candidates = listing.filter((f) => f.subset === subset && f.style === "normal" && /\.woff2?$/.test(f.file));
|
|
288
|
+
const picked = axisPreference.map((axis) => candidates.find((f) => f.key === axis)).find(Boolean) ?? candidates[0];
|
|
289
|
+
if (!picked) return { error: `${spec}: no ${subset}/normal face in files/ (subsets: ${[...new Set(listing.map((f) => f.subset))].join(", ")})` };
|
|
290
|
+
const face = await stageAsTtf(join(filesDir, picked.file), stagingDir);
|
|
291
|
+
const cssPath = join(pkgDir, `${picked.key}.css`);
|
|
292
|
+
return {
|
|
293
|
+
faces: [face],
|
|
294
|
+
webImports: [`@import '${spec}/${picked.key}.css';`],
|
|
295
|
+
webFamily: webFamilyFromCss(cssPath),
|
|
296
|
+
installed: install && wasInstalled ? spec : void 0
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
const weightsWanted = weights?.length ? new Set(weights.map(Number)) : null;
|
|
300
|
+
const candidates = listing.filter((f) => f.subset === subset && f.style === "normal" && /^\d+$/.test(f.key) && (!weightsWanted || weightsWanted.has(Number(f.key))));
|
|
301
|
+
const faces = candidates.filter((f) => f.file.endsWith(".woff"));
|
|
302
|
+
const chosen = faces.length ? faces : candidates.filter((f) => f.file.endsWith(".woff2"));
|
|
303
|
+
if (!chosen.length) return { error: `${spec}: no ${subset}/normal faces for weights ${weights?.join(",") ?? "(any)"}` };
|
|
304
|
+
const staged = [];
|
|
305
|
+
const webImports = [];
|
|
306
|
+
for (const f of chosen) {
|
|
307
|
+
const face = await stageAsTtf(join(filesDir, f.file), stagingDir);
|
|
308
|
+
staged.push({
|
|
309
|
+
...face,
|
|
310
|
+
weight: Number(f.key)
|
|
311
|
+
});
|
|
312
|
+
webImports.push(`@import '${spec}/${subset}-${f.key}.css';`);
|
|
313
|
+
}
|
|
314
|
+
return {
|
|
315
|
+
faces: staged,
|
|
316
|
+
webImports,
|
|
317
|
+
webFamily: webFamilyFromCss(join(pkgDir, `${subset}-${chosen[0].key}.css`)),
|
|
318
|
+
installed: install && wasInstalled ? spec : void 0
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
var quoteList = (names) => [...new Set(names)].map((n) => `'${n.replace(/'/g, "\\'")}'`);
|
|
322
|
+
function managedBlock(token, faces, genericFallbacks) {
|
|
323
|
+
const varName = `--font-${token}`;
|
|
324
|
+
const rules = [`:root,\n.ns-root {\n\t${varName}: ${[...quoteList(faces.flatMap((f) => f.names)), ...genericFallbacks].join(", ")};\n}`];
|
|
325
|
+
if (token === "sans") {
|
|
326
|
+
const defaults = faces.find((f) => f.weight === 400) ?? faces[0];
|
|
327
|
+
rules.push(`.ns-root {\n\tfont-family: ${[...quoteList(defaults.names), genericFallbacks.at(-1)].join(", ")};\n}`, `body {\n\tfont-family: var(${varName}, system-ui, sans-serif);\n}`);
|
|
328
|
+
}
|
|
329
|
+
const staticFaces = faces.filter((f) => !f.variable && Number.isInteger(f.weight) && WEIGHT_UTILITY[f.weight]);
|
|
330
|
+
for (const face of staticFaces) rules.push(`.${WEIGHT_UTILITY[face.weight]} {\n\tfont-family: ${quoteList(face.names).join(", ")};\n}`);
|
|
331
|
+
return `${managedStart(token)}\n${rules.join("\n\n")}\n${managedEnd(token)}`;
|
|
332
|
+
}
|
|
333
|
+
function upsertManagedBlock(css, token, block) {
|
|
334
|
+
const start = css.indexOf(managedStart(token));
|
|
335
|
+
const end = css.indexOf(managedEnd(token));
|
|
336
|
+
if (start !== -1 && end !== -1 && end > start) return css.slice(0, start) + block + css.slice(end + managedEnd(token).length);
|
|
337
|
+
return `${css.trimEnd()}\n\n${block}\n`;
|
|
338
|
+
}
|
|
339
|
+
function ensureWebImport(entrySource) {
|
|
340
|
+
if (entrySource.includes(`'./fonts.css'`) || entrySource.includes("\"./fonts.css\"")) return {
|
|
341
|
+
source: entrySource,
|
|
342
|
+
changed: false
|
|
343
|
+
};
|
|
344
|
+
const lines = entrySource.split("\n");
|
|
345
|
+
const styleLine = lines.findIndex((l) => /^import\s+['"].*style\.css['"]/.test(l));
|
|
346
|
+
if (styleLine !== -1) {
|
|
347
|
+
lines.splice(styleLine + 1, 0, `import './fonts.css';`);
|
|
348
|
+
return {
|
|
349
|
+
source: lines.join("\n"),
|
|
350
|
+
changed: true
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
let lastImport = -1;
|
|
354
|
+
for (let i = 0; i < lines.length; i++) if (/^import\s/.test(lines[i])) lastImport = i;
|
|
355
|
+
if (lastImport !== -1) {
|
|
356
|
+
lines.splice(lastImport + 1, 0, `import './fonts.css';`);
|
|
357
|
+
return {
|
|
358
|
+
source: lines.join("\n"),
|
|
359
|
+
changed: true
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
return {
|
|
363
|
+
source: `import './fonts.css';\n${entrySource}`,
|
|
364
|
+
changed: true
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
function appSourceDir(appDir, read) {
|
|
368
|
+
const config = read(join(appDir, "nativescript.config.ts"));
|
|
369
|
+
if (config !== null) {
|
|
370
|
+
const match = config.toString("utf8").match(/appPath:\s*['"]([^'"]+)['"]/);
|
|
371
|
+
if (match) return join(appDir, match[1]);
|
|
372
|
+
}
|
|
373
|
+
return join(appDir, "src");
|
|
374
|
+
}
|
|
375
|
+
var readManifest = (fontsDir, read) => {
|
|
376
|
+
const manifestPath = join(fontsDir, MANIFEST);
|
|
377
|
+
const bytes = read(manifestPath);
|
|
378
|
+
if (bytes === null) return { tokens: {} };
|
|
379
|
+
try {
|
|
380
|
+
return JSON.parse(bytes.toString("utf8"));
|
|
381
|
+
} catch {
|
|
382
|
+
throw new Error(`Existing font manifest ${manifestPath} is invalid JSON; reconcile it before adding fonts`);
|
|
383
|
+
}
|
|
384
|
+
};
|
|
385
|
+
function planFont(appDir, stagedFaces, { token = "sans", webImports = [], webFamily } = {}) {
|
|
386
|
+
appDir = realpathSync(appDir);
|
|
387
|
+
const files = /* @__PURE__ */ new Map(), inputs = /* @__PURE__ */ new Map(), contents = /* @__PURE__ */ new Map();
|
|
388
|
+
const hash = (bytes) => createHash("sha256").update(bytes).digest("hex");
|
|
389
|
+
const read = (path) => {
|
|
390
|
+
path = resolve(path);
|
|
391
|
+
const rel = relative(appDir, path);
|
|
392
|
+
if (rel === ".." || rel.startsWith(".." + sep) || isAbsolute(rel)) throw new Error(`Font registration leaves the selected app: ${path}`);
|
|
393
|
+
if (contents.has(path)) return contents.get(path);
|
|
394
|
+
let bytes = null;
|
|
395
|
+
try {
|
|
396
|
+
const info = lstatSync(path);
|
|
397
|
+
if (!info.isFile() || info.size > 16777216 || realpathSync(path) !== path) throw new Error(`Font registration input must be an ordinary app file within 16 MiB: ${path}`);
|
|
398
|
+
bytes = readFileSync(path);
|
|
399
|
+
if (bytes.length > 16777216) throw new Error("Font input grew beyond 16 MiB");
|
|
400
|
+
} catch (error) {
|
|
401
|
+
if (error.code !== "ENOENT") throw error;
|
|
402
|
+
}
|
|
403
|
+
contents.set(path, bytes);
|
|
404
|
+
inputs.set(path, {
|
|
405
|
+
path,
|
|
406
|
+
before: bytes === null ? null : hash(bytes)
|
|
407
|
+
});
|
|
408
|
+
return bytes;
|
|
409
|
+
};
|
|
410
|
+
const present = (path) => files.has(path) || read(path) !== null;
|
|
411
|
+
const write = (path, data, mode) => {
|
|
412
|
+
const before = read(path), bytes = Buffer.isBuffer(data) ? data : Buffer.from(data);
|
|
413
|
+
if (bytes.length > 16777216) throw new Error("Font output exceeds 16 MiB");
|
|
414
|
+
files.set(path, {
|
|
415
|
+
path,
|
|
416
|
+
before: before === null ? null : hash(before),
|
|
417
|
+
content: bytes.toString("base64"),
|
|
418
|
+
mode: before === null ? mode ?? 438 & ~process.umask() : lstatSync(path).mode & 511
|
|
419
|
+
});
|
|
420
|
+
};
|
|
421
|
+
const report = {
|
|
422
|
+
copied: [],
|
|
423
|
+
faces: [],
|
|
424
|
+
edited: [],
|
|
425
|
+
names: {
|
|
426
|
+
family: void 0,
|
|
427
|
+
ios: [],
|
|
428
|
+
android: []
|
|
429
|
+
},
|
|
430
|
+
warnings: []
|
|
431
|
+
};
|
|
432
|
+
if (!present(join(appDir, "package.json"))) return { error: `no package.json in ${appDir} — pass --dir <app root>` };
|
|
433
|
+
const srcDir = appSourceDir(appDir, read);
|
|
434
|
+
if (!existsSync(srcDir)) return { error: `no app source directory at ${srcDir}` };
|
|
435
|
+
if (!stagedFaces.length) return { error: "nothing to register" };
|
|
436
|
+
if (!statSync(srcDir).isDirectory() || realpathSync(srcDir) !== srcDir || relative(appDir, srcDir).split(sep).includes("..")) throw new Error("Font source directory must be ordinary and inside the selected app");
|
|
437
|
+
const family = stagedFaces[0].info.family;
|
|
438
|
+
report.names.family = webFamily ?? family;
|
|
439
|
+
const fontsDir = join(srcDir, "fonts");
|
|
440
|
+
const manifest = readManifest(fontsDir, read);
|
|
441
|
+
manifest.tokens[token] ??= {
|
|
442
|
+
faces: [],
|
|
443
|
+
webImports: []
|
|
444
|
+
};
|
|
445
|
+
const state = manifest.tokens[token];
|
|
446
|
+
const changedTokens = /* @__PURE__ */ new Set([token]);
|
|
447
|
+
const changedFaces = /* @__PURE__ */ new Set();
|
|
448
|
+
const registered = new Set(Object.values(manifest.tokens).flatMap((value) => value.faces.map((face) => face.file)));
|
|
449
|
+
for (const face of stagedFaces) {
|
|
450
|
+
const target = join(fontsDir, face.destName);
|
|
451
|
+
const stagedBytes = readFileSync(face.staged);
|
|
452
|
+
const existing = read(target);
|
|
453
|
+
const pending = files.get(target);
|
|
454
|
+
if (pending && !Buffer.from(pending.content, "base64").equals(stagedBytes)) throw new Error(`Conflicting font inputs use the same filename ${face.destName}; rename one input`);
|
|
455
|
+
if (existing !== null && !existing.equals(stagedBytes) && !registered.has(face.destName)) throw new Error(`Existing font ${face.destName} is not registered; choose a different filename or reconcile the existing file`);
|
|
456
|
+
if (existing === null || !existing.equals(stagedBytes)) {
|
|
457
|
+
write(target, stagedBytes, statSync(face.staged).mode & 511);
|
|
458
|
+
if (existing !== null) changedFaces.add(face.destName);
|
|
459
|
+
report.copied.push(face.destName);
|
|
460
|
+
}
|
|
461
|
+
const fileBase = face.destName.replace(/\.ttf$/, "");
|
|
462
|
+
const names = [
|
|
463
|
+
webFamily,
|
|
464
|
+
face.info.family,
|
|
465
|
+
face.info.postscript,
|
|
466
|
+
fileBase
|
|
467
|
+
].filter(Boolean);
|
|
468
|
+
const entry = {
|
|
469
|
+
file: face.destName,
|
|
470
|
+
weight: Number(face.weight ?? face.info.weightClass ?? 400),
|
|
471
|
+
variable: Boolean(face.info.wght),
|
|
472
|
+
names
|
|
473
|
+
};
|
|
474
|
+
for (const [savedToken, savedState] of Object.entries(manifest.tokens)) for (const savedFace of savedState.faces) if (savedFace.file === entry.file) {
|
|
475
|
+
if (JSON.stringify(savedFace.names) !== JSON.stringify(entry.names) || savedFace.variable !== entry.variable) changedFaces.add(entry.file);
|
|
476
|
+
savedFace.names = entry.names;
|
|
477
|
+
savedFace.variable = entry.variable;
|
|
478
|
+
changedTokens.add(savedToken);
|
|
479
|
+
}
|
|
480
|
+
const previous = state.faces.find((f) => f.file === entry.file && f.weight === entry.weight);
|
|
481
|
+
if (previous) Object.assign(previous, entry);
|
|
482
|
+
else state.faces.push(entry);
|
|
483
|
+
report.names.ios.push(face.info.postscript, face.info.family);
|
|
484
|
+
report.names.android.push(fileBase);
|
|
485
|
+
report.faces.push({
|
|
486
|
+
file: face.destName,
|
|
487
|
+
weight: face.info.wght ? `${face.info.wght.min} ${face.info.wght.max}` : String(entry.weight),
|
|
488
|
+
variable: entry.variable,
|
|
489
|
+
imported: Boolean(face.fromPackage)
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
for (const imp of webImports) if (!state.webImports.includes(imp)) state.webImports.push(imp);
|
|
493
|
+
write(join(fontsDir, MANIFEST), JSON.stringify(manifest, null, " ") + "\n");
|
|
494
|
+
report.edited.push(`fonts/${MANIFEST}`);
|
|
495
|
+
const fontsCssPath = join(srcDir, "fonts.css");
|
|
496
|
+
let fontsCss = present(fontsCssPath) ? read(fontsCssPath).toString("utf8") : "/* @font-face faces generated by `xplat fonts add`. Web-only — the native\n * bundles resolve the same families from src/fonts. */\n";
|
|
497
|
+
let fontsCssChanged = false;
|
|
498
|
+
for (const imp of state.webImports) if (!fontsCss.includes(imp)) {
|
|
499
|
+
fontsCss = `${fontsCss.trimEnd()}\n\n${imp}\n`;
|
|
500
|
+
fontsCssChanged = true;
|
|
501
|
+
}
|
|
502
|
+
for (const face of stagedFaces) {
|
|
503
|
+
if (face.fromPackage) continue;
|
|
504
|
+
const weightCss = face.info.wght ? `${face.info.wght.min} ${face.info.wght.max}` : String(face.weight ?? face.info.weightClass ?? 400);
|
|
505
|
+
const faceCss = `@font-face {
|
|
506
|
+
font-family: '${(face.info.family ?? "").replace(/'/g, "\\'")}';
|
|
507
|
+
src: url('./fonts/${face.destName}') format('truetype');
|
|
508
|
+
font-style: normal;
|
|
509
|
+
font-weight: ${weightCss};
|
|
510
|
+
font-display: swap;
|
|
511
|
+
}`;
|
|
512
|
+
const existingFace = fontsCss.match(/@font-face\s*\{[^}]*\}/g)?.find((block) => block.includes(`./fonts/${face.destName}`));
|
|
513
|
+
if (existingFace === faceCss) {
|
|
514
|
+
report.warnings.push(`${face.destName} — @font-face already registered`);
|
|
515
|
+
continue;
|
|
516
|
+
}
|
|
517
|
+
if (existingFace && !changedFaces.has(face.destName)) {
|
|
518
|
+
report.warnings.push(`${face.destName} — custom @font-face already registered; keeping it`);
|
|
519
|
+
continue;
|
|
520
|
+
}
|
|
521
|
+
fontsCss = existingFace ? fontsCss.replace(existingFace, faceCss) : `${fontsCss.trimEnd()}\n\n${faceCss}\n`;
|
|
522
|
+
fontsCssChanged = true;
|
|
523
|
+
}
|
|
524
|
+
if (fontsCssChanged || !present(fontsCssPath)) {
|
|
525
|
+
write(fontsCssPath, fontsCss);
|
|
526
|
+
report.edited.push("fonts.css");
|
|
527
|
+
}
|
|
528
|
+
const entry = WEB_ENTRIES.map((e) => join(srcDir, e)).find((e) => present(e));
|
|
529
|
+
if (entry) {
|
|
530
|
+
const { source, changed } = ensureWebImport(read(entry).toString("utf8"));
|
|
531
|
+
if (changed) {
|
|
532
|
+
write(entry, source);
|
|
533
|
+
report.edited.push(basename(entry));
|
|
534
|
+
}
|
|
535
|
+
} else report.warnings.push(`no web entry (${WEB_ENTRIES.join(" | ")}) in ${srcDir} — add \`import './fonts.css'\` to your web entry manually`);
|
|
536
|
+
const styleTokens = [...changedTokens].filter((value) => value !== "none");
|
|
537
|
+
if (styleTokens.length) {
|
|
538
|
+
const stylePath = join(srcDir, "style.css");
|
|
539
|
+
if (!present(stylePath)) report.warnings.push(`no ${stylePath} — add the token + font-family rules manually`);
|
|
540
|
+
else {
|
|
541
|
+
const styleCss = read(stylePath).toString("utf8");
|
|
542
|
+
let next = styleCss;
|
|
543
|
+
for (const savedToken of styleTokens) {
|
|
544
|
+
const generic = GENERIC_FALLBACKS[savedToken] ?? ["sans-serif"];
|
|
545
|
+
next = upsertManagedBlock(next, savedToken, managedBlock(savedToken, manifest.tokens[savedToken].faces, generic));
|
|
546
|
+
}
|
|
547
|
+
if (next !== styleCss) {
|
|
548
|
+
write(stylePath, next);
|
|
549
|
+
report.edited.push("style.css");
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
report.names.ios = [...new Set(report.names.ios.filter(Boolean))];
|
|
554
|
+
report.names.android = [...new Set(report.names.android)];
|
|
555
|
+
let total = 0;
|
|
556
|
+
for (const file of files.values()) total += Buffer.from(file.content, "base64").length;
|
|
557
|
+
if (files.size > 512 || inputs.size > 2048 || total > 33554432) throw new Error("Font plan exceeds inventory/byte limits");
|
|
558
|
+
return {
|
|
559
|
+
version: 1,
|
|
560
|
+
project: appDir,
|
|
561
|
+
sourceDirectory: srcDir,
|
|
562
|
+
files: [...files.values()],
|
|
563
|
+
inputs: [...inputs.values()],
|
|
564
|
+
report
|
|
565
|
+
};
|
|
566
|
+
}
|
|
567
|
+
async function planFontInputs(appDir, inputs, { name, token, subset, weights, install } = {}) {
|
|
568
|
+
const stagingDir = mkdtempSync(join(tmpdir(), "xplat-fonts-"));
|
|
569
|
+
try {
|
|
570
|
+
const staged = [];
|
|
571
|
+
const webImports = [];
|
|
572
|
+
let webFamily;
|
|
573
|
+
const installed = [];
|
|
574
|
+
for (const input of inputs) if (isFontsourceSpec(input)) {
|
|
575
|
+
const prepared = await prepareFontsource(appDir, input, {
|
|
576
|
+
subset,
|
|
577
|
+
weights,
|
|
578
|
+
install,
|
|
579
|
+
stagingRoot: stagingDir
|
|
580
|
+
});
|
|
581
|
+
if (prepared.error) return { error: prepared.error };
|
|
582
|
+
for (const f of prepared.faces) staged.push({
|
|
583
|
+
...f,
|
|
584
|
+
fromPackage: true
|
|
585
|
+
});
|
|
586
|
+
webImports.push(...prepared.webImports);
|
|
587
|
+
webFamily ??= prepared.webFamily;
|
|
588
|
+
if (prepared.installed) installed.push(prepared.installed);
|
|
589
|
+
} else {
|
|
590
|
+
if (!existsSync(input)) return { error: `no such file: ${input}` };
|
|
591
|
+
const ext = extname(input).toLowerCase();
|
|
592
|
+
if (!FONT_EXTENSIONS.has(ext) && !CONVERTIBLE_EXTENSIONS.has(ext)) return { error: `${basename(input)} — expected .ttf, .otf, .woff, or .woff2` };
|
|
593
|
+
const localStage = mkdtempSync(join(stagingDir, "local-"));
|
|
594
|
+
staged.push(await stageAsTtf(input, localStage, basename(input, ext) + ".ttf"));
|
|
595
|
+
}
|
|
596
|
+
const families = new Set(staged.map((f) => f.info.family).filter(Boolean));
|
|
597
|
+
if (families.size > 1 && token !== "none" && !name) return { error: `inputs resolve to multiple families (${[...families].join(", ")}) — run one family per token or pass --token none` };
|
|
598
|
+
if (name) for (const f of staged) f.info.family = name;
|
|
599
|
+
const plan = planFont(appDir, staged, {
|
|
600
|
+
token,
|
|
601
|
+
webImports,
|
|
602
|
+
webFamily
|
|
603
|
+
});
|
|
604
|
+
if (plan.error) return plan;
|
|
605
|
+
plan.report.installed = installed;
|
|
606
|
+
return plan;
|
|
607
|
+
} finally {
|
|
608
|
+
rmSync(stagingDir, {
|
|
609
|
+
recursive: true,
|
|
610
|
+
force: true
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
export { FONT_EXTENSIONS, parseFontBuffer, planFont, planFontInputs, prepareFontsource, toSfnt, woff2ToSfnt, woffToSfnt };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { processNativeCss } from "./vite.js";
|
|
2
|
+
// ../cli/src/inline-stylesheets.ts
|
|
3
|
+
function xplatInlineStylesheets() {
|
|
4
|
+
const warned = /* @__PURE__ */ new Set();
|
|
5
|
+
return {
|
|
6
|
+
name: "xplat-inline-stylesheets",
|
|
7
|
+
transform: {
|
|
8
|
+
order: "pre",
|
|
9
|
+
handler(code, id) {
|
|
10
|
+
if (id.split("?")[0].endsWith("virtual:bamboo.css")) return;
|
|
11
|
+
if (!id.split("?")[0].endsWith(".css")) return;
|
|
12
|
+
return processNativeCss(code, id, (m) => this.warn(m), { warned });
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
generateBundle: {
|
|
16
|
+
order: "post",
|
|
17
|
+
handler(_opts, bundle) {
|
|
18
|
+
const sheets = [];
|
|
19
|
+
for (const file of Object.values(bundle)) {
|
|
20
|
+
if (file.type !== "asset" || !file.fileName.endsWith(".css")) continue;
|
|
21
|
+
const src = typeof file.source === "string" ? file.source : new TextDecoder().decode(file.source);
|
|
22
|
+
sheets.push([file.fileName, processNativeCss(src, file.fileName, (m) => this.warn(m), { warned })]);
|
|
23
|
+
delete bundle[file.fileName];
|
|
24
|
+
}
|
|
25
|
+
if (!sheets.length) return;
|
|
26
|
+
const entry = Object.values(bundle).find((file) => file.type === "chunk" && file.isEntry);
|
|
27
|
+
if (!entry) {
|
|
28
|
+
this.warn("xplat-inline-stylesheets: no entry chunk found for stylesheet injection");
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
entry.code = `(globalThis.__xplatStylesheets ??= []).push(${sheets.map(([id, css]) => `[${JSON.stringify(id)},${JSON.stringify(css)}]`).join(",")});\n` + entry.code;
|
|
32
|
+
if (entry.map?.mappings) entry.map.mappings = ";" + entry.map.mappings;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export { xplatInlineStylesheets };
|