Skip to content

Commit 51dc858

Browse files
feat: update type definitions for manifest options (#573)
* Update types.ts * Update types.ts * Update types.ts * Update types.ts * Update types.ts * Update types.ts * Update types.ts * Update types.ts * Update types.ts * chore: update icon purpose --------- Co-authored-by: userquin <[email protected]>
1 parent 17cddee commit 51dc858

File tree

3 files changed

+74
-15
lines changed

3 files changed

+74
-15
lines changed

examples/vue-router/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const pwaOptions: Partial<VitePWAOptions> = {
2828
src: 'pwa-512x512.png', // <== don't add slash, for testing
2929
sizes: '512x512',
3030
type: 'image/png',
31-
purpose: 'any maskable',
31+
purpose: ['any', 'maskable'], // testing new type declaration
3232
},
3333
],
3434
},

src/options.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,30 @@ export async function resolveOptions(options: Partial<VitePWAOptions>, viteConfi
135135
devOptions.type = 'classic'
136136
}
137137

138+
// convert icons' purpose
139+
if (manifest) {
140+
if (manifest.icons) {
141+
manifest.icons = manifest.icons.map((icon) => {
142+
if (icon.purpose && Array.isArray(icon.purpose))
143+
icon.purpose = icon.purpose.join(' ')
144+
145+
return icon
146+
})
147+
}
148+
if (manifest.shortcuts) {
149+
manifest.shortcuts.forEach((shortcut) => {
150+
if (shortcut.icons) {
151+
shortcut.icons = shortcut.icons.map((icon) => {
152+
if (icon.purpose && Array.isArray(icon.purpose))
153+
icon.purpose = icon.purpose.join(' ')
154+
155+
return icon
156+
})
157+
}
158+
})
159+
}
160+
}
161+
138162
const resolvedVitePWAOptions: ResolvedVitePWAOptions = {
139163
base: basePath,
140164
mode,

src/types.ts

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,27 @@ export interface ShareTargetFiles {
207207
}
208208

209209
/**
210-
* https://developer.mozilla.org/en-US/docs/Web/Manifest/launch_handler#launch_handler_item_values
210+
* @see https://developer.mozilla.org/en-US/docs/Web/Manifest/launch_handler#launch_handler_item_values
211211
*/
212212
export type LaunchHandlerClientMode = 'auto' | 'focus-existing' | 'navigate-existing' | 'navigate-new'
213213

214+
export type Display = 'fullscreen' | 'standalone' | 'minimal-ui' | 'browser'
215+
export type DisplayOverride = Display | 'window-controls-overlay'
216+
export type IconPurpose = 'monochrome' | 'maskable' | 'any'
217+
218+
/**
219+
* @see https://w3c.github.io/manifest/#manifest-image-resources
220+
*/
221+
export interface IconResource {
222+
sizes?: string
223+
src: string
224+
type?: string
225+
/**
226+
* **NOTE**: string values for backward compatibility with the old type.
227+
*/
228+
purpose?: string | IconPurpose | IconPurpose[]
229+
}
230+
214231
export interface ManifestOptions {
215232
/**
216233
* @default _npm_package_name_
@@ -225,13 +242,20 @@ export interface ManifestOptions {
225242
*/
226243
description: string
227244
/**
228-
*
245+
* @default []
246+
* @see https://developer.mozilla.org/en-US/docs/Web/Manifest/icons
247+
* @see https://w3c.github.io/manifest/#icons-member
229248
*/
230-
icons: Record<string, any>[]
249+
icons: IconResource[]
231250
/**
232-
*
251+
* @default []
252+
* @see https://developer.mozilla.org/en-US/docs/Web/Manifest/file_handlers
253+
* @see https://wicg.github.io/manifest-incubations/#file_handlers-member
233254
*/
234-
file_handlers: Record<string, any>[]
255+
file_handlers: {
256+
action: string
257+
accept: Record<string, string[]>
258+
}[]
235259
/**
236260
* @default `routerBase + '?standalone=true'`
237261
*/
@@ -250,12 +274,16 @@ export interface ManifestOptions {
250274
orientation: 'any' | 'natural' | 'landscape' | 'landscape-primary' | 'landscape-secondary' | 'portrait' | 'portrait-primary' | 'portrait-secondary'
251275
/**
252276
* @default `standalone`
277+
* @see https://developer.mozilla.org/en-US/docs/Web/Manifest/display
278+
* @see https://w3c.github.io/manifest/#display-member
253279
*/
254-
display: string
280+
display: Display
255281
/**
256282
* @default []
283+
* @see https://developer.mozilla.org/en-US/docs/Web/Manifest/display_override
284+
* @see https://wicg.github.io/manifest-incubations/#display_override-member
257285
*/
258-
display_override: string[]
286+
display_override: DisplayOverride[]
259287
/**
260288
* @default `#ffffff`
261289
*/
@@ -297,16 +325,19 @@ export interface ManifestOptions {
297325
}[]
298326
/**
299327
* @default []
328+
* @see https://developer.mozilla.org/en-US/docs/Web/Manifest/shortcuts
329+
* @see https://w3c.github.io/manifest/#shortcuts-member
300330
*/
301331
shortcuts: {
302332
name: string
303333
short_name?: string
304334
url: string
305335
description?: string
306-
icons?: Record<string, any>[]
336+
icons?: IconResource[]
307337
}[]
308338
/**
309339
* @default []
340+
* @see https://developer.mozilla.org/en-US/docs/Web/Manifest/screenshots
310341
*/
311342
screenshots: {
312343
src: string
@@ -324,9 +355,13 @@ export interface ManifestOptions {
324355
* @default ''
325356
*/
326357
iarc_rating_id: string
358+
/**
359+
* @see https://developer.mozilla.org/en-US/docs/Web/Manifest/share_target
360+
* @see https://w3c.github.io/web-share-target/level-2/#share_target-member
361+
*/
327362
share_target: {
328363
action: string
329-
method?: string
364+
method?: 'GET' | 'POST'
330365
enctype?: string
331366
params: {
332367
title?: string
@@ -336,23 +371,23 @@ export interface ManifestOptions {
336371
}
337372
}
338373
/**
339-
* https://github.com/WICG/pwa-url-handler/blob/main/handle_links/explainer.md#handle_links-manifest-member
374+
* @see https://github.com/WICG/pwa-url-handler/blob/main/handle_links/explainer.md#handle_links-manifest-member
340375
*/
341376
handle_links?: 'auto' | 'preferred' | 'not-preferred'
342377
/**
343-
* https://developer.mozilla.org/en-US/docs/Web/Manifest/launch_handler#launch_handler_item_values
378+
* @see https://developer.mozilla.org/en-US/docs/Web/Manifest/launch_handler#launch_handler_item_values
344379
*/
345380
launch_handler?: {
346381
client_mode: LaunchHandlerClientMode | LaunchHandlerClientMode[]
347382
}
348383
/**
349-
* https://learn.microsoft.com/en-us/microsoft-edge/progressive-web-apps-chromium/how-to/sidebar#enable-sidebar-support-in-your-pwa
384+
* @see https://learn.microsoft.com/en-us/microsoft-edge/progressive-web-apps-chromium/how-to/sidebar#enable-sidebar-support-in-your-pwa
350385
*/
351386
edge_side_panel?: {
352387
preferred_width?: number
353388
}
354389
/**
355-
* https://github.com/WICG/manifest-incubations/blob/gh-pages/scope_extensions-explainer.md
390+
* @see https://github.com/WICG/manifest-incubations/blob/gh-pages/scope_extensions-explainer.md
356391
* @default []
357392
*/
358393
scope_extensions: {
@@ -407,7 +442,7 @@ export interface VitePluginPWAAPI {
407442
*/
408443
pwaInDevEnvironment: boolean
409444
/**
410-
* Returns the PWA webmanifest url for the manifest link:
445+
* Returns the PWA web manifest url for the manifest link:
411446
* <link rel="manifest" href="<webManifestUrl>" />
412447
*
413448
* Will also return if the manifest will require credentials:

0 commit comments

Comments
 (0)