Skip to content

Commit

Permalink
chore: improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 28, 2020
1 parent 3de869e commit 27cb449
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 40 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
"repository": "nuxt-community/pwa-module",
"license": "MIT",
"main": "dist/pwa.js",
"types": "./types/index.d.ts",
"types": "./dist/pwa.d.ts",
"files": [
"lib",
"dist",
"templates",
"types"
"templates"
],
"scripts": {
"build": "siroc build",
"dev": "nuxt-ts test/fixture",
"lint": "eslint --ext .js,.vue,.ts .",
"release": "yarn test && standard-version && git push --follow-tags && npm publish",
"release": "yarn test && yarn build && standard-version && git push --follow-tags && npm publish",
"test": "yarn lint && jest"
},
"dependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/icon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { join, resolve } from 'path'
import { fork } from 'child_process'
import fs from 'fs-extra'
import hasha from 'hasha'
import type { IconOptions } from '../types/icon'
import type { PWAContext, IconOptions } from '../types'
import { joinUrl, getRouteParams, sizeName, emitAsset, PKG, PKG_DIR } from './utils'

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

// Defaults
Expand Down
5 changes: 3 additions & 2 deletions src/manifest.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import hasha from 'hasha'
import type { ManifestOptions } from '../types/manifest'
import type { ManifestOptions, PWAContext } from '../types'
import { joinUrl, getRouteParams, emitAsset } from './utils'

export function manifest (nuxt, pwa) {
export function manifest (nuxt, pwa: PWAContext) {
const { routerBase, publicPath } = getRouteParams(nuxt.options)

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

// Remove extra fields from manifest
const manifest = { ...options }
// @ts-ignore
delete manifest.src
delete manifest.publicPath
delete manifest.useWebmanifestExtension
Expand Down
4 changes: 2 additions & 2 deletions src/meta.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { join, resolve } from 'path'
import { existsSync, readJsonSync } from 'fs-extra'
import { mergeMeta } from '../lib/meta.utils'
import type { MetaOptions } from '../types/meta'
import type { MetaOptions, PWAContext } from '../types'
import { isUrl, PKG_DIR } from './utils'

export function meta (nuxt, pwa, moduleContainer) {
export function meta (nuxt, pwa: PWAContext, moduleContainer) {
// Defaults
const defaults: MetaOptions = {
name: process.env.npm_package_name,
Expand Down
22 changes: 20 additions & 2 deletions src/pwa.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { resolve } from 'path'
import serveStatic from 'serve-static'
import type { MetaOptions, ManifestOptions, IconOptions, PWAContext, WorkboxOptions } from '../types'
import { PKG } from './utils'
import { icon } from './icon'
import { manifest } from './manifest'
import { meta, metaRuntime } from './meta'
import { workbox } from './workbox'

export default async function pwa (moduleOptions) {
interface PWAOptions {
meta?: MetaOptions | false
icon?: IconOptions | false
workbox?: WorkboxOptions | false
manifest?: ManifestOptions | false
}

export default async function pwa (moduleOptions: PWAOptions) {
const { nuxt } = this
const moduleContainer = this // TODO: remove dependency when module-utils

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

// Shared options context
nuxt.options.pwa = { ...(nuxt.options.pwa || {}), ...(moduleOptions || {}) }
const { pwa } = nuxt.options
const pwa: PWAContext = nuxt.options.pwa

// Normalize options
for (const name in modules) {
Expand Down Expand Up @@ -60,4 +68,14 @@ export default async function pwa (moduleOptions) {
}
}

declare module '@nuxt/types/config/index' {
interface NuxtOptions {
pwa?: PWAOptions
meta?: MetaOptions | false
icon?: IconOptions | false
workbox?: WorkboxOptions | false
manifest?: ManifestOptions | false
}
}

pwa.meta = PKG
2 changes: 1 addition & 1 deletion src/workbox/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { version as workboxVersion } from 'workbox-cdn/package.json'
import type { WorkboxOptions } from '../../types/workbox'
import type { WorkboxOptions } from '../../types'

export const defaults: WorkboxOptions = {
// General
Expand Down
18 changes: 9 additions & 9 deletions src/workbox/options.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { resolve } from 'path'
import { joinUrl, getRouteParams, startCase, randomString, PKG_DIR } from '../utils'
import type { WorkboxOptions } from '../../types/workbox'
import type { WorkboxOptions, PWAContext } from '../../types'
import { defaults } from './defaults'

export function getOptions (nuxt, pwa): WorkboxOptions {
export function getOptions (nuxt, pwa: PWAContext): WorkboxOptions {
const options: WorkboxOptions = { ...defaults, ...pwa.workbox }

// enabled
Expand Down Expand Up @@ -62,28 +62,28 @@ export function getOptions (nuxt, pwa): WorkboxOptions {
})
}

// Add start_url to precaching
if (pwa.manifest && pwa.manifest.start_url) {
options.preCaching.unshift(pwa.manifest.start_url)
}

// Default revision
if (!options.cacheOptions.revision) {
options.cacheOptions.revision = randomString(12)
}
const normalizePreCaching = (arr: any[]) => arr.map(url => ({
const normalizePreCaching = (arr: any | any[]) => [].concat(arr).map(url => ({
revision: options.cacheOptions.revision,
...(typeof url === 'string' ? { url } : url)
}))

// Add start_url to precaching
if (pwa.manifest && pwa.manifest.start_url) {
options.preCaching.unshift(...normalizePreCaching(pwa.manifest.start_url))
}

// Add offlineAssets to precaching
if (options.offlineAssets.length) {
options.preCaching.unshift(...normalizePreCaching(options.offlineAssets))
}

// Add offlinePage to precaching
if (options.offlinePage) {
options.preCaching.unshift(...(normalizePreCaching([options.offlinePage])))
options.preCaching.unshift(...(normalizePreCaching(options.offlinePage)))
}

// Default cacheId
Expand Down
5 changes: 4 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
"outDir": "dist",
"declaration": true,
"resolveJsonModule": true,
"esModuleInterop": true
"esModuleInterop": true,
"types": [
"@nuxt/types"
]
},
"include": [
"src"
Expand Down
1 change: 0 additions & 1 deletion types/icon.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

export type iOSType = 'ipad' | 'ipadpro9' | 'ipadpro9' | 'ipadpro10' | 'ipadpro12' | 'iphonese' | 'iphone6' | 'iphoneplus' | 'iphonex' | 'iphonexr' | 'iphonexsmax'
export type iOSSize = [number, number, iOSType]

Expand Down
16 changes: 5 additions & 11 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { PWAOptions } from './pwa'

declare module '@nuxt/types/config/index' {
interface NuxtOptions {
pwa?: PWAOptions
meta?: PWAOptions['meta']
icon?: PWAOptions['icon']
workbox?: PWAOptions['workbox']
manifest?: PWAOptions['manifest']
}
}
export { MetaOptions } from './meta'
export { IconOptions } from './icon'
export { WorkboxOptions } from './workbox'
export { ManifestOptions } from './manifest'
export { PWAContext } from './pwa'
12 changes: 7 additions & 5 deletions types/pwa.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { IconOptions } from './icon'
import { WorkboxOptions } from './workbox'
import { ManifestOptions } from './manifest'

export interface PWAOptions {
meta?: MetaOptions | false
icon?: IconOptions | false
workbox?: WorkboxOptions | false
manifest?: ManifestOptions | false
export interface PWAContext {
meta?: MetaOptions
icon?: IconOptions
workbox?: WorkboxOptions
manifest?: ManifestOptions

_manifestMeta: any // vue-meta record
}

0 comments on commit 27cb449

Please sign in to comment.