1 | const hash = require('hash-sum')
|
2 |
|
3 | const { joinUrl, getRouteParams, find } = require('@nuxtjs/pwa-utils')
|
4 |
|
5 | module.exports = function nuxtManifest (options) {
|
6 | const hook = () => {
|
7 | addManifest.call(this, options)
|
8 | }
|
9 |
|
10 | if (this.options.mode === 'spa') {
|
11 | return hook()
|
12 | }
|
13 |
|
14 | this.nuxt.hook('build:before', hook)
|
15 | }
|
16 |
|
17 | function addManifest (_options) {
|
18 | const { routerBase, publicPath } = getRouteParams(this.options)
|
19 |
|
20 |
|
21 | const defaults = {
|
22 | name: process.env.npm_package_name,
|
23 | short_name: process.env.npm_package_name,
|
24 | description: process.env.npm_package_description,
|
25 | publicPath,
|
26 | icons: [],
|
27 | start_url: routerBase + '?standalone=true',
|
28 | display: 'standalone',
|
29 | background_color: '#ffffff',
|
30 | theme_color: this.options.loading && this.options.loading.color,
|
31 | lang: 'en'
|
32 | }
|
33 | const options = { ...defaults, ...this.options.manifest, ..._options }
|
34 |
|
35 |
|
36 | const manifest = { ...options }
|
37 | delete manifest.src
|
38 | delete manifest.publicPath
|
39 |
|
40 |
|
41 | const manifestSource = JSON.stringify(manifest)
|
42 | const manifestFileName = `manifest.${hash(manifestSource)}.json`
|
43 |
|
44 |
|
45 | if (!this.options.manifest) {
|
46 | this.options.manifest = {}
|
47 | }
|
48 | Object.assign(this.options.manifest, manifest)
|
49 |
|
50 |
|
51 | this.options.build.plugins.push({
|
52 | apply (compiler) {
|
53 | compiler.hooks.emit.tap('nuxt-pwa-manifest', (compilation) => {
|
54 | compilation.assets[manifestFileName] = {
|
55 | source: () => manifestSource,
|
56 | size: () => manifestSource.length
|
57 | }
|
58 | })
|
59 | }
|
60 | })
|
61 |
|
62 |
|
63 | if (!find(this.options.head.link, 'rel', 'manifest')) {
|
64 | const baseAttribute = { rel: 'manifest', href: joinUrl(options.publicPath, manifestFileName) }
|
65 | const attribute = manifest.crossorigin ? Object.assign({}, baseAttribute, { crossorigin: manifest.crossorigin }) : baseAttribute
|
66 | this.options.head.link.push(attribute)
|
67 | } else {
|
68 | console.warn('Manifest meta already provided!')
|
69 | }
|
70 | }
|
71 |
|
72 | module.exports.meta = require('./package.json')
|