From 27cb44905fc326c806b3c18535266f8ed384086a Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sat, 28 Nov 2020 14:59:21 +0100 Subject: [PATCH] chore: improve types --- package.json | 7 +++---- src/icon.ts | 4 ++-- src/manifest.ts | 5 +++-- src/meta.ts | 4 ++-- src/pwa.ts | 22 ++++++++++++++++++++-- src/workbox/defaults.ts | 2 +- src/workbox/options.ts | 18 +++++++++--------- tsconfig.json | 5 ++++- types/icon.d.ts | 1 - types/index.d.ts | 16 +++++----------- types/pwa.d.ts | 12 +++++++----- 11 files changed, 56 insertions(+), 40 deletions(-) diff --git a/package.json b/package.json index 84dfc23e..d613eb60 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/icon.ts b/src/icon.ts index ecdd96b1..0d3b12c9 100755 --- a/src/icon.ts +++ b/src/icon.ts @@ -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 diff --git a/src/manifest.ts b/src/manifest.ts index 4d4f6e8a..20c0864f 100755 --- a/src/manifest.ts +++ b/src/manifest.ts @@ -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 @@ -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 diff --git a/src/meta.ts b/src/meta.ts index 3247e2fe..1aaee0f7 100755 --- a/src/meta.ts +++ b/src/meta.ts @@ -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, diff --git a/src/pwa.ts b/src/pwa.ts index 941414b9..0409193e 100755 --- a/src/pwa.ts +++ b/src/pwa.ts @@ -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 @@ -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) { @@ -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 diff --git a/src/workbox/defaults.ts b/src/workbox/defaults.ts index 947c7155..5dafc030 100644 --- a/src/workbox/defaults.ts +++ b/src/workbox/defaults.ts @@ -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 diff --git a/src/workbox/options.ts b/src/workbox/options.ts index cd74a753..f53e0288 100644 --- a/src/workbox/options.ts +++ b/src/workbox/options.ts @@ -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 @@ -62,20 +62,20 @@ 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)) @@ -83,7 +83,7 @@ export function getOptions (nuxt, pwa): WorkboxOptions { // Add offlinePage to precaching if (options.offlinePage) { - options.preCaching.unshift(...(normalizePreCaching([options.offlinePage]))) + options.preCaching.unshift(...(normalizePreCaching(options.offlinePage))) } // Default cacheId diff --git a/tsconfig.json b/tsconfig.json index d008c000..1f675ab9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,7 +6,10 @@ "outDir": "dist", "declaration": true, "resolveJsonModule": true, - "esModuleInterop": true + "esModuleInterop": true, + "types": [ + "@nuxt/types" + ] }, "include": [ "src" diff --git a/types/icon.d.ts b/types/icon.d.ts index db9f0748..a00f572b 100644 --- a/types/icon.d.ts +++ b/types/icon.d.ts @@ -1,4 +1,3 @@ - export type iOSType = 'ipad' | 'ipadpro9' | 'ipadpro9' | 'ipadpro10' | 'ipadpro12' | 'iphonese' | 'iphone6' | 'iphoneplus' | 'iphonex' | 'iphonexr' | 'iphonexsmax' export type iOSSize = [number, number, iOSType] diff --git a/types/index.d.ts b/types/index.d.ts index 70844b14..fc8f34d1 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -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' diff --git a/types/pwa.d.ts b/types/pwa.d.ts index c5ae61ec..14caa57c 100644 --- a/types/pwa.d.ts +++ b/types/pwa.d.ts @@ -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 }