-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
options.js
223 lines (188 loc) · 4.89 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
const noop = () => {};
/** @typedef {import('./types').ConfigDefinition} ConfigDefinition */
/** @type {Record<string, ConfigDefinition>} */
const options = {
compilerOptions: {
type: 'leaf',
default: null,
validate: noop
},
extensions: {
type: 'leaf',
default: ['.svelte'],
validate: (option, keypath) => {
if (!Array.isArray(option) || !option.every((page) => typeof page === 'string')) {
throw new Error(`${keypath} must be an array of strings`);
}
option.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 option;
}
},
kit: {
type: 'branch',
children: {
adapter: {
type: 'leaf',
default: null,
validate: (option, keypath) => {
if (typeof option !== 'object' || !option.adapt) {
let message = `${keypath} should be an object with an "adapt" method`;
if (Array.isArray(option) || typeof option === '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 option;
}
},
amp: expect_boolean(false),
appDir: expect_string('_app', false),
files: {
type: 'branch',
children: {
assets: expect_string('static'),
hooks: expect_string('src/hooks'),
lib: expect_string('src/lib'),
routes: expect_string('src/routes'),
serviceWorker: expect_string('src/service-worker'),
// TODO remove this, eventually
setup: expect_string('src/setup'),
template: expect_string('src/app.html')
}
},
floc: expect_boolean(false),
host: expect_string(null),
hostHeader: expect_string(null),
hydrate: expect_boolean(true),
paths: {
type: 'branch',
children: {
base: expect_string(''),
assets: expect_string('')
}
},
prerender: {
type: 'branch',
children: {
crawl: expect_boolean(true),
enabled: expect_boolean(true),
force: expect_boolean(false),
pages: {
type: 'leaf',
default: ['*'],
validate: (option, keypath) => {
if (!Array.isArray(option) || !option.every((page) => typeof page === 'string')) {
throw new Error(`${keypath} must be an array of strings`);
}
option.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 option;
}
}
}
},
router: expect_boolean(true),
ssr: expect_boolean(true),
target: expect_string(null),
trailingSlash: expect_enum(['never', 'always', 'ignore']),
vite: {
type: 'leaf',
default: () => ({}),
validate: (option, keypath) => {
if (typeof option === 'object') {
const config = option;
option = () => config;
}
if (typeof option !== 'function') {
throw new Error(
`${keypath} must be a Vite config object (https://vitejs.dev/config) or a function that returns one`
);
}
return option;
}
}
}
},
preprocess: {
type: 'leaf',
default: null,
validate: noop
}
};
/**
* @param {string} string
* @param {boolean} allow_empty
* @returns {ConfigDefinition}
*/
function expect_string(string, allow_empty = true) {
return {
type: 'leaf',
default: string,
validate: (option, keypath) => {
assert_is_string(option, keypath);
if (!allow_empty && option === '') {
throw new Error(`${keypath} cannot be empty`);
}
return option;
}
};
}
/**
* @param {boolean} boolean
* @returns {ConfigDefinition}
*/
function expect_boolean(boolean) {
return {
type: 'leaf',
default: boolean,
validate: (option, keypath) => {
if (typeof option !== 'boolean') {
throw new Error(`${keypath} should be true or false, if specified`);
}
return option;
}
};
}
/**
* @param {string[]} options
* @returns {ConfigDefinition}
*/
function expect_enum(options, def = options[0]) {
return {
type: 'leaf',
default: def,
validate: (option, keypath) => {
if (!options.includes(option)) {
// prettier-ignore
const msg = options.length > 2
? `${keypath} should be one of ${options.slice(0, -1).map(option => `"${option}"`).join(', ')} or "${options[options.length - 1]}"`
: `${keypath} should be either "${options[0]}" or "${options[1]}"`;
throw new Error(msg);
}
return option;
}
};
}
/**
* @param {any} option
* @param {string} keypath
*/
function assert_is_string(option, keypath) {
if (typeof option !== 'string') {
throw new Error(`${keypath} should be a string, if specified`);
}
}
export default options;