Skip to content

Commit 27cb449

Browse files
committed
chore: improve types
1 parent 3de869e commit 27cb449

File tree

11 files changed

+56
-40
lines changed

11 files changed

+56
-40
lines changed

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@
55
"repository": "nuxt-community/pwa-module",
66
"license": "MIT",
77
"main": "dist/pwa.js",
8-
"types": "./types/index.d.ts",
8+
"types": "./dist/pwa.d.ts",
99
"files": [
1010
"lib",
1111
"dist",
12-
"templates",
13-
"types"
12+
"templates"
1413
],
1514
"scripts": {
1615
"build": "siroc build",
1716
"dev": "nuxt-ts test/fixture",
1817
"lint": "eslint --ext .js,.vue,.ts .",
19-
"release": "yarn test && standard-version && git push --follow-tags && npm publish",
18+
"release": "yarn test && yarn build && standard-version && git push --follow-tags && npm publish",
2019
"test": "yarn lint && jest"
2120
},
2221
"dependencies": {

src/icon.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { join, resolve } from 'path'
22
import { fork } from 'child_process'
33
import fs from 'fs-extra'
44
import hasha from 'hasha'
5-
import type { IconOptions } from '../types/icon'
5+
import type { PWAContext, IconOptions } from '../types'
66
import { joinUrl, getRouteParams, sizeName, emitAsset, PKG, PKG_DIR } from './utils'
77

8-
export async function icon (nuxt, pwa, moduleContainer) {
8+
export async function icon (nuxt, pwa: PWAContext, moduleContainer) {
99
const { publicPath } = getRouteParams(nuxt.options)
1010

1111
// Defaults

src/manifest.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import hasha from 'hasha'
2-
import type { ManifestOptions } from '../types/manifest'
2+
import type { ManifestOptions, PWAContext } from '../types'
33
import { joinUrl, getRouteParams, emitAsset } from './utils'
44

5-
export function manifest (nuxt, pwa) {
5+
export function manifest (nuxt, pwa: PWAContext) {
66
const { routerBase, publicPath } = getRouteParams(nuxt.options)
77

88
// Combine sources
@@ -27,6 +27,7 @@ export function manifest (nuxt, pwa) {
2727

2828
// Remove extra fields from manifest
2929
const manifest = { ...options }
30+
// @ts-ignore
3031
delete manifest.src
3132
delete manifest.publicPath
3233
delete manifest.useWebmanifestExtension

src/meta.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { join, resolve } from 'path'
22
import { existsSync, readJsonSync } from 'fs-extra'
33
import { mergeMeta } from '../lib/meta.utils'
4-
import type { MetaOptions } from '../types/meta'
4+
import type { MetaOptions, PWAContext } from '../types'
55
import { isUrl, PKG_DIR } from './utils'
66

7-
export function meta (nuxt, pwa, moduleContainer) {
7+
export function meta (nuxt, pwa: PWAContext, moduleContainer) {
88
// Defaults
99
const defaults: MetaOptions = {
1010
name: process.env.npm_package_name,

src/pwa.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
import { resolve } from 'path'
22
import serveStatic from 'serve-static'
3+
import type { MetaOptions, ManifestOptions, IconOptions, PWAContext, WorkboxOptions } from '../types'
34
import { PKG } from './utils'
45
import { icon } from './icon'
56
import { manifest } from './manifest'
67
import { meta, metaRuntime } from './meta'
78
import { workbox } from './workbox'
89

9-
export default async function pwa (moduleOptions) {
10+
interface PWAOptions {
11+
meta?: MetaOptions | false
12+
icon?: IconOptions | false
13+
workbox?: WorkboxOptions | false
14+
manifest?: ManifestOptions | false
15+
}
16+
17+
export default async function pwa (moduleOptions: PWAOptions) {
1018
const { nuxt } = this
1119
const moduleContainer = this // TODO: remove dependency when module-utils
1220

@@ -24,7 +32,7 @@ export default async function pwa (moduleOptions) {
2432

2533
// Shared options context
2634
nuxt.options.pwa = { ...(nuxt.options.pwa || {}), ...(moduleOptions || {}) }
27-
const { pwa } = nuxt.options
35+
const pwa: PWAContext = nuxt.options.pwa
2836

2937
// Normalize options
3038
for (const name in modules) {
@@ -60,4 +68,14 @@ export default async function pwa (moduleOptions) {
6068
}
6169
}
6270

71+
declare module '@nuxt/types/config/index' {
72+
interface NuxtOptions {
73+
pwa?: PWAOptions
74+
meta?: MetaOptions | false
75+
icon?: IconOptions | false
76+
workbox?: WorkboxOptions | false
77+
manifest?: ManifestOptions | false
78+
}
79+
}
80+
6381
pwa.meta = PKG

src/workbox/defaults.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { version as workboxVersion } from 'workbox-cdn/package.json'
2-
import type { WorkboxOptions } from '../../types/workbox'
2+
import type { WorkboxOptions } from '../../types'
33

44
export const defaults: WorkboxOptions = {
55
// General

src/workbox/options.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { resolve } from 'path'
22
import { joinUrl, getRouteParams, startCase, randomString, PKG_DIR } from '../utils'
3-
import type { WorkboxOptions } from '../../types/workbox'
3+
import type { WorkboxOptions, PWAContext } from '../../types'
44
import { defaults } from './defaults'
55

6-
export function getOptions (nuxt, pwa): WorkboxOptions {
6+
export function getOptions (nuxt, pwa: PWAContext): WorkboxOptions {
77
const options: WorkboxOptions = { ...defaults, ...pwa.workbox }
88

99
// enabled
@@ -62,28 +62,28 @@ export function getOptions (nuxt, pwa): WorkboxOptions {
6262
})
6363
}
6464

65-
// Add start_url to precaching
66-
if (pwa.manifest && pwa.manifest.start_url) {
67-
options.preCaching.unshift(pwa.manifest.start_url)
68-
}
69-
7065
// Default revision
7166
if (!options.cacheOptions.revision) {
7267
options.cacheOptions.revision = randomString(12)
7368
}
74-
const normalizePreCaching = (arr: any[]) => arr.map(url => ({
69+
const normalizePreCaching = (arr: any | any[]) => [].concat(arr).map(url => ({
7570
revision: options.cacheOptions.revision,
7671
...(typeof url === 'string' ? { url } : url)
7772
}))
7873

74+
// Add start_url to precaching
75+
if (pwa.manifest && pwa.manifest.start_url) {
76+
options.preCaching.unshift(...normalizePreCaching(pwa.manifest.start_url))
77+
}
78+
7979
// Add offlineAssets to precaching
8080
if (options.offlineAssets.length) {
8181
options.preCaching.unshift(...normalizePreCaching(options.offlineAssets))
8282
}
8383

8484
// Add offlinePage to precaching
8585
if (options.offlinePage) {
86-
options.preCaching.unshift(...(normalizePreCaching([options.offlinePage])))
86+
options.preCaching.unshift(...(normalizePreCaching(options.offlinePage)))
8787
}
8888

8989
// Default cacheId

tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
"outDir": "dist",
77
"declaration": true,
88
"resolveJsonModule": true,
9-
"esModuleInterop": true
9+
"esModuleInterop": true,
10+
"types": [
11+
"@nuxt/types"
12+
]
1013
},
1114
"include": [
1215
"src"

types/icon.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
export type iOSType = 'ipad' | 'ipadpro9' | 'ipadpro9' | 'ipadpro10' | 'ipadpro12' | 'iphonese' | 'iphone6' | 'iphoneplus' | 'iphonex' | 'iphonexr' | 'iphonexsmax'
32
export type iOSSize = [number, number, iOSType]
43

types/index.d.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
import { PWAOptions } from './pwa'
2-
3-
declare module '@nuxt/types/config/index' {
4-
interface NuxtOptions {
5-
pwa?: PWAOptions
6-
meta?: PWAOptions['meta']
7-
icon?: PWAOptions['icon']
8-
workbox?: PWAOptions['workbox']
9-
manifest?: PWAOptions['manifest']
10-
}
11-
}
1+
export { MetaOptions } from './meta'
2+
export { IconOptions } from './icon'
3+
export { WorkboxOptions } from './workbox'
4+
export { ManifestOptions } from './manifest'
5+
export { PWAContext } from './pwa'

0 commit comments

Comments
 (0)