/
index.js
100 lines (83 loc) · 2.88 KB
/
index.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
import fs from 'fs';
import path from 'path';
import * as url from 'url';
import options from './options.js';
/**
* Loads the template (src/app.html by default) and validates that it has the
* required content.
* @param {string} cwd
* @param {import('types').ValidatedConfig} config
*/
export function load_template(cwd, config) {
const { appTemplate } = config.kit.files;
const relative = path.relative(cwd, appTemplate);
if (fs.existsSync(appTemplate)) {
const contents = fs.readFileSync(appTemplate, 'utf8');
const expected_tags = ['%sveltekit.head%', '%sveltekit.body%'];
expected_tags.forEach((tag) => {
if (contents.indexOf(tag) === -1) {
throw new Error(`${relative} is missing ${tag}`);
}
});
} else {
throw new Error(`${relative} does not exist`);
}
return fs.readFileSync(appTemplate, 'utf-8');
}
/**
* Loads the error page (src/error.html by default) if it exists.
* Falls back to a generic error page content.
* @param {import('types').ValidatedConfig} config
*/
export function load_error_page(config) {
let { errorTemplate } = config.kit.files;
// Don't do this inside resolving the config, because that would mean
// adding/removing error.html isn't detected and would require a restart.
if (!fs.existsSync(config.kit.files.errorTemplate)) {
errorTemplate = url.fileURLToPath(new URL('./default-error.html', import.meta.url));
}
return fs.readFileSync(errorTemplate, 'utf-8');
}
/**
* Loads and validates svelte.config.js
* @param {{ cwd?: string }} options
* @returns {Promise<import('types').ValidatedConfig>}
*/
export async function load_config({ cwd = process.cwd() } = {}) {
const config_file = path.join(cwd, 'svelte.config.js');
if (!fs.existsSync(config_file)) {
return process_config({}, { cwd });
}
const config = await import(`${url.pathToFileURL(config_file).href}?ts=${Date.now()}`);
return process_config(config.default, { cwd });
}
/**
* @param {import('types').Config} config
* @returns {import('types').ValidatedConfig}
*/
function process_config(config, { cwd = process.cwd() } = {}) {
const validated = validate_config(config);
validated.kit.outDir = path.resolve(cwd, validated.kit.outDir);
for (const key in validated.kit.files) {
if (key === 'hooks') {
validated.kit.files.hooks.client = path.resolve(cwd, validated.kit.files.hooks.client);
validated.kit.files.hooks.server = path.resolve(cwd, validated.kit.files.hooks.server);
} else {
// @ts-expect-error
validated.kit.files[key] = path.resolve(cwd, validated.kit.files[key]);
}
}
return validated;
}
/**
* @param {import('types').Config} config
* @returns {import('types').ValidatedConfig}
*/
export function validate_config(config) {
if (typeof config !== 'object') {
throw new Error(
'svelte.config.js must have a configuration object as its default export. See https://kit.svelte.dev/docs/configuration'
);
}
return options(config, 'config');
}