-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvelte_config.ts
More file actions
140 lines (121 loc) · 4.45 KB
/
svelte_config.ts
File metadata and controls
140 lines (121 loc) · 4.45 KB
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
import type {Config as SvelteConfig} from '@sveltejs/kit';
import type {CompileOptions, ModuleCompileOptions, PreprocessorGroup} from 'svelte/compiler';
import {join} from 'node:path';
import {EMPTY_OBJECT} from '@fuzdev/fuz_util/object.js';
import {SVELTE_CONFIG_FILENAME} from './constants.ts';
/* eslint-disable @typescript-eslint/no-deprecated */
// see https://github.com/sveltejs/kit/discussions/14240
/*
This module is intended to have minimal dependencies to avoid over-imports in the CLI.
*/
/**
* Loads a SvelteKit config at `dir`.
* @returns `null` if no config is found
*/
export const load_svelte_config = async ({
dir = process.cwd(),
config_filename = SVELTE_CONFIG_FILENAME,
}: {dir?: string; config_filename?: string} = EMPTY_OBJECT): Promise<SvelteConfig | null> => {
try {
return (await import(join(dir, config_filename))).default;
} catch (_err) {
return null;
}
};
/**
* A subset of SvelteKit's config in a form that Gro uses
* because SvelteKit doesn't expose its config resolver.
* Flattens things out to keep them simple and easy to pass around,
* and doesn't deal with most properties, but includes the full `svelte_config`.
* The `base` and `assets` in particular are renamed for clarity with Gro's internal systems,
* so these properties become first-class vocabulary inside Gro.
*/
export interface ParsedSvelteConfig {
// TODO probably fill these out with defaults
svelte_config: SvelteConfig | null;
alias: Record<string, string>;
base_url: '' | `/${string}` | undefined;
assets_url: '' | `http://${string}` | `https://${string}` | undefined;
// TODO others, but maybe replace with a Zod schema? https://svelte.dev/docs/kit/configuration
/**
* Same as the SvelteKit `files.assets`.
*/
assets_path: string;
/**
* Same as the SvelteKit `files.lib`.
*/
lib_path: string;
/**
* Same as the SvelteKit `files.routes`.
*/
routes_path: string;
env_dir: string | undefined;
private_prefix: string | undefined;
public_prefix: string | undefined;
svelte_compile_options: CompileOptions;
svelte_compile_module_options: ModuleCompileOptions;
svelte_preprocessors: PreprocessorGroup | Array<PreprocessorGroup> | undefined;
}
// TODO currently incomplete and hack - maybe rethink
/**
* Returns Gro-relevant properties of a SvelteKit config
* as a convenience wrapper around `load_svelte_config`.
* Needed because SvelteKit doesn't expose its config resolver.
*/
export const parse_svelte_config = async ({
dir_or_config = process.cwd(), // TODO maybe not the best API, maybe a type union? `({svelte_config} | {dir}) & {config_filename}`
config_filename = SVELTE_CONFIG_FILENAME,
}: {
dir_or_config?: string | SvelteConfig;
config_filename?: string;
} = EMPTY_OBJECT): Promise<ParsedSvelteConfig> => {
const svelte_config =
typeof dir_or_config === 'string'
? await load_svelte_config({dir: dir_or_config, config_filename})
: dir_or_config;
const kit = svelte_config?.kit;
const alias = {$lib: 'src/lib', ...kit?.alias};
const base_url = kit?.paths?.base;
const assets_url = kit?.paths?.assets;
// TODO probably a Zod schema instead
const assets_path = kit?.files?.assets ?? 'static';
const lib_path = kit?.files?.lib ?? 'src/lib';
const routes_path = kit?.files?.routes ?? 'src/routes';
const env_dir = kit?.env?.dir;
const private_prefix = kit?.env?.privatePrefix;
const public_prefix = kit?.env?.publicPrefix;
const svelte_compile_options: CompileOptions = svelte_config?.compilerOptions ?? {};
// Change the default to `generate: 'server'`,
// because SvelteKit handles the client in the normal cases.
if (svelte_compile_options.generate === undefined) {
svelte_compile_options.generate = 'server';
}
const svelte_compile_module_options = to_default_compile_module_options(svelte_compile_options); // TODO will kit have these separately?
const svelte_preprocessors = svelte_config?.preprocess;
return {
svelte_config,
alias,
base_url,
assets_url,
assets_path,
lib_path,
routes_path,
env_dir,
private_prefix,
public_prefix,
svelte_compile_options,
svelte_compile_module_options,
svelte_preprocessors,
};
};
export const to_default_compile_module_options = ({
dev,
generate,
filename,
rootDir,
warningFilter,
}: CompileOptions): ModuleCompileOptions => ({dev, generate, filename, rootDir, warningFilter});
/**
* The parsed SvelteKit config for the cwd, cached globally at the module level.
*/
export const default_svelte_config = await parse_svelte_config(); // always load it to keep things simple ahead