-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
schema.ts
277 lines (268 loc) · 8.97 KB
/
schema.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import type { RehypePlugin, RemarkPlugin, RemarkRehype } from '@astrojs/markdown-remark';
import { markdownConfigDefaults } from '@astrojs/markdown-remark';
import type { ILanguageRegistration, IThemeRegistration, Theme } from 'shiki';
import type { AstroUserConfig, ViteUserConfig } from '../../@types/astro';
import { OutgoingHttpHeaders } from 'http';
import { BUNDLED_THEMES } from 'shiki';
import { z } from 'zod';
import { appendForwardSlash, prependForwardSlash, trimSlashes } from '../path.js';
const ASTRO_CONFIG_DEFAULTS: AstroUserConfig & any = {
root: '.',
srcDir: './src',
publicDir: './public',
outDir: './dist',
base: '/',
trailingSlash: 'ignore',
build: {
format: 'directory',
client: './dist/client/',
server: './dist/server/',
assets: '_astro',
serverEntry: 'entry.mjs',
},
server: {
host: false,
port: 3000,
streaming: true,
},
integrations: [],
markdown: {
drafts: false,
...markdownConfigDefaults,
},
vite: {},
legacy: {},
};
export const AstroConfigSchema = z.object({
root: z
.string()
.optional()
.default(ASTRO_CONFIG_DEFAULTS.root)
.transform((val) => new URL(val)),
srcDir: z
.string()
.optional()
.default(ASTRO_CONFIG_DEFAULTS.srcDir)
.transform((val) => new URL(val)),
publicDir: z
.string()
.optional()
.default(ASTRO_CONFIG_DEFAULTS.publicDir)
.transform((val) => new URL(val)),
outDir: z
.string()
.optional()
.default(ASTRO_CONFIG_DEFAULTS.outDir)
.transform((val) => new URL(val)),
site: z
.string()
.url()
.optional()
.transform((val) => (val ? appendForwardSlash(val) : val)),
base: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.base),
trailingSlash: z
.union([z.literal('always'), z.literal('never'), z.literal('ignore')])
.optional()
.default(ASTRO_CONFIG_DEFAULTS.trailingSlash),
output: z
.union([z.literal('static'), z.literal('server')])
.optional()
.default('static'),
adapter: z.object({ name: z.string(), hooks: z.object({}).passthrough().default({}) }).optional(),
integrations: z.preprocess(
// preprocess
(val) => (Array.isArray(val) ? val.flat(Infinity).filter(Boolean) : val),
// validate
z
.array(z.object({ name: z.string(), hooks: z.object({}).passthrough().default({}) }))
.default(ASTRO_CONFIG_DEFAULTS.integrations)
),
build: z
.object({
format: z
.union([z.literal('file'), z.literal('directory')])
.optional()
.default(ASTRO_CONFIG_DEFAULTS.build.format),
client: z
.string()
.optional()
.default(ASTRO_CONFIG_DEFAULTS.build.client)
.transform((val) => new URL(val)),
server: z
.string()
.optional()
.default(ASTRO_CONFIG_DEFAULTS.build.server)
.transform((val) => new URL(val)),
assets: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.assets),
serverEntry: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.serverEntry),
})
.optional()
.default({}),
server: z.preprocess(
// preprocess
// NOTE: Uses the "error" command here because this is overwritten by the
// individualized schema parser with the correct command.
(val) => (typeof val === 'function' ? val({ command: 'error' }) : val),
// validate
z
.object({
host: z
.union([z.string(), z.boolean()])
.optional()
.default(ASTRO_CONFIG_DEFAULTS.server.host),
port: z.number().optional().default(ASTRO_CONFIG_DEFAULTS.server.port),
headers: z.custom<OutgoingHttpHeaders>().optional(),
})
.optional()
.default({})
),
markdown: z
.object({
drafts: z.boolean().default(false),
syntaxHighlight: z
.union([z.literal('shiki'), z.literal('prism'), z.literal(false)])
.default(ASTRO_CONFIG_DEFAULTS.markdown.syntaxHighlight),
shikiConfig: z
.object({
langs: z.custom<ILanguageRegistration>().array().default([]),
theme: z
.enum(BUNDLED_THEMES as [Theme, ...Theme[]])
.or(z.custom<IThemeRegistration>())
.default(ASTRO_CONFIG_DEFAULTS.markdown.shikiConfig.theme),
wrap: z.boolean().or(z.null()).default(ASTRO_CONFIG_DEFAULTS.markdown.shikiConfig.wrap),
})
.default({}),
remarkPlugins: z
.union([
z.string(),
z.tuple([z.string(), z.any()]),
z.custom<RemarkPlugin>((data) => typeof data === 'function'),
z.tuple([z.custom<RemarkPlugin>((data) => typeof data === 'function'), z.any()]),
])
.array()
.default(ASTRO_CONFIG_DEFAULTS.markdown.remarkPlugins),
rehypePlugins: z
.union([
z.string(),
z.tuple([z.string(), z.any()]),
z.custom<RehypePlugin>((data) => typeof data === 'function'),
z.tuple([z.custom<RehypePlugin>((data) => typeof data === 'function'), z.any()]),
])
.array()
.default(ASTRO_CONFIG_DEFAULTS.markdown.rehypePlugins),
remarkRehype: z
.custom<RemarkRehype>((data) => data instanceof Object && !Array.isArray(data))
.optional()
.default(ASTRO_CONFIG_DEFAULTS.markdown.remarkRehype),
gfm: z.boolean().default(ASTRO_CONFIG_DEFAULTS.markdown.gfm),
smartypants: z.boolean().default(ASTRO_CONFIG_DEFAULTS.markdown.smartypants),
})
.default({}),
vite: z
.custom<ViteUserConfig>((data) => data instanceof Object && !Array.isArray(data))
.default(ASTRO_CONFIG_DEFAULTS.vite),
experimental: z.object({}).optional().default({}),
legacy: z.object({}).optional().default({}),
});
export function createRelativeSchema(cmd: string, fileProtocolRoot: URL) {
// We need to extend the global schema to add transforms that are relative to root.
// This is type checked against the global schema to make sure we still match.
const AstroConfigRelativeSchema = AstroConfigSchema.extend({
root: z
.string()
.default(ASTRO_CONFIG_DEFAULTS.root)
.transform((val) => new URL(appendForwardSlash(val), fileProtocolRoot)),
srcDir: z
.string()
.default(ASTRO_CONFIG_DEFAULTS.srcDir)
.transform((val) => new URL(appendForwardSlash(val), fileProtocolRoot)),
publicDir: z
.string()
.default(ASTRO_CONFIG_DEFAULTS.publicDir)
.transform((val) => new URL(appendForwardSlash(val), fileProtocolRoot)),
outDir: z
.string()
.default(ASTRO_CONFIG_DEFAULTS.outDir)
.transform((val) => new URL(appendForwardSlash(val), fileProtocolRoot)),
build: z
.object({
format: z
.union([z.literal('file'), z.literal('directory')])
.optional()
.default(ASTRO_CONFIG_DEFAULTS.build.format),
client: z
.string()
.optional()
.default(ASTRO_CONFIG_DEFAULTS.build.client)
.transform((val) => new URL(val, fileProtocolRoot)),
server: z
.string()
.optional()
.default(ASTRO_CONFIG_DEFAULTS.build.server)
.transform((val) => new URL(val, fileProtocolRoot)),
assets: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.assets),
serverEntry: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.serverEntry),
})
.optional()
.default({}),
server: z.preprocess(
// preprocess
(val) => {
if (typeof val === 'function') {
const result = val({ command: cmd === 'dev' ? 'dev' : 'preview' });
// @ts-expect-error revive attached prop added from CLI flags
if (val.port) result.port = val.port;
// @ts-expect-error revive attached prop added from CLI flags
if (val.host) result.host = val.host;
return result;
} else {
return val;
}
},
// validate
z
.object({
host: z
.union([z.string(), z.boolean()])
.optional()
.default(ASTRO_CONFIG_DEFAULTS.server.host),
port: z.number().optional().default(ASTRO_CONFIG_DEFAULTS.server.port),
headers: z.custom<OutgoingHttpHeaders>().optional(),
streaming: z.boolean().optional().default(true),
})
.optional()
.default({})
),
}).transform((config) => {
// If the user changed outDir but not build.server, build.config, adjust so those
// are relative to the outDir, as is the expected default.
if (
!config.build.server.toString().startsWith(config.outDir.toString()) &&
config.build.server.toString().endsWith('dist/server/')
) {
config.build.server = new URL('./dist/server/', config.outDir);
}
if (
!config.build.client.toString().startsWith(config.outDir.toString()) &&
config.build.client.toString().endsWith('dist/client/')
) {
config.build.client = new URL('./dist/client/', config.outDir);
}
const trimmedBase = trimSlashes(config.base);
// If there is no base but there is a base for site config, warn.
const sitePathname = config.site && new URL(config.site).pathname;
if (!trimmedBase.length && sitePathname && sitePathname !== '/') {
config.base = sitePathname;
/* eslint-disable no-console */
console.warn(`The site configuration value includes a pathname of ${sitePathname} but there is no base configuration.
A future version of Astro will stop using the site pathname when producing <link> and <script> tags. Set your site's base with the base configuration.`);
}
if (trimmedBase.length && config.trailingSlash === 'never') {
config.base = prependForwardSlash(trimmedBase);
} else {
config.base = prependForwardSlash(appendForwardSlash(trimmedBase));
}
return config;
});
return AstroConfigRelativeSchema;
}