Permalink
Browse files

feat(manifest): meta module compatibility

  • Loading branch information...
pi0 committed Jun 6, 2017
1 parent 17b7db1 commit 8a41fda49fe6e15eba5bb65095a3af0f1450eaa0
Showing with 15 additions and 82 deletions.
  1. +14 −77 modules/manifest/index.js
  2. +1 −5 modules/manifest/package.json
@@ -1,10 +1,10 @@
const fs = require('fs-extra')
const path = require('path')
const _ = require('lodash')
const hash = require('hash-sum')
const fixUrl = url => url.replace(/\/\//g, '/').replace(':/', '://')
const isUrl = url => url.indexOf('http') === 0 || url.indexOf('//') === 0
const find = (arr, key, val) => arr.find(obj => val ? obj[key] === val : obj[key])
module.exports = function nuxtManifest (options) {
// routerBase and publicPath
@@ -17,7 +17,7 @@ module.exports = function nuxtManifest (options) {
}
}
// Infer defaults based on environment
// Defaults
const defaults = {
name: process.env.npm_package_name,
short_name: process.env.npm_package_name,
@@ -26,87 +26,24 @@ module.exports = function nuxtManifest (options) {
start_url: routerBase,
display: 'standalone',
background_color: '#ffffff',
theme_color: (this.options.loading && this.options.loading.color) || '#3f51b5',
lang: 'en',
// internal options
_meta: true,
_icons: true,
_openGraph: false
theme_color: this.options.loading && this.options.loading.color,
lang: 'en'
}
// Combine sources
const manifest = _.defaultsDeep({}, options, this.options.manifest, defaults)
// Generate icons
if (manifest._icons) {
// TODO
}
// Head Meta Tags
if (manifest._meta) {
// Add favicon
if (!_.find(this.options.head.link, {rel: 'shortcut icon'})) {
this.options.head.link.push({rel: 'shortcut icon', href: manifest.icons[0].src})
}
if (!_.find(this.options.head.link, {rel: 'apple-touch-icon'})) {
this.options.head.link.push({rel: 'apple-touch-icon', href: manifest.icons[0].src})
}
// Set title
if (manifest.name && !this.options.head.title) {
this.options.head.title = manifest.name
}
// Add description meta
if (manifest.description && !_.find(this.options.head.meta, {name: 'description'})) {
this.options.head.meta.push({hid: 'description', name: 'description', content: manifest.description})
}
// Add theme-color meta
if (manifest.theme_color && !_.find(this.options.head.meta, {name: 'theme-color'})) {
this.options.head.meta.push({name: 'theme-color', content: manifest.theme_color})
}
// Add lang to html tag
if (manifest.lang && !(this.options.head.htmlAttrs && this.options.head.htmlAttrs.lang)) {
if (!this.options.head.htmlAttrs) {
this.options.head.htmlAttrs = {}
}
this.options.head.htmlAttrs.lang = manifest.lang
}
}
// OpenGraph headers
if (manifest._openGraph) {
// Add og:type
if (!_.find(this.options.head.meta, {property: 'og:type'})) {
this.options.head.meta.push({property: 'og:type', content: manifest.type || 'website'})
}
// Add og:title
if (!_.find(this.options.head.meta, {property: 'og:title'})) {
this.options.head.meta.push({property: 'og:title', content: manifest.name})
}
// Add og:description
if (!_.find(this.options.head.meta, {property: 'og:description'})) {
this.options.head.meta.push({property: 'og:description', content: manifest.description})
}
}
// Cleanup internals
const manifest = Object.assign({}, defaults, this.options.manifest, options)
delete manifest.src
delete manifest._icons
delete manifest._openGraph
delete manifest._meta
// Stringify manifest & generate hash
const manifestSource = JSON.stringify(manifest)
const manifestFileName = `manifest.${hash(manifestSource)}.json`
// Merge final manifest into options.manifest for other modules
if (!this.options.manifest) {
this.options.manifest = {}
}
Object.assign(this.options.manifest, manifest)
// Register webpack plugin to emit manifest
this.options.build.plugins.push({
apply (compiler) {
@@ -121,10 +58,10 @@ module.exports = function nuxtManifest (options) {
})
// Add manifest meta
if (!_.find(this.options.head.link, {rel: 'manifest'})) {
this.options.head.link.push({rel: 'manifest', href: fixUrl(`${publicPath}/${manifestFileName}`)})
if (!find(this.options.head.link, 'rel', 'manifest')) {
this.options.head.link.push({ rel: 'manifest', href: fixUrl(`${publicPath}/${manifestFileName}`) })
} else {
console.warn('Manifest meta already provided! Skipping manifest.')
console.warn('Manifest meta already provided!')
}
}
@@ -9,10 +9,6 @@
"access": "public"
},
"dependencies": {
"fs-extra": "^3.0.1",
"hash-sum": "^1.0.2",
"lodash": "^4.17.4",
"jimp": "^0.2.28",
"png-to-ico": "^1.0.2"
"hash-sum": "^1.0.2"
}
}

0 comments on commit 8a41fda

Please sign in to comment.