@xingwangzhe/stalux 1.3.8 → 1.4.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/package.json +1 -1
- package/src/index.ts +28 -20
- package/src/internal/font-subset.ts +17 -3
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -136,7 +136,7 @@ export function stalux(options: StaluxOptions = {}): AstroIntegration {
|
|
|
136
136
|
return {
|
|
137
137
|
name: "stalux",
|
|
138
138
|
hooks: {
|
|
139
|
-
"astro:config:setup": ({
|
|
139
|
+
"astro:config:setup": async ({
|
|
140
140
|
injectRoute,
|
|
141
141
|
injectScript,
|
|
142
142
|
updateConfig,
|
|
@@ -188,27 +188,35 @@ export function stalux(options: StaluxOptions = {}): AstroIntegration {
|
|
|
188
188
|
}
|
|
189
189
|
|
|
190
190
|
logger.info(`Stalux initialized (contentDir: ${opt.contentDir})`);
|
|
191
|
+
|
|
192
|
+
// 5. 字体裁剪(在 config:setup 阶段执行,保证 dev/build 都能生成)
|
|
193
|
+
try {
|
|
194
|
+
const projectRoot = process.cwd();
|
|
195
|
+
await runFontSubsetting(projectRoot, logger);
|
|
196
|
+
} catch (error) {
|
|
197
|
+
logger.debug(`Font subsetting deferred: ${String(error)}`);
|
|
198
|
+
}
|
|
191
199
|
},
|
|
192
200
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
201
|
+
"astro:build:start": async ({ logger }) => {
|
|
202
|
+
// 6. 字体裁剪(构建时备用)
|
|
203
|
+
try {
|
|
204
|
+
const projectRoot = process.cwd();
|
|
205
|
+
await runFontSubsetting(projectRoot, logger);
|
|
206
|
+
} catch (error) {
|
|
207
|
+
logger.warn(`Font subsetting skipped: ${String(error)}`);
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
|
|
211
|
+
"astro:server:setup": async ({ logger }) => {
|
|
212
|
+
// 7. 字体裁剪(开发模式备用)
|
|
213
|
+
try {
|
|
214
|
+
const projectRoot = process.cwd();
|
|
215
|
+
await runFontSubsetting(projectRoot, logger);
|
|
216
|
+
} catch (error) {
|
|
217
|
+
logger.debug(`Font subsetting deferred in dev: ${String(error)}`);
|
|
218
|
+
}
|
|
219
|
+
},
|
|
212
220
|
|
|
213
221
|
"astro:build:done": async ({ dir, logger }) => {
|
|
214
222
|
// 6. 后处理:Pagefind 搜索索引
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
|
|
18
18
|
import { createHash } from "node:crypto";
|
|
19
19
|
import { existsSync, mkdirSync, readFileSync, writeFileSync, readdirSync } from "node:fs";
|
|
20
|
-
import { resolve, basename, extname, join } from "node:path";
|
|
20
|
+
import { resolve, basename, extname, join, dirname } from "node:path";
|
|
21
|
+
import { fileURLToPath } from "node:url";
|
|
21
22
|
|
|
22
23
|
import type { AstroIntegrationLogger } from "astro";
|
|
23
24
|
// subset-font 是 WASM 模块,需要静态导入(Vite module runner 在构建钩子中不兼容动态 import)
|
|
@@ -190,6 +191,20 @@ function collectCommonChars(projectRoot: string): string {
|
|
|
190
191
|
|
|
191
192
|
scanDir(srcDir);
|
|
192
193
|
|
|
194
|
+
// Also scan user config YAML files for nav titles, descriptions, etc.
|
|
195
|
+
const configDir = resolve(projectRoot, CONTENT_ROOT, "config");
|
|
196
|
+
if (existsSync(configDir)) {
|
|
197
|
+
for (const f of readdirSync(configDir)) {
|
|
198
|
+
if (!f.endsWith(".yml") && !f.endsWith(".yaml")) continue;
|
|
199
|
+
try {
|
|
200
|
+
const content = readFileSync(join(configDir, f), "utf-8");
|
|
201
|
+
for (const ch of content) chars.add(ch);
|
|
202
|
+
} catch {
|
|
203
|
+
/* skip */
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
193
208
|
return [...chars].sort().join("");
|
|
194
209
|
}
|
|
195
210
|
|
|
@@ -223,11 +238,10 @@ export async function runFontSubsetting(
|
|
|
223
238
|
logger.info("Font subsetting: scanning content...");
|
|
224
239
|
|
|
225
240
|
// 1. Locate font file (check project root first, then stalux package dir)
|
|
226
|
-
const { fileURLToPath } = await import("node:url");
|
|
227
241
|
const fontPaths = [
|
|
228
242
|
resolve(projectRoot, FONT_INPUT),
|
|
229
243
|
resolve(projectRoot, "node_modules", "@xingwangzhe", "stalux", FONT_INPUT),
|
|
230
|
-
resolve(fileURLToPath(
|
|
244
|
+
resolve(dirname(fileURLToPath(import.meta.url)), "..", "..", FONT_INPUT),
|
|
231
245
|
];
|
|
232
246
|
|
|
233
247
|
let fontPath: string | undefined;
|