diff --git a/lib/icon/module.js b/lib/icon/module.js index 2587416b..4128eca1 100755 --- a/lib/icon/module.js +++ b/lib/icon/module.js @@ -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 @@ -34,7 +34,7 @@ async function run (iconOptions, _emitAssets) { // Merge options const options = { ...defaults, - ...iconOptions + ...pwa.icon } // Find iconSrc @@ -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) { @@ -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) { diff --git a/lib/manifest/module.js b/lib/manifest/module.js index e9641441..8b0de0ce 100755 --- a/lib/manifest/module.js +++ b/lib/manifest/module.js @@ -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') { @@ -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 @@ -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 } diff --git a/lib/meta/module.js b/lib/meta/module.js index 2c6e5bde..c4fd3b08 100755 --- a/lib/meta/module.js +++ b/lib/meta/module.js @@ -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') { @@ -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, @@ -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) { diff --git a/lib/module.js b/lib/module.js index e38417ed..ee252554 100755 --- a/lib/module.js +++ b/lib/module.js @@ -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) } } diff --git a/lib/onesignal/module.js b/lib/onesignal/module.js index a9261f4a..a8d61060 100755 --- a/lib/onesignal/module.js +++ b/lib/onesignal/module.js @@ -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) {