Skip to content

Commit

Permalink
fix: use shared pwa context
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Sep 9, 2019
1 parent ae12f89 commit 3c19bae
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 27 deletions.
25 changes: 12 additions & 13 deletions lib/icon/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ const hasha = require('hasha')
const { fork } = require('child_process')
const { joinUrl, getRouteParams } = require('../utils')

module.exports = function (iconOptions) {
this.nuxt.hook('build:before', () => run.call(this, iconOptions, true))
module.exports = function (pwa) {
this.nuxt.hook('build:before', () => run.call(this, pwa, true))

if (this.options.mode === 'spa' && !this.options.dev) {
return run.call(this, iconOptions, false) // Fill meta
return run.call(this, pwa, false) // Fill meta
}
}

async function run (iconOptions, _emitAssets) {
async function run (pwa, _emitAssets) {
const { publicPath } = getRouteParams(this.options)

// Defaults
Expand All @@ -34,7 +34,7 @@ async function run (iconOptions, _emitAssets) {
// Merge options
const options = {
...defaults,
...iconOptions
...pwa.icon
}

// Find iconSrc
Expand All @@ -49,7 +49,7 @@ async function run (iconOptions, _emitAssets) {
await generateIcons.call(this, options)

// Add manifest
addManifest.call(this, options)
addManifest.call(this, options, pwa)

// Add plugin
if (options.accessibleIcons) {
Expand Down Expand Up @@ -117,16 +117,15 @@ async function generateIcons (options) {
}))
}

function addManifest (options) {
if (!this.options.manifest) {
this.options.manifest = {}
function addManifest (options, pwa) {
if (!pwa.manifest) {
pwa.manifest = {}
}

if (!this.options.manifest.icons) {
this.options.manifest.icons = []
if (!pwa.manifest.icons) {
pwa.manifest.icons = []
}

this.options.manifest.icons.push(...options._assetIcons)
pwa.manifest.icons.push(...options._assetIcons)
}

function emitAssets (options) {
Expand Down
8 changes: 4 additions & 4 deletions lib/manifest/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ const hash = require('hasha')

const { joinUrl, getRouteParams, find } = require('../utils')

module.exports = function nuxtManifest (manifestOptions) {
module.exports = function nuxtManifest (pwa) {
const hook = () => {
addManifest.call(this, manifestOptions)
addManifest.call(this, pwa)
}

if (this.options.mode === 'spa') {
Expand All @@ -14,7 +14,7 @@ module.exports = function nuxtManifest (manifestOptions) {
this.nuxt.hook('build:before', hook)
}

function addManifest (manifestOptions) {
function addManifest (pwa) {
const { routerBase, publicPath } = getRouteParams(this.options)

// Combine sources
Expand All @@ -31,7 +31,7 @@ function addManifest (manifestOptions) {
lang: 'en'
}

const options = { ...defaults, ...manifestOptions }
const options = { ...defaults, ...pwa.manifest }

// Remve extra fields from manifest
const manifest = { ...options }
Expand Down
8 changes: 4 additions & 4 deletions lib/meta/module.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { find, isUrl } = require('../utils')

module.exports = function nuxtMeta (metaOptions) {
module.exports = function nuxtMeta (pwa) {
const hook = () => {
generateMeta.call(this, metaOptions)
generateMeta.call(this, pwa)
}

if (this.options.mode === 'spa') {
Expand All @@ -12,7 +12,7 @@ module.exports = function nuxtMeta (metaOptions) {
this.nuxt.hook('build:before', hook)
}

function generateMeta (metaOptions) {
function generateMeta (pwa) {
// Defaults
const defaults = {
name: process.env.npm_package_name,
Expand Down Expand Up @@ -40,7 +40,7 @@ function generateMeta (metaOptions) {
}

// Combine sources
const options = { ...defaults, ...this.options.manifest, ...metaOptions }
const options = { ...defaults, ...pwa.manifest, ...pwa.meta }

// Default value for viewport
if (options.viewport === undefined) {
Expand Down
27 changes: 22 additions & 5 deletions lib/module.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
module.exports = async function nuxtPWA (options) {
module.exports = async function nuxtPWA (moduleOptions) {
const modules = ['icon', 'manifest', 'meta', 'workbox']

const pwaOptions = { ...this.options.pwa, ...options }
// Shared options context
const pwa = { ...this.options.pwa, ...moduleOptions }

// Normalize options
for (const name of modules) {
if (pwaOptions[name] === false || this.options[name] === false) {
// Skip disabled modules
if (pwa[name] === false) {
continue
}
// Ensure options are an object
if (pwa[name] === undefined) {
pwa[name] = {}
}
// Backward compatibility for top-level options
if (this.options[name] !== undefined) {
pwa[name] = { ...this.options[name], ...pwa[name] }
}
}

// Execute modules in sequence
for (const name of modules) {
if (pwa[name] === false) {
continue
}
const moduleFn = require(`./${name}/module.js`)
const moduleOptions = { ...this.options[name], ...pwaOptions[name] }
await moduleFn.call(this, moduleOptions)
await moduleFn.call(this, pwa)
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/onesignal/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function addOneSignal (oneSignalOptions) {
}
}

const options = defu(oneSignalOptions, defaults)
const options = defu({ ...this.options.oneSignal, ...oneSignalOptions }, defaults)

if (options.OneSignalSDK === undefined) {
if (options.cdn) {
Expand Down

0 comments on commit 3c19bae

Please sign in to comment.