-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
config.ts
179 lines (165 loc) · 4.45 KB
/
config.ts
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
import type { LoadConfigResult, LoadConfigSource } from 'unconfig'
import type { HtmlLinkPreset } from './api'
import type { Preset } from './preset.ts'
import type { LogLevel } from './types.ts'
import { existsSync, statSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
import process from 'node:process'
import { createConfigLoader as createLoader } from 'unconfig'
export type { LoadConfigResult, LoadConfigSource }
export * from './api/defaults.ts'
export * from './presets/index.ts'
export * from './splash.ts'
export * from './types.ts'
export { defaultAssetName } from './utils.ts'
export type { Preset }
/**
* Built-in presets.
* - `minimal-2023`: Only generate the bare minimum assets.
* - `minimal`: Only generate the bare minimum assets (deprecated).
* - `android`: Generate assets for Android.
* - `windows`: Generate assets for Windows.
* - `ios`: Generate assets for iOS.
* - `all`: `android`, `windows` and `ios` presets combined.
*/
export type BuiltInPreset = 'minimal' | 'minimal-2023' | 'android' | 'windows' | 'ios' | 'all'
export interface HeadLinkOptions {
/**
* Base path to generate the html head links.
*
* @default '/'
*/
basePath?: string
/**
* The preset to use.
*
* If using the built-in presets from CLI (`minimal` or `minimal-2023`), this option will be ignored (will be set to `default` or `2023` for `minimal` and `minimal-2023` respectively).
*
* @default 'default'
*/
preset?: HtmlLinkPreset
/**
* By default, the SVG favicon will use the SVG file name as the name.
*
* For example, if you provide `public/logo.svg` as the image source, the href in the link will be `<basePath>logo.svg`.
*
* @param name The name of the SVG icons.
*/
resolveSvgName?: (name: string) => string
/**
* Generate an id when generating the html head links.
*
* @default false
*/
xhtml?: boolean
/**
* Include the id when generating the html head links.
*
* @default false
*/
includeId?: boolean
}
export interface UserConfig {
/**
* Project root directory. Can be an absolute path, or a path relative from
* the location of the config file itself.
* @default process.cwd()
*/
root?: string
/**
* Path to the config file.
*
* Default resolving to `pwa-assets.config.[js|mjs|cjs]`
*
* Setting to `false` will disable config resolving.
*/
config?: string | false
/**
* Override assets?
*
* @default true
*/
overrideAssets?: boolean
/**
* Log level.
*
* @default 'info'
*/
logLevel?: LogLevel
/**
* Path relative to `root` where to find the images to use for generating PWA assets.
*
* PWA Assets will be generated in the same directory.
*/
images?: string | string[]
/**
* Preset to use.
*
* @default 'minimal'
*/
preset?: BuiltInPreset | Preset
/**
* Options for generating the html head links for `apple-touch-icon` and favicons.
*/
headLinkOptions?: HeadLinkOptions
/**
* Show the PWA web manifest icons' entry.
*
* @default true
*/
manifestIconsEntry?: boolean
}
export interface ResolvedConfig extends Required<Omit<UserConfig, 'preset'>> {
preset: Preset
}
export function defineConfig(config: UserConfig): UserConfig {
return config
}
export async function loadConfig<U extends UserConfig>(
cwd = process.cwd(),
configOrPath: string | U = cwd,
extraConfigSources: LoadConfigSource[] = [],
defaults: UserConfig = { overrideAssets: true, logLevel: 'info' },
): Promise<LoadConfigResult<U>> {
let inlineConfig = {} as U
if (typeof configOrPath !== 'string') {
inlineConfig = configOrPath
if (inlineConfig.config === false) {
return {
config: inlineConfig as U,
sources: [],
}
}
else {
configOrPath = inlineConfig.config || process.cwd()
}
}
const resolved = resolve(cwd, configOrPath)
let isFile = false
if (existsSync(resolved) && statSync(resolved).isFile()) {
isFile = true
cwd = dirname(resolved).replace(/\\/g, '/')
}
const loader = createLoader<U>({
sources: isFile
? [
{
files: resolved,
extensions: [],
},
]
: [
{
files: [
'pwa-assets.config',
],
},
...extraConfigSources,
],
cwd,
defaults: inlineConfig,
})
const result = await loader.load()
result.config = Object.assign(defaults, result.config || inlineConfig)
return result
}