@tschk/moonshine-astro 0.4.4 → 0.4.80

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/src/config.ts ADDED
@@ -0,0 +1,269 @@
1
+ /** Independent host-free Astro Configuration helpers (`astro/config`). */
2
+ import { envField, type EnvFieldDefinition } from "./env";
3
+ import type { AstroI18nConfig } from "./i18n";
4
+ import type { AstroSessionOptions, ValidRedirectStatus } from "./context";
5
+
6
+ export { envField };
7
+
8
+ export interface ImageServiceConfig<TConfig = any> {
9
+ entrypoint: string;
10
+ config?: TConfig;
11
+ }
12
+
13
+ export interface SharpImageServiceConfig {
14
+ limitInputPixels?: number | boolean;
15
+ kernel?: "nearest" | "cubic" | "mitchell" | "lanczos2" | "lanczos3";
16
+ }
17
+
18
+ export function passthroughImageService(): ImageServiceConfig {
19
+ return {
20
+ entrypoint: "astro/assets/services/noop",
21
+ config: {},
22
+ };
23
+ }
24
+
25
+ export function sharpImageService(
26
+ config: SharpImageServiceConfig = {},
27
+ ): ImageServiceConfig<SharpImageServiceConfig> {
28
+ return {
29
+ entrypoint: "astro/assets/services/sharp",
30
+ config,
31
+ };
32
+ }
33
+
34
+ export function squooshImageService(): ImageServiceConfig {
35
+ return {
36
+ entrypoint: "astro/assets/services/squoosh",
37
+ config: {},
38
+ };
39
+ }
40
+
41
+ export type RedirectConfig =
42
+ | string
43
+ | {
44
+ status?: ValidRedirectStatus;
45
+ destination: string;
46
+ };
47
+
48
+ export interface AstroIntegration {
49
+ name: string;
50
+ hooks: Record<string, (...args: unknown[]) => unknown>;
51
+ }
52
+
53
+ export interface AstroUserConfig {
54
+ root?: string;
55
+ srcDir?: string;
56
+ publicDir?: string;
57
+ outDir?: string;
58
+ cacheDir?: string;
59
+ site?: string;
60
+ compressHTML?: boolean;
61
+ base?: string;
62
+ trailingSlash?: "always" | "never" | "ignore";
63
+ scopedStyleStrategy?: "attribute" | "class" | "where";
64
+ output?: "static" | "server" | "hybrid";
65
+ adapter?: {
66
+ name: string;
67
+ hooks?: Record<string, (...args: unknown[]) => unknown>;
68
+ };
69
+ integrations?: (
70
+ AstroIntegration | AstroIntegration[] | false | undefined | null
71
+ )[];
72
+ build?: {
73
+ format?: "file" | "directory" | "preserve";
74
+ client?: string;
75
+ server?: string;
76
+ assets?: string;
77
+ assetsPrefix?: string | Record<string, string>;
78
+ serverEntry?: string;
79
+ redirects?: boolean;
80
+ inlineStylesheets?: "auto" | "always" | "never";
81
+ concurrency?: number;
82
+ };
83
+ server?:
84
+ | {
85
+ host?: string | boolean;
86
+ port?: number;
87
+ open?: string | boolean;
88
+ headers?: Record<string, string>;
89
+ }
90
+ | ((options: { command: "dev" | "preview" }) => {
91
+ host?: string | boolean;
92
+ port?: number;
93
+ open?: string | boolean;
94
+ headers?: Record<string, string>;
95
+ });
96
+ redirects?: Record<string, RedirectConfig>;
97
+ prefetch?:
98
+ | boolean
99
+ | {
100
+ prefetchAll?: boolean;
101
+ defaultStrategy?: "tap" | "hover" | "viewport" | "load";
102
+ };
103
+ image?: {
104
+ endpoint?: string | { route: string; entrypoint?: string };
105
+ service?: ImageServiceConfig;
106
+ domains?: string[];
107
+ remotePatterns?: {
108
+ protocol?: string;
109
+ hostname?: string;
110
+ port?: string;
111
+ pathname?: string;
112
+ }[];
113
+ experimentalLayout?: "responsive" | "fixed" | "full-width" | "none";
114
+ };
115
+ markdown?: {
116
+ drafts?: boolean;
117
+ syntaxHighlight?: "shiki" | "prism" | false;
118
+ shikiConfig?: Record<string, unknown>;
119
+ remarkPlugins?: unknown[];
120
+ rehypePlugins?: unknown[];
121
+ gfm?: boolean;
122
+ smartypants?: boolean;
123
+ };
124
+ i18n?: AstroI18nConfig;
125
+ env?: {
126
+ schema?: Record<string, EnvFieldDefinition<unknown>>;
127
+ validateSecrets?: boolean;
128
+ };
129
+ session?: AstroSessionOptions & {
130
+ driver?: string | { name: string; options?: Record<string, unknown> };
131
+ };
132
+ security?: {
133
+ checkOrigin?: boolean;
134
+ };
135
+ vite?: Record<string, unknown>;
136
+ experimental?: Record<string, unknown>;
137
+ }
138
+
139
+ export interface AstroConfig extends Omit<
140
+ AstroUserConfig,
141
+ "root" | "srcDir" | "publicDir" | "outDir" | "cacheDir" | "integrations"
142
+ > {
143
+ root: URL;
144
+ srcDir: URL;
145
+ publicDir: URL;
146
+ outDir: URL;
147
+ cacheDir: URL;
148
+ base: string;
149
+ trailingSlash: "always" | "never" | "ignore";
150
+ compressHTML: boolean;
151
+ scopedStyleStrategy: "attribute" | "class" | "where";
152
+ output: "static" | "server" | "hybrid";
153
+ integrations: AstroIntegration[];
154
+ redirects: Record<string, RedirectConfig>;
155
+ }
156
+
157
+ export function defineConfig<T extends AstroUserConfig>(config: T): T {
158
+ return config;
159
+ }
160
+
161
+ function isPlainObject(value: unknown): value is Record<string, unknown> {
162
+ if (typeof value !== "object" || value === null) return false;
163
+ if (value instanceof URL || value instanceof RegExp || value instanceof Date)
164
+ return false;
165
+ const proto = Object.getPrototypeOf(value);
166
+ return proto === null || proto === Object.prototype;
167
+ }
168
+
169
+ export function mergeConfig<
170
+ D extends Record<string, unknown>,
171
+ O extends Record<string, unknown>,
172
+ >(defaults: D, overrides: O, isRoot = true): D & O {
173
+ const merged: Record<string, unknown> = { ...defaults };
174
+ for (const [key, value] of Object.entries(overrides)) {
175
+ if (value === undefined) continue;
176
+ const existing = merged[key];
177
+ if (Array.isArray(existing) && Array.isArray(value)) {
178
+ merged[key] = [...existing, ...value];
179
+ } else if (isPlainObject(existing) && isPlainObject(value)) {
180
+ merged[key] = mergeConfig(existing, value, isRoot && key === "astro");
181
+ } else {
182
+ merged[key] = value;
183
+ }
184
+ }
185
+ return merged as D & O;
186
+ }
187
+
188
+ function resolveDirUrl(
189
+ dir: string | undefined,
190
+ fallback: string,
191
+ root: URL,
192
+ ): URL {
193
+ const raw = dir ?? fallback;
194
+ const normalized = raw.endsWith("/") ? raw : `${raw}/`;
195
+ return new URL(normalized, root);
196
+ }
197
+
198
+ export async function validateConfig(
199
+ userConfig: AstroUserConfig = {},
200
+ root = "file:///app/",
201
+ ): Promise<AstroConfig> {
202
+ if (typeof userConfig !== "object" || userConfig === null) {
203
+ throw new TypeError("Astro configuration must be an object");
204
+ }
205
+ if (userConfig.site !== undefined) {
206
+ try {
207
+ const parsed = new URL(userConfig.site);
208
+ if (!parsed.protocol) {
209
+ throw new Error("Missing protocol");
210
+ }
211
+ } catch {
212
+ throw new Error(
213
+ `Invalid Astro config "site": "${userConfig.site}" is not a valid URL.`,
214
+ );
215
+ }
216
+ }
217
+ const rootBase = root.startsWith("file://")
218
+ ? root.endsWith("/")
219
+ ? root
220
+ : `${root}/`
221
+ : `file://${root.startsWith("/") ? "" : "/"}${root.endsWith("/") ? root : `${root}/`}`;
222
+ const rootUrl = new URL(
223
+ userConfig.root
224
+ ? userConfig.root.endsWith("/")
225
+ ? userConfig.root
226
+ : `${userConfig.root}/`
227
+ : "./",
228
+ rootBase,
229
+ );
230
+ const flatIntegrations: AstroIntegration[] = [];
231
+ for (const item of userConfig.integrations ?? []) {
232
+ if (!item) continue;
233
+ if (Array.isArray(item)) {
234
+ for (const nested of item) {
235
+ if (nested) flatIntegrations.push(nested);
236
+ }
237
+ } else {
238
+ flatIntegrations.push(item);
239
+ }
240
+ }
241
+
242
+ return {
243
+ ...userConfig,
244
+ root: rootUrl,
245
+ srcDir: resolveDirUrl(userConfig.srcDir, "./src/", rootUrl),
246
+ publicDir: resolveDirUrl(userConfig.publicDir, "./public/", rootUrl),
247
+ outDir: resolveDirUrl(userConfig.outDir, "./dist/", rootUrl),
248
+ cacheDir: resolveDirUrl(
249
+ userConfig.cacheDir,
250
+ "./node_modules/.astro/",
251
+ rootUrl,
252
+ ),
253
+ base: userConfig.base ?? "/",
254
+ trailingSlash: userConfig.trailingSlash ?? "ignore",
255
+ compressHTML: userConfig.compressHTML ?? true,
256
+ scopedStyleStrategy: userConfig.scopedStyleStrategy ?? "attribute",
257
+ output: userConfig.output ?? "static",
258
+ integrations: flatIntegrations,
259
+ redirects: userConfig.redirects ?? {},
260
+ image: {
261
+ service: sharpImageService(),
262
+ ...userConfig.image,
263
+ },
264
+ security: {
265
+ checkOrigin: true,
266
+ ...userConfig.security,
267
+ },
268
+ };
269
+ }