Skip to content

Commit 3c19bae

Browse files
committedSep 9, 2019
fix: use shared pwa context
1 parent ae12f89 commit 3c19bae

File tree

5 files changed

+43
-27
lines changed

5 files changed

+43
-27
lines changed
 

‎lib/icon/module.js

+12-13
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ const hasha = require('hasha')
44
const { fork } = require('child_process')
55
const { joinUrl, getRouteParams } = require('../utils')
66

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

1010
if (this.options.mode === 'spa' && !this.options.dev) {
11-
return run.call(this, iconOptions, false) // Fill meta
11+
return run.call(this, pwa, false) // Fill meta
1212
}
1313
}
1414

15-
async function run (iconOptions, _emitAssets) {
15+
async function run (pwa, _emitAssets) {
1616
const { publicPath } = getRouteParams(this.options)
1717

1818
// Defaults
@@ -34,7 +34,7 @@ async function run (iconOptions, _emitAssets) {
3434
// Merge options
3535
const options = {
3636
...defaults,
37-
...iconOptions
37+
...pwa.icon
3838
}
3939

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

5151
// Add manifest
52-
addManifest.call(this, options)
52+
addManifest.call(this, options, pwa)
5353

5454
// Add plugin
5555
if (options.accessibleIcons) {
@@ -117,16 +117,15 @@ async function generateIcons (options) {
117117
}))
118118
}
119119

120-
function addManifest (options) {
121-
if (!this.options.manifest) {
122-
this.options.manifest = {}
120+
function addManifest (options, pwa) {
121+
if (!pwa.manifest) {
122+
pwa.manifest = {}
123123
}
124-
125-
if (!this.options.manifest.icons) {
126-
this.options.manifest.icons = []
124+
if (!pwa.manifest.icons) {
125+
pwa.manifest.icons = []
127126
}
128127

129-
this.options.manifest.icons.push(...options._assetIcons)
128+
pwa.manifest.icons.push(...options._assetIcons)
130129
}
131130

132131
function emitAssets (options) {

‎lib/manifest/module.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ const hash = require('hasha')
22

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

5-
module.exports = function nuxtManifest (manifestOptions) {
5+
module.exports = function nuxtManifest (pwa) {
66
const hook = () => {
7-
addManifest.call(this, manifestOptions)
7+
addManifest.call(this, pwa)
88
}
99

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

17-
function addManifest (manifestOptions) {
17+
function addManifest (pwa) {
1818
const { routerBase, publicPath } = getRouteParams(this.options)
1919

2020
// Combine sources
@@ -31,7 +31,7 @@ function addManifest (manifestOptions) {
3131
lang: 'en'
3232
}
3333

34-
const options = { ...defaults, ...manifestOptions }
34+
const options = { ...defaults, ...pwa.manifest }
3535

3636
// Remve extra fields from manifest
3737
const manifest = { ...options }

‎lib/meta/module.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const { find, isUrl } = require('../utils')
22

3-
module.exports = function nuxtMeta (metaOptions) {
3+
module.exports = function nuxtMeta (pwa) {
44
const hook = () => {
5-
generateMeta.call(this, metaOptions)
5+
generateMeta.call(this, pwa)
66
}
77

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

15-
function generateMeta (metaOptions) {
15+
function generateMeta (pwa) {
1616
// Defaults
1717
const defaults = {
1818
name: process.env.npm_package_name,
@@ -40,7 +40,7 @@ function generateMeta (metaOptions) {
4040
}
4141

4242
// Combine sources
43-
const options = { ...defaults, ...this.options.manifest, ...metaOptions }
43+
const options = { ...defaults, ...pwa.manifest, ...pwa.meta }
4444

4545
// Default value for viewport
4646
if (options.viewport === undefined) {

‎lib/module.js

+22-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
1-
module.exports = async function nuxtPWA (options) {
1+
module.exports = async function nuxtPWA (moduleOptions) {
22
const modules = ['icon', 'manifest', 'meta', 'workbox']
33

4-
const pwaOptions = { ...this.options.pwa, ...options }
4+
// Shared options context
5+
const pwa = { ...this.options.pwa, ...moduleOptions }
56

7+
// Normalize options
68
for (const name of modules) {
7-
if (pwaOptions[name] === false || this.options[name] === false) {
9+
// Skip disabled modules
10+
if (pwa[name] === false) {
11+
continue
12+
}
13+
// Ensure options are an object
14+
if (pwa[name] === undefined) {
15+
pwa[name] = {}
16+
}
17+
// Backward compatibility for top-level options
18+
if (this.options[name] !== undefined) {
19+
pwa[name] = { ...this.options[name], ...pwa[name] }
20+
}
21+
}
22+
23+
// Execute modules in sequence
24+
for (const name of modules) {
25+
if (pwa[name] === false) {
826
continue
927
}
1028
const moduleFn = require(`./${name}/module.js`)
11-
const moduleOptions = { ...this.options[name], ...pwaOptions[name] }
12-
await moduleFn.call(this, moduleOptions)
29+
await moduleFn.call(this, pwa)
1330
}
1431
}
1532

‎lib/onesignal/module.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function addOneSignal (oneSignalOptions) {
4343
}
4444
}
4545

46-
const options = defu(oneSignalOptions, defaults)
46+
const options = defu({ ...this.options.oneSignal, ...oneSignalOptions }, defaults)
4747

4848
if (options.OneSignalSDK === undefined) {
4949
if (options.cdn) {

0 commit comments

Comments
 (0)
Please sign in to comment.