forked from sveltejs/kit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.js
327 lines (274 loc) · 8.29 KB
/
options.js
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/** @typedef {import('./types').ConfigDefinition} ConfigDefinition */
/** @typedef {import('./types').Validator} Validator */
/** @type {Validator} */
const options = object(
{
extensions: validate(['.svelte'], (input, keypath) => {
if (!Array.isArray(input) || !input.every((page) => typeof page === 'string')) {
throw new Error(`${keypath} must be an array of strings`);
}
input.forEach((extension) => {
if (extension[0] !== '.') {
throw new Error(`Each member of ${keypath} must start with '.' — saw '${extension}'`);
}
if (!/^(\.[a-z0-9]+)+$/i.test(extension)) {
throw new Error(`File extensions must be alphanumeric — saw '${extension}'`);
}
});
return input;
}),
kit: object({
adapter: validate(null, (input, keypath) => {
if (typeof input !== 'object' || !input.adapt) {
let message = `${keypath} should be an object with an "adapt" method`;
if (Array.isArray(input) || typeof input === 'string') {
// for the early adapter adopters
message += ', rather than the name of an adapter';
}
throw new Error(`${message}. See https://kit.svelte.dev/docs#adapters`);
}
return input;
}),
amp: boolean(false),
appDir: validate('_app', (input, keypath) => {
assert_string(input, keypath);
if (input) {
if (input.startsWith('/') || input.endsWith('/')) {
throw new Error(
"config.kit.appDir cannot start or end with '/'. See https://kit.svelte.dev/docs#configuration"
);
}
} else {
throw new Error(`${keypath} cannot be empty`);
}
return input;
}),
files: object({
assets: string('static'),
hooks: string('src/hooks'),
lib: string('src/lib'),
routes: string('src/routes'),
serviceWorker: string('src/service-worker'),
template: string('src/app.html')
}),
floc: boolean(false),
host: string(null),
hostHeader: string(null),
hydrate: boolean(true),
package: object({
dir: string('package'),
// excludes all .d.ts and filename starting with _
exports: fun((filepath) => !/^_|\/_|\.d\.ts$/.test(filepath)),
files: fun(() => true),
emitTypes: boolean(true)
}),
paths: object({
base: validate('', (input, keypath) => {
assert_string(input, keypath);
if (input !== '' && (input.endsWith('/') || !input.startsWith('/'))) {
throw new Error(
`${keypath} option must be a root-relative path that starts but doesn't end with '/'. See https://kit.svelte.dev/docs#configuration-paths`
);
}
return input;
}),
assets: validate('', (input, keypath) => {
assert_string(input, keypath);
if (input) {
if (!/^[a-z]+:\/\//.test(input)) {
throw new Error(
`${keypath} option must be an absolute path, if specified. See https://kit.svelte.dev/docs#configuration-paths`
);
}
if (input.endsWith('/')) {
throw new Error(
`${keypath} option must not end with '/'. See https://kit.svelte.dev/docs#configuration-paths`
);
}
}
return input;
})
}),
prerender: object({
crawl: boolean(true),
enabled: boolean(true),
entries: validate(['*'], (input, keypath) => {
if (!Array.isArray(input) || !input.every((page) => typeof page === 'string')) {
throw new Error(`${keypath} must be an array of strings`);
}
input.forEach((page) => {
if (page !== '*' && page[0] !== '/') {
throw new Error(
`Each member of ${keypath} must be either '*' or an absolute path beginning with '/' — saw '${page}'`
);
}
});
return input;
}),
// TODO: remove this for the 1.0 release
force: validate(undefined, (input, keypath) => {
if (typeof input !== undefined) {
const newSetting = input ? 'continue' : 'fail';
const needsSetting = newSetting === 'continue';
throw new Error(
`${keypath} has been removed in favor of \`onError\`. In your case, set \`onError\` to "${newSetting}"${
needsSetting ? '' : ' (or leave it undefined)'
} to get the same behavior as you would with \`force: ${JSON.stringify(input)}\``
);
}
}),
onError: validate('fail', (input, keypath) => {
if (typeof input === 'function') return input;
if (['continue', 'fail'].includes(input)) return input;
throw new Error(
`${keypath} should be either a custom function or one of "continue" or "fail"`
);
}),
// TODO: remove this for the 1.0 release
pages: validate(undefined, (input, keypath) => {
if (typeof input !== undefined) {
throw new Error(`${keypath} has been renamed to \`entries\`.`);
}
})
}),
router: boolean(true),
serviceWorker: object({
files: fun((filename) => !/\.DS_STORE/.test(filename))
}),
ssr: boolean(true),
target: string(null),
trailingSlash: list(['never', 'always', 'ignore']),
vite: validate(
() => ({}),
(input, keypath) => {
if (typeof input === 'object') {
const config = input;
input = () => config;
}
if (typeof input !== 'function') {
throw new Error(
`${keypath} must be a Vite config object (https://vitejs.dev/config) or a function that returns one`
);
}
return input;
}
),
legacy: validate(null, (input, keypath) => {
if (typeof input !== 'object' || Array.isArray(input)) {
throw new Error(
`${keypath} should be an object. See https://github.com/vitejs/vite/tree/main/packages/plugin-legacy for all avaliable options`
);
}
return input;
})
})
},
true
);
/**
* @param {Record<string, Validator>} children
* @param {boolean} [allow_unknown]
* @returns {Validator}
*/
function object(children, allow_unknown = false) {
return (input, keypath) => {
/** @type {Record<string, any>} */
const output = {};
if ((input && typeof input !== 'object') || Array.isArray(input)) {
throw new Error(`${keypath} should be an object`);
}
for (const key in input) {
if (!(key in children)) {
if (allow_unknown) {
output[key] = input[key];
} else {
let message = `Unexpected option ${keypath}.${key}`;
// special case
if (keypath === 'config.kit' && key in options) {
message += ` (did you mean config.${key}?)`;
}
throw new Error(message);
}
}
}
for (const key in children) {
const validator = children[key];
output[key] = validator(input && input[key], `${keypath}.${key}`);
}
return output;
};
}
/**
* @param {any} fallback
* @param {(value: any, keypath: string) => any} fn
* @returns {Validator}
*/
function validate(fallback, fn) {
return (input, keypath) => {
return input === undefined ? fallback : fn(input, keypath);
};
}
/**
* @param {string | null} fallback
* @param {boolean} allow_empty
* @returns {Validator}
*/
function string(fallback, allow_empty = true) {
return validate(fallback, (input, keypath) => {
assert_string(input, keypath);
if (!allow_empty && input === '') {
throw new Error(`${keypath} cannot be empty`);
}
return input;
});
}
/**
* @param {boolean} fallback
* @returns {Validator}
*/
function boolean(fallback) {
return validate(fallback, (input, keypath) => {
if (typeof input !== 'boolean') {
throw new Error(`${keypath} should be true or false, if specified`);
}
return input;
});
}
/**
* @param {string[]} options
* @returns {Validator}
*/
function list(options, fallback = options[0]) {
return validate(fallback, (input, keypath) => {
if (!options.includes(input)) {
// prettier-ignore
const msg = options.length > 2
? `${keypath} should be one of ${options.slice(0, -1).map(input => `"${input}"`).join(', ')} or "${options[options.length - 1]}"`
: `${keypath} should be either "${options[0]}" or "${options[1]}"`;
throw new Error(msg);
}
return input;
});
}
/**
* @param {(filename: string) => boolean} fallback
* @returns {Validator}
*/
function fun(fallback) {
return validate(fallback, (input, keypath) => {
if (typeof input !== 'function') {
throw new Error(`${keypath} should be a function, if specified`);
}
return input;
});
}
/**
* @param {string} input
* @param {string} keypath
*/
function assert_string(input, keypath) {
if (typeof input !== 'string') {
throw new Error(`${keypath} should be a string, if specified`);
}
}
export default options;