-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
index.js
87 lines (73 loc) · 2.69 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
import fs from 'fs';
import path from 'path';
import * as url from 'url';
import { logger } from '../utils.js';
import options from './options.js';
/**
* @param {string} cwd
* @param {import('types/config').ValidatedConfig} config
*/
export function load_template(cwd, config) {
const { template } = config.kit.files;
const relative = path.relative(cwd, template);
if (fs.existsSync(template)) {
const contents = fs.readFileSync(template, 'utf8');
const expected_tags = ['%svelte.head%', '%svelte.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(template, 'utf-8');
}
export async function load_config({ cwd = process.cwd() } = {}) {
let config_file = path.join(cwd, 'svelte.config.js');
const config_file_cjs = path.join(cwd, 'svelte.config.cjs');
if (!fs.existsSync(config_file) && fs.existsSync(config_file_cjs)) {
config_file = config_file_cjs;
}
const config = await import(url.pathToFileURL(config_file).href);
const validated = validate_config(config.default);
validated.kit.files.assets = path.resolve(cwd, validated.kit.files.assets);
validated.kit.files.hooks = path.resolve(cwd, validated.kit.files.hooks);
validated.kit.files.lib = path.resolve(cwd, validated.kit.files.lib);
validated.kit.files.routes = path.resolve(cwd, validated.kit.files.routes);
validated.kit.files.serviceWorker = path.resolve(cwd, validated.kit.files.serviceWorker);
validated.kit.files.template = path.resolve(cwd, validated.kit.files.template);
return validated;
}
/**
* @param {import('types/config').Config} config
* @returns {import('types/config').ValidatedConfig}
*/
export function validate_config(config) {
const type = typeof config;
if (type === 'undefined') {
throw new Error(
'Your config is missing default exports. Make sure to include "export default config;"'
);
}
if (type !== 'object') {
throw new Error(
`Unexpected config type "${type}", make sure your default export is an object.`
);
}
return options(config, 'config');
}
/**
* @param {string[]} conflicts - array of conflicts in dotted notation
* @param {string=} pathPrefix - prepended in front of the path
* @param {string=} scope - used to prefix the whole error message
*/
export function print_config_conflicts(conflicts, pathPrefix = '', scope) {
const prefix = scope ? scope + ': ' : '';
const log = logger({ verbose: false });
conflicts.forEach((conflict) => {
log.error(
`${prefix}The value for ${pathPrefix}${conflict} specified in svelte.config.js has been ignored. This option is controlled by SvelteKit.`
);
});
}